Skip to content

Instantly share code, notes, and snippets.

@tzarskyz
Forked from nickstenning/appversions
Created September 7, 2013 11:15
Show Gist options
  • Save tzarskyz/6474756 to your computer and use it in GitHub Desktop.
Save tzarskyz/6474756 to your computer and use it in GitHub Desktop.
#!/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