From 6275de5043c03bb01065b38e3fc4b5c245562f15 Mon Sep 17 00:00:00 2001 From: jamiewalch Date: Wed, 17 Jun 2015 16:25:34 -0700 Subject: 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} --- tools/licenses.py | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'tools/licenses.py') 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 = "" 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__ -- cgit v1.1