mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-12 21:30:45 +01:00
Reland of PyLint fixes for tools-webrtc and webrtc/tools (patchset #1 id:1 of https://codereview.webrtc.org/2737233003/ )
Reason for revert: Fixing errors for reland. I have tested that this does not make Chromium video quality tests fail. Original issue's description: > 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} > Committed:94f4d9effc
NOTRY=true BUG=webrtc:7312 Review-Url: https://codereview.webrtc.org/2741733003 Cr-Commit-Position: refs/heads/master@{#17541}
This commit is contained in:
parent
adf0635ed5
commit
c88b5d56ad
15 changed files with 154 additions and 157 deletions
12
PRESUBMIT.py
12
PRESUBMIT.py
|
@ -126,7 +126,7 @@ def _VerifyNativeApiHeadersListIsValid(input_api, output_api):
|
|||
non_existing_paths)]
|
||||
return []
|
||||
|
||||
api_change_msg = """
|
||||
API_CHANGE_MSG = """
|
||||
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
|
||||
this is done by keeping the existing method signatures unchanged.
|
||||
|
@ -155,7 +155,7 @@ def _CheckNativeApiHeaderChanges(input_api, output_api):
|
|||
files.append(f)
|
||||
|
||||
if files:
|
||||
return [output_api.PresubmitNotifyResult(api_change_msg, files)]
|
||||
return [output_api.PresubmitNotifyResult(API_CHANGE_MSG, files)]
|
||||
return []
|
||||
|
||||
|
||||
|
@ -200,7 +200,7 @@ def _CheckNoPragmaOnce(input_api, output_api):
|
|||
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
|
||||
FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
|
||||
used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
|
||||
|
@ -466,13 +466,13 @@ def _CheckJSONParseErrors(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)
|
||||
|
||||
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)
|
||||
]
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ class WebRTCLinkSetup(object):
|
|||
del self._links_db[source]
|
||||
|
||||
|
||||
def _initialize_database(filename):
|
||||
def _InitializeDatabase(filename):
|
||||
links_database = shelve.open(filename)
|
||||
# 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.
|
||||
|
@ -89,7 +89,7 @@ def main():
|
|||
# The database file gets .db appended on some platforms.
|
||||
db_filenames = [LINKS_DB, LINKS_DB + '.db']
|
||||
if any(os.path.isfile(f) for f in db_filenames):
|
||||
links_database = _initialize_database(LINKS_DB)
|
||||
links_database = _InitializeDatabase(LINKS_DB)
|
||||
try:
|
||||
symlink_creator = WebRTCLinkSetup(links_database, options.dry_run)
|
||||
symlink_creator.CleanupLinks()
|
||||
|
|
|
@ -49,7 +49,7 @@ class FakeCmd(object):
|
|||
def __init__(self):
|
||||
self.expectations = []
|
||||
|
||||
def add_expectation(self, *args, **kwargs):
|
||||
def AddExpectation(self, *args, **kwargs):
|
||||
returns = kwargs.pop('_returns', None)
|
||||
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.fake = FakeCmd()
|
||||
self.old_RunCommand = getattr(roll_deps, '_RunCommand')
|
||||
self.old_run_command = getattr(roll_deps, '_RunCommand')
|
||||
setattr(roll_deps, '_RunCommand', self.fake)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self._output_dir, ignore_errors=True)
|
||||
self.assertEqual(self.fake.expectations, [])
|
||||
setattr(roll_deps, '_RunCommand', self.old_RunCommand)
|
||||
setattr(roll_deps, '_RunCommand', self.old_run_command)
|
||||
|
||||
def testUpdateDepsFile(self):
|
||||
new_rev = 'aaaaabbbbbcccccdddddeeeeefffff0000011111'
|
||||
|
@ -98,10 +98,10 @@ class TestRollChromiumRevision(unittest.TestCase):
|
|||
local_scope = ParseDepsDict(deps_contents)
|
||||
vars_dict = local_scope['vars']
|
||||
|
||||
def assertVar(variable_name):
|
||||
def AssertVar(variable_name):
|
||||
self.assertEquals(vars_dict[variable_name], TEST_DATA_VARS[variable_name])
|
||||
assertVar('chromium_git')
|
||||
assertVar('chromium_revision')
|
||||
AssertVar('chromium_git')
|
||||
AssertVar('chromium_revision')
|
||||
self.assertEquals(len(local_scope['deps']), 3)
|
||||
self.assertEquals(len(local_scope['deps_os']), 1)
|
||||
|
||||
|
@ -137,7 +137,7 @@ class TestRollChromiumRevision(unittest.TestCase):
|
|||
|
||||
def _SetupGitLsRemoteCall(cmd_fake, 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__':
|
||||
|
|
|
@ -39,7 +39,7 @@ class Logger(object):
|
|||
self.log_level = DISPLAY_LEVEL
|
||||
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):
|
||||
if self.messages_left is not None:
|
||||
if not self.messages_left:
|
||||
|
@ -91,7 +91,7 @@ def _CheckBuildFile(build_file_path, packages, logger):
|
|||
line_number = subpackages_match.group('line_number')
|
||||
if subpackage:
|
||||
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)
|
||||
|
||||
return found_violations
|
||||
|
|
|
@ -30,7 +30,7 @@ class Logger(object):
|
|||
self.messages = []
|
||||
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):
|
||||
build_file_path = os.path.relpath(build_file_path, self.test_dir)
|
||||
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'))
|
||||
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'))
|
||||
|
||||
def test_multiple_errors_single_target(self):
|
||||
def testMultipleErrorsSingleTarget(self):
|
||||
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'))
|
||||
|
||||
def test_common_prefix(self):
|
||||
def testCommonPrefix(self):
|
||||
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)
|
||||
|
||||
|
||||
|
|
|
@ -15,19 +15,16 @@ This file emits the list of reasons why a particular build needs to be clobbered
|
|||
import os
|
||||
import sys
|
||||
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
checkout_root = os.path.abspath(os.path.join(script_dir, os.pardir))
|
||||
sys.path.insert(0, os.path.join(checkout_root, 'build'))
|
||||
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir))
|
||||
sys.path.insert(0, os.path.join(CHECKOUT_ROOT, 'build'))
|
||||
import landmine_utils
|
||||
|
||||
|
||||
distributor = landmine_utils.distributor
|
||||
gyp_defines = landmine_utils.gyp_defines
|
||||
gyp_msvs_version = landmine_utils.gyp_msvs_version
|
||||
platform = landmine_utils.platform
|
||||
platform = landmine_utils.platform # pylint: disable=invalid-name
|
||||
|
||||
|
||||
def print_landmines():
|
||||
def print_landmines(): # pylint: disable=invalid-name
|
||||
"""
|
||||
ALL LANDMINES ARE EMITTED FROM HERE.
|
||||
"""
|
||||
|
|
|
@ -43,7 +43,7 @@ class TemporaryDirectory(object):
|
|||
def __enter__(self):
|
||||
return self._name
|
||||
|
||||
def __exit__(self, exc, value, tb):
|
||||
def __exit__(self, exc, value, _tb):
|
||||
if self._name and not self._closed:
|
||||
shutil.rmtree(self._name)
|
||||
self._closed = True
|
||||
|
|
|
@ -55,14 +55,14 @@ class NonStrippingEpilogOptionParser(optparse.OptionParser):
|
|||
return self.epilog
|
||||
|
||||
|
||||
def _get_external_ip():
|
||||
def _GetExternalIp():
|
||||
"""Finds out the machine's external IP by connecting to google.com."""
|
||||
external_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
external_socket.connect(('google.com', 80))
|
||||
return external_socket.getsockname()[0]
|
||||
|
||||
|
||||
def _parse_args():
|
||||
def _ParseArgs():
|
||||
"""Define and parse the command-line arguments."""
|
||||
presets_string = '\n'.join(str(p) for p in _PRESETS)
|
||||
parser = NonStrippingEpilogOptionParser(epilog=(
|
||||
|
@ -123,11 +123,11 @@ def _parse_args():
|
|||
except ValueError:
|
||||
parser.error('Invalid port range specified.')
|
||||
|
||||
_set_logger(options.verbose)
|
||||
_InitLogging(options.verbose)
|
||||
return options
|
||||
|
||||
|
||||
def _set_logger(verbose):
|
||||
def _InitLogging(verbose):
|
||||
"""Setup logging."""
|
||||
log_level = _DEFAULT_LOG_LEVEL
|
||||
if verbose:
|
||||
|
@ -135,8 +135,8 @@ def _set_logger(verbose):
|
|||
logging.basicConfig(level=log_level, format='%(message)s')
|
||||
|
||||
|
||||
def _main():
|
||||
options = _parse_args()
|
||||
def main():
|
||||
options = _ParseArgs()
|
||||
|
||||
# Build a configuration object. Override any preset configuration settings if
|
||||
# a value of a setting was also given as a flag.
|
||||
|
@ -154,19 +154,19 @@ def _main():
|
|||
emulator = network_emulator.NetworkEmulator(connection_config,
|
||||
options.port_range)
|
||||
try:
|
||||
emulator.check_permissions()
|
||||
emulator.CheckPermissions()
|
||||
except network_emulator.NetworkEmulatorError as e:
|
||||
logging.error('Error: %s\n\nCause: %s', e.fail_msg, e.error)
|
||||
return -1
|
||||
|
||||
if not options.target_ip:
|
||||
external_ip = _get_external_ip()
|
||||
external_ip = _GetExternalIp()
|
||||
else:
|
||||
external_ip = options.target_ip
|
||||
|
||||
logging.info('Constraining traffic to/from IP: %s', external_ip)
|
||||
try:
|
||||
emulator.emulate(external_ip)
|
||||
emulator.Emulate(external_ip)
|
||||
logging.info('Started network emulation with the following configuration:\n'
|
||||
' Receive 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])
|
||||
raw_input('Press Enter to abort Network Emulation...')
|
||||
logging.info('Flushing all Dummynet rules...')
|
||||
network_emulator.cleanup()
|
||||
network_emulator.Cleanup()
|
||||
logging.info('Completed Network Emulation.')
|
||||
return 0
|
||||
except network_emulator.NetworkEmulatorError as e:
|
||||
|
@ -192,4 +192,4 @@ def _main():
|
|||
return -2
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(_main())
|
||||
sys.exit(main())
|
||||
|
|
|
@ -53,20 +53,20 @@ class NetworkEmulator(object):
|
|||
self._port_range = port_range
|
||||
self._connection_config = connection_config
|
||||
|
||||
def emulate(self, target_ip):
|
||||
def Emulate(self, target_ip):
|
||||
"""Starts a network emulation by setting up Dummynet rules.
|
||||
|
||||
Args:
|
||||
target_ip: The IP address of the interface that shall be that have the
|
||||
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.delay_ms,
|
||||
self._connection_config.packet_loss_percent,
|
||||
self._connection_config.queue_slots)
|
||||
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.delay_ms,
|
||||
self._connection_config.packet_loss_percent,
|
||||
|
@ -74,15 +74,15 @@ class NetworkEmulator(object):
|
|||
logging.debug('Created send pipe: %s', send_pipe_id)
|
||||
|
||||
# Adding the rules will start the emulation.
|
||||
incoming_rule_id = self._create_dummynet_rule(receive_pipe_id, 'any',
|
||||
target_ip, self._port_range)
|
||||
incoming_rule_id = self._CreateDummynetRule(receive_pipe_id, 'any',
|
||||
target_ip, self._port_range)
|
||||
logging.debug('Created incoming rule: %s', incoming_rule_id)
|
||||
outgoing_rule_id = self._create_dummynet_rule(send_pipe_id, target_ip,
|
||||
'any', self._port_range)
|
||||
outgoing_rule_id = self._CreateDummynetRule(send_pipe_id, target_ip,
|
||||
'any', self._port_range)
|
||||
logging.debug('Created outgoing rule: %s', outgoing_rule_id)
|
||||
|
||||
@staticmethod
|
||||
def check_permissions():
|
||||
def CheckPermissions():
|
||||
"""Checks if permissions are available to run Dummynet commands.
|
||||
|
||||
Raises:
|
||||
|
@ -99,8 +99,8 @@ class NetworkEmulator(object):
|
|||
raise NetworkEmulatorError('You must run this script with administrator'
|
||||
' privileges.')
|
||||
|
||||
def _create_dummynet_rule(self, pipe_id, from_address, to_address,
|
||||
port_range):
|
||||
def _CreateDummynetRule(self, pipe_id, from_address, to_address,
|
||||
port_range):
|
||||
"""Creates a network emulation rule and returns its ID.
|
||||
|
||||
Args:
|
||||
|
@ -118,14 +118,14 @@ class NetworkEmulator(object):
|
|||
self._rule_counter += 100
|
||||
add_part = ['add', self._rule_counter, 'pipe', pipe_id,
|
||||
'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.')
|
||||
_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.')
|
||||
return self._rule_counter
|
||||
|
||||
def _create_dummynet_pipe(self, bandwidth_kbps, delay_ms, packet_loss_percent,
|
||||
queue_slots):
|
||||
def _CreateDummynetPipe(self, bandwidth_kbps, delay_ms, packet_loss_percent,
|
||||
queue_slots):
|
||||
"""Creates a Dummynet pipe and return its ID.
|
||||
|
||||
Args:
|
||||
|
@ -146,21 +146,21 @@ class NetworkEmulator(object):
|
|||
if sys.platform.startswith('linux'):
|
||||
error_message += ('Make sure you have loaded the ipfw_mod.ko module to '
|
||||
'your kernel (sudo insmod /path/to/ipfw_mod.ko).')
|
||||
_run_ipfw_command(cmd, error_message)
|
||||
_RunIpfwCommand(cmd, error_message)
|
||||
return self._pipe_counter
|
||||
|
||||
def cleanup():
|
||||
def Cleanup():
|
||||
"""Stops the network emulation by flushing all Dummynet rules.
|
||||
|
||||
Notice that this will flush any rules that may have been created previously
|
||||
before starting the emulation.
|
||||
"""
|
||||
_run_ipfw_command(['-f', 'flush'],
|
||||
_RunIpfwCommand(['-f', 'flush'],
|
||||
'Failed to flush Dummynet rules!')
|
||||
_run_ipfw_command(['-f', 'pipe', 'flush'],
|
||||
_RunIpfwCommand(['-f', 'pipe', 'flush'],
|
||||
'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
|
||||
Windows or Linux/UNIX.
|
||||
|
||||
|
|
|
@ -89,10 +89,10 @@ def main():
|
|||
|
||||
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])
|
||||
|
||||
convert_video(test_video, test_video_yuv)
|
||||
ConvertVideo(test_video, test_video_yuv)
|
||||
|
||||
reference_video = os.path.join(SRC_DIR,
|
||||
'resources', 'reference_video_640x360_30fps.y4m')
|
||||
|
@ -100,7 +100,7 @@ def main():
|
|||
reference_video_yuv = os.path.join(temp_dir,
|
||||
'reference_video_640x360_30fps.yuv')
|
||||
|
||||
convert_video(reference_video, reference_video_yuv)
|
||||
ConvertVideo(reference_video, reference_video_yuv)
|
||||
|
||||
# Run compare script.
|
||||
compare_script = os.path.join(SRC_DIR, 'webrtc', 'tools',
|
||||
|
|
|
@ -21,7 +21,7 @@ import helper_functions
|
|||
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):
|
||||
"""Converts a YUV video file into PNG frames.
|
||||
|
||||
|
@ -50,7 +50,7 @@ def convert_yuv_to_png_files(yuv_file_name, yuv_frame_width, yuv_frame_height,
|
|||
try:
|
||||
print 'Converting YUV file to PNG images (may take a while)...'
|
||||
print ' '.join(command)
|
||||
helper_functions.run_shell_command(
|
||||
helper_functions.RunShellCommand(
|
||||
command, fail_msg='Error during YUV to PNG conversion')
|
||||
except helper_functions.HelperError, err:
|
||||
print 'Error executing command: %s. Error: %s' % (command, err)
|
||||
|
@ -61,7 +61,7 @@ def convert_yuv_to_png_files(yuv_file_name, yuv_frame_width, yuv_frame_height,
|
|||
return True
|
||||
|
||||
|
||||
def decode_frames(input_directory, zxing_path):
|
||||
def DecodeFrames(input_directory, zxing_path):
|
||||
"""Decodes the barcodes overlaid in each frame.
|
||||
|
||||
The function uses the Zxing command-line tool from the Zxing C++ distribution
|
||||
|
@ -83,13 +83,13 @@ def decode_frames(input_directory, zxing_path):
|
|||
if not zxing_path:
|
||||
zxing_path = 'zxing.exe' if sys.platform == 'win32' else 'zxing'
|
||||
print 'Decoding barcodes from PNG files with %s...' % zxing_path
|
||||
return helper_functions.perform_action_on_all_files(
|
||||
return helper_functions.PerformActionOnAllFiles(
|
||||
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)
|
||||
|
||||
|
||||
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.
|
||||
|
||||
Args:
|
||||
|
@ -101,7 +101,7 @@ def _decode_barcode_in_file(file_name, command_line_decoder):
|
|||
"""
|
||||
command = [command_line_decoder, '--try-harder', '--dump-raw', file_name]
|
||||
try:
|
||||
out = helper_functions.run_shell_command(
|
||||
out = helper_functions.RunShellCommand(
|
||||
command, fail_msg='Error during decoding of %s' % file_name)
|
||||
text_file = open('%s.txt' % file_name[:-4], 'w')
|
||||
text_file.write(out)
|
||||
|
@ -116,7 +116,7 @@ def _decode_barcode_in_file(file_name, command_line_decoder):
|
|||
return True
|
||||
|
||||
|
||||
def _generate_stats_file(stats_file_name, input_directory='.'):
|
||||
def _GenerateStatsFile(stats_file_name, input_directory='.'):
|
||||
"""Generate statistics file.
|
||||
|
||||
The function generates a statistics file. The contents of the file are in the
|
||||
|
@ -128,19 +128,19 @@ def _generate_stats_file(stats_file_name, input_directory='.'):
|
|||
stats_file = open(stats_file_name, 'w')
|
||||
|
||||
print 'Generating stats file: %s' % stats_file_name
|
||||
for i in range(1, _count_frames_in(input_directory=input_directory) + 1):
|
||||
frame_number = helper_functions.zero_pad(i)
|
||||
for i in range(1, _CountFramesIn(input_directory=input_directory) + 1):
|
||||
frame_number = helper_functions.ZeroPad(i)
|
||||
barcode_file_name = file_prefix + frame_number + '.txt'
|
||||
png_frame = file_prefix + frame_number + '.png'
|
||||
entry_frame_number = helper_functions.zero_pad(i-1)
|
||||
entry_frame_number = helper_functions.ZeroPad(i-1)
|
||||
entry = 'frame_' + entry_frame_number + ' '
|
||||
|
||||
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)
|
||||
|
||||
if _check_barcode(barcode):
|
||||
entry += (helper_functions.zero_pad(int(barcode[0:11])) + '\n')
|
||||
if _CheckBarcode(barcode):
|
||||
entry += (helper_functions.ZeroPad(int(barcode[0:11])) + '\n')
|
||||
else:
|
||||
entry += 'Barcode error\n' # Barcode is wrongly detected.
|
||||
else: # Barcode file doesn't exist.
|
||||
|
@ -152,7 +152,7 @@ def _generate_stats_file(stats_file_name, input_directory='.'):
|
|||
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.
|
||||
|
||||
Args:
|
||||
|
@ -166,7 +166,7 @@ def _read_barcode_from_text_file(barcode_file_name):
|
|||
return barcode
|
||||
|
||||
|
||||
def _check_barcode(barcode):
|
||||
def _CheckBarcode(barcode):
|
||||
"""Check weather the UPC-A barcode was decoded correctly.
|
||||
|
||||
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])
|
||||
|
||||
|
||||
def _count_frames_in(input_directory='.'):
|
||||
def _CountFramesIn(input_directory='.'):
|
||||
"""Calculates the number of frames in the input directory.
|
||||
|
||||
The function calculates the number of frames in the input directory. The
|
||||
|
@ -217,7 +217,7 @@ def _count_frames_in(input_directory='.'):
|
|||
num = 1
|
||||
|
||||
while file_exists:
|
||||
file_name = (file_prefix + helper_functions.zero_pad(num) + '.png')
|
||||
file_name = (file_prefix + helper_functions.ZeroPad(num) + '.png')
|
||||
if os.path.isfile(file_name):
|
||||
num += 1
|
||||
else:
|
||||
|
@ -225,7 +225,7 @@ def _count_frames_in(input_directory='.'):
|
|||
return num - 1
|
||||
|
||||
|
||||
def _parse_args():
|
||||
def _ParseArgs():
|
||||
"""Registers the command-line options."""
|
||||
usage = "usage: %prog [options]"
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
|
@ -256,7 +256,7 @@ def _parse_args():
|
|||
return options
|
||||
|
||||
|
||||
def _main():
|
||||
def main():
|
||||
"""The main function.
|
||||
|
||||
A simple invocation is:
|
||||
|
@ -265,27 +265,27 @@ def _main():
|
|||
--yuv_frame_width=640 --yuv_frame_height=480
|
||||
--stats_file=<path_and_name_to_stats_file>
|
||||
"""
|
||||
options = _parse_args()
|
||||
options = _ParseArgs()
|
||||
|
||||
# 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,
|
||||
options.yuv_frame_height,
|
||||
output_directory=options.png_working_dir,
|
||||
ffmpeg_path=options.ffmpeg_path):
|
||||
if not ConvertYuvToPngFiles(options.yuv_file, options.yuv_frame_width,
|
||||
options.yuv_frame_height,
|
||||
output_directory=options.png_working_dir,
|
||||
ffmpeg_path=options.ffmpeg_path):
|
||||
print 'An error occurred converting from YUV to PNG frames.'
|
||||
return -1
|
||||
|
||||
# Decode the barcodes from the PNG frames.
|
||||
if not decode_frames(input_directory=options.png_working_dir,
|
||||
zxing_path=options.zxing_path):
|
||||
if not DecodeFrames(input_directory=options.png_working_dir,
|
||||
zxing_path=options.zxing_path):
|
||||
print 'An error occurred decoding barcodes from PNG frames.'
|
||||
return -2
|
||||
|
||||
# Generate statistics file.
|
||||
_generate_stats_file(options.stats_file,
|
||||
input_directory=options.png_working_dir)
|
||||
_GenerateStatsFile(options.stats_file,
|
||||
input_directory=options.png_working_dir)
|
||||
print 'Completed barcode decoding.'
|
||||
return 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(_main())
|
||||
sys.exit(main())
|
||||
|
|
|
@ -17,9 +17,9 @@ _DEFAULT_BARCODE_WIDTH = 352
|
|||
_DEFAULT_BARCODES_FILE = 'barcodes.yuv'
|
||||
|
||||
|
||||
def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
|
||||
output_directory='.',
|
||||
path_to_zxing='zxing-read-only'):
|
||||
def GenerateUpcaBarcodes(number_of_barcodes, barcode_width, barcode_height,
|
||||
output_directory='.',
|
||||
path_to_zxing='zxing-read-only'):
|
||||
"""Generates UPC-A barcodes.
|
||||
|
||||
This function generates a number_of_barcodes UPC-A barcodes. The function
|
||||
|
@ -39,16 +39,16 @@ def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
|
|||
(bool): True if the conversion is successful.
|
||||
"""
|
||||
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'
|
||||
barcode_width = str(barcode_width)
|
||||
barcode_height = str(barcode_height)
|
||||
|
||||
errors = False
|
||||
for i in range(number_of_barcodes):
|
||||
suffix = helper_functions.zero_pad(i)
|
||||
suffix = helper_functions.ZeroPad(i)
|
||||
# Barcodes starting from 0
|
||||
content = helper_functions.zero_pad(i, 11)
|
||||
content = helper_functions.ZeroPad(i, 11)
|
||||
output_file_name = base_file_name + suffix + ".png"
|
||||
|
||||
command = ["java", "-cp", jars, command_line_encoder,
|
||||
|
@ -56,7 +56,7 @@ def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
|
|||
"--width=%s" % barcode_width,
|
||||
"--output=%s" % (output_file_name), "%s" % (content)]
|
||||
try:
|
||||
helper_functions.run_shell_command(
|
||||
helper_functions.RunShellCommand(
|
||||
command, fail_msg=('Error during barcode %s generation' % content))
|
||||
except helper_functions.HelperError as err:
|
||||
print >> sys.stderr, err
|
||||
|
@ -64,7 +64,7 @@ def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
|
|||
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.
|
||||
|
||||
This function reads all the PNG files from the input directory which are in
|
||||
|
@ -78,12 +78,12 @@ def convert_png_to_yuv_barcodes(input_directory='.', output_directory='.'):
|
|||
Return:
|
||||
(bool): True if the conversion was without errors.
|
||||
"""
|
||||
return helper_functions.perform_action_on_all_files(
|
||||
input_directory, 'barcode_', 'png', 0, _convert_to_yuv_and_delete,
|
||||
return helper_functions.PerformActionOnAllFiles(
|
||||
input_directory, 'barcode_', 'png', 0, _ConvertToYuvAndDelete,
|
||||
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.
|
||||
|
||||
Args:
|
||||
|
@ -106,7 +106,7 @@ def _convert_to_yuv_and_delete(output_directory, file_name, pattern):
|
|||
command = ['ffmpeg', '-i', '%s' % (file_name), '-pix_fmt', 'yuv420p',
|
||||
'%s' % (yuv_file_name)]
|
||||
try:
|
||||
helper_functions.run_shell_command(
|
||||
helper_functions.RunShellCommand(
|
||||
command, fail_msg=('Error during PNG to YUV conversion of %s' %
|
||||
file_name))
|
||||
os.remove(file_name)
|
||||
|
@ -116,7 +116,7 @@ def _convert_to_yuv_and_delete(output_directory, file_name, pattern):
|
|||
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.
|
||||
|
||||
The function combines the YUV frames from input_directory into one YUV video
|
||||
|
@ -131,13 +131,13 @@ def combine_yuv_frames_into_one_file(output_file_name, input_directory='.'):
|
|||
(bool): True if the frame stitching went OK.
|
||||
"""
|
||||
output_file = open(output_file_name, "wb")
|
||||
success = helper_functions.perform_action_on_all_files(
|
||||
input_directory, 'barcode_', 'yuv', 0, _add_to_file_and_delete,
|
||||
success = helper_functions.PerformActionOnAllFiles(
|
||||
input_directory, 'barcode_', 'yuv', 0, _AddToFileAndDelete,
|
||||
output_file=output_file)
|
||||
output_file.close()
|
||||
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.
|
||||
|
||||
Args:
|
||||
|
@ -159,9 +159,9 @@ def _add_to_file_and_delete(output_file, file_name):
|
|||
return True
|
||||
|
||||
|
||||
def _overlay_barcode_and_base_frames(barcodes_file, base_file, output_file,
|
||||
barcodes_component_sizes,
|
||||
base_component_sizes):
|
||||
def _OverlayBarcodeAndBaseFrames(barcodes_file, base_file, output_file,
|
||||
barcodes_component_sizes,
|
||||
base_component_sizes):
|
||||
"""Overlays the next YUV frame from a file with a barcode.
|
||||
|
||||
Args:
|
||||
|
@ -201,8 +201,8 @@ def _overlay_barcode_and_base_frames(barcodes_file, base_file, output_file,
|
|||
return True
|
||||
|
||||
|
||||
def overlay_yuv_files(barcode_width, barcode_height, base_width, base_height,
|
||||
barcodes_file_name, base_file_name, output_file_name):
|
||||
def OverlayYuvFiles(barcode_width, barcode_height, base_width, base_height,
|
||||
barcodes_file_name, base_file_name, output_file_name):
|
||||
"""Overlays two YUV files starting from the upper left corner of both.
|
||||
|
||||
Args:
|
||||
|
@ -230,17 +230,17 @@ def overlay_yuv_files(barcode_width, barcode_height, base_width, base_height,
|
|||
|
||||
data_left = True
|
||||
while data_left:
|
||||
data_left = _overlay_barcode_and_base_frames(barcodes_file, base_file,
|
||||
output_file,
|
||||
barcodes_component_sizes,
|
||||
base_component_sizes)
|
||||
data_left = _OverlayBarcodeAndBaseFrames(barcodes_file, base_file,
|
||||
output_file,
|
||||
barcodes_component_sizes,
|
||||
base_component_sizes)
|
||||
|
||||
barcodes_file.close()
|
||||
base_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.
|
||||
|
||||
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
|
||||
|
||||
|
||||
def _form_jars_string(path_to_zxing):
|
||||
def _FormJarsString(path_to_zxing):
|
||||
"""Forms the the Zxing core and javase jars argument.
|
||||
|
||||
Args:
|
||||
|
@ -273,7 +273,7 @@ def _form_jars_string(path_to_zxing):
|
|||
delimiter = ';'
|
||||
return javase_jar + delimiter + core_jar
|
||||
|
||||
def _parse_args():
|
||||
def _ParseArgs():
|
||||
"""Registers the command-line options."""
|
||||
usage = "usage: %prog [options]"
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
|
@ -320,7 +320,7 @@ def _parse_args():
|
|||
return options
|
||||
|
||||
|
||||
def _main():
|
||||
def main():
|
||||
"""The main function.
|
||||
|
||||
A simple invocation will be:
|
||||
|
@ -329,7 +329,7 @@ def _main():
|
|||
--base_yuv=<path_and_name_of_base_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
|
||||
# explicitly specified at the command line.
|
||||
if options.barcode_width == _DEFAULT_BARCODE_WIDTH:
|
||||
|
@ -342,26 +342,26 @@ def _main():
|
|||
|
||||
# Calculate the number of barcodes - it is equal to the number of frames in
|
||||
# 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)
|
||||
|
||||
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
|
||||
zxing_dir = os.path.join(script_dir, 'third_party', 'zxing')
|
||||
# Generate barcodes - will generate them in PNG.
|
||||
generate_upca_barcodes(number_of_barcodes, options.barcode_width,
|
||||
options.barcode_height,
|
||||
output_directory=options.png_barcodes_output_dir,
|
||||
path_to_zxing=zxing_dir)
|
||||
GenerateUpcaBarcodes(number_of_barcodes, options.barcode_width,
|
||||
options.barcode_height,
|
||||
output_directory=options.png_barcodes_output_dir,
|
||||
path_to_zxing=zxing_dir)
|
||||
# Convert the PNG barcodes to to YUV format.
|
||||
convert_png_to_yuv_barcodes(options.png_barcodes_input_dir,
|
||||
options.yuv_barcodes_output_dir)
|
||||
ConvertPngToYuvBarcodes(options.png_barcodes_input_dir,
|
||||
options.yuv_barcodes_output_dir)
|
||||
# Combine the YUV barcodes into one YUV file.
|
||||
combine_yuv_frames_into_one_file(options.barcodes_yuv,
|
||||
input_directory=options.yuv_frames_input_dir)
|
||||
CombineYuvFramesIntoOneFile(options.barcodes_yuv,
|
||||
input_directory=options.yuv_frames_input_dir)
|
||||
# Overlay the barcodes over the base file.
|
||||
overlay_yuv_files(options.barcode_width, options.barcode_height,
|
||||
options.base_frame_width, options.base_frame_height,
|
||||
options.barcodes_yuv, options.base_yuv, options.output_yuv)
|
||||
OverlayYuvFiles(options.barcode_width, options.barcode_height,
|
||||
options.base_frame_width, options.base_frame_height,
|
||||
options.barcodes_yuv, options.base_yuv, options.output_yuv)
|
||||
|
||||
if not keep_barcodes_yuv_file:
|
||||
# Remove the temporary barcodes YUV file
|
||||
|
@ -369,4 +369,4 @@ def _main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(_main())
|
||||
sys.exit(main())
|
||||
|
|
|
@ -12,7 +12,7 @@ import subprocess
|
|||
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."""
|
||||
ant_executable = 'ant'
|
||||
if sys.platform == 'win32':
|
||||
|
@ -32,13 +32,13 @@ def run_ant_build_command(path_to_ant_build_file):
|
|||
e)
|
||||
return -1
|
||||
|
||||
def _main():
|
||||
def main():
|
||||
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')
|
||||
return run_ant_build_command(javase_build)
|
||||
return RunAntBuildCommand(javase_build)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(_main())
|
||||
sys.exit(main())
|
||||
|
|
|
@ -20,7 +20,7 @@ class HelperError(Exception):
|
|||
pass
|
||||
|
||||
|
||||
def zero_pad(number, padding=_DEFAULT_PADDING):
|
||||
def ZeroPad(number, padding=_DEFAULT_PADDING):
|
||||
"""Converts an int into a zero padded string.
|
||||
|
||||
Args:
|
||||
|
@ -35,7 +35,7 @@ def zero_pad(number, padding=_DEFAULT_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.
|
||||
|
||||
Args:
|
||||
|
@ -60,8 +60,8 @@ def run_shell_command(cmd_list, fail_msg=None):
|
|||
return output.strip()
|
||||
|
||||
|
||||
def perform_action_on_all_files(directory, file_pattern, file_extension,
|
||||
start_number, action, **kwargs):
|
||||
def PerformActionOnAllFiles(directory, file_pattern, file_extension,
|
||||
start_number, action, **kwargs):
|
||||
"""Function that performs a given action on all files matching a pattern.
|
||||
|
||||
It is assumed that the files are named file_patternxxxx.file_extension, where
|
||||
|
@ -86,7 +86,7 @@ def perform_action_on_all_files(directory, file_pattern, file_extension,
|
|||
process_pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
|
||||
results = []
|
||||
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
|
||||
if not os.path.isfile(file_name):
|
||||
break
|
||||
|
|
|
@ -12,7 +12,7 @@ import os
|
|||
import sys
|
||||
|
||||
|
||||
def _crop_one_frame(yuv_file, output_file, component_sizes):
|
||||
def _CropOneFrame(yuv_file, output_file, component_sizes):
|
||||
"""Crops one frame.
|
||||
|
||||
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
|
||||
|
||||
|
||||
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.
|
||||
|
||||
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
|
||||
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()
|
||||
output_file.close()
|
||||
|
||||
|
||||
def _parse_args():
|
||||
def _ParseArgs():
|
||||
"""Registers the command-line options."""
|
||||
usage = "usage: %prog [options]"
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
|
@ -101,7 +101,7 @@ def _parse_args():
|
|||
return options
|
||||
|
||||
|
||||
def _main():
|
||||
def main():
|
||||
"""A tool to crop rows of pixels from the top part of a YUV file.
|
||||
|
||||
A simple invocation will be:
|
||||
|
@ -109,17 +109,17 @@ def _main():
|
|||
--yuv_file=<path_and_name_of_yuv_file>
|
||||
--output_yuv=<path and name_of_output_file>
|
||||
"""
|
||||
options = _parse_args()
|
||||
options = _ParseArgs()
|
||||
|
||||
if os.path.getsize(options.yuv_file) == 0:
|
||||
sys.stderr.write('Error: The YUV file you have passed has size 0. The '
|
||||
'produced output will also have size 0.\n')
|
||||
return -1
|
||||
|
||||
crop_frames(options.yuv_file, options.output_file, options.width,
|
||||
options.height, options.crop_height)
|
||||
CropFrames(options.yuv_file, options.output_file, options.width,
|
||||
options.height, options.crop_height)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(_main())
|
||||
sys.exit(main())
|
||||
|
|
Loading…
Reference in a new issue