PyLint fixes for tools-webrtc and webrtc/tools

Fix a lot of errors before bringing in the new config in
https://codereview.webrtc.org/2737963003/

BUG=webrtc:7303
NOTRY=True

Review-Url: https://codereview.webrtc.org/2736233003
Cr-Commit-Position: refs/heads/master@{#17137}
This commit is contained in:
kjellander
2017-03-09 02:26:46 -08:00
committed by Commit bot
parent 735da3ad38
commit f5318e1f39
15 changed files with 142 additions and 145 deletions

View File

@ -116,7 +116,7 @@ def _VerifyNativeApiHeadersListIsValid(input_api, output_api):
non_existing_paths)] non_existing_paths)]
return [] return []
api_change_msg = """ API_CHANGE_MSG = """
You seem to be changing native API header files. Please make sure that you: You seem to be changing native API header files. Please make sure that you:
1. Make compatible changes that don't break existing clients. Usually 1. Make compatible changes that don't break existing clients. Usually
this is done by keeping the existing method signatures unchanged. this is done by keeping the existing method signatures unchanged.
@ -145,7 +145,7 @@ def _CheckNativeApiHeaderChanges(input_api, output_api):
files.append(f) files.append(f)
if files: if files:
return [output_api.PresubmitNotifyResult(api_change_msg, files)] return [output_api.PresubmitNotifyResult(API_CHANGE_MSG, files)]
return [] return []
@ -190,7 +190,7 @@ def _CheckNoPragmaOnce(input_api, output_api):
return [] return []
def _CheckNoFRIEND_TEST(input_api, output_api): def _CheckNoFRIEND_TEST(input_api, output_api): # pylint: disable=invalid-name
"""Make sure that gtest's FRIEND_TEST() macro is not used, the """Make sure that gtest's FRIEND_TEST() macro is not used, the
FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes.""" used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
@ -452,13 +452,13 @@ def _CheckJSONParseErrors(input_api, output_api):
def _RunPythonTests(input_api, output_api): def _RunPythonTests(input_api, output_api):
def join(*args): def Join(*args):
return input_api.os_path.join(input_api.PresubmitLocalPath(), *args) return input_api.os_path.join(input_api.PresubmitLocalPath(), *args)
test_directories = [ test_directories = [
join('webrtc', 'tools', 'py_event_log_analyzer') Join('webrtc', 'tools', 'py_event_log_analyzer')
] + [ ] + [
root for root, _, files in os.walk(join('tools-webrtc')) root for root, _, files in os.walk(Join('tools-webrtc'))
if any(f.endswith('_test.py') for f in files) if any(f.endswith('_test.py') for f in files)
] ]

View File

@ -53,7 +53,7 @@ class WebRTCLinkSetup(object):
del self._links_db[source] del self._links_db[source]
def _initialize_database(filename): def _InitializeDatabase(filename):
links_database = shelve.open(filename) links_database = shelve.open(filename)
# Wipe the database if this version of the script ends up looking at a # Wipe the database if this version of the script ends up looking at a
# newer (future) version of the links db, just to be sure. # newer (future) version of the links db, just to be sure.
@ -89,7 +89,7 @@ def main():
# The database file gets .db appended on some platforms. # The database file gets .db appended on some platforms.
db_filenames = [LINKS_DB, LINKS_DB + '.db'] db_filenames = [LINKS_DB, LINKS_DB + '.db']
if any(os.path.isfile(f) for f in db_filenames): if any(os.path.isfile(f) for f in db_filenames):
links_database = _initialize_database(LINKS_DB) links_database = _InitializeDatabase(LINKS_DB)
try: try:
symlink_creator = WebRTCLinkSetup(links_database, options.dry_run) symlink_creator = WebRTCLinkSetup(links_database, options.dry_run)
symlink_creator.CleanupLinks() symlink_creator.CleanupLinks()

View File

@ -49,7 +49,7 @@ class FakeCmd(object):
def __init__(self): def __init__(self):
self.expectations = [] self.expectations = []
def add_expectation(self, *args, **kwargs): def AddExpectation(self, *args, **kwargs):
returns = kwargs.pop('_returns', None) returns = kwargs.pop('_returns', None)
self.expectations.append((args, kwargs, returns)) self.expectations.append((args, kwargs, returns))
@ -74,13 +74,13 @@ class TestRollChromiumRevision(unittest.TestCase):
self._new_cr_depsfile = os.path.join(self._output_dir, 'DEPS.chromium.new') self._new_cr_depsfile = os.path.join(self._output_dir, 'DEPS.chromium.new')
self.fake = FakeCmd() self.fake = FakeCmd()
self.old_RunCommand = getattr(roll_deps, '_RunCommand') self.old_run_command = getattr(roll_deps, '_RunCommand')
setattr(roll_deps, '_RunCommand', self.fake) setattr(roll_deps, '_RunCommand', self.fake)
def tearDown(self): def tearDown(self):
shutil.rmtree(self._output_dir, ignore_errors=True) shutil.rmtree(self._output_dir, ignore_errors=True)
self.assertEqual(self.fake.expectations, []) self.assertEqual(self.fake.expectations, [])
setattr(roll_deps, '_RunCommand', self.old_RunCommand) setattr(roll_deps, '_RunCommand', self.old_run_command)
def testUpdateDepsFile(self): def testUpdateDepsFile(self):
new_rev = 'aaaaabbbbbcccccdddddeeeeefffff0000011111' new_rev = 'aaaaabbbbbcccccdddddeeeeefffff0000011111'
@ -98,10 +98,10 @@ class TestRollChromiumRevision(unittest.TestCase):
local_scope = ParseDepsDict(deps_contents) local_scope = ParseDepsDict(deps_contents)
vars_dict = local_scope['vars'] vars_dict = local_scope['vars']
def assertVar(variable_name): def AssertVar(variable_name):
self.assertEquals(vars_dict[variable_name], TEST_DATA_VARS[variable_name]) self.assertEquals(vars_dict[variable_name], TEST_DATA_VARS[variable_name])
assertVar('chromium_git') AssertVar('chromium_git')
assertVar('chromium_revision') AssertVar('chromium_revision')
self.assertEquals(len(local_scope['deps']), 3) self.assertEquals(len(local_scope['deps']), 3)
self.assertEquals(len(local_scope['deps_os']), 1) self.assertEquals(len(local_scope['deps_os']), 1)
@ -137,7 +137,7 @@ class TestRollChromiumRevision(unittest.TestCase):
def _SetupGitLsRemoteCall(cmd_fake, url, revision): def _SetupGitLsRemoteCall(cmd_fake, url, revision):
cmd = ['git', 'ls-remote', url, revision] cmd = ['git', 'ls-remote', url, revision]
cmd_fake.add_expectation(cmd, _returns=(revision, None)) cmd_fake.AddExpectation(cmd, _returns=(revision, None))
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -39,7 +39,7 @@ class Logger(object):
self.log_level = DISPLAY_LEVEL self.log_level = DISPLAY_LEVEL
self.messages_left = messages_left self.messages_left = messages_left
def log(self, build_file_path, line_number, target_name, source_file, def Log(self, build_file_path, line_number, target_name, source_file,
subpackage): subpackage):
if self.messages_left is not None: if self.messages_left is not None:
if not self.messages_left: if not self.messages_left:
@ -91,7 +91,7 @@ def _CheckBuildFile(build_file_path, packages, logger):
line_number = subpackages_match.group('line_number') line_number = subpackages_match.group('line_number')
if subpackage: if subpackage:
found_violations = True found_violations = True
logger.log(build_file_path, line_number, target_name, source_file, logger.Log(build_file_path, line_number, target_name, source_file,
subpackage) subpackage)
return found_violations return found_violations

View File

@ -30,7 +30,7 @@ class Logger(object):
self.messages = [] self.messages = []
self.test_dir = test_dir self.test_dir = test_dir
def log(self, build_file_path, line_number, target_name, source_file, def Log(self, build_file_path, line_number, target_name, source_file,
subpackage): subpackage):
build_file_path = os.path.relpath(build_file_path, self.test_dir) build_file_path = os.path.relpath(build_file_path, self.test_dir)
build_file_path = build_file_path.replace(os.path.sep, '/') build_file_path = build_file_path.replace(os.path.sep, '/')
@ -48,19 +48,19 @@ class UnitTest(unittest.TestCase):
expected_messages = ReadPylFile(os.path.join(test_dir, 'expected.pyl')) expected_messages = ReadPylFile(os.path.join(test_dir, 'expected.pyl'))
self.assertListEqual(sorted(expected_messages), sorted(logger.messages)) self.assertListEqual(sorted(expected_messages), sorted(logger.messages))
def test_no_errors(self): def testNoErrors(self):
self.RunTest(os.path.join(TESTDATA_DIR, 'no_errors')) self.RunTest(os.path.join(TESTDATA_DIR, 'no_errors'))
def test_multiple_errors_single_target(self): def testMultipleErrorsSingleTarget(self):
self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_single_target')) self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_single_target'))
def test_multiple_errors_multiple_targets(self): def testMultipleErrorsMultipleTargets(self):
self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_multiple_targets')) self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_multiple_targets'))
def test_common_prefix(self): def testCommonPrefix(self):
self.RunTest(os.path.join(TESTDATA_DIR, 'common_prefix')) self.RunTest(os.path.join(TESTDATA_DIR, 'common_prefix'))
def test_all_build_files(self): def testAllBuildFiles(self):
self.RunTest(os.path.join(TESTDATA_DIR, 'all_build_files'), True) self.RunTest(os.path.join(TESTDATA_DIR, 'all_build_files'), True)

View File

@ -15,19 +15,16 @@ This file emits the list of reasons why a particular build needs to be clobbered
import os import os
import sys import sys
script_dir = os.path.dirname(os.path.realpath(__file__)) SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
checkout_root = os.path.abspath(os.path.join(script_dir, os.pardir)) CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir))
sys.path.insert(0, os.path.join(checkout_root, 'build')) sys.path.insert(0, os.path.join(CHECKOUT_ROOT, 'build'))
import landmine_utils import landmine_utils
distributor = landmine_utils.distributor platform = landmine_utils.platform # pylint: disable=invalid-name
gyp_defines = landmine_utils.gyp_defines
gyp_msvs_version = landmine_utils.gyp_msvs_version
platform = landmine_utils.platform
def print_landmines(): def print_landmines(): # pylint: disable=invalid-name
""" """
ALL LANDMINES ARE EMITTED FROM HERE. ALL LANDMINES ARE EMITTED FROM HERE.
""" """
@ -54,7 +51,7 @@ def print_landmines():
# is no longer the case. # is no longer the case.
print 'Clobber due to iOS compile errors (crbug.com/694721)' print 'Clobber due to iOS compile errors (crbug.com/694721)'
print 'Clobber to unblock https://codereview.webrtc.org/2709573003' print 'Clobber to unblock https://codereview.webrtc.org/2709573003'
print 'Clobber to fix https://codereview.webrtc.org/2709573003 after landing' print 'Clobber to fix after https://codereview.webrtc.org/2709573003'
def main(): def main():

View File

@ -43,7 +43,7 @@ class TemporaryDirectory(object):
def __enter__(self): def __enter__(self):
return self._name return self._name
def __exit__(self, exc, value, tb): def __exit__(self, exc, value, _tb):
if self._name and not self._closed: if self._name and not self._closed:
shutil.rmtree(self._name) shutil.rmtree(self._name)
self._closed = True self._closed = True

View File

@ -55,14 +55,14 @@ class NonStrippingEpilogOptionParser(optparse.OptionParser):
return self.epilog return self.epilog
def _get_external_ip(): def _GetExternalIp():
"""Finds out the machine's external IP by connecting to google.com.""" """Finds out the machine's external IP by connecting to google.com."""
external_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) external_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
external_socket.connect(('google.com', 80)) external_socket.connect(('google.com', 80))
return external_socket.getsockname()[0] return external_socket.getsockname()[0]
def _parse_args(): def _ParseArgs():
"""Define and parse the command-line arguments.""" """Define and parse the command-line arguments."""
presets_string = '\n'.join(str(p) for p in _PRESETS) presets_string = '\n'.join(str(p) for p in _PRESETS)
parser = NonStrippingEpilogOptionParser(epilog=( parser = NonStrippingEpilogOptionParser(epilog=(
@ -123,11 +123,11 @@ def _parse_args():
except ValueError: except ValueError:
parser.error('Invalid port range specified.') parser.error('Invalid port range specified.')
_set_logger(options.verbose) _InitLogging(options.verbose)
return options return options
def _set_logger(verbose): def _InitLogging(verbose):
"""Setup logging.""" """Setup logging."""
log_level = _DEFAULT_LOG_LEVEL log_level = _DEFAULT_LOG_LEVEL
if verbose: if verbose:
@ -135,8 +135,8 @@ def _set_logger(verbose):
logging.basicConfig(level=log_level, format='%(message)s') logging.basicConfig(level=log_level, format='%(message)s')
def _main(): def main():
options = _parse_args() options = _ParseArgs()
# Build a configuration object. Override any preset configuration settings if # Build a configuration object. Override any preset configuration settings if
# a value of a setting was also given as a flag. # a value of a setting was also given as a flag.
@ -154,19 +154,19 @@ def _main():
emulator = network_emulator.NetworkEmulator(connection_config, emulator = network_emulator.NetworkEmulator(connection_config,
options.port_range) options.port_range)
try: try:
emulator.check_permissions() emulator.CheckPermissions()
except network_emulator.NetworkEmulatorError as e: except network_emulator.NetworkEmulatorError as e:
logging.error('Error: %s\n\nCause: %s', e.fail_msg, e.error) logging.error('Error: %s\n\nCause: %s', e.fail_msg, e.error)
return -1 return -1
if not options.target_ip: if not options.target_ip:
external_ip = _get_external_ip() external_ip = _GetExternalIp()
else: else:
external_ip = options.target_ip external_ip = options.target_ip
logging.info('Constraining traffic to/from IP: %s', external_ip) logging.info('Constraining traffic to/from IP: %s', external_ip)
try: try:
emulator.emulate(external_ip) emulator.Emulate(external_ip)
logging.info('Started network emulation with the following configuration:\n' logging.info('Started network emulation with the following configuration:\n'
' Receive bandwidth: %s kbps (%s kB/s)\n' ' Receive bandwidth: %s kbps (%s kB/s)\n'
' Send bandwidth : %s kbps (%s kB/s)\n' ' Send bandwidth : %s kbps (%s kB/s)\n'
@ -184,7 +184,7 @@ def _main():
options.port_range[0], options.port_range[1]) options.port_range[0], options.port_range[1])
raw_input('Press Enter to abort Network Emulation...') raw_input('Press Enter to abort Network Emulation...')
logging.info('Flushing all Dummynet rules...') logging.info('Flushing all Dummynet rules...')
network_emulator.cleanup() network_emulator.Cleanup()
logging.info('Completed Network Emulation.') logging.info('Completed Network Emulation.')
return 0 return 0
except network_emulator.NetworkEmulatorError as e: except network_emulator.NetworkEmulatorError as e:
@ -192,4 +192,4 @@ def _main():
return -2 return -2
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(_main()) sys.exit(main())

View File

@ -53,20 +53,20 @@ class NetworkEmulator(object):
self._port_range = port_range self._port_range = port_range
self._connection_config = connection_config self._connection_config = connection_config
def emulate(self, target_ip): def Emulate(self, target_ip):
"""Starts a network emulation by setting up Dummynet rules. """Starts a network emulation by setting up Dummynet rules.
Args: Args:
target_ip: The IP address of the interface that shall be that have the target_ip: The IP address of the interface that shall be that have the
network constraints applied to it. network constraints applied to it.
""" """
receive_pipe_id = self._create_dummynet_pipe( receive_pipe_id = self._CreateDummynetPipe(
self._connection_config.receive_bw_kbps, self._connection_config.receive_bw_kbps,
self._connection_config.delay_ms, self._connection_config.delay_ms,
self._connection_config.packet_loss_percent, self._connection_config.packet_loss_percent,
self._connection_config.queue_slots) self._connection_config.queue_slots)
logging.debug('Created receive pipe: %s', receive_pipe_id) logging.debug('Created receive pipe: %s', receive_pipe_id)
send_pipe_id = self._create_dummynet_pipe( send_pipe_id = self._CreateDummynetPipe(
self._connection_config.send_bw_kbps, self._connection_config.send_bw_kbps,
self._connection_config.delay_ms, self._connection_config.delay_ms,
self._connection_config.packet_loss_percent, self._connection_config.packet_loss_percent,
@ -74,15 +74,15 @@ class NetworkEmulator(object):
logging.debug('Created send pipe: %s', send_pipe_id) logging.debug('Created send pipe: %s', send_pipe_id)
# Adding the rules will start the emulation. # Adding the rules will start the emulation.
incoming_rule_id = self._create_dummynet_rule(receive_pipe_id, 'any', incoming_rule_id = self._CreateDummynetRule(receive_pipe_id, 'any',
target_ip, self._port_range) target_ip, self._port_range)
logging.debug('Created incoming rule: %s', incoming_rule_id) logging.debug('Created incoming rule: %s', incoming_rule_id)
outgoing_rule_id = self._create_dummynet_rule(send_pipe_id, target_ip, outgoing_rule_id = self._CreateDummynetRule(send_pipe_id, target_ip,
'any', self._port_range) 'any', self._port_range)
logging.debug('Created outgoing rule: %s', outgoing_rule_id) logging.debug('Created outgoing rule: %s', outgoing_rule_id)
@staticmethod @staticmethod
def check_permissions(): def CheckPermissions():
"""Checks if permissions are available to run Dummynet commands. """Checks if permissions are available to run Dummynet commands.
Raises: Raises:
@ -99,7 +99,7 @@ class NetworkEmulator(object):
raise NetworkEmulatorError('You must run this script with administrator' raise NetworkEmulatorError('You must run this script with administrator'
' privileges.') ' privileges.')
def _create_dummynet_rule(self, pipe_id, from_address, to_address, def _CreateDummynetRule(self, pipe_id, from_address, to_address,
port_range): port_range):
"""Creates a network emulation rule and returns its ID. """Creates a network emulation rule and returns its ID.
@ -118,13 +118,13 @@ class NetworkEmulator(object):
self._rule_counter += 100 self._rule_counter += 100
add_part = ['add', self._rule_counter, 'pipe', pipe_id, add_part = ['add', self._rule_counter, 'pipe', pipe_id,
'ip', 'from', from_address, 'to', to_address] 'ip', 'from', from_address, 'to', to_address]
_run_ipfw_command(add_part + ['src-port', '%s-%s' % port_range], _RunIpfwCommand(add_part + ['src-port', '%s-%s' % port_range],
'Failed to add Dummynet src-port rule.') 'Failed to add Dummynet src-port rule.')
_run_ipfw_command(add_part + ['dst-port', '%s-%s' % port_range], _RunIpfwCommand(add_part + ['dst-port', '%s-%s' % port_range],
'Failed to add Dummynet dst-port rule.') 'Failed to add Dummynet dst-port rule.')
return self._rule_counter return self._rule_counter
def _create_dummynet_pipe(self, bandwidth_kbps, delay_ms, packet_loss_percent, def _CreateDummynetPipe(self, bandwidth_kbps, delay_ms, packet_loss_percent,
queue_slots): queue_slots):
"""Creates a Dummynet pipe and return its ID. """Creates a Dummynet pipe and return its ID.
@ -146,21 +146,21 @@ class NetworkEmulator(object):
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
error_message += ('Make sure you have loaded the ipfw_mod.ko module to ' error_message += ('Make sure you have loaded the ipfw_mod.ko module to '
'your kernel (sudo insmod /path/to/ipfw_mod.ko).') 'your kernel (sudo insmod /path/to/ipfw_mod.ko).')
_run_ipfw_command(cmd, error_message) _RunIpfwCommand(cmd, error_message)
return self._pipe_counter return self._pipe_counter
def cleanup(): def Cleanup():
"""Stops the network emulation by flushing all Dummynet rules. """Stops the network emulation by flushing all Dummynet rules.
Notice that this will flush any rules that may have been created previously Notice that this will flush any rules that may have been created previously
before starting the emulation. before starting the emulation.
""" """
_run_ipfw_command(['-f', 'flush'], _RunIpfwCommand(['-f', 'flush'],
'Failed to flush Dummynet rules!') 'Failed to flush Dummynet rules!')
_run_ipfw_command(['-f', 'pipe', 'flush'], _RunIpfwCommand(['-f', 'pipe', 'flush'],
'Failed to flush Dummynet pipes!') 'Failed to flush Dummynet pipes!')
def _run_ipfw_command(command, fail_msg=None): def _RunIpfwCommand(command, fail_msg=None):
"""Executes a command and prefixes the appropriate command for """Executes a command and prefixes the appropriate command for
Windows or Linux/UNIX. Windows or Linux/UNIX.

View File

@ -89,10 +89,10 @@ def main():
ffmpeg_path = os.path.join(toolchain_dir, 'linux', 'ffmpeg') ffmpeg_path = os.path.join(toolchain_dir, 'linux', 'ffmpeg')
def convert_video(input_video, output_video): def ConvertVideo(input_video, output_video):
_RunCommand([ffmpeg_path, '-y', '-i', input_video, output_video]) _RunCommand([ffmpeg_path, '-y', '-i', input_video, output_video])
convert_video(test_video, test_video_yuv) ConvertVideo(test_video, test_video_yuv)
reference_video = os.path.join(SRC_DIR, reference_video = os.path.join(SRC_DIR,
'resources', 'reference_video_640x360_30fps.y4m') 'resources', 'reference_video_640x360_30fps.y4m')
@ -100,7 +100,7 @@ def main():
reference_video_yuv = os.path.join(temp_dir, reference_video_yuv = os.path.join(temp_dir,
'reference_video_640x360_30fps.yuv') 'reference_video_640x360_30fps.yuv')
convert_video(reference_video, reference_video_yuv) ConvertVideo(reference_video, reference_video_yuv)
# Run compare script. # Run compare script.
compare_script = os.path.join(SRC_DIR, 'webrtc', 'tools', compare_script = os.path.join(SRC_DIR, 'webrtc', 'tools',

View File

@ -21,7 +21,7 @@ import helper_functions
sys.stderr = sys.stdout sys.stderr = sys.stdout
def convert_yuv_to_png_files(yuv_file_name, yuv_frame_width, yuv_frame_height, def ConvertYuvToPngFiles(yuv_file_name, yuv_frame_width, yuv_frame_height,
output_directory, ffmpeg_path): output_directory, ffmpeg_path):
"""Converts a YUV video file into PNG frames. """Converts a YUV video file into PNG frames.
@ -61,7 +61,7 @@ def convert_yuv_to_png_files(yuv_file_name, yuv_frame_width, yuv_frame_height,
return True return True
def decode_frames(input_directory, zxing_path): def DecodeFrames(input_directory, zxing_path):
"""Decodes the barcodes overlaid in each frame. """Decodes the barcodes overlaid in each frame.
The function uses the Zxing command-line tool from the Zxing C++ distribution The function uses the Zxing command-line tool from the Zxing C++ distribution
@ -85,11 +85,11 @@ def decode_frames(input_directory, zxing_path):
print 'Decoding barcodes from PNG files with %s...' % zxing_path print 'Decoding barcodes from PNG files with %s...' % zxing_path
return helper_functions.perform_action_on_all_files( return helper_functions.perform_action_on_all_files(
directory=input_directory, file_pattern='frame_', directory=input_directory, file_pattern='frame_',
file_extension='png', start_number=1, action=_decode_barcode_in_file, file_extension='png', start_number=1, action=_DecodeBarcodeInFile,
command_line_decoder=zxing_path) command_line_decoder=zxing_path)
def _decode_barcode_in_file(file_name, command_line_decoder): def _DecodeBarcodeInFile(file_name, command_line_decoder):
"""Decodes the barcode in the upper left corner of a PNG file. """Decodes the barcode in the upper left corner of a PNG file.
Args: Args:
@ -116,7 +116,7 @@ def _decode_barcode_in_file(file_name, command_line_decoder):
return True return True
def _generate_stats_file(stats_file_name, input_directory='.'): def _GenerateStatsFile(stats_file_name, input_directory='.'):
"""Generate statistics file. """Generate statistics file.
The function generates a statistics file. The contents of the file are in the The function generates a statistics file. The contents of the file are in the
@ -128,7 +128,7 @@ def _generate_stats_file(stats_file_name, input_directory='.'):
stats_file = open(stats_file_name, 'w') stats_file = open(stats_file_name, 'w')
print 'Generating stats file: %s' % stats_file_name print 'Generating stats file: %s' % stats_file_name
for i in range(1, _count_frames_in(input_directory=input_directory) + 1): for i in range(1, _CountFramesIn(input_directory=input_directory) + 1):
frame_number = helper_functions.zero_pad(i) frame_number = helper_functions.zero_pad(i)
barcode_file_name = file_prefix + frame_number + '.txt' barcode_file_name = file_prefix + frame_number + '.txt'
png_frame = file_prefix + frame_number + '.png' png_frame = file_prefix + frame_number + '.png'
@ -136,10 +136,10 @@ def _generate_stats_file(stats_file_name, input_directory='.'):
entry = 'frame_' + entry_frame_number + ' ' entry = 'frame_' + entry_frame_number + ' '
if os.path.isfile(barcode_file_name): if os.path.isfile(barcode_file_name):
barcode = _read_barcode_from_text_file(barcode_file_name) barcode = _ReadBarcodeFromTextFile(barcode_file_name)
os.remove(barcode_file_name) os.remove(barcode_file_name)
if _check_barcode(barcode): if _CheckBarcode(barcode):
entry += (helper_functions.zero_pad(int(barcode[0:11])) + '\n') entry += (helper_functions.zero_pad(int(barcode[0:11])) + '\n')
else: else:
entry += 'Barcode error\n' # Barcode is wrongly detected. entry += 'Barcode error\n' # Barcode is wrongly detected.
@ -152,7 +152,7 @@ def _generate_stats_file(stats_file_name, input_directory='.'):
stats_file.close() stats_file.close()
def _read_barcode_from_text_file(barcode_file_name): def _ReadBarcodeFromTextFile(barcode_file_name):
"""Reads the decoded barcode for a .txt file. """Reads the decoded barcode for a .txt file.
Args: Args:
@ -166,7 +166,7 @@ def _read_barcode_from_text_file(barcode_file_name):
return barcode return barcode
def _check_barcode(barcode): def _CheckBarcode(barcode):
"""Check weather the UPC-A barcode was decoded correctly. """Check weather the UPC-A barcode was decoded correctly.
This function calculates the check digit of the provided barcode and compares This function calculates the check digit of the provided barcode and compares
@ -200,7 +200,7 @@ def _check_barcode(barcode):
return dsum == int(barcode[11]) return dsum == int(barcode[11])
def _count_frames_in(input_directory='.'): def _CountFramesIn(input_directory='.'):
"""Calculates the number of frames in the input directory. """Calculates the number of frames in the input directory.
The function calculates the number of frames in the input directory. The The function calculates the number of frames in the input directory. The
@ -225,7 +225,7 @@ def _count_frames_in(input_directory='.'):
return num - 1 return num - 1
def _parse_args(): def _ParseArgs():
"""Registers the command-line options.""" """Registers the command-line options."""
usage = "usage: %prog [options]" usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage=usage) parser = optparse.OptionParser(usage=usage)
@ -256,7 +256,7 @@ def _parse_args():
return options return options
def _main(): def main():
"""The main function. """The main function.
A simple invocation is: A simple invocation is:
@ -265,10 +265,10 @@ def _main():
--yuv_frame_width=640 --yuv_frame_height=480 --yuv_frame_width=640 --yuv_frame_height=480
--stats_file=<path_and_name_to_stats_file> --stats_file=<path_and_name_to_stats_file>
""" """
options = _parse_args() options = _ParseArgs()
# Convert the overlaid YUV video into a set of PNG frames. # Convert the overlaid YUV video into a set of PNG frames.
if not convert_yuv_to_png_files(options.yuv_file, options.yuv_frame_width, if not ConvertYuvToPngFiles(options.yuv_file, options.yuv_frame_width,
options.yuv_frame_height, options.yuv_frame_height,
output_directory=options.png_working_dir, output_directory=options.png_working_dir,
ffmpeg_path=options.ffmpeg_path): ffmpeg_path=options.ffmpeg_path):
@ -276,16 +276,16 @@ def _main():
return -1 return -1
# Decode the barcodes from the PNG frames. # Decode the barcodes from the PNG frames.
if not decode_frames(input_directory=options.png_working_dir, if not DecodeFrames(input_directory=options.png_working_dir,
zxing_path=options.zxing_path): zxing_path=options.zxing_path):
print 'An error occurred decoding barcodes from PNG frames.' print 'An error occurred decoding barcodes from PNG frames.'
return -2 return -2
# Generate statistics file. # Generate statistics file.
_generate_stats_file(options.stats_file, _GenerateStatsFile(options.stats_file,
input_directory=options.png_working_dir) input_directory=options.png_working_dir)
print 'Completed barcode decoding.' print 'Completed barcode decoding.'
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(_main()) sys.exit(main())

View File

@ -17,7 +17,7 @@ _DEFAULT_BARCODE_WIDTH = 352
_DEFAULT_BARCODES_FILE = 'barcodes.yuv' _DEFAULT_BARCODES_FILE = 'barcodes.yuv'
def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height, def GenerateUpcaBarcodes(number_of_barcodes, barcode_width, barcode_height,
output_directory='.', output_directory='.',
path_to_zxing='zxing-read-only'): path_to_zxing='zxing-read-only'):
"""Generates UPC-A barcodes. """Generates UPC-A barcodes.
@ -39,7 +39,7 @@ def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
(bool): True if the conversion is successful. (bool): True if the conversion is successful.
""" """
base_file_name = os.path.join(output_directory, "barcode_") base_file_name = os.path.join(output_directory, "barcode_")
jars = _form_jars_string(path_to_zxing) jars = _FormJarsString(path_to_zxing)
command_line_encoder = 'com.google.zxing.client.j2se.CommandLineEncoder' command_line_encoder = 'com.google.zxing.client.j2se.CommandLineEncoder'
barcode_width = str(barcode_width) barcode_width = str(barcode_width)
barcode_height = str(barcode_height) barcode_height = str(barcode_height)
@ -64,7 +64,7 @@ def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
return not errors return not errors
def convert_png_to_yuv_barcodes(input_directory='.', output_directory='.'): def ConvertPngToYuvBarcodes(input_directory='.', output_directory='.'):
"""Converts PNG barcodes to YUV barcode images. """Converts PNG barcodes to YUV barcode images.
This function reads all the PNG files from the input directory which are in This function reads all the PNG files from the input directory which are in
@ -79,11 +79,11 @@ def convert_png_to_yuv_barcodes(input_directory='.', output_directory='.'):
(bool): True if the conversion was without errors. (bool): True if the conversion was without errors.
""" """
return helper_functions.perform_action_on_all_files( return helper_functions.perform_action_on_all_files(
input_directory, 'barcode_', 'png', 0, _convert_to_yuv_and_delete, input_directory, 'barcode_', 'png', 0, _ConvertToYuvAndDelete,
output_directory=output_directory, pattern='barcode_') output_directory=output_directory, pattern='barcode_')
def _convert_to_yuv_and_delete(output_directory, file_name, pattern): def _ConvertToYuvAndDelete(output_directory, file_name, pattern):
"""Converts a PNG file to a YUV file and deletes the PNG file. """Converts a PNG file to a YUV file and deletes the PNG file.
Args: Args:
@ -116,7 +116,7 @@ def _convert_to_yuv_and_delete(output_directory, file_name, pattern):
return True return True
def combine_yuv_frames_into_one_file(output_file_name, input_directory='.'): def CombineYuvFramesIntoOneFile(output_file_name, input_directory='.'):
"""Combines several YUV frames into one YUV video file. """Combines several YUV frames into one YUV video file.
The function combines the YUV frames from input_directory into one YUV video The function combines the YUV frames from input_directory into one YUV video
@ -132,12 +132,12 @@ def combine_yuv_frames_into_one_file(output_file_name, input_directory='.'):
""" """
output_file = open(output_file_name, "wb") output_file = open(output_file_name, "wb")
success = helper_functions.perform_action_on_all_files( success = helper_functions.perform_action_on_all_files(
input_directory, 'barcode_', 'yuv', 0, _add_to_file_and_delete, input_directory, 'barcode_', 'yuv', 0, _AddToFileAndDelete,
output_file=output_file) output_file=output_file)
output_file.close() output_file.close()
return success return success
def _add_to_file_and_delete(output_file, file_name): def _AddToFileAndDelete(output_file, file_name):
"""Adds the contents of a file to a previously opened file. """Adds the contents of a file to a previously opened file.
Args: Args:
@ -159,7 +159,7 @@ def _add_to_file_and_delete(output_file, file_name):
return True return True
def _overlay_barcode_and_base_frames(barcodes_file, base_file, output_file, def _OverlayBarcodeAndBaseFrames(barcodes_file, base_file, output_file,
barcodes_component_sizes, barcodes_component_sizes,
base_component_sizes): base_component_sizes):
"""Overlays the next YUV frame from a file with a barcode. """Overlays the next YUV frame from a file with a barcode.
@ -201,7 +201,7 @@ def _overlay_barcode_and_base_frames(barcodes_file, base_file, output_file,
return True return True
def overlay_yuv_files(barcode_width, barcode_height, base_width, base_height, def OverlayYuvFiles(barcode_width, barcode_height, base_width, base_height,
barcodes_file_name, base_file_name, output_file_name): barcodes_file_name, base_file_name, output_file_name):
"""Overlays two YUV files starting from the upper left corner of both. """Overlays two YUV files starting from the upper left corner of both.
@ -230,7 +230,7 @@ def overlay_yuv_files(barcode_width, barcode_height, base_width, base_height,
data_left = True data_left = True
while data_left: while data_left:
data_left = _overlay_barcode_and_base_frames(barcodes_file, base_file, data_left = _OverlayBarcodeAndBaseFrames(barcodes_file, base_file,
output_file, output_file,
barcodes_component_sizes, barcodes_component_sizes,
base_component_sizes) base_component_sizes)
@ -240,7 +240,7 @@ def overlay_yuv_files(barcode_width, barcode_height, base_width, base_height,
output_file.close() output_file.close()
def calculate_frames_number_from_yuv(yuv_width, yuv_height, file_name): def CalculateFramesNumberFromYuv(yuv_width, yuv_height, file_name):
"""Calculates the number of frames of a YUV video. """Calculates the number of frames of a YUV video.
Args: Args:
@ -258,7 +258,7 @@ def calculate_frames_number_from_yuv(yuv_width, yuv_height, file_name):
return int(file_size/frame_size) # Should be int anyway return int(file_size/frame_size) # Should be int anyway
def _form_jars_string(path_to_zxing): def _FormJarsString(path_to_zxing):
"""Forms the the Zxing core and javase jars argument. """Forms the the Zxing core and javase jars argument.
Args: Args:
@ -273,7 +273,7 @@ def _form_jars_string(path_to_zxing):
delimiter = ';' delimiter = ';'
return javase_jar + delimiter + core_jar return javase_jar + delimiter + core_jar
def _parse_args(): def _ParseArgs():
"""Registers the command-line options.""" """Registers the command-line options."""
usage = "usage: %prog [options]" usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage=usage) parser = optparse.OptionParser(usage=usage)
@ -320,7 +320,7 @@ def _parse_args():
return options return options
def _main(): def main():
"""The main function. """The main function.
A simple invocation will be: A simple invocation will be:
@ -329,7 +329,7 @@ def _main():
--base_yuv=<path_and_name_of_base_file> --base_yuv=<path_and_name_of_base_file>
--output_yuv=<path and name_of_output_file> --output_yuv=<path and name_of_output_file>
""" """
options = _parse_args() options = _ParseArgs()
# The barcodes with will be different than the base frame width only if # The barcodes with will be different than the base frame width only if
# explicitly specified at the command line. # explicitly specified at the command line.
if options.barcode_width == _DEFAULT_BARCODE_WIDTH: if options.barcode_width == _DEFAULT_BARCODE_WIDTH:
@ -342,24 +342,24 @@ def _main():
# Calculate the number of barcodes - it is equal to the number of frames in # Calculate the number of barcodes - it is equal to the number of frames in
# the base file. # the base file.
number_of_barcodes = calculate_frames_number_from_yuv( number_of_barcodes = CalculateFramesNumberFromYuv(
options.base_frame_width, options.base_frame_height, options.base_yuv) options.base_frame_width, options.base_frame_height, options.base_yuv)
script_dir = os.path.dirname(os.path.abspath(sys.argv[0])) script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
zxing_dir = os.path.join(script_dir, 'third_party', 'zxing') zxing_dir = os.path.join(script_dir, 'third_party', 'zxing')
# Generate barcodes - will generate them in PNG. # Generate barcodes - will generate them in PNG.
generate_upca_barcodes(number_of_barcodes, options.barcode_width, GenerateUpcaBarcodes(number_of_barcodes, options.barcode_width,
options.barcode_height, options.barcode_height,
output_directory=options.png_barcodes_output_dir, output_directory=options.png_barcodes_output_dir,
path_to_zxing=zxing_dir) path_to_zxing=zxing_dir)
# Convert the PNG barcodes to to YUV format. # Convert the PNG barcodes to to YUV format.
convert_png_to_yuv_barcodes(options.png_barcodes_input_dir, ConvertPngToYuvBarcodes(options.png_barcodes_input_dir,
options.yuv_barcodes_output_dir) options.yuv_barcodes_output_dir)
# Combine the YUV barcodes into one YUV file. # Combine the YUV barcodes into one YUV file.
combine_yuv_frames_into_one_file(options.barcodes_yuv, CombineYuvFramesIntoOneFile(options.barcodes_yuv,
input_directory=options.yuv_frames_input_dir) input_directory=options.yuv_frames_input_dir)
# Overlay the barcodes over the base file. # Overlay the barcodes over the base file.
overlay_yuv_files(options.barcode_width, options.barcode_height, OverlayYuvFiles(options.barcode_width, options.barcode_height,
options.base_frame_width, options.base_frame_height, options.base_frame_width, options.base_frame_height,
options.barcodes_yuv, options.base_yuv, options.output_yuv) options.barcodes_yuv, options.base_yuv, options.output_yuv)
@ -369,4 +369,4 @@ def _main():
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(_main()) sys.exit(main())

View File

@ -12,7 +12,7 @@ import subprocess
import sys import sys
def run_ant_build_command(path_to_ant_build_file): def RunAntBuildCommand(path_to_ant_build_file):
"""Tries to build the passed build file with ant.""" """Tries to build the passed build file with ant."""
ant_executable = 'ant' ant_executable = 'ant'
if sys.platform == 'win32': if sys.platform == 'win32':
@ -32,13 +32,13 @@ def run_ant_build_command(path_to_ant_build_file):
e) e)
return -1 return -1
def _main(): def main():
core_build = os.path.join('third_party', 'zxing', 'core', 'build.xml') core_build = os.path.join('third_party', 'zxing', 'core', 'build.xml')
run_ant_build_command(core_build) RunAntBuildCommand(core_build)
javase_build = os.path.join('third_party', 'zxing', 'javase', 'build.xml') javase_build = os.path.join('third_party', 'zxing', 'javase', 'build.xml')
return run_ant_build_command(javase_build) return RunAntBuildCommand(javase_build)
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(_main()) sys.exit(main())

View File

@ -20,7 +20,7 @@ class HelperError(Exception):
pass pass
def zero_pad(number, padding=_DEFAULT_PADDING): def ZeroPad(number, padding=_DEFAULT_PADDING):
"""Converts an int into a zero padded string. """Converts an int into a zero padded string.
Args: Args:
@ -35,7 +35,7 @@ def zero_pad(number, padding=_DEFAULT_PADDING):
return str(number).zfill(padding) return str(number).zfill(padding)
def run_shell_command(cmd_list, fail_msg=None): def RunShellCommand(cmd_list, fail_msg=None):
"""Executes a command. """Executes a command.
Args: Args:
@ -60,7 +60,7 @@ def run_shell_command(cmd_list, fail_msg=None):
return output.strip() return output.strip()
def perform_action_on_all_files(directory, file_pattern, file_extension, def PerformActionOnAllFiles(directory, file_pattern, file_extension,
start_number, action, **kwargs): start_number, action, **kwargs):
"""Function that performs a given action on all files matching a pattern. """Function that performs a given action on all files matching a pattern.
@ -86,7 +86,7 @@ def perform_action_on_all_files(directory, file_pattern, file_extension,
process_pool = multiprocessing.Pool(processes=multiprocessing.cpu_count()) process_pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
results = [] results = []
while True: while True:
zero_padded_file_number = zero_pad(file_number) zero_padded_file_number = ZeroPad(file_number)
file_name = file_prefix + zero_padded_file_number + '.' + file_extension file_name = file_prefix + zero_padded_file_number + '.' + file_extension
if not os.path.isfile(file_name): if not os.path.isfile(file_name):
break break

View File

@ -12,7 +12,7 @@ import os
import sys import sys
def _crop_one_frame(yuv_file, output_file, component_sizes): def _CropOneFrame(yuv_file, output_file, component_sizes):
"""Crops one frame. """Crops one frame.
This function crops one frame going through all the YUV planes and cropping This function crops one frame going through all the YUV planes and cropping
@ -44,7 +44,7 @@ def _crop_one_frame(yuv_file, output_file, component_sizes):
return True return True
def crop_frames(yuv_file_name, output_file_name, width, height, crop_height): def CropFrames(yuv_file_name, output_file_name, width, height, crop_height):
"""Crops rows of pixels from the top of the YUV frames. """Crops rows of pixels from the top of the YUV frames.
This function goes through all the frames in a video and crops the crop_height This function goes through all the frames in a video and crops the crop_height
@ -69,13 +69,13 @@ def crop_frames(yuv_file_name, output_file_name, width, height, crop_height):
data_left = True data_left = True
while data_left: while data_left:
data_left = _crop_one_frame(yuv_file, output_file, component_sizes) data_left = _CropOneFrame(yuv_file, output_file, component_sizes)
yuv_file.close() yuv_file.close()
output_file.close() output_file.close()
def _parse_args(): def _ParseArgs():
"""Registers the command-line options.""" """Registers the command-line options."""
usage = "usage: %prog [options]" usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage=usage) parser = optparse.OptionParser(usage=usage)
@ -101,7 +101,7 @@ def _parse_args():
return options return options
def _main(): def main():
"""A tool to crop rows of pixels from the top part of a YUV file. """A tool to crop rows of pixels from the top part of a YUV file.
A simple invocation will be: A simple invocation will be:
@ -109,17 +109,17 @@ def _main():
--yuv_file=<path_and_name_of_yuv_file> --yuv_file=<path_and_name_of_yuv_file>
--output_yuv=<path and name_of_output_file> --output_yuv=<path and name_of_output_file>
""" """
options = _parse_args() options = _ParseArgs()
if os.path.getsize(options.yuv_file) == 0: if os.path.getsize(options.yuv_file) == 0:
sys.stderr.write('Error: The YUV file you have passed has size 0. The ' sys.stderr.write('Error: The YUV file you have passed has size 0. The '
'produced output will also have size 0.\n') 'produced output will also have size 0.\n')
return -1 return -1
crop_frames(options.yuv_file, options.output_file, options.width, CropFrames(options.yuv_file, options.output_file, options.width,
options.height, options.crop_height) options.height, options.crop_height)
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(_main()) sys.exit(main())