Skip to content

Instantly share code, notes, and snippets.

@yvbeek
Created May 29, 2017 23:20
Show Gist options
  • Save yvbeek/0d015c618d46093b4f815e62a6a33969 to your computer and use it in GitHub Desktop.
Save yvbeek/0d015c618d46093b4f815e62a6a33969 to your computer and use it in GitHub Desktop.
Acknowledgements Generator
#!/usr/bin/env python
import os
import sys
import re
import plistlib
from codecs import open
from copy import deepcopy
# Process the input arguments
folder_arg = sys.argv[1]
output_arg = sys.argv[2]
max_depth = sys.argv[3] if len(sys.argv) > 3 else 1
os.chdir(folder_arg)
# Prepare the result
plist = {'PreferenceSpecifiers': [], 'StringsTable': 'Acknowledgements', 'Title': 'Acknowledgements'}
base_group = {'Type': 'PSGroupSpecifier', 'FooterText': '', 'Title': ''}
# Add a header
header_group = deepcopy(base_group)
header_group['Title'] = 'Acknowledgements'
header_group['FooterText'] = 'This application uses the following third party libraries:'
plist['PreferenceSpecifiers'].append(header_group)
# Enumerate all files in the provided folder
for root, dirs, files in os.walk("."):
for file in files:
# Igore licenses in deep folders
if root.count(os.path.sep) > max_depth:
continue
# Look for license files
if file.endswith('LICENSE') or file.endswith('LICENSE.txt'):
full_path = os.path.join(root, file)
print("Processing " + full_path)
# Read the contents of the license
license_title = os.path.basename(os.path.dirname(full_path))
license_text = open(full_path, 'r', 'utf-8').read()
# Clean up the text
license_text = license_text.replace(' ', ' ')
license_text = re.sub(r'(\S)[ \t]*(?:\r\n|\n)[ \t]*(\S)', '\\1 \\2', license_text)
# Create a plist block
group = deepcopy(base_group)
group['Title'] = license_title
group['FooterText'] = license_text
plist['PreferenceSpecifiers'].append(group)
# Write the plist to disk
os.chdir(sys.path[0])
plistlib.writePlist(plist, output_arg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment