<commit_before>#!/usr/bin/env python
# Adapted from SO: http://stackoverflow.com/questions/7404792/how-to-create-mac-application-bundle-for-python-script-via-python

import stat
import sys
import os, os.path

assert len(sys.argv) > 1
apppath = sys.argv[1]
assert os.path.splitext(apppath)[1] == ".app"

os.mkdir(apppath)
os.mkdir(apppath + "/Contents")
os.mkdir(apppath + "/Contents/MacOS")

CFBundleVersion = "1.0.0"
CFBundleName = "Test"
CFBundleGetInfoString = CFBundleName + " " + CFBundleVersion
CFBundleShortVersionString = CFBundleGetInfoString
CFBundleIdentifier = "com.gpilab.test"

info_plist = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleExecutable</key>
    <string>main.py</string>
    <key>CFBundleGetInfoString</key>
    <string>%s</string>
    <key>CFBundleIconFile</key>
    <string>app.icns</string>
    <key>CFBundleIdentifier</key>
    <string>%s</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>%s</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>%s</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>%s</string>
    <key>NSAppleScriptEnabled</key>
    <string>YES</string>
    <key>NSMainNibFile</key>
    <string>MainMenu</string>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
</dict>
</plist>
"""

main_py = """#!/usr/bin/env python
import sys
from PyQt4 import QtGui

def main():
    
    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.show()
    w.raise_()
    
    sys.exit(app.exec_())

main()
"""

with open(apppath + "/Contents/Info.plist", "w") as f:
    f.write(info_plist % (CFBundleGetInfoString, CFBundleIdentifier, CFBundleName, CFBundleShortVersionString, CFBundleVersion))

with open(apppath + "/Contents/PkgInfo", "w") as f:
    f.write("APPL????")

with open(apppath + "/Contents/MacOS/main.py", "w") as f:
    f.write(main_py)

oldmode = os.stat(apppath + "/Contents/MacOS/main.py").st_mode
os.chmod(apppath + "/Contents/MacOS/main.py", oldmode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)<commit_msg>Added arg parser and cleaned up plist generator code.<commit_after>#!/usr/bin/env python
# Author: <NAME>
# Date: 2015oct31 
# Adapted from SO: http://stackoverflow.com/questions/7404792/how-to-create-mac-application-bundle-for-python-script-via-python

import os
import sys
import stat
import shutil
import optparse

parser = optparse.OptionParser()
parser.add_option("-n", "--name", dest='name', help="The name of this app.")
parser.add_option("-v", "--version", dest='version', help="The version of this app.", default='0.1')
parser.add_option("-i", "--icon", dest='icon_file', help="Icon file to be used in the bundle.")
options, args = parser.parse_args()

if options.icon_file is not None:
    if not os.path.isfile(options.icon_file):
        print "The supplied icon path doesn\'t point to a valid file."
        parser.print_help()
        sys.exit(1)
    if os.path.splitext(options.icon_file)[1] != ".icns":
        print "The supplied icon path must point to an .icns file."
        parser.print_help()
        sys.exit(1)
if options.name == '' or options.name is None:
    print "The app name is a required input."
    parser.print_help()
    sys.exit(1)

CFBundleVersion = options.version
CFBundleName = options.name
CFBundleGetInfoString = CFBundleName + " " + CFBundleVersion
CFBundleShortVersionString = CFBundleGetInfoString
CFBundleIdentifier = "com.gpilab."+CFBundleName
CFBundleIconFile = 'app.icns'

apppath = './'+CFBundleName+'.app'

os.mkdir(apppath)
os.mkdir(apppath + "/Contents")
os.mkdir(apppath + "/Contents/MacOS")
os.mkdir(apppath + "/Contents/Resources")

if options.icon_file is not None:
    shutil.copy(options.icon_file, apppath + "/Contents/Resources/" + CFBundleIconFile)

info_plist = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleExecutable</key>
    <string>main.py</string>
    <key>CFBundleGetInfoString</key>
    <string>%s</string>
    <key>CFBundleIconFile</key>
    <string>%s</string>
    <key>CFBundleIdentifier</key>
    <string>%s</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>%s</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>%s</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>%s</string>
    <key>NSAppleScriptEnabled</key>
    <string>YES</string>
    <key>NSMainNibFile</key>
    <string>MainMenu</string>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
</dict>
</plist>
"""

main_py = """#!/usr/bin/env python
import sys
from PyQt4 import QtGui

def main():
    
    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.show()
    w.raise_()
    
    sys.exit(app.exec_())

main()
"""

with open(apppath + "/Contents/Info.plist", "w") as f:
    f.write(info_plist % (CFBundleGetInfoString, CFBundleIconFile, CFBundleIdentifier, CFBundleName, CFBundleShortVersionString, CFBundleVersion))

with open(apppath + "/Contents/PkgInfo", "w") as f:
    f.write("APPL????")

with open(apppath + "/Contents/MacOS/main.py", "w") as f:
    f.write(main_py)

oldmode = os.stat(apppath + "/Contents/MacOS/main.py").st_mode
os.chmod(apppath + "/Contents/MacOS/main.py", oldmode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
{'fraction_non_alphanumeric': 0.1674342105263158, 'fraction_numerical': 0.013157894736842105, 'mean_word_length': 4.09723386420788, 'pattern_counts': {'":': 1, '<': 128, '<?xml version=': 2, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5137886', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2162, 'n_words': 388}