Skip to content

Commit

Permalink
Use logging module over print.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TheModMaker committed May 10, 2017
1 parent 8d1dfbd commit fddcf0c
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 55 deletions.
25 changes: 13 additions & 12 deletions build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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] == '@':
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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':
Expand All @@ -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:
Expand All @@ -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
Expand Down
17 changes: 9 additions & 8 deletions build/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Run the linter to check for style violations.
"""

import logging
import os
import re
import sys
Expand All @@ -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',
Expand All @@ -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']
Expand All @@ -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$')
Expand All @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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

Expand Down
20 changes: 10 additions & 10 deletions build/checkversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

"""Checks that all the versions match."""

import logging
import os
import re
import sys

import shakaBuildHelpers

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion build/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion build/gendeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

"""Creates the Closure dependencies file required to run in uncompiled mode."""

import logging
import os
import subprocess
import sys
Expand All @@ -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()
Expand Down
15 changes: 10 additions & 5 deletions build/shakaBuildHelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

import errno
import logging
import os
import platform
import re
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)
11 changes: 6 additions & 5 deletions build/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"""

import json
import logging
import math
import os
import string
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit fddcf0c

Please sign in to comment.