diff --git a/devlib/collector/ftrace.py b/devlib/collector/ftrace.py index 057d732..d356154 100644 --- a/devlib/collector/ftrace.py +++ b/devlib/collector/ftrace.py @@ -408,8 +408,7 @@ class FtraceCollector(CollectorBase): self.logger.debug(command) process = subprocess.Popen(command, stderr=subprocess.PIPE, shell=True) _, error = process.communicate() - if sys.version_info[0] == 3: - error = error.decode(sys.stdout.encoding or 'utf-8', 'replace') + error = error.decode(sys.stdout.encoding or 'utf-8', 'replace') if process.returncode: raise TargetStableError('trace-cmd returned non-zero exit code {}'.format(process.returncode)) if error: diff --git a/devlib/instrument/acmecape.py b/devlib/instrument/acmecape.py index ec0a77b..cfbcbe0 100644 --- a/devlib/instrument/acmecape.py +++ b/devlib/instrument/acmecape.py @@ -116,10 +116,7 @@ class AcmeCapeInstrument(Instrument): msg = 'Could not terminate iio-capture:\n{}' raise HostError(msg.format(output)) if self.process.returncode != 15: # iio-capture exits with 15 when killed - if sys.version_info[0] == 3: - output += self.process.stdout.read().decode(sys.stdout.encoding or 'utf-8', 'replace') - else: - output += self.process.stdout.read() + output += self.process.stdout.read().decode(sys.stdout.encoding or 'utf-8', 'replace') self.logger.info('ACME instrument encountered an error, ' 'you may want to try rebooting the ACME device:\n' ' ssh root@{} reboot'.format(self.host)) diff --git a/devlib/instrument/energy_probe.py b/devlib/instrument/energy_probe.py index b0b5180..6a97c5b 100644 --- a/devlib/instrument/energy_probe.py +++ b/devlib/instrument/energy_probe.py @@ -86,9 +86,8 @@ class EnergyProbeInstrument(Instrument): self.process.poll() if self.process.returncode is not None: stdout, stderr = self.process.communicate() - if sys.version_info[0] == 3: - stdout = stdout.decode(sys.stdout.encoding or 'utf-8', 'replace') - stderr = stderr.decode(sys.stdout.encoding or 'utf-8', 'replace') + stdout = stdout.decode(sys.stdout.encoding or 'utf-8', 'replace') + stderr = stderr.decode(sys.stdout.encoding or 'utf-8', 'replace') raise HostError( 'Energy Probe: Caiman exited unexpectedly with exit code {}.\n' 'stdout:\n{}\nstderr:\n{}'.format(self.process.returncode, diff --git a/devlib/instrument/monsoon.py b/devlib/instrument/monsoon.py index ac1741d..9799917 100644 --- a/devlib/instrument/monsoon.py +++ b/devlib/instrument/monsoon.py @@ -100,9 +100,8 @@ class MonsoonInstrument(Instrument): process.poll() if process.returncode is not None: stdout, stderr = process.communicate() - if sys.version_info[0] == 3: - stdout = stdout.encode(sys.stdout.encoding or 'utf-8') - stderr = stderr.encode(sys.stdout.encoding or 'utf-8') + stdout = stdout.encode(sys.stdout.encoding or 'utf-8') + stderr = stderr.encode(sys.stdout.encoding or 'utf-8') raise HostError( 'Monsoon script exited unexpectedly with exit code {}.\n' 'stdout:\n{}\nstderr:\n{}'.format(process.returncode, diff --git a/devlib/target.py b/devlib/target.py index 1d56b0e..d69b74c 100644 --- a/devlib/target.py +++ b/devlib/target.py @@ -2029,10 +2029,7 @@ class AndroidTarget(Target): parsed_xml = xml.dom.minidom.parse(filepath) with open(filepath, 'w') as f: - if sys.version_info[0] == 3: - f.write(parsed_xml.toprettyxml()) - else: - f.write(parsed_xml.toprettyxml().encode('utf-8')) + f.write(parsed_xml.toprettyxml()) @asyn.asyncf async def is_installed(self, name): diff --git a/devlib/utils/android.py b/devlib/utils/android.py index a88f34d..f107667 100755 --- a/devlib/utils/android.py +++ b/devlib/utils/android.py @@ -246,8 +246,7 @@ class ApkInfo(object): logger.debug(' '.join(command)) try: output = subprocess.check_output(command, stderr=subprocess.STDOUT) - if sys.version_info[0] == 3: - output = output.decode(sys.stdout.encoding or 'utf-8', 'replace') + output = output.decode(sys.stdout.encoding or 'utf-8', 'replace') except subprocess.CalledProcessError as e: raise HostError('Error while running "{}":\n{}' .format(command, e.output)) diff --git a/devlib/utils/csvutil.py b/devlib/utils/csvutil.py index 2cb1212..b696449 100644 --- a/devlib/utils/csvutil.py +++ b/devlib/utils/csvutil.py @@ -60,10 +60,7 @@ from contextlib import contextmanager @contextmanager def csvwriter(filepath, *args, **kwargs): - if sys.version_info[0] == 3: - wfh = open(filepath, 'w', newline='') - else: - wfh = open(filepath, 'wb') + wfh = open(filepath, 'w', newline='') try: yield csv.writer(wfh, *args, **kwargs) @@ -73,10 +70,7 @@ def csvwriter(filepath, *args, **kwargs): @contextmanager def csvreader(filepath, *args, **kwargs): - if sys.version_info[0] == 3: - fh = open(filepath, 'r', newline='') - else: - fh = open(filepath, 'rb') + fh = open(filepath, 'r', newline='') try: yield csv.reader(fh, *args, **kwargs) @@ -85,16 +79,10 @@ def csvreader(filepath, *args, **kwargs): def create_writer(filepath, *args, **kwargs): - if sys.version_info[0] == 3: - wfh = open(filepath, 'w', newline='') - else: - wfh = open(filepath, 'wb') + wfh = open(filepath, 'w', newline='') return csv.writer(wfh, *args, **kwargs), wfh def create_reader(filepath, *args, **kwargs): - if sys.version_info[0] == 3: - fh = open(filepath, 'r', newline='') - else: - fh = open(filepath, 'rb') + fh = open(filepath, 'r', newline='') return csv.reader(fh, *args, **kwargs), fh diff --git a/devlib/utils/misc.py b/devlib/utils/misc.py index 4734892..58cf6ec 100644 --- a/devlib/utils/misc.py +++ b/devlib/utils/misc.py @@ -594,11 +594,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: - TRANS_TABLE = string.maketrans(BAD_CHARS, '_' * len(BAD_CHARS)) +TRANS_TABLE = str.maketrans(BAD_CHARS, '_' * len(BAD_CHARS)) def to_identifier(text): diff --git a/devlib/utils/rendering.py b/devlib/utils/rendering.py index 3df82d6..52d4f00 100644 --- a/devlib/utils/rendering.py +++ b/devlib/utils/rendering.py @@ -217,10 +217,7 @@ class GfxinfoFrameCollector(FrameCollector): def collect_frames(self, wfh): cmd = 'dumpsys gfxinfo {} framestats' result = self.target.execute(cmd.format(self.package)) - if sys.version_info[0] == 3: - wfh.write(result.encode('utf-8')) - else: - wfh.write(result) + wfh.write(result.encode('utf-8')) def clear(self): pass diff --git a/devlib/utils/ssh.py b/devlib/utils/ssh.py index 0feceaf..c985ed2 100644 --- a/devlib/utils/ssh.py +++ b/devlib/utils/ssh.py @@ -758,11 +758,7 @@ class SshConnection(SshConnectionBase): output_chunks, exit_code = _read_paramiko_streams(stdout, stderr, select_timeout, callback, []) # Join in one go to avoid O(N^2) concatenation output = b''.join(output_chunks) - - if sys.version_info[0] == 3: - output = output.decode(sys.stdout.encoding or 'utf-8', 'replace') - if strip_colors: - output = strip_bash_colors(output) + output = output.decode(sys.stdout.encoding or 'utf-8', 'replace') return (exit_code, output) @@ -970,10 +966,7 @@ class TelnetConnection(SshConnectionBase): logger.debug(command) self._sendline(command) timed_out = self._wait_for_prompt(timeout) - if sys.version_info[0] == 3: - output = process_backspaces(self.conn.before.decode(sys.stdout.encoding or 'utf-8', 'replace')) - else: - output = process_backspaces(self.conn.before) + output = process_backspaces(self.conn.before.decode(sys.stdout.encoding or 'utf-8', 'replace')) if timed_out: self.cancel_running_command() diff --git a/devlib/utils/types.py b/devlib/utils/types.py index f212930..d7c8864 100644 --- a/devlib/utils/types.py +++ b/devlib/utils/types.py @@ -136,35 +136,25 @@ def bitmask(value): regex_type = type(re.compile('')) -if sys.version_info[0] == 3: - def regex(value): - if isinstance(value, regex_type): - if isinstance(value.pattern, str): - return value - return re.compile(value.pattern.decode(), - value.flags | re.UNICODE) - else: - if isinstance(value, bytes): - value = value.decode() - return re.compile(value) - - - def bytes_regex(value): - if isinstance(value, regex_type): - if isinstance(value.pattern, bytes): - return value - return re.compile(value.pattern.encode(sys.stdout.encoding or 'utf-8'), - value.flags & ~re.UNICODE) - else: - if isinstance(value, str): - value = value.encode(sys.stdout.encoding or 'utf-8') - return re.compile(value) -else: - def regex(value): - if isinstance(value, regex_type): +def regex(value): + if isinstance(value, regex_type): + if isinstance(value.pattern, str): return value - else: - return re.compile(value) + return re.compile(value.pattern.decode(), + value.flags | re.UNICODE) + else: + if isinstance(value, bytes): + value = value.decode() + return re.compile(value) - bytes_regex = regex +def bytes_regex(value): + if isinstance(value, regex_type): + if isinstance(value.pattern, bytes): + return value + return re.compile(value.pattern.encode(sys.stdout.encoding or 'utf-8'), + value.flags & ~re.UNICODE) + else: + if isinstance(value, str): + value = value.encode(sys.stdout.encoding or 'utf-8') + return re.compile(value) diff --git a/devlib/utils/version.py b/devlib/utils/version.py index a44498b..ec6a3f1 100644 --- a/devlib/utils/version.py +++ b/devlib/utils/version.py @@ -42,7 +42,7 @@ def get_commit(): p.wait() if p.returncode: return None - if sys.version_info[0] == 3 and isinstance(std, bytes): + if isinstance(std, bytes): return std[:8].decode(sys.stdout.encoding or 'utf-8', 'replace') else: return std[:8]