forked from carloe/LicenseGenerator-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credits.py
executable file
·161 lines (132 loc) · 5.53 KB
/
credits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/python
# coding: utf-8
"""
Recursively searches the input directory for 'LICENSE.*' files and compiles
them into a Settings.bundle friendly plist. Inspired by JosephH
and Sean's comments on stackoverflow: http://stackoverflow.com/q/6428353
:usage: ./credits.py -s project/ -o project/Settings.bundle/Credits.plist
:author: Carlo Eugster (http://carlo.io)
:license: MIT, see LICENSE for more details.
"""
import os
import sys
import plistlib
import re
import codecs
from optparse import OptionParser
from optparse import Option, OptionValueError
from copy import deepcopy
VERSION = '0.4.0'
PROG = os.path.basename(os.path.splitext(__file__)[0])
DESCRIPTION = """Generate a `Settings.bundle` friendly plist file from all
'LICENSE.*' files in a given directory. Inspired by JosephH and Sean's
comments on stackoverflow: http://stackoverflow.com/q/6428353"""
class MultipleOption(Option):
ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
values.ensure_value(dest, []).append(value)
else:
Option.take_action(self, action, dest, opt, value, values, parser)
def main(_):
def exclude_callback(option, _, value, option_parser):
setattr(option_parser.values, option.dest, value.split(','))
parser = OptionParser(option_class=MultipleOption,
usage='usage: %prog -s source_path -o output_plist -e [exclude_paths]',
version='%s %s' % (PROG, VERSION),
description=DESCRIPTION)
parser.add_option('-s', '--source',
type="string",
dest='input_path',
metavar='source_path',
help='source directory to search for licenses')
parser.add_option('-o', '--output-plist',
type="string",
dest='output_file',
metavar='output_plist',
help='path to the plist to be generated')
parser.add_option('-e', '--exclude',
action="callback", type="string",
dest='excludes',
metavar='path1, ...',
help='comma separated list of paths to be excluded',
callback=exclude_callback)
parser.add_option('-t', '--test',
action="store_true",
dest='include_tests',
metavar='include_tests',
default=False,
help='include files in the `Tests` directory for unit testing')
if len(sys.argv) == 1:
parser.parse_args(['--help'])
options, args = parser.parse_args()
if not os.path.isdir(options.input_path):
print("Error: Invalid source path: %s" % options.input_path)
sys.exit(2)
if not options.output_file.endswith('.plist'):
print("Error: Outputfile must end in .plist")
sys.exit(2)
plist = plist_from_dir(
options.input_path,
options.excludes,
options.include_tests
)
plistlib.writePlist(plist, options.output_file)
return 0
def plist_from_dir(directory, excludes, include_tests):
"""
Recursively search 'dir' to generates plist objects from LICENSE files.
"""
plist = {'PreferenceSpecifiers': [], 'StringsTable': 'Acknowledgements'}
license_paths = license_paths_form_dir(directory)
plist_paths = (plist_path for plist_path in license_paths if not exclude_path(plist_path, excludes, include_tests))
for plist_path in plist_paths:
license_dict = plist_from_file(plist_path)
plist['PreferenceSpecifiers'].append(license_dict)
plist['PreferenceSpecifiers'] = sorted(plist['PreferenceSpecifiers'], key=lambda x: x['Title'])
return plist
def license_paths_form_dir(directory):
return_dict = []
os.chdir(sys.path[0])
for dir_path, _, file_names in os.walk(directory):
file_names = (file_name for file_name in file_names if file_name.startswith("LICENSE"))
for file_name in file_names:
return_dict.append(os.path.join(dir_path, file_name))
return return_dict
def plist_from_file(path):
"""
Returns a plist representation of the file at 'path'. Uses the name of the
parent folder for the title property.
"""
base_group = {'Type': 'PSGroupSpecifier', 'FooterText': '', 'Title': ''}
current_file = open(path, 'r')
group = deepcopy(base_group)
title = path.split("/")[-2]
group['Title'] = unicode(title, 'utf-8')
src_body = current_file.read()
body = ""
for match in re.finditer(r'(?s)((?:[^\n][\n]?)+)', src_body):
body = body + re.sub("(\\n)", " ", match.group()) + "\n\n"
body = unicode(body, 'utf-8')
group['FooterText'] = rchop(body, " \n\n")
return group
def exclude_path(path, excludes, is_testing):
if "/LicenseGenerator-iOS/Example/" in path:
return True
elif "/LicenseGenerator-iOS/Tests/" in path:
return not is_testing
elif excludes is None:
return False
for pattern in excludes:
if re.search(pattern.strip(), path, re.S) is not None:
return True
return False
def rchop(str, ending):
if str.endswith(ending):
return str[:-len(ending)]
return str
if __name__ == "__main__":
main(sys.argv[1:])