2012-03-05

A quick script to convert your BASH environment settings to environment.plist

When a new Linux guy who switchs to Mac, the first problem for them to use environment.plist. Here's a simple example to convert environment settings to environment.plist. This is especially true when the project you work with uses a lot of environment settings in your XCode project file.

So I created the script below to help me sort it out. It is not a complete script to handle all special cases like differences between ${} and $ token in BASH, but already good enough in my environment.

Try modify code yourself and enjoy hacking!

#!/usr/bin/env python
import sys
import re

keyval_pat = re.compile(r"[ ]*export ([^=]*)=(.*)")
fd = open(sys.argv[1])

print(r'<?xml version="1.0" encoding="utf-8">')
print(r'''
<!DOCTYPE plist PUBLIC 
    "-//Apple DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">''')
print('<plist version="1.0">')
print('<dict>')
for each_line in fd:
    m = keyval_pat.search(each_line)
    if m is None:
        continue
    key = m.group(1)
    value = m.group(2)
    print("<key>%s</key><string>%s</string>" % (key, value))
print('</dict>')
print('</plist>')
fd.close()

1 comment:

多路由光纤专线 said...

不错的script,收藏学习了!