0

I want to make a python script that checks a user's machine form factor (laptop or desktop), and then I would be able to use conditions like that:

chassis_type = some_module.get_chassis_type()
if chassis_type == 'laptop':
    print("You are out of luck, because you are using laptop.")
if chassis_type == 'desktop':
    print("You are good to go, because you are using desktop.")

I do not want to check if battery is presented, but I want to use more generic way. Is there a way how can I achieve that using python?

Ashark
  • 643
  • 7
  • 16
  • Even self-answered questions need to adhere to [question quality norms](https://stackoverflow.com/help/quality-standards-error). I upvoted it, but consider improving it. – PM 77-1 Jul 09 '20 at 15:10

1 Answers1

0

There are several ways you can do this.

You can read this information from firmware using dmidecode module, but this requires permissions. Also, you can read /sys/class/dmi/id/chassis_type file directly and translate a number to a specified chassis string.

But I currently use the following way:

import re
import subprocess

machine_info = subprocess.check_output(["hostnamectl", "status"], universal_newlines=True)
m = re.search('Chassis: (.+?)\n', machine_info)
chassis_type = m.group(1)
print("Your chassis type is", chassis_type)

This method relies on a standard systemd specification, which checks a firmware and also /etc/machine-id.

Ashark
  • 643
  • 7
  • 16