Skip to content

Instantly share code, notes, and snippets.

@eoghain
Last active August 25, 2018 02:16
Show Gist options
  • Save eoghain/7362771 to your computer and use it in GitHub Desktop.
Save eoghain/7362771 to your computer and use it in GitHub Desktop.
Python script to generate plists into a Settings.bundle for all licenses of open source software you are using in your application.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Setup
#
# 1. Create a directory to hold your 3rd party code, each module in it's own named directory (i.e. 3rdParty/AFNetworking).
# 2. Make sure under the module directory there is the LICENSE file (file must have LICENSE all uppercase in the name)
# 3. In your Settings.bundle add a row to the Root.plist with: Type=PSChildPaneSpecifier, Filename=Acknowledgements, Name=Acknowledgements.
# 4. In the script below,
# - set the settingsDir to the directory where the Settings.bundle resides (if not source root),
# - set the licenseDirs to the directory(s) where you place your 3rd party modules (i.e. Pods)
# 5. Add a build script to run this python script just after Target Dependancies.
import os
import shutil
import re
from plistlib import *
def main():
licenseDirs = []
settingsDir = ''
srcRoot = os.environ.get('SOURCE_ROOT')
if (srcRoot == None):
srcRoot = '.'
settingsBundle = os.path.join(srcRoot, settingsDir, 'Settings.bundle')
acknowledgementsPlist = dict(PreferenceSpecifiers = [])
regex = re.compile('(^.*LICENSE.*$)')
for licenseDir in licenseDirs:
licenseDir = os.path.join(srcRoot, licenseDir)
for root, dirs, files in os.walk(licenseDir):
for file in files:
licenseMatch = re.match(regex, file)
if licenseMatch:
moduleName = os.path.split(root)[1]
print 'moduleName:%s' % moduleName
licenseFile = open(os.path.join(root, licenseMatch.group(0)), 'r')
acknowledgementDict = dict(Type = "PSChildPaneSpecifier", Title = moduleName, File = moduleName)
acknowledgementsPlist["PreferenceSpecifiers"].append(acknowledgementDict)
licensePlist = dict(StringsTable = moduleName, PreferenceSpecifiers = [])
compiledLine = '';
for line in licenseFile:
if len(line) == 1:
lineDict = dict(Type = "PSGroupSpecifier", FooterText = compiledLine.rstrip('\n'))
licensePlist["PreferenceSpecifiers"].append(lineDict)
compiledLine = '';
else:
compiledLine += line.decode('utf-8');
if len(compiledLine):
lineDict = dict(Type = "PSGroupSpecifier", FooterText = compiledLine.rstrip('\n'))
licensePlist["PreferenceSpecifiers"].append(lineDict)
licenseFile.close()
writePlist(licensePlist, os.path.join(settingsBundle, "%s.%s" % (moduleName, "plist")))
writePlist(acknowledgementsPlist, os.path.join(settingsBundle, "Acknowledgements.plist"))
if __name__ == '__main__':
main()
@eoghain
Copy link
Author

eoghain commented Oct 6, 2015

Made able to handle any license file that contains LICENSE in the name. Found this was needed for some CocoaPods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment