Check if running on laptop or desktop chassis type in Python

676 views Asked by At

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?

1

There are 1 answers

0
Ashark On

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.