From 73d85c2b4ef50c552c1b3ebc87bea6e6b10b71bf Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Wed, 17 Jun 2015 17:41:24 +0100 Subject: [PATCH] cleaning up initialize() - standardisded on a single context argument - removed Device.init() no longer necessary as initilize now automatically gets propagated up the hierarchy. Renamed the existing use of it to "initilize". - related pylint cleanup. --- wlauto/commands/create.py | 2 +- wlauto/commands/list.py | 2 +- wlauto/commands/run.py | 2 +- wlauto/commands/show.py | 2 +- wlauto/common/android/device.py | 3 +- wlauto/common/linux/device.py | 12 +++---- wlauto/core/command.py | 6 ++-- wlauto/core/device.py | 37 +++++----------------- wlauto/core/extension.py | 8 ++--- wlauto/core/instrumentation.py | 4 +-- wlauto/core/result.py | 4 +-- wlauto/core/workload.py | 4 +-- wlauto/devices/android/nexus10/__init__.py | 2 +- wlauto/devices/android/note3/__init__.py | 2 +- wlauto/modules/cgroups.py | 2 +- wlauto/modules/cpufreq.py | 2 +- wlauto/modules/cpuidle.py | 2 +- wlauto/tests/test_extension.py | 18 +++++------ 18 files changed, 46 insertions(+), 68 deletions(-) diff --git a/wlauto/commands/create.py b/wlauto/commands/create.py index 6db925c6..8eca1204 100644 --- a/wlauto/commands/create.py +++ b/wlauto/commands/create.py @@ -177,7 +177,7 @@ class CreateCommand(Command): formatter_class = argparse.RawDescriptionHelpFormatter subcmd_classes = [CreateWorkloadSubcommand, CreatePackageSubcommand] - def initialize(self): + def initialize(self, context): subparsers = self.parser.add_subparsers(dest='what') self.subcommands = [] # pylint: disable=W0201 for subcmd_cls in self.subcmd_classes: diff --git a/wlauto/commands/list.py b/wlauto/commands/list.py index ee1f9cbb..fd3ff132 100644 --- a/wlauto/commands/list.py +++ b/wlauto/commands/list.py @@ -24,7 +24,7 @@ class ListCommand(Command): name = 'list' description = 'List available WA extensions with a short description of each.' - def initialize(self): + def initialize(self, context): extension_types = ['{}s'.format(ext.name) for ext in settings.extensions] self.parser.add_argument('kind', metavar='KIND', help=('Specify the kind of extension to list. Must be ' diff --git a/wlauto/commands/run.py b/wlauto/commands/run.py index 192d013a..4b65c1f2 100644 --- a/wlauto/commands/run.py +++ b/wlauto/commands/run.py @@ -30,7 +30,7 @@ class RunCommand(Command): name = 'run' description = 'Execute automated workloads on a remote device and process the resulting output.' - def initialize(self): + def initialize(self, context): self.parser.add_argument('agenda', metavar='AGENDA', help='Agenda for this workload automation run. This defines which workloads will ' + 'be executed, how many times, with which tunables, etc. ' + diff --git a/wlauto/commands/show.py b/wlauto/commands/show.py index ee95e66e..de478260 100644 --- a/wlauto/commands/show.py +++ b/wlauto/commands/show.py @@ -33,7 +33,7 @@ class ShowCommand(Command): Display documentation for the specified extension (workload, instrument, etc.). """ - def initialize(self): + def initialize(self, context): self.parser.add_argument('name', metavar='EXTENSION', help='''The name of the extension for which information will be shown.''') diff --git a/wlauto/common/android/device.py b/wlauto/common/android/device.py index 56f44e2a..c74362fe 100644 --- a/wlauto/common/android/device.py +++ b/wlauto/common/android/device.py @@ -182,7 +182,7 @@ class AndroidDevice(BaseLinuxDevice): # pylint: disable=W0223 self._just_rebooted = False self._is_ready = True - def initialize(self, context, *args, **kwargs): + def initialize(self, context): self.execute('mkdir -p {}'.format(self.working_directory)) if self.is_rooted: if not self.executable_is_installed('busybox'): @@ -193,7 +193,6 @@ class AndroidDevice(BaseLinuxDevice): # pylint: disable=W0223 self.disable_selinux() if self.enable_screen_check: self.ensure_screen_is_on() - self.init(context, *args, **kwargs) def disconnect(self): if self._logcat_poller: diff --git a/wlauto/common/linux/device.py b/wlauto/common/linux/device.py index 52a7efc3..d599c5b0 100644 --- a/wlauto/common/linux/device.py +++ b/wlauto/common/linux/device.py @@ -131,7 +131,7 @@ class BaseLinuxDevice(Device): # pylint: disable=abstract-method """ if self._number_of_cores is None: - corere = re.compile('^\s*cpu\d+\s*$') + corere = re.compile(r'^\s*cpu\d+\s*$') output = self.execute('ls /sys/devices/system/cpu') self._number_of_cores = 0 for entry in output.split(): @@ -175,14 +175,13 @@ class BaseLinuxDevice(Device): # pylint: disable=abstract-method if self.iks_switch_frequency is None and self.scheduler == 'iks': # pylint: disable=E0203 self.iks_switch_frequency = 800000 # pylint: disable=W0201 - def initialize(self, context, *args, **kwargs): + def initialize(self, context): self.execute('mkdir -p {}'.format(self.working_directory)) if self.is_rooted: if not self.is_installed('busybox'): self.busybox = self.deploy_busybox(context) else: self.busybox = 'busybox' - self.init(context, *args, **kwargs) def get_properties(self, context): for propfile in self.property_files: @@ -395,7 +394,7 @@ class BaseLinuxDevice(Device): # pylint: disable=abstract-method num_active_cores += 1 return num_active_cores - def set_number_of_active_cores(self, core, number): + def set_number_of_active_cores(self, core, number): # NOQA if core not in self.core_names: raise ValueError('Unexpected core: {}; must be in {}'.format(core, list(set(self.core_names)))) core_ids = [i for i, c in enumerate(self.core_names) if c == core] @@ -611,7 +610,8 @@ class LinuxDevice(BaseLinuxDevice): def get_pids_of(self, process_name): """Returns a list of PIDs of all processes with the specified name.""" # result should be a column of PIDs with the first row as "PID" header - result = self.execute('ps -C {} -o pid'.format(process_name), check_exit_code=False).strip().split() + result = self.execute('ps -C {} -o pid'.format(process_name), # NOQA + check_exit_code=False).strip().split() if len(result) >= 2: # at least one row besides the header return map(int, result[1:]) else: @@ -624,7 +624,7 @@ class LinuxDevice(BaseLinuxDevice): result = [] for line in lines: - parts = re.split('\s+', line, maxsplit=8) + parts = re.split(r'\s+', line, maxsplit=8) if parts: result.append(PsEntry(*(parts[0:1] + map(int, parts[1:5]) + parts[5:]))) diff --git a/wlauto/core/command.py b/wlauto/core/command.py index 5822145a..26d1e4f9 100644 --- a/wlauto/core/command.py +++ b/wlauto/core/command.py @@ -45,12 +45,12 @@ class Command(Extension): parser_params['formatter_class'] = self.formatter_class self.parser = subparsers.add_parser(self.name, **parser_params) init_argument_parser(self.parser) # propagate top-level options - self.initialize() + self.initialize(None) - def initialize(self): + def initialize(self, context): """ Perform command-specific initialisation (e.g. adding command-specific options to the command's - parser). + parser). ``context`` is always ``None``. """ pass diff --git a/wlauto/core/device.py b/wlauto/core/device.py index 94ae7a1a..121ca4a0 100644 --- a/wlauto/core/device.py +++ b/wlauto/core/device.py @@ -191,6 +191,14 @@ class Device(Extension): if len(self.core_names) != len(self.core_clusters): raise ConfigError('core_names and core_clusters are of different lengths.') + def initialize(self, context): + """ + Initialization that is performed at the begining of the run (after the device has + been connecte). + + """ + pass + def reset(self): """ Initiate rebooting of the device. @@ -222,35 +230,6 @@ class Device(Extension): """ Close the established connection to the device. """ raise NotImplementedError() - def initialize(self, context, *args, **kwargs): - """ - Default implementation just calls through to init(). May be overriden by specialised - abstract sub-cleasses to implent platform-specific intialization without requiring - concrete implementations to explicitly invoke parent's init(). - - Added in version 2.1.3. - - """ - self.init(context, *args, **kwargs) - - def init(self, context, *args, **kwargs): - """ - Initialize the device. This method *must* be called after a device reboot before - any other commands can be issued, however it may also be called without rebooting. - - It is up to device-specific implementations to identify what initialisation needs - to be preformed on a particular invocation. Bear in mind that no assumptions can be - made about the state of the device prior to the initiation of workload execution, - so full initialisation must be performed at least once, even if no reboot has occurred. - After that, the device-specific implementation may choose to skip initialization if - the device has not been rebooted; it is up to the implementation to keep track of - that, however. - - All arguments are device-specific (see the documentation for the your device). - - """ - pass - def ping(self): """ This must return successfully if the device is able to receive commands, or must diff --git a/wlauto/core/extension.py b/wlauto/core/extension.py index 037f2b34..3787fcbd 100644 --- a/wlauto/core/extension.py +++ b/wlauto/core/extension.py @@ -553,10 +553,10 @@ class Extension(object): for param in self.parameters: param.validate(self) - def initialize(self, *args, **kwargs): + def initialize(self, context): pass - def finalize(self, *args, **kwargs): + def finalize(self, context): pass def check_artifacts(self, context, level): @@ -615,7 +615,7 @@ class Extension(object): raise ValueError(message.format(module_spec)) module = loader.get_module(name, owner=self, **params) - module.initialize() + module.initialize(None) for capability in module.capabilities: if capability not in self.capabilities: self.capabilities.append(capability) @@ -678,6 +678,6 @@ class Module(Extension): if owner.name == self.name: raise ValueError('Circular module import for {}'.format(self.name)) - def initialize(self): + def initialize(self, context): pass diff --git a/wlauto/core/instrumentation.py b/wlauto/core/instrumentation.py index 9986d976..5609f2a8 100644 --- a/wlauto/core/instrumentation.py +++ b/wlauto/core/instrumentation.py @@ -385,10 +385,10 @@ class Instrument(Extension): self.is_enabled = True self.is_broken = False - def initialize(self, context): # pylint: disable=arguments-differ + def initialize(self, context): pass - def finalize(self, context): # pylint: disable=arguments-differ + def finalize(self, context): pass def __str__(self): diff --git a/wlauto/core/result.py b/wlauto/core/result.py index e290186f..bacb228c 100644 --- a/wlauto/core/result.py +++ b/wlauto/core/result.py @@ -140,7 +140,7 @@ class ResultProcessor(Extension): """ - def initialize(self, context): # pylint: disable=arguments-differ + def initialize(self, context): pass def process_iteration_result(self, result, context): @@ -155,7 +155,7 @@ class ResultProcessor(Extension): def export_run_result(self, result, context): pass - def finalize(self, context): # pylint: disable=arguments-differ + def finalize(self, context): pass diff --git a/wlauto/core/workload.py b/wlauto/core/workload.py index fa61a545..b0a35ee0 100644 --- a/wlauto/core/workload.py +++ b/wlauto/core/workload.py @@ -61,7 +61,7 @@ class Workload(Extension): """ pass - def initialize(self, context): # pylint: disable=arguments-differ + def initialize(self, context): """ This method should be used to perform once-per-run initialization of a workload instance, i.e., unlike ``setup()`` it will not be invoked on each iteration. @@ -96,7 +96,7 @@ class Workload(Extension): """ Perform any final clean up for the Workload. """ pass - def finalize(self, context): # pylint: disable=arguments-differ + def finalize(self, context): pass def __str__(self): diff --git a/wlauto/devices/android/nexus10/__init__.py b/wlauto/devices/android/nexus10/__init__.py index ad6f2555..ee8894a8 100644 --- a/wlauto/devices/android/nexus10/__init__.py +++ b/wlauto/devices/android/nexus10/__init__.py @@ -41,7 +41,7 @@ class Nexus10Device(AndroidDevice): Parameter('core_clusters', default=[0, 0], override=True), ] - def init(self, context, *args, **kwargs): + def initialize(self, context): time.sleep(self.long_delay) self.execute('svc power stayon true', check_exit_code=False) time.sleep(self.long_delay) diff --git a/wlauto/devices/android/note3/__init__.py b/wlauto/devices/android/note3/__init__.py index 9c8f42ae..f9055f5a 100644 --- a/wlauto/devices/android/note3/__init__.py +++ b/wlauto/devices/android/note3/__init__.py @@ -44,7 +44,7 @@ class Note3Device(AndroidDevice): super(Note3Device, self).__init__(**kwargs) self._just_rebooted = False - def init(self, context): + def initialize(self, context): self.execute('svc power stayon true', check_exit_code=False) def reset(self): diff --git a/wlauto/modules/cgroups.py b/wlauto/modules/cgroups.py index 5cfa64c8..0339e730 100644 --- a/wlauto/modules/cgroups.py +++ b/wlauto/modules/cgroups.py @@ -154,7 +154,7 @@ class Cgroups(Module): description='Location where cgroups are mounted on the device.'), ] - def initialize(self): + def initialize(self, context): self.device = self.root_owner signal.connect(self._on_device_init, signal.RUN_INIT, priority=1) diff --git a/wlauto/modules/cpufreq.py b/wlauto/modules/cpufreq.py index 3dff6952..3a3f7695 100644 --- a/wlauto/modules/cpufreq.py +++ b/wlauto/modules/cpufreq.py @@ -28,7 +28,7 @@ class CpufreqModule(Module): """ capabilities = ['cpufreq'] - def initialize(self): + def initialize(self, context): # pylint: disable=W0201 CpufreqModule._available_governors = {} CpufreqModule._available_governor_tunables = {} diff --git a/wlauto/modules/cpuidle.py b/wlauto/modules/cpuidle.py index 8257a80f..4c60ea40 100644 --- a/wlauto/modules/cpuidle.py +++ b/wlauto/modules/cpuidle.py @@ -86,7 +86,7 @@ class Cpuidle(Module): root_path = '/sys/devices/system/cpu/cpuidle' - def initialize(self): + def initialize(self, context): self.device = self.root_owner signal.connect(self._on_device_init, signal.RUN_INIT, priority=1) diff --git a/wlauto/tests/test_extension.py b/wlauto/tests/test_extension.py index cfa51cef..77e244f6 100644 --- a/wlauto/tests/test_extension.py +++ b/wlauto/tests/test_extension.py @@ -123,7 +123,7 @@ class MyCoolModule(Module): capabilities = ['fizzle'] - def initialize(self): + def initialize(self, context): self.fizzle_factor = 0 # pylint: disable=attribute-defined-outside-init def fizzle(self): @@ -227,16 +227,16 @@ class ExtensionMetaTest(TestCase): def __init__(self, *args, **kwargs): super(MyExt, self).__init__(*args, **kwargs) self.instance_init = 0 - def initialize(self): + def initialize(self, context): self.values['a'] += 1 class MyChildExt(MyExt): name = 'mychildext' - def initialize(self): + def initialize(self, context): self.instance_init += 1 ext = _instantiate(MyChildExt) - ext.initialize() + ext.initialize(None) assert_equal(MyExt.values['a'], 1) assert_equal(ext.instance_init, 1) @@ -249,14 +249,14 @@ class ExtensionMetaTest(TestCase): super(MyExt, self).__init__(*args, **kwargs) self.instance_init = 0 self.instance_validate = 0 - def initialize(self): + def initialize(self, context): self.values['a'] += 1 def validate(self): self.instance_validate += 1 class MyChildExt(MyExt): name = 'mychildext' - def initialize(self): + def initialize(self, context): self.instance_init += 1 def validate(self): self.instance_validate += 1 @@ -264,9 +264,9 @@ class ExtensionMetaTest(TestCase): ext1 = _instantiate(MyExt) ext2 = _instantiate(MyExt) ext3 = _instantiate(MyChildExt) - ext1.initialize() - ext2.initialize() - ext3.initialize() + ext1.initialize(None) + ext2.initialize(None) + ext3.initialize(None) ext1.validate() ext2.validate() ext3.validate()