1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-09-23 12:21:54 +01:00

pylint fixes

This commit is contained in:
Marc Bonnici
2018-07-11 17:30:45 +01:00
committed by setrofim
parent 5cb551b315
commit 454b94501c
44 changed files with 330 additions and 275 deletions

View File

@@ -12,5 +12,3 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

View File

@@ -20,21 +20,18 @@ Utility functions for working with Android devices through adb.
"""
# pylint: disable=E1103
import os
import pexpect
import time
import subprocess
import logging
import re
import threading
import tempfile
import queue
import sys
import time
import logging
import tempfile
import subprocess
from collections import defaultdict
import pexpect
from devlib.exception import TargetError, HostError, DevlibError
from devlib.utils.misc import check_output, which, memoized, ABI_MAP
from devlib.exception import TargetError, HostError
from devlib.utils.misc import check_output, which, ABI_MAP
from devlib.utils.misc import escape_single_quotes, escape_double_quotes
from devlib import host
logger = logging.getLogger('android')
@@ -117,6 +114,7 @@ class AdbDevice(object):
self.name = name
self.status = status
# pylint: disable=undefined-variable
def __cmp__(self, other):
if isinstance(other, AdbDevice):
return cmp(self.name, other.name)
@@ -146,6 +144,7 @@ class ApkInfo(object):
self.permissions = []
self.parse(path)
# pylint: disable=too-many-branches
def parse(self, apk_path):
_check_env()
command = [aapt, 'dump', 'badging', apk_path]
@@ -222,6 +221,7 @@ class AdbConnection(object):
self.ls_command = 'ls'
logger.debug("ls command is set to {}".format(self.ls_command))
# pylint: disable=unused-argument
def __init__(self, device=None, timeout=None, platform=None, adb_server=None):
self.timeout = timeout if timeout is not None else self.default_timeout
if device is None:
@@ -255,6 +255,7 @@ class AdbConnection(object):
command = "pull '{}' '{}'".format(source, dest)
return adb_command(self.device, command, timeout=timeout, adb_server=self.adb_server)
# pylint: disable=unused-argument
def execute(self, command, timeout=None, check_exit_code=False,
as_root=False, strip_colors=True):
return adb_shell(self.device, command, timeout, check_exit_code,
@@ -365,12 +366,13 @@ def _ping(device):
command = "adb{} shell \"ls /data/local/tmp > /dev/null\"".format(device_string)
logger.debug(command)
result = subprocess.call(command, stderr=subprocess.PIPE, shell=True)
if not result:
if not result: # pylint: disable=simplifiable-if-statement
return True
else:
return False
# pylint: disable=too-many-locals
def adb_shell(device, command, timeout=None, check_exit_code=False,
as_root=False, adb_server=None): # NOQA
_check_env()
@@ -443,7 +445,7 @@ def adb_background_shell(device, command,
def adb_list_devices(adb_server=None):
output = adb_command(None, 'devices',adb_server=adb_server)
output = adb_command(None, 'devices', adb_server=adb_server)
devices = []
for line in output.splitlines():
parts = [p.strip() for p in line.split()]
@@ -575,6 +577,8 @@ class LogcatMonitor(object):
self.target = target
self._regexps = regexps
self._logcat = None
self._logfile = None
def start(self, outfile=None):
"""
@@ -651,7 +655,7 @@ class LogcatMonitor(object):
return [line for line in fh]
def clear_log(self):
with open(self._logfile.name, 'w') as fh:
with open(self._logfile.name, 'w') as _:
pass
def search(self, regexp):
@@ -679,7 +683,7 @@ class LogcatMonitor(object):
res = [line for line in log if re.match(regexp, line)]
# Found some matches, return them
if len(res) > 0:
if res:
return res
# Store the number of lines we've searched already, so we don't have to

View File

