diff options
author | jamiewalch <jamiewalch@chromium.org> | 2015-06-17 16:25:34 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-17 23:27:03 +0000 |
commit | 6275de5043c03bb01065b38e3fc4b5c245562f15 (patch) | |
tree | b6578e74843e585934b3608ab479b5b02cd1f946 /tools/licenses.py | |
parent | 10fabd01b91cbc82f1e25ef1e332ec9246d68860 (diff) | |
download | chromium_src-6275de5043c03bb01065b38e3fc4b5c245562f15.zip chromium_src-6275de5043c03bb01065b38e3fc4b5c245562f15.tar.gz chromium_src-6275de5043c03bb01065b38e3fc4b5c245562f15.tar.bz2 |
Add support for custom license templates.
This allows products that are built from the Chromium source to include
third-party license attribution, even if they cannot link directly to
chrome://credits. Chrome apps such as Chromoting, for example, fall into
this category, for example.
BUG=499478
Review URL: https://codereview.chromium.org/1178323002
Cr-Commit-Position: refs/heads/master@{#334957}
Diffstat (limited to 'tools/licenses.py')
-rwxr-xr-x | tools/licenses.py | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/tools/licenses.py b/tools/licenses.py index 6b8311a..1bbb3d0 100755 --- a/tools/licenses.py +++ b/tools/licenses.py @@ -15,6 +15,7 @@ Commands: (You can also import this as a module.) """ +import argparse import cgi import os import sys @@ -422,13 +423,9 @@ def ScanThirdPartyDirs(root=None): return len(errors) == 0 -def GenerateCredits(): +def GenerateCredits(file_template_file, entry_template_file, output_file): """Generate about:credits.""" - if len(sys.argv) not in (2, 3): - print 'usage: licenses.py credits [output_file]' - return False - def EvaluateTemplate(template, env, escape=True): """Expand a template with variables like {{foo}} using a dictionary of expansions.""" @@ -441,8 +438,15 @@ def GenerateCredits(): root = os.path.join(os.path.dirname(__file__), '..') third_party_dirs = FindThirdPartyDirs(PRUNE_PATHS, root) - entry_template = open(os.path.join(root, 'chrome', 'browser', 'resources', - 'about_credits_entry.tmpl'), 'rb').read() + if not file_template_file: + file_template_file = os.path.join(root, 'chrome', 'browser', + 'resources', 'about_credits.tmpl') + if not entry_template_file: + entry_template_file = os.path.join(root, 'chrome', 'browser', + 'resources', + 'about_credits_entry.tmpl') + + entry_template = open(entry_template_file).read() entries = [] for path in third_party_dirs: try: @@ -465,32 +469,37 @@ def GenerateCredits(): entries.sort(key=lambda entry: (entry['name'], entry['content'])) entries_contents = '\n'.join([entry['content'] for entry in entries]) - file_template = open(os.path.join(root, 'chrome', 'browser', 'resources', - 'about_credits.tmpl'), 'rb').read() + file_template = open(file_template_file).read() template_contents = "<!-- Generated by licenses.py; do not edit. -->" template_contents += EvaluateTemplate(file_template, {'entries': entries_contents}, escape=False) - if len(sys.argv) == 3: - with open(sys.argv[2], 'w') as output_file: - output_file.write(template_contents) - elif len(sys.argv) == 2: + if output_file: + with open(output_file, 'w') as output: + output.write(template_contents) + else: print template_contents return True def main(): - command = 'help' - if len(sys.argv) > 1: - command = sys.argv[1] - - if command == 'scan': + parser = argparse.ArgumentParser() + parser.add_argument('--file-template', + help='Template HTML to use for the license page.') + parser.add_argument('--entry-template', + help='Template HTML to use for each license.') + parser.add_argument('command', choices=['help', 'scan', 'credits']) + parser.add_argument('output_file', nargs='?') + args = parser.parse_args() + + if args.command == 'scan': if not ScanThirdPartyDirs(): return 1 - elif command == 'credits': - if not GenerateCredits(): + elif args.command == 'credits': + if not GenerateCredits(args.file_template, args.entry_template, + args.output_file): return 1 else: print __doc__ |