Skip to content

Instantly share code, notes, and snippets.

@mattieb
Created August 7, 2013 13:58
Show Gist options
  • Save mattieb/6174270 to your computer and use it in GitHub Desktop.
Save mattieb/6174270 to your computer and use it in GitHub Desktop.
A slightly less quick-and-dirty way to look up a Mac's friendly model name.
#!/usr/bin/env python
#
# Looks up a Mac's friendly model name.
#
# Based on http://apple.stackexchange.com/a/98089/21050
#
from subprocess import check_output
from urllib import urlopen
import xml.etree.ElementTree as ET
p_xml = check_output(['system_profiler', '-xml', 'SPHardwareDataType'])
p_root = ET.fromstring(p_xml)
p_dict = p_root.find('array/dict')
def locate_key(elem, search_key):
"""locate a specific key in a dict"""
for child in elem:
if child.tag == 'key':
key = child.text
elif key == search_key:
return child
raise LookupError(search_key)
p_items = locate_key(p_dict, '_items').find('dict')
p_serial = locate_key(p_items, 'serial_number')
serial = p_serial.text
m_xml = urlopen(
'http://support-sp.apple.com/sp/product?cc=' + serial[-4:]
).read()
m_root = ET.fromstring(m_xml)
m_configcode = m_root.find('configCode')
print m_configcode.text
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment