aboutsummaryrefslogtreecommitdiffhomepage
path: root/gbp
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2017-01-02 15:36:19 +0100
committerGuido Günther <agx@sigxcpu.org>2017-01-02 16:01:18 +0100
commitbbee246b1e2c62fa869c7918c1dfcda52354283f (patch)
tree53e1eb2d5c2280ef5e5c4a4ac6ae50850a74a33b /gbp
parent351d45c5e49363c9b5d0f4eb4a3867a7f111a42a (diff)
GitVfs: make objects usable as context managers
Diffstat (limited to 'gbp')
-rw-r--r--gbp/deb/source.py10
-rw-r--r--gbp/git/vfs.py6
-rwxr-xr-xgbp/scripts/pq.py5
3 files changed, 13 insertions, 8 deletions
diff --git a/gbp/deb/source.py b/gbp/deb/source.py
index 7b8ef98e..8c5f3f77 100644
--- a/gbp/deb/source.py
+++ b/gbp/deb/source.py
@@ -26,7 +26,7 @@ import six
class FileVfs(object):
def __init__(self, dir):
"""
- Access files in a unpaced Debian source package.
+ Access files in an unpacked Debian source package.
@param dir: the toplevel of the source tree
@type dir: C{str}
@@ -66,8 +66,8 @@ class DebianSource(object):
Whether this is a native Debian package
"""
try:
- ff = self._vfs.open('debian/source/format')
- f = DebianSourceFormat(ff.read())
+ with self._vfs.open('debian/source/format') as ff:
+ f = DebianSourceFormat(ff.read())
if f.type:
return f.type == 'native'
except IOError as e:
@@ -85,8 +85,8 @@ class DebianSource(object):
"""
if not self._changelog:
try:
- clf = self._vfs.open('debian/changelog')
- self._changelog = ChangeLog(clf.read())
+ with self._vfs.open('debian/changelog') as clf:
+ self._changelog = ChangeLog(clf.read())
except IOError as err:
raise DebianSourceError('Failed to read changelog: %s' % err)
return self._changelog
diff --git a/gbp/git/vfs.py b/gbp/git/vfs.py
index 191cf621..4ca29625 100644
--- a/gbp/git/vfs.py
+++ b/gbp/git/vfs.py
@@ -44,6 +44,12 @@ class GitVfs(object):
def close(self):
return self._data.close()
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self.close()
+
def __init__(self, repo, committish=None):
"""
@param repo: the git repository to act on
diff --git a/gbp/scripts/pq.py b/gbp/scripts/pq.py
index 37968edd..8c278123 100755
--- a/gbp/scripts/pq.py
+++ b/gbp/scripts/pq.py
@@ -157,9 +157,8 @@ def commit_patches(repo, branch, patches, options, patch_dir):
vfs = gbp.git.vfs.GitVfs(repo, branch)
try:
- oldseries = vfs.open('debian/patches/series')
- oldpatches = [p.strip() for p in oldseries.readlines()]
- oldseries.close()
+ with vfs.open('debian/patches/series') as oldseries:
+ oldpatches = [p.strip() for p in oldseries.readlines()]
except IOError:
# No series file yet
oldpatches = []