@@ -28,7 +28,7 @@ logger = logging.getLogger('gem5')
def iter_statistics_dump(stats_file):
'''
Yields statistics dumps as dicts. The parameter is assumed to be a stream
Yields statistics dumps as dicts. The parameter is assumed to be a stream
reading from the statistics log file.
'''
cur_dump = {}
@@ -40,14 +40,13 @@ def iter_statistics_dump(stats_file):
yield cur_dump
cur_dump = {}
else:
res = GEM5STATS_FIELD_REGEX.match(line)
res = GEM5STATS_FIELD_REGEX.match(line)
if res:
k = res.group("key")
vtext = res.group("value")
try:
v = list(map(numeric, vtext.split()))
cur_dump[k] = v[0] if len(v)==1 else set(v)
cur_dump[k] = v[0] if len(v) == 1 else set(v)
except ValueError:
msg = 'Found non-numeric entry in gem5 stats ({}: {})'
logger.warning(msg.format(k, vtext))

View File

@@ -19,27 +19,28 @@ Miscellaneous functions that don't fit anywhere else.
"""
from __future__ import division
import os
import sys
import re
import string
import threading
import signal
import subprocess
import pkgutil
import logging
import random
import ctypes
import threading
from operator import itemgetter
from functools import partial, reduce
from itertools import groupby
from functools import partial
from operator import itemgetter
import ctypes
import logging
import os
import pkgutil
import random
import re
import signal
import string
import subprocess
import sys
import threading
import wrapt
from past.builtins import basestring
# pylint: disable=redefined-builtin
from devlib.exception import HostError, TimeoutError
from functools import reduce
# ABI --> architectures list
@@ -184,7 +185,7 @@ def check_output(command, timeout=None, ignore=None, inputtext=None,
# Currently errors=replace is needed as 0x8c throws an error
output = output.decode(sys.stdout.encoding, "replace")
if error:
error = error.decode(sys.stderr.encoding, "replace")
error = error.decode(sys.stderr.encoding, "replace")
finally:
if timeout:
timer.cancel()
@@ -523,6 +524,12 @@ def get_random_string(length):
class LoadSyntaxError(Exception):
@property
def message(self):
if self.args:
return self.args[0]
return str(self)
def __init__(self, message, filepath, lineno):
super(LoadSyntaxError, self).__init__(message)
self.filepath = filepath
@@ -535,6 +542,7 @@ class LoadSyntaxError(Exception):
RAND_MOD_NAME_LEN = 30
BAD_CHARS = string.punctuation + string.whitespace
# pylint: disable=no-member
if sys.version_info[0] == 3:
TRANS_TABLE = str.maketrans(BAD_CHARS, '_' * len(BAD_CHARS))
else:
@@ -639,7 +647,7 @@ def __get_memo_id(obj):
@wrapt.decorator
def memoized(wrapped, instance, args, kwargs):
def memoized(wrapped, instance, args, kwargs): # pylint: disable=unused-argument
"""A decorator for memoizing functions and methods."""
func_id = repr(wrapped)
@@ -652,4 +660,3 @@ def memoized(wrapped, instance, args, kwargs):
return __memo_cache[id_string]
return memoize_wrapper(*args, **kwargs)

View File

@@ -28,18 +28,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import getopt
import subprocess
import logging
import signal
import serial
import time
import math
import sys
logger = logging.getLogger('aep-parser')
# pylint: disable=attribute-defined-outside-init
class AepParser(object):
prepared = False
@@ -94,7 +90,7 @@ class AepParser(object):
continue
if parent not in virtual:
virtual[parent] = { supply : index }
virtual[parent] = {supply : index}
virtual[parent][supply] = index
@@ -102,7 +98,7 @@ class AepParser(object):
# child
for supply in list(virtual.keys()):
if len(virtual[supply]) == 1:
del virtual[supply];
del virtual[supply]
for supply in list(virtual.keys()):
# Add label, hide and duplicate columns for virtual domains
@@ -121,7 +117,7 @@ class AepParser(object):
label[0] = array[0]
unit[0] = "(S)"
for i in range(1,len(array)):
for i in range(1, len(array)):
label[i] = array[i][:-3]
unit[i] = array[i][-3:]
@@ -138,7 +134,7 @@ class AepParser(object):
# By default we assume that there is no child
duplicate = [0] * len(label)
for i in range(len(label)):
for i in range(len(label)): # pylint: disable=consider-using-enumerate
# We only care about time and Watt
if label[i] == 'time':
hide[i] = 0
@@ -167,7 +163,7 @@ class AepParser(object):
@staticmethod
def parse_text(array, hide):
data = [0]*len(array)
for i in range(len(array)):
for i in range(len(array)): # pylint: disable=consider-using-enumerate
if hide[i]:
continue
@@ -193,18 +189,18 @@ class AepParser(object):
return data
@staticmethod
def delta_nrj(array, delta, min, max, hide):
def delta_nrj(array, delta, minimu, maximum, hide):
# Compute the energy consumed in this time slice and add it
# delta[0] is used to save the last time stamp
if (delta[0] < 0):
if delta[0] < 0:
delta[0] = array[0]
time = array[0] - delta[0]
if (time <= 0):
if time <= 0:
return delta
for i in range(len(array)):
for i in range(len(array)): # pylint: disable=consider-using-enumerate
if hide[i]:
continue
@@ -213,10 +209,10 @@ class AepParser(object):
except ValueError:
continue
if (data < min[i]):
min[i] = data
if (data > max[i]):
max[i] = data
if data < minimu[i]:
minimu[i] = data
if data > maximum[i]:
maximum[i] = data
delta[i] += time * data
# save last time stamp
@@ -225,11 +221,11 @@ class AepParser(object):
return delta
def output_label(self, label, hide):
self.fo.write(label[0]+"(uS)")
self.fo.write(label[0] + "(uS)")
for i in range(1, len(label)):
if hide[i]:
continue
self.fo.write(" "+label[i]+"(uW)")
self.fo.write(" " + label[i] + "(uW)")
self.fo.write("\n")
@@ -248,34 +244,34 @@ class AepParser(object):
self.fo.write("\n")
def prepare(self, infile, outfile, summaryfile):
# pylint: disable-redefined-outer-name,
def prepare(self, input_file, outfile, summaryfile):
try:
self.fi = open(infile, "r")
self.fi = open(input_file, "r")
except IOError:
logger.warn('Unable to open input file {}'.format(infile))
logger.warn('Usage: parse_arp.py -i <inputfile> [-o <outputfile>]')
logger.warning('Unable to open input file {}'.format(input_file))
logger.warning('Usage: parse_arp.py -i <inputfile> [-o <outputfile>]')
sys.exit(2)
self.parse = True
if len(outfile) > 0:
if outfile:
try:
self.fo = open(outfile, "w")
except IOError:
logger.warn('Unable to create {}'.format(outfile))
logger.warning('Unable to create {}'.format(outfile))
self.parse = False
else:
self.parse = False
self.parse = False
self.summary = True
if len(summaryfile) > 0:
if summaryfile:
try:
self.fs = open(summaryfile, "w")
except IOError:
logger.warn('Unable to create {}'.format(summaryfile))
logger.warning('Unable to create {}'.format(summaryfile))
self.fs = sys.stdout
else:
self.fs = sys.stdout
self.fs = sys.stdout
self.prepared = True
@@ -291,7 +287,7 @@ class AepParser(object):
self.prepared = False
# pylint: disable=too-many-branches,too-many-statements
# pylint: disable=too-many-branches,too-many-statements,redefined-outer-name,too-many-locals
def parse_aep(self, start=0, length=-1):
# Parse aep data and calculate the energy consumed
begin = 0
@@ -303,7 +299,7 @@ class AepParser(object):
lines = self.fi.readlines()
for myline in lines:
array = myline.split()
array = myline.split()
if "#" in myline:
# update power topology
@@ -332,8 +328,8 @@ class AepParser(object):
# Init arrays
nrj = [0]*len(label)
min = [100000000]*len(label)
max = [0]*len(label)
minimum = [100000000]*len(label)
maximum = [0]*len(label)
offset = [0]*len(label)
continue
@@ -357,7 +353,7 @@ class AepParser(object):
data = self.add_virtual_data(data, virtual)
# extract power figures
self.delta_nrj(data, nrj, min, max, hide)
self.delta_nrj(data, nrj, minimum, maximum, hide)
# write data into new file
if self.parse:
@@ -366,7 +362,6 @@ class AepParser(object):
# if there is no data just return
if label_line or len(nrj) == 1:
raise ValueError('No data found in the data file. Please check the Arm Energy Probe')
return
# display energy consumption of each channel and total energy consumption
total = 0
@@ -378,27 +373,33 @@ class AepParser(object):
nrj[i] -= offset[i] * nrj[0]
total_nrj = nrj[i]/1000000000000.0
duration = (max[0]-min[0])/1000000.0
duration = (maximum[0]-minimum[0])/1000000.0
channel_name = label[i]
average_power = total_nrj/duration
self.fs.write("Total nrj: %8.3f J for %s -- duration %8.3f sec -- min %8.3f W -- max %8.3f W\n" % (nrj[i]/1000000000000.0, label[i], (max[0]-min[0])/1000000.0, min[i]/1000000.0, max[i]/1000000.0))
total = nrj[i]/1000000000000.0
duration = (maximum[0]-minimum[0])/1000000.0
min_power = minimum[i]/1000000.0
max_power = maximum[i]/1000000.0
output = "Total nrj: %8.3f J for %s -- duration %8.3f sec -- min %8.3f W -- max %8.3f W\n"
self.fs.write(output.format(total, label[i], duration, min_power, max_power))
# store each AEP channel info except Platform in the results table
results_table[channel_name] = total_nrj, average_power
if (min[i] < offset[i]):
self.fs.write ("!!! Min below offset\n")
if minimum[i] < offset[i]:
self.fs.write("!!! Min below offset\n")
if duplicate[i]:
continue
total += nrj[i]
self.fs.write ("Total nrj: %8.3f J for %s -- duration %8.3f sec\n" % (total/1000000000000.0, "Platform ", (max[0]-min[0])/1000000.0))
output = "Total nrj: %8.3f J for Platform -- duration %8.3f sec\n"
self.fs.write(output.format(total/1000000000000.0, (maximum[0]-minimum[0])/1000000.0))
total_nrj = total/1000000000000.0
duration = (max[0]-min[0])/1000000.0
duration = (maximum[0]-minimum[0])/1000000.0
average_power = total_nrj/duration
# store AEP Platform channel info in the results table
@@ -406,11 +407,12 @@ class AepParser(object):
return results_table
# pylint: disable=too-many-branches,no-self-use,too-many-locals
def topology_from_config(self, topofile):
try:
ft = open(topofile, "r")
except IOError:
logger.warn('Unable to open config file {}'.format(topofile))
logger.warning('Unable to open config file {}'.format(topofile))
return
lines = ft.readlines()
@@ -452,10 +454,11 @@ class AepParser(object):
topo[items[0]] = info
# Increase index
index +=1
index += 1
# Create an entry for each virtual parent
# pylint: disable=consider-iterating-dictionary
for supply in topo.keys():
# Parent is in the topology
parent = topo[supply]['parent']
@@ -463,23 +466,25 @@ class AepParser(object):
continue
if parent not in virtual:
virtual[parent] = { supply : topo[supply]['index'] }
virtual[parent] = {supply : topo[supply]['index']}
virtual[parent][supply] = topo[supply]['index']
# Remove parent with 1 child as they don't give more information than their
# child
# pylint: disable=consider-iterating-dictionary
for supply in list(virtual.keys()):
if len(virtual[supply]) == 1:
del virtual[supply];
del virtual[supply]
topo_list = ['']*(1+len(topo)+len(virtual))
topo_list[0] = 'time'
# pylint: disable=consider-iterating-dictionary
for chnl in topo.keys():
topo_list[topo[chnl]['index']] = chnl
for chnl in virtual.keys():
index +=1
index += 1
topo_list[index] = chnl
ft.close()
@@ -491,6 +496,7 @@ class AepParser(object):
if __name__ == '__main__':
# pylint: disable=unused-argument
def handleSigTERM(signum, frame):
sys.exit(2)
@@ -502,8 +508,8 @@ if __name__ == '__main__':
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
infile = ""
outfile = ""
in_file = ""
out_file = ""
figurefile = ""
start = 0
length = -1
@@ -516,22 +522,22 @@ if __name__ == '__main__':
for o, a in opts:
if o == "-i":
infile = a
in_file = a
if o == "-v":
logger.setLevel(logging.DEBUG)
if o == "-o":
parse = True
outfile = a
out_file = a
if o == "-s":
start = int(float(a)*1000000)
if o == "-l":
length = int(float(a)*1000000)
if o == "-t":
topofile = a
topfile = a
parser = AepParser()
print(parser.topology_from_config(topofile))
print(parser.topology_from_config(topfile))
exit(0)
parser = AepParser()
parser.prepare(infile, outfile, figurefile)
parser.prepare(in_file, out_file, figurefile)
parser.parse_aep(start, length)

View File

@@ -15,15 +15,14 @@
import logging
import os
import re
import shutil
import sys
import tempfile
import threading
import time
from collections import namedtuple, OrderedDict
from distutils.version import LooseVersion
from collections import namedtuple
# pylint: disable=redefined-builtin
from devlib.exception import WorkerThreadError, TargetNotRespondingError, TimeoutError
from devlib.utils.csvutil import csvwriter
@@ -190,7 +189,7 @@ class GfxinfoFrameCollector(FrameCollector):
def __init__(self, target, period, package, header=None):
super(GfxinfoFrameCollector, self).__init__(target, period)
self.package = package
self.header = None
self.header = None
self._init_header(header)
def collect_frames(self, wfh):
@@ -261,7 +260,7 @@ def gfxinfo_get_last_dump(filepath):
ix = buf.find(' **\n')
if ix >= 0:
buf = next(fh_iter) + buf
buf = next(fh_iter) + buf
ix = buf.find('** Graphics')
if ix < 0:
msg = '"{}" appears to be corrupted'

View File

@@ -20,6 +20,7 @@ from logging import Logger
import serial
# pylint: disable=import-error,wrong-import-position,ungrouped-imports,wrong-import-order
import pexpect
from distutils.version import StrictVersion as V
if V(pexpect.__version__) < V('4.0.0'):
@@ -48,6 +49,7 @@ def pulse_dtr(conn, state=True, duration=0.1):
conn.setDTR(not state)
# pylint: disable=keyword-arg-before-vararg
def get_connection(timeout, init_dtr=None, logcls=SerialLogger,
logfile=None, *args, **kwargs):
if init_dtr is not None:
@@ -89,6 +91,7 @@ def write_characters(conn, line, delay=0.05):
conn.sendline('')
# pylint: disable=keyword-arg-before-vararg
@contextmanager
def open_serial_connection(timeout, get_conn=False, init_dtr=None,
logcls=SerialLogger, *args, **kwargs):
@@ -118,4 +121,3 @@ def open_serial_connection(timeout, get_conn=False, init_dtr=None,
target.close() # Closes the file descriptor used by the conn.
del conn

View File

@@ -26,6 +26,7 @@ import socket
import sys
import time
# pylint: disable=import-error,wrong-import-position,ungrouped-imports,wrong-import-order
import pexpect
from distutils.version import StrictVersion as V
if V(pexpect.__version__) < V('4.0.0'):
@@ -34,6 +35,7 @@ else:
from pexpect import pxssh
from pexpect import EOF, TIMEOUT, spawn
# pylint: disable=redefined-builtin,wrong-import-position
from devlib.exception import HostError, TargetError, TimeoutError
from devlib.utils.misc import which, strip_bash_colors, check_output
from devlib.utils.misc import (escape_single_quotes, escape_double_quotes,
@@ -73,7 +75,7 @@ def ssh_get_shell(host, username, password=None, keyfile=None, port=None, timeou
raise TargetError(message.format(host))
time.sleep(5)
conn.setwinsize(500,200)
conn.setwinsize(500, 200)
conn.sendline('')
conn.prompt()
conn.setecho(False)
@@ -147,12 +149,13 @@ class SshConnection(object):
default_password_prompt = '[sudo] password'
max_cancel_attempts = 5
default_timeout=10
default_timeout = 10
@property
def name(self):
return self.host
# pylint: disable=unused-argument,super-init-not-called
def __init__(self,
host,
username,
@@ -310,6 +313,7 @@ class SshConnection(object):
class TelnetConnection(SshConnection):
# pylint: disable=super-init-not-called
def __init__(self,
host,
username,
@@ -333,6 +337,7 @@ class TelnetConnection(SshConnection):
class Gem5Connection(TelnetConnection):
# pylint: disable=super-init-not-called
def __init__(self,
platform,
host=None,
@@ -348,9 +353,9 @@ class Gem5Connection(TelnetConnection):
host_system = socket.gethostname()
if host_system != host:
raise TargetError("Gem5Connection can only connect to gem5 "
"simulations on your current host, which "
"differs from the one given {}!"
.format(host_system, host))
"simulations on your current host {}, which "
"differs from the one given {}!"
.format(host_system, host))
if username is not None and username != 'root':
raise ValueError('User should be root in gem5!')
if password is not None and password != '':
@@ -517,8 +522,8 @@ class Gem5Connection(TelnetConnection):
trial = 0
while os.path.isfile(redirection_file):
# Log file already exists so add to name
redirection_file = 'BACKGROUND_{}{}.log'.format(command_name, trial)
trial += 1
redirection_file = 'BACKGROUND_{}{}.log'.format(command_name, trial)
trial += 1
# Create the command to pass on to gem5 shell
complete_command = '{} >> {} 2>&1 &'.format(command, redirection_file)
@@ -548,7 +553,7 @@ class Gem5Connection(TelnetConnection):
try:
shutil.rmtree(self.gem5_interact_dir)
except OSError:
gem5_logger.warn("Failed to remove the temporary directory!")
gem5_logger.warning("Failed to remove the temporary directory!")
# Delete the lock file
os.remove(self.lock_file_name)
@@ -563,6 +568,7 @@ class Gem5Connection(TelnetConnection):
self.connect_gem5(port, gem5_simulation, gem5_interact_dir, gem5_out_dir)
# Handle the EOF exception raised by pexpect
# pylint: disable=no-self-use
def _gem5_EOF_handler(self, gem5_simulation, gem5_out_dir, err):
# If we have reached the "EOF", it typically means
# that gem5 crashed and closed the connection. Let's
@@ -576,6 +582,7 @@ class Gem5Connection(TelnetConnection):
raise err
# This function connects to the gem5 simulation
# pylint: disable=too-many-statements
def connect_gem5(self, port, gem5_simulation, gem5_interact_dir,
gem5_out_dir):
"""
@@ -754,7 +761,7 @@ class Gem5Connection(TelnetConnection):
# prompt has returned. Hence, we have a bit of an issue. We
# warn, and return the whole output.
if command_index == -1:
gem5_logger.warn("gem5_shell: Unable to match command in "
gem5_logger.warning("gem5_shell: Unable to match command in "
"command output. Expect parsing errors!")
command_index = 0

View File

@@ -113,4 +113,3 @@ class UbootMenu(object):
except TIMEOUT:
pass
self.conn.buffer = ''

View File

@@ -237,5 +237,3 @@ class UefiMenu(object):
self.options = {}
self.prompt = None
self.empty_buffer()