Wednesday, October 18, 2017

#1 2017-09-25 12:24:37 pm

backinmac
Member
Registered: 2017-09-25
Posts: 2

Retrieving system info programatically with Python

Hi Guys

I hope that someone here can lend me a hand. I know that this is not AppleScript but rather Python The subject is still very script-centric and related specifically to the Mac.

I try to use Python as much as I can in my IT job to help speed things up and get work done with fewer errors. One task is booking in machines into the database via saved SPX (system profiler) files. Right now I have to do it all manually. The copy and pasting is a drag!

I've found a wonder script on Github https://gist.github.com/pudquick/83ec0c … c1ce64a389. As it is there, it only returns a serial number for whichever .spx file is given as a command-line argument. I've added a few more lines of code to help return the other basic info such as physical RAM, number of cores etc. All the stuff you see immediately when you open System Profiler...

#!/usr/bin/python
# https://gist.github.com/pudquick/83ec0c … c1ce64a389
# python 2.7.11
import pyperclip


import plistlib, sys
import time
import os
import pyautogui
import pyperclip


file_path_as_string = str(file_path)
file_data = plistlib.readPlist(file_path)
candidates = []


for x in file_data:
    if x.get('_parentDataType', None) == 'SPRootDataType':
        if x.get('_dataType', None) == 'SPHardwareDataType':
            candidates.append(x)


for x in candidates:
    pyperclip.copy(x['_items'][0]['machine_model'])
    print x['_items'][0]['machine_model']
    print x['_items'][0]['cpu_type']
    print x['_items'][0]['current_processor_speed']
    print x['_items'][0]['serial_number']
    print x['_items'][0]['physical_memory']


I like to also get back info such as Graphics card type, the amount of HD storage etc. This appears to be a little more complicated... In the case of HD info, the category or ('_dataType' according to the code) is 'SPSerialATADataType'

I append onto the code this...

for y in file_data:
    if y.get('_parentDataType', None) == 'SPHardwareDataType':
        if y.get('_dataType', None) == 'SPSerialATADataType':
            candidates2.append(y)

...

for y in candidates2:
    print y['_items'][0]['_name']


With this section added on I now get an extra line of feedback when I run the script.
That particular line is "Apple SSD Controller"

I examine the SXP file in an XML reader. The string "Apple SSD Controller" is found at a particular level of the xml structure. I can also print out some of its 'neighbours' at the same level (by substituting 'spsata_physical_interconnect') for example and get the corresponding string value.

The data I want to get at is 'one level deeper' in the xml structure. When I try to run

for y in candidates2:
    print y['_items'][0]['size']


for example, I get a KeyError: 'size' error. 'Size' in this context is related to the internal SSD.

I hope somebody can point out to me where I am going wrong and show me how I dive a level of two deeper in the XML structure.

Thanks.

P.S Here is the dropbox link to the python script. If you have an IDE such as Pycharm you'll need to set an spx file you've saved as a script parameter.

https://www.dropbox.com/s/cugwm95pljfxq … ta.py?dl=0

Offline

 

#2 2017-09-26 05:15:12 pm

backinmac
Member
Registered: 2017-09-25
Posts: 2

Re: Retrieving system info programatically with Python

Issue resolved.

CLOSED

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)