Get Manufacturer (Linux)
Description
The get_manufacturer_linux
function retrieves the manufacturer of the system on Linux. It first attempts to get the model information from the /proc/cpuinfo
file. If the system is a Raspberry Pi, it returns the parsed model. If the system is not a Raspberry Pi, it falls back to using dmidecode
to retrieve the processor's manufacturer. If an error occurs, it returns an empty string and a status code of 1
.
Function Signature:
def get_manufacturer_linux() -> tuple[str, int]:
Parameters
This function does not take any parameters.
Returns
- tuple: A tuple containing two values:
- str: The manufacturer name or model (e.g., "Raspberry Pi", "Intel").
- int: A status code indicating the result of the operation (e.g., 0 for success, 1 for failure).
Example Usage
Here’s an example of how the function works:
manufacturer, status = get_manufacturer_linux()
print(f"Manufacturer: {manufacturer}, Status: {status}")
Example Output:
Manufacturer: Raspberry Pi, Status: 0
Code
def get_manufacturer_linux() -> tuple[str, int]:
try:
with open("/proc/cpuinfo", "r") as f:
for line in f:
if line.startswith("Model"):
model = line.split(":")[1].strip()
if "Raspberry Pi" in model:
return parse_raspberry_pi_model(model), 0
# Fall back to using dmidecode if not a Raspberry Pi
result = subprocess.run(['dmidecode', '-t', 'processor'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
for line in result.stdout.splitlines():
if line.strip().startswith("Manufacturer"):
return parse_manufacturer(line.split(":")[1].strip()), 0
except Exception as e:
print(f"Exception in get_manufacturer_linux: {e}")
return "", 1
return "", 1
Errors and Exceptions
- If an error occurs while querying the
/proc/cpuinfo
file or running thedmidecode
command, an exception is caught, and the function returns an empty string and a failure status code (1
).
Notes
This function is platform-specific and is designed to work on Linux systems. It uses the /proc/cpuinfo
file and the dmidecode
command to fetch details about the system's manufacturer or model. The function also supports identifying Raspberry Pi models.