Revert of PyLint fixes for tools-webrtc and webrtc/tools (patchset #3 id:40001 of https://codereview.webrtc.org/2736233003/ )

Reason for revert:
Fails video quality tests in Chrome:  http://build.chromium.org/p/chromium.webrtc.fyi/builders/Win10%20Tester/builds/6568
I should have looked more closer at those :(

Original issue's description:
> 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}
> Committed: f5318e1f39

TBR=oprypin@webrtc.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=webrtc:7303

Review-Url: https://codereview.webrtc.org/2737233003
Cr-Commit-Position: refs/heads/master@{#17142}
This commit is contained in:
kjellander
2017-03-09 06:09:33 -08:00
committed by Commit bot
parent 09f3e3e7b4
commit 94f4d9effc
15 changed files with 145 additions and 142 deletions

View File

@ -115,7 +115,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.
@ -144,7 +144,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 []
@ -189,7 +189,7 @@ def _CheckNoPragmaOnce(input_api, output_api):
return [] return []
def _CheckNoFRIEND_TEST(input_api, output_api): # pylint: disable=invalid-name def _CheckNoFRIEND_TEST(input_api, output_api):
"""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."""
@ -451,13 +451,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 _InitializeDatabase(filename): def _initialize_database(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 = _InitializeDatabase(LINKS_DB) links_database = _initialize_database(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 AddExpectation(self, *args, **kwargs): def add_expectation(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_run_command = getattr(roll_deps, '_RunCommand') self.old_RunCommand = 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_run_command) setattr(roll_deps, '_RunCommand', self.old_RunCommand)
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.AddExpectation(cmd, _returns=(revision, None)) cmd_fake.add_expectation(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 testNoErrors(self): def test_no_errors(self):
self.RunTest(os.path.join(TESTDATA_DIR, 'no_errors')) self.RunTest(os.path.join(TESTDATA_DIR, 'no_errors'))
def testMultipleErrorsSingleTarget(self): def test_multiple_errors_single_target(self):
self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_single_target')) self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_single_target'))
def testMultipleErrorsMultipleTargets(self): def test_multiple_errors_multiple_targets(self):
self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_multiple_targets')) self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_multiple_targets'))
def testCommonPrefix(self): def test_common_prefix(self):
self.RunTest(os.path.join(TESTDATA_DIR, 'common_prefix')) self.RunTest(os.path.join(TESTDATA_DIR, 'common_prefix'))
def testAllBuildFiles(self): def test_all_build_files(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,16 +15,19 @@ 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
platform = landmine_utils.platform # pylint: disable=invalid-name distributor = landmine_utils.distributor
gyp_defines = landmine_utils.gyp_defines
gyp_msvs_version = landmine_utils.gyp_msvs_version
platform = landmine_utils.platform
def print_landmines(): # pylint: disable=invalid-name def print_landmines():
""" """
ALL LANDMINES ARE EMITTED FROM HERE. ALL LANDMINES ARE EMITTED FROM HERE.
""" """
@ -51,7 +54,7 @@ def print_landmines(): # pylint: disable=invalid-name
# 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 after https://codereview.webrtc.org/2709573003' print 'Clobber to fix https://codereview.webrtc.org/2709573003 after landing'
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 _GetExternalIp(): def _get_external_ip():
"""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 _ParseArgs(): def _parse_args():
"""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 _ParseArgs():
except ValueError: except ValueError:
parser.error('Invalid port range specified.') parser.error('Invalid port range specified.')
_InitLogging(options.verbose) _set_logger(options.verbose)
return options return options
def _InitLogging(verbose): def _set_logger(verbose):
"""Setup logging.""" """Setup logging."""
log_level = _DEFAULT_LOG_LEVEL log_level = _DEFAULT_LOG_LEVEL
if verbose: if verbose:
@ -135,8 +135,8 @@ def _InitLogging(verbose):
logging.basicConfig(level=log_level, format='%(message)s') logging.basicConfig(level=log_level, format='%(message)s')
def main(): def _main():
options = _ParseArgs() options = _parse_args()
# 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.CheckPermissions() emulator.check_permissions()
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 = _GetExternalIp() external_ip = _get_external_ip()
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._CreateDummynetPipe( receive_pipe_id = self._create_dummynet_pipe(
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._CreateDummynetPipe( send_pipe_id = self._create_dummynet_pipe(
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._CreateDummynetRule(receive_pipe_id, 'any', incoming_rule_id = self._create_dummynet_rule(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._CreateDummynetRule(send_pipe_id, target_ip, outgoing_rule_id = self._create_dummynet_rule(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 CheckPermissions(): def check_permissions():
"""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 _CreateDummynetRule(self, pipe_id, from_address, to_address, def _create_dummynet_rule(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]
_RunIpfwCommand(add_part + ['src-port', '%s-%s' % port_range], _run_ipfw_command(add_part + ['src-port', '%s-%s' % port_range],
'Failed to add Dummynet src-port rule.') 'Failed to add Dummynet src-port rule.')
_RunIpfwCommand(add_part + ['dst-port', '%s-%s' % port_range], _run_ipfw_command(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 _CreateDummynetPipe(self, bandwidth_kbps, delay_ms, packet_loss_percent, def _create_dummynet_pipe(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).')
_RunIpfwCommand(cmd, error_message) _run_ipfw_command(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.
""" """
_RunIpfwCommand(['-f', 'flush'], _run_ipfw_command(['-f', 'flush'],
'Failed to flush Dummynet rules!') 'Failed to flush Dummynet rules!')
_RunIpfwCommand(['-f', 'pipe', 'flush'], _run_ipfw_command(['-f', 'pipe', 'flush'],
'Failed to flush Dummynet pipes!') 'Failed to flush Dummynet pipes!')
def _RunIpfwCommand(command, fail_msg=None): def _run_ipfw_command(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 ConvertVideo(input_video, output_video): def convert_video(input_video, output_video):
_RunCommand([ffmpeg_path, '-y', '-i', input_video, output_video]) _RunCommand([ffmpeg_path, '-y', '-i', input_video, output_video])
ConvertVideo(test_video, test_video_yuv) convert_video(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')
ConvertVideo(reference_video, reference_video_yuv) convert_video(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 ConvertYuvToPngFiles(yuv_file_name, yuv_frame_width, yuv_frame_height, def convert_yuv_to_png_files(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 ConvertYuvToPngFiles(yuv_file_name, yuv_frame_width, yuv_frame_height,
return True return True
def DecodeFrames(input_directory, zxing_path): def decode_frames(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 DecodeFrames(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=_DecodeBarcodeInFile, file_extension='png', start_number=1, action=_decode_barcode_in_file,
command_line_decoder=zxing_path) command_line_decoder=zxing_path)
def _DecodeBarcodeInFile(file_name, command_line_decoder): def _decode_barcode_in_file(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 _DecodeBarcodeInFile(file_name, command_line_decoder):
return True return True
def _GenerateStatsFile(stats_file_name, input_directory='.'): def _generate_stats_file(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 _GenerateStatsFile(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, _CountFramesIn(input_directory=input_directory) + 1): for i in range(1, _count_frames_in(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 _GenerateStatsFile(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 = _ReadBarcodeFromTextFile(barcode_file_name) barcode = _read_barcode_from_text_file(barcode_file_name)
os.remove(barcode_file_name) os.remove(barcode_file_name)
if _CheckBarcode(barcode): if _check_barcode(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 _GenerateStatsFile(stats_file_name, input_directory='.'):
stats_file.close() stats_file.close()
def _ReadBarcodeFromTextFile(barcode_file_name): def _read_barcode_from_text_file(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 _ReadBarcodeFromTextFile(barcode_file_name):
return barcode return barcode
def _CheckBarcode(barcode): def _check_barcode(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 _CheckBarcode(barcode):
return dsum == int(barcode[11]) return dsum == int(barcode[11])
def _CountFramesIn(input_directory='.'): def _count_frames_in(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 _CountFramesIn(input_directory='.'):
return num - 1 return num - 1
def _ParseArgs(): def _parse_args():
"""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 _ParseArgs():
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 = _ParseArgs() options = _parse_args()
# Convert the overlaid YUV video into a set of PNG frames. # Convert the overlaid YUV video into a set of PNG frames.
if not ConvertYuvToPngFiles(options.yuv_file, options.yuv_frame_width, if not convert_yuv_to_png_files(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 DecodeFrames(input_directory=options.png_working_dir, if not decode_frames(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.
_GenerateStatsFile(options.stats_file, _generate_stats_file(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 GenerateUpcaBarcodes(number_of_barcodes, barcode_width, barcode_height, def generate_upca_barcodes(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 GenerateUpcaBarcodes(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 = _FormJarsString(path_to_zxing) jars = _form_jars_string(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 GenerateUpcaBarcodes(number_of_barcodes, barcode_width, barcode_height,
return not errors return not errors
def ConvertPngToYuvBarcodes(input_directory='.', output_directory='.'): def convert_png_to_yuv_barcodes(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 ConvertPngToYuvBarcodes(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, _ConvertToYuvAndDelete, input_directory, 'barcode_', 'png', 0, _convert_to_yuv_and_delete,
output_directory=output_directory, pattern='barcode_') output_directory=output_directory, pattern='barcode_')
def _ConvertToYuvAndDelete(output_directory, file_name, pattern): def _convert_to_yuv_and_delete(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 _ConvertToYuvAndDelete(output_directory, file_name, pattern):
return True return True
def CombineYuvFramesIntoOneFile(output_file_name, input_directory='.'): def combine_yuv_frames_into_one_file(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 CombineYuvFramesIntoOneFile(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, _AddToFileAndDelete, input_directory, 'barcode_', 'yuv', 0, _add_to_file_and_delete,
output_file=output_file) output_file=output_file)
output_file.close() output_file.close()
return success return success
def _AddToFileAndDelete(output_file, file_name): def _add_to_file_and_delete(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 _AddToFileAndDelete(output_file, file_name):
return True return True
def _OverlayBarcodeAndBaseFrames(barcodes_file, base_file, output_file, def _overlay_barcode_and_base_frames(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 _OverlayBarcodeAndBaseFrames(barcodes_file, base_file, output_file,
return True return True
def OverlayYuvFiles(barcode_width, barcode_height, base_width, base_height, def overlay_yuv_files(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 OverlayYuvFiles(barcode_width, barcode_height, base_width, base_height,
data_left = True data_left = True
while data_left: while data_left:
data_left = _OverlayBarcodeAndBaseFrames(barcodes_file, base_file, data_left = _overlay_barcode_and_base_frames(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 OverlayYuvFiles(barcode_width, barcode_height, base_width, base_height,
output_file.close() output_file.close()
def CalculateFramesNumberFromYuv(yuv_width, yuv_height, file_name): def calculate_frames_number_from_yuv(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 CalculateFramesNumberFromYuv(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 _FormJarsString(path_to_zxing): def _form_jars_string(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 _FormJarsString(path_to_zxing):
delimiter = ';' delimiter = ';'
return javase_jar + delimiter + core_jar return javase_jar + delimiter + core_jar
def _ParseArgs(): def _parse_args():
"""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 _ParseArgs():
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 = _ParseArgs() options = _parse_args()
# 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 = CalculateFramesNumberFromYuv( number_of_barcodes = calculate_frames_number_from_yuv(
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.
GenerateUpcaBarcodes(number_of_barcodes, options.barcode_width, generate_upca_barcodes(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.
ConvertPngToYuvBarcodes(options.png_barcodes_input_dir, convert_png_to_yuv_barcodes(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.
CombineYuvFramesIntoOneFile(options.barcodes_yuv, combine_yuv_frames_into_one_file(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.
OverlayYuvFiles(options.barcode_width, options.barcode_height, overlay_yuv_files(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 RunAntBuildCommand(path_to_ant_build_file): def run_ant_build_command(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 RunAntBuildCommand(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')
RunAntBuildCommand(core_build) run_ant_build_command(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 RunAntBuildCommand(javase_build) return run_ant_build_command(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 ZeroPad(number, padding=_DEFAULT_PADDING): def zero_pad(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 ZeroPad(number, padding=_DEFAULT_PADDING):
return str(number).zfill(padding) return str(number).zfill(padding)
def RunShellCommand(cmd_list, fail_msg=None): def run_shell_command(cmd_list, fail_msg=None):
"""Executes a command. """Executes a command.
Args: Args:
@ -60,7 +60,7 @@ def RunShellCommand(cmd_list, fail_msg=None):
return output.strip() return output.strip()
def PerformActionOnAllFiles(directory, file_pattern, file_extension, def perform_action_on_all_files(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 PerformActionOnAllFiles(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 = ZeroPad(file_number) zero_padded_file_number = zero_pad(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 _CropOneFrame(yuv_file, output_file, component_sizes): def _crop_one_frame(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 _CropOneFrame(yuv_file, output_file, component_sizes):
return True return True
def CropFrames(yuv_file_name, output_file_name, width, height, crop_height): def crop_frames(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 CropFrames(yuv_file_name, output_file_name, width, height, crop_height):
data_left = True data_left = True
while data_left: while data_left:
data_left = _CropOneFrame(yuv_file, output_file, component_sizes) data_left = _crop_one_frame(yuv_file, output_file, component_sizes)
yuv_file.close() yuv_file.close()
output_file.close() output_file.close()
def _ParseArgs(): def _parse_args():
"""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 _ParseArgs():
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 = _ParseArgs() options = _parse_args()
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
CropFrames(options.yuv_file, options.output_file, options.width, crop_frames(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())