From fddcf0c25914fb7c4497363926f9627b035a162a Mon Sep 17 00:00:00 2001 From: Jacob Trimble Date: Wed, 10 May 2017 10:57:55 -0700 Subject: [PATCH] Use logging module over print. Rather than using print, use the logging module. This allows embedding environments to setup logging how they want. This also ensures the logs are flushed properly. The prints for check.py, checkversion.py, stats.py, and for the --help flag will still be printed to stdout since that would be the expected output location. Change-Id: I26e1bc866803c42981628c7e62261d13c7b3ff2b --- build/build.py | 25 +++++++++++++------------ build/check.py | 17 +++++++++-------- build/checkversion.py | 20 ++++++++++---------- build/docs.py | 4 +++- build/gendeps.py | 3 ++- build/shakaBuildHelpers.py | 15 ++++++++++----- build/stats.py | 11 ++++++----- build/test.py | 28 +++++++++++++++------------- 8 files changed, 68 insertions(+), 55 deletions(-) diff --git a/build/build.py b/build/build.py index 63db4de864..cbeb1e6e3e 100755 --- a/build/build.py +++ b/build/build.py @@ -39,9 +39,9 @@ build.py --name custom +@manifests +@networking +../my_plugin.js """ +import logging import os import re -import subprocess import sys import shakaBuildHelpers @@ -127,14 +127,14 @@ def _get_build_file_path(self, name, root): build_path = os.path.join(source_base, 'build', 'types', name) if (os.path.isfile(local_path) and os.path.isfile(build_path) and local_path != build_path): - print >> sys.stderr, 'Build file "%s" is ambiguous' % name + logging.error('Build file "%s" is ambiguous', name) return None elif os.path.isfile(local_path): return local_path elif os.path.isfile(build_path): return build_path else: - print >> sys.stderr, 'Build file not found: ' + name + logging.error('Build file not found: %s', name) return None def _combine(self, other): @@ -162,7 +162,7 @@ def add_core(self): core_build.parse_build(['+@core'], os.getcwd()) core_files = core_build.include if self.exclude & core_files: - print >> sys.stderr, 'Cannot exclude files from core' + logging.error('Cannot exclude files from core') self.include |= core_files def parse_build(self, lines, root): @@ -196,7 +196,7 @@ def parse_build(self, lines, root): is_neg = True line = line[1:].strip() else: - print >> sys.stderr, 'Operation (+/-) required' + logging.error('Operation (+/-) required') return False if line[0] == '@': @@ -221,7 +221,7 @@ def parse_build(self, lines, root): if not os.path.isabs(line): line = os.path.abspath(os.path.join(root, line)) if not os.path.isfile(line): - print >> sys.stderr, 'Unable to find file ' + line + logging.error('Unable to find file: %s', line) return False if is_neg: @@ -256,7 +256,7 @@ def build_raw(self, extra_opts, is_debug): cmd_line = ['java', '-jar', jar] + closure_opts + extra_opts + files if shakaBuildHelpers.execute_get_code(cmd_line) != 0: - print >> sys.stderr, 'Build failed' + logging.error('Build failed') return False return True @@ -281,7 +281,7 @@ def generate_externs(self, name): cmd_line = ['node', extern_generator, '--output', output] + files if shakaBuildHelpers.execute_get_code(cmd_line) != 0: - print >> sys.stderr, 'Externs generation failed' + logging.error('Externs generation failed') return False return True @@ -321,7 +321,8 @@ def build_library(self, name, rebuild, is_debug): edited_files = [f for f in complete_build.include if os.path.getmtime(f) > build_time] if not edited_files: - print 'No changes detected, not building. Use --force to override.' + logging.warning('No changes detected, not building. Use --force ' + 'to override.') return True opts = ['--create_source_map', result_map, '--js_output_file', result_file, @@ -363,7 +364,7 @@ def main(args): if args[i] == '--name': i += 1 if i == len(args): - print >> sys.stderr, '--name requires an argument' + logging.error('--name requires an argument') return 1 name = args[i] elif args[i] == '--debug': @@ -374,7 +375,7 @@ def main(args): usage() return 0 elif args[i].startswith('--'): - print >> sys.stderr, 'Unknown option', args[i] + logging.error('Unknown option: %s', args[i]) usage() return 1 else: @@ -384,7 +385,7 @@ def main(args): if not lines: lines = ['+@complete'] - print 'Compiling the library...' + logging.info('Compiling the library...') custom_build = Build() if not custom_build.parse_build(lines, os.getcwd()): return 1 diff --git a/build/check.py b/build/check.py index a5f9e08dd6..830bb13854 100755 --- a/build/check.py +++ b/build/check.py @@ -22,6 +22,7 @@ * Run the linter to check for style violations. """ +import logging import os import re import sys @@ -41,7 +42,7 @@ def get(arg): def check_lint(): """Runs the linter over the library files.""" - print 'Running Closure linter...' + logging.info('Running Closure linter...') jsdoc3_tags = ','.join([ 'static', 'summary', 'namespace', 'event', 'description', 'property', @@ -68,7 +69,7 @@ def check_html_lint(): if not shakaBuildHelpers.update_node_modules(): return False - print 'Running htmlhint...' + logging.info('Running htmlhint...') htmlhint_path = shakaBuildHelpers.get_node_binary_path('htmlhint') base = shakaBuildHelpers.get_source_base() files = ['index.html', 'demo/index.html', 'support.html'] @@ -87,14 +88,14 @@ def check_complete(): Returns: True on success, False on failure. """ - print 'Checking that the build files are complete...' + logging.info('Checking that the build files are complete...') complete = build.Build() # Normally we don't need to include @core, but because we look at the build # object directly, we need to include it here. When using main(), it will # call addCore which will ensure core is included. if not complete.parse_build(['+@complete', '+@core'], os.getcwd()): - print >> sys.stderr, 'Error parsing complete build' + logging.error('Error parsing complete build') return False match = re.compile(r'.*\.js$') @@ -103,10 +104,10 @@ def check_complete(): missing_files = set(all_files) - complete.include if missing_files: - print >> sys.stderr, 'There are files missing from the complete build:' + logging.error('There are files missing from the complete build:') for missing in missing_files: # Convert to a path relative to source base. - print >> sys.stderr, ' ' + os.path.relpath(missing, base) + logging.error(' ' + os.path.relpath(missing, base)) return False return True @@ -117,7 +118,7 @@ def check_tests(): Returns: True on success, False on failure. """ - print 'Checking the tests for type errors...' + logging.info('Checking the tests for type errors...') match = re.compile(r'.*\.js$') base = shakaBuildHelpers.get_source_base() @@ -146,7 +147,7 @@ def main(args): usage() return 0 else: - print >> sys.stderr, 'Unknown option', arg + logging.error('Unknown option: %s', arg) usage() return 1 diff --git a/build/checkversion.py b/build/checkversion.py index 0984a4a318..998859c84f 100755 --- a/build/checkversion.py +++ b/build/checkversion.py @@ -16,9 +16,9 @@ """Checks that all the versions match.""" +import logging import os import re -import sys import shakaBuildHelpers @@ -53,26 +53,26 @@ def check_version(_): ret = 0 if 'dirty' in git: - print >> sys.stderr, 'Git version is dirty.' + logging.error('Git version is dirty.') ret = 1 elif 'unknown' in git: - print >> sys.stderr, 'Git version is not a tag.' + logging.error('Git version is not a tag.') ret = 1 elif not re.match(r'^v[0-9]+\.[0-9]+\.[0-9]+(?:-[a-z0-9]+)?$', git): - print >> sys.stderr, 'Git version is a malformed release version.' - print >> sys.stderr, 'It should be a \'v\', followed by three numbers' - print >> sys.stderr, 'separated by dots, optionally followed by a hyphen' - print >> sys.stderr, 'and a pre-release identifier. See http://semver.org/' + logging.error('Git version is a malformed release version.') + logging.error('It should be a \'v\', followed by three numbers') + logging.error('separated by dots, optionally followed by a hyphen') + logging.error('and a pre-release identifier. See http://semver.org/') ret = 1 if 'v' + npm != git: - print >> sys.stderr, 'NPM version does not match git version.' + logging.error('NPM version does not match git version.') ret = 1 if player != git + '-debug': - print >> sys.stderr, 'Player version does not match git version.' + logging.error('Player version does not match git version.') ret = 1 if 'v' + changelog != git: - print >> sys.stderr, 'Changelog version does not match git version.' + logging.error('Changelog version does not match git version.') ret = 1 return ret diff --git a/build/docs.py b/build/docs.py index 44d47390e5..7bbffd4aca 100755 --- a/build/docs.py +++ b/build/docs.py @@ -19,15 +19,17 @@ This deletes the old documentation first. """ +import logging import os import shutil +import sys import shakaBuildHelpers def build_docs(_): """Builds the source code documentation.""" - print 'Building the docs...' + logging.info('Building the docs...') base = shakaBuildHelpers.get_source_base() shutil.rmtree(os.path.join(base, 'docs', 'api'), ignore_errors=True) diff --git a/build/gendeps.py b/build/gendeps.py index 2f8e5861e9..8d7b909a25 100755 --- a/build/gendeps.py +++ b/build/gendeps.py @@ -16,6 +16,7 @@ """Creates the Closure dependencies file required to run in uncompiled mode.""" +import logging import os import subprocess import sys @@ -31,7 +32,7 @@ def gen_deps(_): """Generates the uncompiled dependencies files.""" - print 'Generating Closure dependencies...' + logging.info('Generating Closure dependencies...') # Make the dist/ folder, ignore errors. base = shakaBuildHelpers.get_source_base() diff --git a/build/shakaBuildHelpers.py b/build/shakaBuildHelpers.py index 4693bec328..284619c6a7 100644 --- a/build/shakaBuildHelpers.py +++ b/build/shakaBuildHelpers.py @@ -21,6 +21,7 @@ """ import errno +import logging import os import platform import re @@ -114,13 +115,13 @@ def execute_subprocess(args, pipeOut=True): The same value as subprocess.Popen. """ if os.environ.get('PRINT_ARGUMENTS'): - print ' '.join([quote_argument(x) for x in args]) + logging.info(' '.join([quote_argument(x) for x in args])) try: out = subprocess.PIPE if pipeOut else None return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=out) except OSError as e: if e.errno == errno.ENOENT: - print >> sys.stderr, '*** A required dependency is missing: ' + args[0] + logging.error('*** A required dependency is missing: %s', args[0]) # Exit early to avoid showing a confusing stack trace. sys.exit(1) raise @@ -237,8 +238,8 @@ def update_node_modules(): version = execute_get_output([cmd, '-v']) if _parse_version(version) < _parse_version('1.3.12'): - print >> sys.stderr, 'npm version is too old, please upgrade. e.g.:' - print >> sys.stderr, ' npm install -g npm' + logging.error('npm version is too old, please upgrade. e.g.:') + logging.error(' npm install -g npm') return False # Update the modules. @@ -256,11 +257,15 @@ def run_main(main): Args: main: The main function to call. """ + logging.getLogger().setLevel(logging.INFO) + fmt = '[%(levelname)s] %(message)s' + logging.basicConfig(format=fmt) + try: sys.exit(main(sys.argv[1:])) except KeyboardInterrupt: if os.environ.get('RAISE_INTERRUPT'): raise print >> sys.stderr # Clear the current line that has ^C on it. - print >> sys.stderr, 'Keyboard interrupt' + logging.error('Keyboard interrupt') sys.exit(1) diff --git a/build/stats.py b/build/stats.py index 125f89f42d..9733fae6e9 100755 --- a/build/stats.py +++ b/build/stats.py @@ -33,6 +33,7 @@ """ import json +import logging import math import os import string @@ -822,7 +823,7 @@ def main(args): print_help() return 0 else: - print >> sys.stderr, 'Unrecognized argument:', arg + logging.error('Unrecognized argument: %s', arg) print_help() return 1 @@ -844,18 +845,18 @@ def main(args): os.path.join(base, 'dist', 'shaka-player.' + name + '.debug.map')): name = os.path.join(base, 'dist', 'shaka-player.' + name + '.debug.map') else: - print >> sys.stderr, name, 'not found; build Shaka first.' + logging.error('"%s" not found; build Shaka first.', name) return 1 # Verify arguments are correct. if (options.print_sizes + options.print_deps + options.print_tokens + options.is_class) != 1: - print >> sys.stderr, 'Must include exactly one output type.' + logging.error('Must include exactly one output type.') print_help() return 1 elif options.in_dot and not options.print_deps and not options.is_class: - line = '--dot-format only valid with --function-deps or --class-deps.' - print >> sys.stderr, line + logging.error('--dot-format only valid with --function-deps or ' + '--class-deps.') return 1 else: process(open(name).read(), options) diff --git a/build/test.py b/build/test.py index b31c702259..d1f1967c84 100755 --- a/build/test.py +++ b/build/test.py @@ -16,8 +16,8 @@ """Runs unit and integrations tests on the library.""" +import logging import platform -import sys import build import gendeps @@ -35,20 +35,21 @@ def run_tests_single(args): # Get the browsers supported on the local system. browsers = _get_browsers() if not browsers: - print >> sys.stderr, 'Unrecognized system "%s"' % platform.uname()[0] + logging.error('Unrecognized system: %s', platform.uname()[0]) return 1 - print 'Starting tests...' + logging.info('Starting tests...') if not args: # Run tests in all available browsers. - print 'Running with platform default:', '--browsers', browsers + logging.warning('Running with platform default: --browsers %s', browsers) cmd_line = cmd + ['--browsers', browsers] return shakaBuildHelpers.execute_get_code(cmd_line) else: # Run with command-line arguments from the user. if '--browsers' not in args: - print 'No --browsers specified.' - print 'In this mode, browsers must be manually connected to karma.' + logging.warning('No --browsers specified.') + logging.warning('In this mode, browsers must be manually connected to ' + 'karma.') cmd_line = cmd + args return shakaBuildHelpers.execute_get_code(cmd_line) @@ -57,25 +58,26 @@ def run_tests_multiple(args): """Runs multiple iterations of the tests when --runs is set.""" index = args.index('--runs') + 1 if index == len(args) or args[index].startswith('--'): - print >> sys.stderr, 'Argument Error: --runs requires a value.' + logging.error('Argument Error: --runs requires a value.') return 1 try: runs = int(args[index]) except ValueError: - print >> sys.stderr, 'Argument Error: --runs value must be an integer.' + logging.error('Argument Error: --runs value must be an integer.') return 1 if runs <= 0: - print >> sys.stderr, 'Argument Error: --runs value must be greater than 0.' + logging.error('Argument Error: --runs value must be greater than 0.') return 1 results = [] - print '\nRunning the tests %d times.' % runs + logging.info('Running the tests %d times.', runs) for _ in range(runs): results.append(run_tests_single(args)) - print '\nAll runs completed.' - print '%d passed out of %d total runs.' % (results.count(0), len(results)) - print 'Results (exit code): %r' % results + logging.info('\nAll runs completed.') + logging.info('%d passed out of %d total runs.', results.count(0), + len(results)) + logging.info('Results (exit code): %r', results) return all(result == 0 for result in results)