From aab6f2b5d3621d5760145d133a72495ef2ac1f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jard=C3=B3n?= Date: Fri, 13 Mar 2015 13:02:24 +0000 Subject: Use python3 compatible notation for catching exceptions Change-Id: Ibda7a938cd16e35517a531140f39ef4664d85c72 --- distbuild/build_controller.py | 2 +- distbuild/connection_machine.py | 4 ++-- distbuild/sockbuf.py | 4 ++-- distbuild/socketsrc.py | 2 +- morphlib/bins.py | 4 ++-- morphlib/branchmanager.py | 4 ++-- morphlib/buildbranch.py | 2 +- morphlib/builder.py | 6 +++--- morphlib/extractedtarball.py | 4 ++-- morphlib/git.py | 4 ++-- morphlib/gitdir.py | 6 +++--- morphlib/gitversion.py | 2 +- morphlib/localrepocache.py | 6 +++--- morphlib/mountableimage.py | 8 ++++---- morphlib/plugins/artifact_inspection_plugin.py | 2 +- morphlib/plugins/cross-bootstrap_plugin.py | 4 ++-- morphlib/remoteartifactcache.py | 4 ++-- morphlib/remoterepocache.py | 4 ++-- morphlib/sourceresolver.py | 2 +- morphlib/stagingarea.py | 2 +- morphlib/writeexts.py | 4 ++-- 21 files changed, 40 insertions(+), 40 deletions(-) diff --git a/distbuild/build_controller.py b/distbuild/build_controller.py index 83e44f89..d6f3398f 100644 --- a/distbuild/build_controller.py +++ b/distbuild/build_controller.py @@ -303,7 +303,7 @@ class BuildController(distbuild.StateMachine): text = self._artifact_data.peek() try: artifact = distbuild.deserialise_artifact(text) - except ValueError, e: + except ValueError as e: logging.error(traceback.format_exc()) self.fail('Failed to compute build graph: %s' % e) return diff --git a/distbuild/connection_machine.py b/distbuild/connection_machine.py index b17a0e1d..b8248931 100644 --- a/distbuild/connection_machine.py +++ b/distbuild/connection_machine.py @@ -105,7 +105,7 @@ class ConnectionMachine(distbuild.StateMachine): distbuild.set_nonblocking(self._socket) try: self._socket.connect((self._addr, self._port)) - except socket.error, e: + except socket.error as e: if e.errno != errno.EINPROGRESS: raise socket.error( "%s (attempting connection to distbuild controller " @@ -117,7 +117,7 @@ class ConnectionMachine(distbuild.StateMachine): def _connect(self, event_source, event): try: self._socket.connect((self._addr, self._port)) - except socket.error, e: + except socket.error as e: logging.error( 'Failed to connect to %s:%s: %s' % (self._addr, self._port, str(e))) diff --git a/distbuild/sockbuf.py b/distbuild/sockbuf.py index 1cb524a5..6feb8669 100644 --- a/distbuild/sockbuf.py +++ b/distbuild/sockbuf.py @@ -143,7 +143,7 @@ class SocketBuffer(StateMachine): def _fill(self, event_source, event): try: data = event.sock.read(self._max_buffer) - except (IOError, OSError), e: + except (IOError, OSError) as e: logging.debug( '%s: _fill(): Exception %s from sock.read()', self, e) return [SocketError(event.sock, e)] @@ -163,7 +163,7 @@ class SocketBuffer(StateMachine): data = self._wbuf.read(max_write) try: n = event.sock.write(data) - except (IOError, OSError), e: + except (IOError, OSError) as e: logging.debug( '%s: _flush(): Exception %s from sock.write()', self, e) return [SocketError(event.sock, e)] diff --git a/distbuild/socketsrc.py b/distbuild/socketsrc.py index b2a21a16..daba1610 100644 --- a/distbuild/socketsrc.py +++ b/distbuild/socketsrc.py @@ -77,7 +77,7 @@ class ListeningSocketEventSource(EventSource): if self._accepting and self.sock.fileno() in r: try: conn, addr = self.sock.accept() - except socket.error, e: + except socket.error as e: return [SocketError(self.sock, e)] else: logging.info( diff --git a/morphlib/bins.py b/morphlib/bins.py index 41e271dd..2e8ba0b3 100644 --- a/morphlib/bins.py +++ b/morphlib/bins.py @@ -74,7 +74,7 @@ if sys.version_info < (2, 7, 3): # pragma: no cover else: if sys.platform != "os2emx": os.chown(targetpath, u, g) - except EnvironmentError, e: + except EnvironmentError as e: raise ExtractError("could not change owner") tarfile.TarFile.chown = fixed_chown @@ -186,7 +186,7 @@ def unpack_binary_from_file(f, dirname): # pragma: no cover prepare_extract(tarinfo, targetpath) try: ret = real(tarinfo, targetpath) - except (IOError, OSError), e: + except (IOError, OSError) as e: if e.errno != errno.EEXIST: if e.filename is None: e.filename = targetpath diff --git a/morphlib/branchmanager.py b/morphlib/branchmanager.py index 6feb094b..92a1f4be 100644 --- a/morphlib/branchmanager.py +++ b/morphlib/branchmanager.py @@ -91,7 +91,7 @@ class LocalRefManager(object): op, args = d.pop() try: op(*args) - except Exception, e: + except Exception as e: exceptions.append((op, args, e)) if exceptions: raise RefCleanupError(primary, exceptions) @@ -200,7 +200,7 @@ class RemoteRefManager(object): remote, refspecs = d.pop() try: remote.push(*refspecs) - except Exception, e: + except Exception as e: exceptions.append((remote, refspecs, e)) if exceptions: raise RefCleanupError(primary, exceptions) diff --git a/morphlib/buildbranch.py b/morphlib/buildbranch.py index 6c736c7b..80cecd75 100644 --- a/morphlib/buildbranch.py +++ b/morphlib/buildbranch.py @@ -292,7 +292,7 @@ class BuildBranch(object): func, args, kwargs = self._cleanup.pop() try: func(*args, **kwargs) - except Exception, e: + except Exception as e: exceptions.append(e) if exceptions: raise BuildBranchCleanupError(self, exceptions) diff --git a/morphlib/builder.py b/morphlib/builder.py index 94eeacb8..04ebd149 100644 --- a/morphlib/builder.py +++ b/morphlib/builder.py @@ -289,7 +289,7 @@ class ChunkBuilder(BuilderBase): self.create_devices(destdir) os.rename(temppath, logpath) - except BaseException, e: + except BaseException as e: logging.error('Caught exception: %s' % str(e)) logging.info('Cleaning up staging area') self.staging_area.chroot_close() @@ -375,7 +375,7 @@ class ChunkBuilder(BuilderBase): if stdout: stdout.flush() - except cliapp.AppException, e: + except cliapp.AppException as e: if not stdout: with open(logfilepath, 'r') as log: self.app.output.write("%s failed\n" % step) @@ -656,7 +656,7 @@ class SystemBuilder(BuilderBase): # pragma: no cover msg = error_message_for_containerised_commandline( argv, err, container_config) raise cliapp.AppException(msg) - except BaseException, e: + except BaseException as e: self.app.status( msg='Error while running system integration commands', error=True) diff --git a/morphlib/extractedtarball.py b/morphlib/extractedtarball.py index 42744d7e..95c0582d 100644 --- a/morphlib/extractedtarball.py +++ b/morphlib/extractedtarball.py @@ -41,7 +41,7 @@ class ExtractedTarball(object): # pragma: no cover self.tempdir = tempfile.mkdtemp(dir=self.app.settings['tempdir']) try: morphlib.bins.unpack_binary(self.tarball, self.tempdir) - except BaseException, e: + except BaseException as e: logging.error('Caught exception: %s' % str(e)) logging.debug('Removing temporary directory %s' % self.tempdir) shutil.rmtree(self.tempdir) @@ -53,7 +53,7 @@ class ExtractedTarball(object): # pragma: no cover tarball=os.path.basename(self.tarball), chatty=True) try: shutil.rmtree(self.tempdir) - except BaseException, e: + except BaseException as e: logging.warning( 'Error when removing temporary directory %s: %s' % (self.tempdir, str(e))) diff --git a/morphlib/git.py b/morphlib/git.py index 4fc73a09..acda6137 100644 --- a/morphlib/git.py +++ b/morphlib/git.py @@ -190,7 +190,7 @@ def get_user_name(runcmd): try: config = check_config_set(runcmd, keys={"user.name": "My Name"}) return config['user.name'] - except ConfigNotSetException, e: + except ConfigNotSetException as e: raise IdentityNotSetException(e.missing) @@ -201,7 +201,7 @@ def get_user_email(runcmd): try: cfg = check_config_set(runcmd, keys={"user.email": "me@example.com"}) return cfg['user.email'] - except ConfigNotSetException, e: + except ConfigNotSetException as e: raise IdentityNotSetException(e.missing) def check_config_set(runcmd, keys, cwd='.'): diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index 34791b3f..03640a22 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -706,7 +706,7 @@ class GitDirectory(object): # this ensures it will fail if the branch already exists try: return self._update_ref((ref, sha1, '0' * 40), message) - except Exception, e: + except Exception as e: raise RefAddError(self, ref, sha1, e) def update_ref(self, ref, sha1, old_sha1, message=None): @@ -726,7 +726,7 @@ class GitDirectory(object): self._check_is_sha1(old_sha1) try: return self._update_ref((ref, sha1, old_sha1), message) - except Exception, e: + except Exception as e: raise RefUpdateError(self, ref, old_sha1, sha1, e) def delete_ref(self, ref, old_sha1, message=None): @@ -744,7 +744,7 @@ class GitDirectory(object): self._check_is_sha1(old_sha1) try: return self._update_ref(('-d', ref, old_sha1), message) - except Exception, e: + except Exception as e: raise RefDeleteError(self, ref, old_sha1, e) def describe(self): diff --git a/morphlib/gitversion.py b/morphlib/gitversion.py index edd4d722..7b4459a4 100644 --- a/morphlib/gitversion.py +++ b/morphlib/gitversion.py @@ -33,7 +33,7 @@ try: commit = pkgutil.get_data('morphlib', 'commit') tree = pkgutil.get_data('morphlib', 'tree') ref = pkgutil.get_data('morphlib', 'ref') -except IOError, e: +except IOError as e: from os.path import dirname def run_git(*args): command = ['git'] + list(args) diff --git a/morphlib/localrepocache.py b/morphlib/localrepocache.py index af557b1a..4fc3916a 100644 --- a/morphlib/localrepocache.py +++ b/morphlib/localrepocache.py @@ -172,7 +172,7 @@ class LocalRepoCache(object): self._git(['config', 'remote.origin.mirror', 'true'], cwd=path) self._git(['config', 'remote.origin.fetch', '+refs/*:refs/*'], cwd=path) - except BaseException, e: # pragma: no cover + except BaseException as e: # pragma: no cover if self.fs.exists(path): self.fs.removedir(path, force=True) return False, 'Unable to extract tarball %s: %s' % ( @@ -192,7 +192,7 @@ class LocalRepoCache(object): try: return self.get_repo(reponame) - except NotCached, e: + except NotCached as e: pass repourl = self._resolver.pull_url(reponame) @@ -213,7 +213,7 @@ class LocalRepoCache(object): try: self._git(['clone', '--mirror', '-n', repourl, target], echo_stderr=self._app.settings['verbose']) - except cliapp.AppException, e: + except cliapp.AppException as e: errors.append('Unable to clone from %s to %s: %s' % (repourl, target, e)) if self.fs.exists(target): diff --git a/morphlib/mountableimage.py b/morphlib/mountableimage.py index ecca58f3..dcc496e7 100644 --- a/morphlib/mountableimage.py +++ b/morphlib/mountableimage.py @@ -45,7 +45,7 @@ class MountableImage(object): # pragma: no cover infh = gzip.open(path, "rb") morphlib.util.copyfileobj(infh, outfh) infh.close() - except BaseException, e: + except BaseException as e: logging.error('Caught exception: %s' % str(e)) logging.info('Removing temporary file %s' % self.temp_path) os.unlink(self.temp_path) @@ -64,17 +64,17 @@ class MountableImage(object): # pragma: no cover chatty=True) try: morphlib.fsutils.unmount(self.app.runcmd, mount_point) - except BaseException, e: + except BaseException as e: logging.info('Ignoring error when unmounting: %s' % str(e)) try: morphlib.fsutils.undo_device_mapping(self.app.runcmd, path) - except BaseException, e: + except BaseException as e: logging.info( 'Ignoring error when undoing device mapping: %s' % str(e)) try: os.rmdir(mount_point) os.unlink(path) - except BaseException, e: + except BaseException as e: logging.info( 'Ignoring error when removing temporary files: %s' % str(e)) diff --git a/morphlib/plugins/artifact_inspection_plugin.py b/morphlib/plugins/artifact_inspection_plugin.py index f1f41b3a..b16f393c 100644 --- a/morphlib/plugins/artifact_inspection_plugin.py +++ b/morphlib/plugins/artifact_inspection_plugin.py @@ -162,7 +162,7 @@ class VersionGuesser(object): version = guesser.guess_version(repo, ref, tree) if version: break - except cliapp.AppException, err: + except cliapp.AppException as err: self.app.status(msg='%(repo)s: Failed to list files in %(ref)s', repo=repo, ref=ref, chatty=True) return version diff --git a/morphlib/plugins/cross-bootstrap_plugin.py b/morphlib/plugins/cross-bootstrap_plugin.py index e1f566c2..79609cb5 100644 --- a/morphlib/plugins/cross-bootstrap_plugin.py +++ b/morphlib/plugins/cross-bootstrap_plugin.py @@ -65,7 +65,7 @@ class BootstrapSystemBuilder(morphlib.builder.BuilderBase): self.unpack_sources(fs_root) self.write_build_script(fs_root) self.create_tarball(handle, fs_root, system_name) - except BaseException, e: + except BaseException as e: logging.error(traceback.format_exc()) self.app.status(msg='Error while building bootstrap image', error=True) @@ -84,7 +84,7 @@ class BootstrapSystemBuilder(morphlib.builder.BuilderBase): with cache.get(chunk_artifact) as chunk_file: try: morphlib.bins.unpack_binary_from_file(chunk_file, dest) - except BaseException, e: + except BaseException as e: self.app.status( msg='Error unpacking binary chunk %(name)s', name=chunk_artifact.name, diff --git a/morphlib/remoteartifactcache.py b/morphlib/remoteartifactcache.py index ba5fd5e7..427e4cbb 100644 --- a/morphlib/remoteartifactcache.py +++ b/morphlib/remoteartifactcache.py @@ -71,14 +71,14 @@ class RemoteArtifactCache(object): def get(self, artifact, log=logging.error): try: return self._get_file(artifact.basename()) - except urllib2.URLError, e: + except urllib2.URLError as e: log(str(e)) raise GetError(self, artifact) def get_artifact_metadata(self, artifact, name, log=logging.error): try: return self._get_file(artifact.metadata_basename(name)) - except urllib2.URLError, e: + except urllib2.URLError as e: log(str(e)) raise GetArtifactMetadataError(self, artifact, name) diff --git a/morphlib/remoterepocache.py b/morphlib/remoterepocache.py index 49441d71..4a6d9fe9 100644 --- a/morphlib/remoterepocache.py +++ b/morphlib/remoterepocache.py @@ -54,7 +54,7 @@ class RemoteRepoCache(object): repo_url = self._resolver.pull_url(repo_name) try: return self._resolve_ref_for_repo_url(repo_url, ref) - except BaseException, e: + except BaseException as e: logging.error('Caught exception: %s' % str(e)) raise ResolveRefError(repo_name, ref) @@ -73,7 +73,7 @@ class RemoteRepoCache(object): try: info = json.loads(self._ls_tree_for_repo_url(repo_url, ref)) return info['tree'].keys() - except BaseException, e: + except BaseException as e: logging.error('Caught exception: %s' % str(e)) raise LsTreeError(repo_name, ref) diff --git a/morphlib/sourceresolver.py b/morphlib/sourceresolver.py index 0b8ab5db..d2b47d35 100644 --- a/morphlib/sourceresolver.py +++ b/morphlib/sourceresolver.py @@ -206,7 +206,7 @@ class SourceResolver(object): reponame=reponame, ref=ref, chatty=True) - except BaseException, e: + except BaseException as e: logging.warning('Caught (and ignored) exception: %s' % str(e)) if absref is None: diff --git a/morphlib/stagingarea.py b/morphlib/stagingarea.py index a6496f4b..8c2781aa 100644 --- a/morphlib/stagingarea.py +++ b/morphlib/stagingarea.py @@ -165,7 +165,7 @@ class StagingArea(object): try: morphlib.bins.unpack_binary_from_file( handle, savedir + '/') - except BaseException, e: # pragma: no cover + except BaseException as e: # pragma: no cover shutil.rmtree(savedir) raise # TODO: This rename is not concurrency safe if two builds are diff --git a/morphlib/writeexts.py b/morphlib/writeexts.py index 3382767c..129b2bc4 100644 --- a/morphlib/writeexts.py +++ b/morphlib/writeexts.py @@ -177,7 +177,7 @@ class WriteExtension(cliapp.Application): self.create_btrfs_system_layout( temp_root, mp, version_label='factory', disk_uuid=self.get_uuid(raw_disk)) - except BaseException, e: + except BaseException as e: sys.stderr.write('Error creating Btrfs system layout') raise @@ -279,7 +279,7 @@ class WriteExtension(cliapp.Application): cliapp.runcmd(['mount', location, mount_point]) else: cliapp.runcmd(['mount', '-o', 'loop', location, mount_point]) - except BaseException, e: + except BaseException as e: sys.stderr.write('Error mounting filesystem') os.rmdir(mount_point) raise -- cgit v1.2.1