From d7430d42ceaa6028ce54877e5c6a77c966e1b139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Thu, 7 Mar 2019 03:07:42 +0100 Subject: [PATCH] Make pylint happy no-else-raise warning --- qubesadmin/backup/restore.py | 52 ++++++++++++-------------- qubesadmin/base.py | 5 +-- qubesadmin/firewall.py | 2 +- qubesadmin/tools/qvm_backup_restore.py | 7 ++-- qubesadmin/utils.py | 2 +- 5 files changed, 31 insertions(+), 37 deletions(-) diff --git a/qubesadmin/backup/restore.py b/qubesadmin/backup/restore.py index 0ec7bb0..57893cd 100644 --- a/qubesadmin/backup/restore.py +++ b/qubesadmin/backup/restore.py @@ -1003,8 +1003,7 @@ class BackupRestore(object): if f_one.read() != f_two.read(): raise QubesException( 'Invalid hmac on {}'.format(filename)) - else: - return True + return True with open(os.path.join(self.tmpdir, filename), 'rb') as f_input: hmac_proc = subprocess.Popen( @@ -1016,23 +1015,22 @@ class BackupRestore(object): if hmac_stderr: raise QubesException( "ERROR: verify file {0}: {1}".format(filename, hmac_stderr)) - else: - self.log.debug("Loading hmac for file %s", filename) - try: - with open(os.path.join(self.tmpdir, hmacfile), 'r', - encoding='ascii') as f_hmac: - hmac = load_hmac(f_hmac.read()) - except UnicodeDecodeError as err: - raise QubesException('Cannot load hmac file: ' + str(err)) - if hmac and load_hmac(hmac_stdout.decode('ascii')) == hmac: - os.unlink(os.path.join(self.tmpdir, hmacfile)) - self.log.debug( - "File verification OK -> Sending file %s", filename) - return True - raise QubesException( - "ERROR: invalid hmac for file {0}: {1}. " - "Is the passphrase correct?". - format(filename, load_hmac(hmac_stdout.decode('ascii')))) + self.log.debug("Loading hmac for file %s", filename) + try: + with open(os.path.join(self.tmpdir, hmacfile), 'r', + encoding='ascii') as f_hmac: + hmac = load_hmac(f_hmac.read()) + except UnicodeDecodeError as err: + raise QubesException('Cannot load hmac file: ' + str(err)) + if hmac and load_hmac(hmac_stdout.decode('ascii')) == hmac: + os.unlink(os.path.join(self.tmpdir, hmacfile)) + self.log.debug( + "File verification OK -> Sending file %s", filename) + return True + raise QubesException( + "ERROR: invalid hmac for file {0}: {1}. " + "Is the passphrase correct?". + format(filename, load_hmac(hmac_stdout.decode('ascii')))) def _verify_and_decrypt(self, filename, output=None): '''Handle scrypt-wrapped file @@ -1242,7 +1240,7 @@ class BackupRestore(object): """ if self.header_data.version == 1: raise NotImplementedError('Backup format version 1 not supported') - elif self.header_data.version in [2, 3]: + if self.header_data.version in [2, 3]: self._retrieve_backup_header_files( ['qubes.xml.000', 'qubes.xml.000.hmac']) self._verify_hmac("qubes.xml.000", "qubes.xml.000.hmac") @@ -1407,12 +1405,11 @@ class BackupRestore(object): raise QubesException( 'retrieved backup size exceed expected size, if you ' 'believe this is ok, use --ignore-size-limit option') - else: - raise QubesException( - "unable to read the qubes backup file {} ({}): {}" - .format(self.backup_location, - retrieve_proc.returncode, error_pipe.read( - MAX_STDERR_BYTES))) + raise QubesException( + "unable to read the qubes backup file {} ({}): {}" + .format(self.backup_location, + retrieve_proc.returncode, error_pipe.read( + MAX_STDERR_BYTES))) # wait for other processes (if any) for proc in self.processes_to_kill_on_cancel: proc.wait() @@ -1837,8 +1834,7 @@ class BackupRestore(object): except QubesException as err: if self.options.verify_only: raise - else: - self.log.error('Error extracting data: %s', str(err)) + self.log.error('Error extracting data: %s', str(err)) finally: if self.log.getEffectiveLevel() > logging.DEBUG: shutil.rmtree(self.tmpdir) diff --git a/qubesadmin/base.py b/qubesadmin/base.py index cb0a2db..6b36da9 100644 --- a/qubesadmin/base.py +++ b/qubesadmin/base.py @@ -100,9 +100,8 @@ class PropertyHolder(object): exc_class = qubesadmin.exc.QubesException # TODO: handle traceback if given raise exc_class(format_string, *args) - else: - raise qubesadmin.exc.QubesDaemonCommunicationError( - 'Invalid response format') + raise qubesadmin.exc.QubesDaemonCommunicationError( + 'Invalid response format') def property_list(self): ''' diff --git a/qubesadmin/firewall.py b/qubesadmin/firewall.py index 3f6a6e5..b103cf1 100644 --- a/qubesadmin/firewall.py +++ b/qubesadmin/firewall.py @@ -89,7 +89,7 @@ class DstHost(RuleOption): # TODO: in python >= 3.3 ipaddress module could be used if value.count('/') > 1: raise ValueError('Too many /: ' + value) - elif not value.count('/'): + if not value.count('/'): # add prefix length to bare IP addresses try: socket.inet_pton(socket.AF_INET6, value) diff --git a/qubesadmin/tools/qvm_backup_restore.py b/qubesadmin/tools/qvm_backup_restore.py index d083019..4c7a1db 100644 --- a/qubesadmin/tools/qvm_backup_restore.py +++ b/qubesadmin/tools/qvm_backup_restore.py @@ -142,7 +142,7 @@ def handle_broken(app, args, restore_info): raise qubesadmin.exc.QubesException( "Install them before proceeding with the restore." "Or pass: --skip-broken or --ignore-missing.") - elif args.skip_broken: + if args.skip_broken: app.log.warning("Skipping broken entries: VMs that depend on " "missing TemplateVMs will NOT be restored.") elif args.ignore_missing: @@ -160,7 +160,7 @@ def handle_broken(app, args, restore_info): raise qubesadmin.exc.QubesException( "Install them before proceeding with the restore." "Or pass: --skip-broken or --ignore-missing.") - elif args.skip_broken: + if args.skip_broken: app.log.warning("Skipping broken entries: VMs that depend on " "missing NetVMs will NOT be restored.") elif args.ignore_missing: @@ -180,8 +180,7 @@ def handle_broken(app, args, restore_info): "Skip restoring the dom0 home directory " "(--skip-dom0-home), or pass " "--ignore-username-mismatch to continue anyway.") - else: - app.log.warning("Continuing as directed.") + app.log.warning("Continuing as directed.") app.log.warning("NOTE: The archived dom0 home directory " "will be restored to a new directory " "'home-restore-' " diff --git a/qubesadmin/utils.py b/qubesadmin/utils.py index be5380a..c40cf04 100644 --- a/qubesadmin/utils.py +++ b/qubesadmin/utils.py @@ -91,7 +91,7 @@ def get_entry_point_one(group, name): epoints = tuple(pkg_resources.iter_entry_points(group, name)) if not epoints: raise KeyError(name) - elif len(epoints) > 1: + if len(epoints) > 1: raise TypeError( 'more than 1 implementation of {!r} found: {}'.format(name, ', '.join('{}.{}'.format(ep.module_name, '.'.join(ep.attrs))