views:

155

answers:

1

My problem is one like a simulated problem from http://my.safaribooksonline.com/0596007973/pythoncook2-CHP-10-SECT-17 which eventually made its way into Python Cookbook, 2nd Edition using an outdated xpath method from 2005 that I haven't been able to get to work with 10.6's build-in python(nor installing older packages)

I want to ... "retrieve detailed information about a Mac OS X system" using system_profiler to summarize it in a script each time a computer starts up(The script will launch on login).
The information I'm gathering varies from SW versions to HW config.

An example line is, system_profiler SPSoftwareDataType | grep 'Boot Volume' which returns the startup volume name. I make 15 to 20 other calls for information.

I've tried to output the full 'system_profiler > data' and parse that using cat data | grep, but that's obviously inefficient to the point where it's been faster if I just run each line like my example above.
18 seconds if ouputting to a file and cat | grep.

13 seconds if making individual calls

*I'm trying to make it as fast as possible.

I deduce that I probably need to create a dictionary and use keys to reference out the data but I'm wondering what's the most efficient way for me to parse and retrieve the data? I've seen a suggestion elsewhere to use system_profiler to output to XML and use a XML parser but I think there's probably some cache and parse method that does it more efficiently than outputting to a file first.

+3  A: 

Use the -xml option to system_profiler to format the output in a standard OS X plist format, then use Python's built-in plistlib to parse into an appropriate data structure you can introspect. A simple example:

>>> from subprocess import Popen, PIPE
>>> from plistlib import readPlistFromString
>>> from pprint import pprint
>>> sp = Popen(["system_profiler", "-xml"], stdout=PIPE).communicate()[0]
>>> pprint(readPlistFromString(sp))
[{'_dataType': 'SPHardwareDataType',
  '_detailLevel': '-2',
  '_items': [{'SMC_version_system': '1.21f4',
              '_name': 'hardware_overview',
              'boot_rom_version': 'IM71.007A.B03',
              'bus_speed': '800 MHz',
              'cpu_type': 'Intel Core 2 Duo',
 ...
Ned Deily
Thanks, trying it.