Created
June 26, 2013 11:52
-
-
Save nickstenning/5866835 to your computer and use it in GitHub Desktop.
Print a list of installed applications and their versions on Mac OS X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function, absolute_import | |
import csv | |
import re | |
import sys | |
from subprocess import Popen, PIPE | |
LSREGISTER = ('/System/Library/Frameworks/CoreServices.framework/Versions/A' | |
'/Frameworks/LaunchServices.framework/Versions/A' | |
'/Support/lsregister') | |
def parse_bundle(lines): | |
bundle = {} | |
for line in lines: | |
if line.startswith('bundle'): | |
line = line[len('bundle'):] | |
if re.match('^\t[\w ]+:\s+.+$', line): | |
key, val = line.split(':', 1) | |
bundle[key.strip()] = val.strip() | |
return bundle | |
def extract_bundles(fp): | |
in_bundle = False | |
lines = [] | |
for line in iter(fp.readline, ''): | |
if in_bundle: | |
if re.match(r'^-+$', line): | |
# Got to the end of the bundle | |
in_bundle = False | |
yield parse_bundle(lines) | |
lines = [] | |
continue | |
lines.append(line) | |
continue | |
if line.startswith('bundle'): | |
in_bundle = True | |
lines.append(line) | |
def main(): | |
proc = Popen([LSREGISTER, '-dump'], stdout=PIPE) | |
out = csv.DictWriter(sys.stdout, | |
fieldnames=('identifier', 'name', 'version', 'path')) | |
out.writeheader() | |
for bundle in extract_bundles(proc.stdout): | |
data = {'identifier': bundle['identifier'].split()[0], | |
'name': bundle['name'], | |
'version': bundle['version'], | |
'path': bundle['path']} | |
if data['name'] and data['identifier'] != '(0x0)': | |
out.writerow(data) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment