|
#!/usr/bin/env python |
|
import os,sys,re |
|
import fnmatch |
|
import shutil |
|
import urllib2 |
|
|
|
def main(): |
|
|
|
lre = re.compile('^(([^C]+)Copyright [(]c[)] ___YEAR___ ___ORGANIZATIONNAME___[.] All rights reserved[.][^\n]*\n)',re.M) |
|
lcm = re.compile('(^)',re.M) |
|
lxc = re.compile('/([^/]+[.]xctemplate)$') |
|
|
|
if 'LICENSE_TYPE' not in os.environ: |
|
license_type = '1697386/GNU-GPLv3' |
|
else: |
|
license_type = os.environ['LICENSE_TYPE'] |
|
|
|
licenses = dict() |
|
|
|
for f in ['File','Proj']: |
|
req = urllib2.Request('https://raw.github.com/gist/%s.%s.txt' % (license_type,f)) |
|
try: |
|
response = urllib2.urlopen(req) |
|
licenses[f] = response.read() |
|
except: |
|
pass |
|
|
|
if not len(licenses): |
|
return 1 |
|
|
|
if 'File' not in licenses: |
|
licenses['File'] = licenses['Proj'] |
|
elif 'Proj' not in licenses: |
|
licenses['Proj'] = licenses['File'] |
|
|
|
name_prefix = os.path.basename(license_type) |
|
|
|
comments = dict() |
|
|
|
templates='/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates' |
|
if not os.path.isdir(templates): |
|
templates='/Developer/Library/Xcode/Templates' |
|
|
|
# Get each of the directories from the Templates/ |
|
for d in [os.path.join(templates, name) for name in os.listdir(templates) if os.path.isdir(os.path.join(templates, name))]: |
|
|
|
# Walk the directory |
|
for root, dirnames, filenames in os.walk(d): |
|
|
|
if not lxc.search(root): |
|
continue |
|
|
|
# Extract the [:4] of the directory to use as hash key. |
|
# This is used to correlate the fetched license and the saved comment style. |
|
untemplated = root.replace('%s/'%templates,'') |
|
template_type = untemplated[:4] |
|
|
|
dst=os.path.join(os.path.dirname(untemplated),"%s %s" % (name_prefix,os.path.basename(untemplated))) |
|
shutil.copytree(root,dst) |
|
|
|
# Iterate the files in the walked directories |
|
for xcroot, xcdirnames, xcfilenames in os.walk(dst): |
|
|
|
for filename in [os.path.join(xcroot,fn) for fn in xcfilenames]: |
|
|
|
# Slurp the file |
|
f = os.path.join(root, filename) |
|
text = file(filename).read() |
|
|
|
# Check if the file has the Copyright template |
|
m = lre.search(text) |
|
if m: |
|
|
|
# Save the commenting style of the Copyright line |
|
style = m.group(2) |
|
|
|
# If the style hasn't already been seen, then save it. |
|
if style not in comments: |
|
comments[style] = dict() |
|
# Save the license template with the comment style updated. |
|
if template_type not in comments[style]: |
|
comments[style][template_type] = lcm.sub('%s\\1' % style,licenses[template_type]) |
|
|
|
# Write the template with the updated license. |
|
f = None |
|
try: |
|
f = open(filename, 'w') |
|
f.write(lre.sub('\\1%s\n%s\n' % (style,comments[style][template_type]),text)) |
|
finally: |
|
if f: |
|
f.close() |
|
|
|
return 0 |
|
|
|
# |
|
if __name__ == "__main__": |
|
sys.exit(main()) |