aboutsummaryrefslogtreecommitdiffhomepage
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
parent351d45c5e49363c9b5d0f4eb4a3867a7f111a42a (diff)
GitVfs: make objects usable as context managers
-rw-r--r--gbp/deb/source.py10
-rw-r--r--gbp/git/vfs.py6
-rwxr-xr-xgbp/scripts/pq.py5
-rw-r--r--tests/doctests/test_GitVfs.py40
4 files changed, 44 insertions, 17 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 = []
diff --git a/tests/doctests/test_GitVfs.py b/tests/doctests/test_GitVfs.py
index fb845e58..fdb15455 100644
--- a/tests/doctests/test_GitVfs.py
+++ b/tests/doctests/test_GitVfs.py
@@ -4,6 +4,7 @@
Test L{gbp.git.GitVfs}
"""
+import os
import gbp.log
from .. import context # noqa: F401
@@ -11,6 +12,17 @@ from .. import context # noqa: F401
gbp.log.setup(color=False, verbose=True)
+def setup_repo():
+ repo_dir = context.new_tmpdir(__name__)
+ repo = gbp.git.GitRepository.create(str(repo_dir))
+ content = 'al pha\na\nb\nc'
+ with open(os.path.join(repo.path, 'foo.txt'), 'w') as f:
+ f.write(content)
+ repo.add_files(repo.path, force=True)
+ repo.commit_all(msg="foo")
+ return (repo, content)
+
+
def test_read():
"""
Create a repository
@@ -22,15 +34,8 @@ def test_read():
- L{gbp.git.GitVfs._File.read}
- L{gbp.git.GitVfs._File.close}
- >>> import os, gbp.git.vfs
- >>> repo_dir = context.new_tmpdir(__name__)
- >>> repo = gbp.git.GitRepository.create(str(repo_dir))
- >>> f = open(os.path.join(repo.path, 'foo.txt'), 'w')
- >>> content = 'al pha\\na\\nb\\nc'
- >>> ret = f.write('al pha\\na\\nb\\nc')
- >>> f.close()
- >>> repo.add_files(repo.path, force=True)
- >>> repo.commit_all(msg="foo")
+ >>> import gbp.git.vfs
+ >>> (repo, content) = setup_repo()
>>> vfs = gbp.git.vfs.GitVfs(repo, 'HEAD')
>>> gf = vfs.open('foo.txt')
>>> gf.readline()
@@ -54,3 +59,20 @@ def test_read():
IOError: can't get HEAD:doesnotexist: fatal: Path 'doesnotexist' does not exist in 'HEAD'
>>> context.teardown()
"""
+
+
+def test_content_manager():
+ """
+ Create a repository
+
+ Methods tested:
+ - L{gbp.git.GitVfs.open}
+
+ >>> import gbp.git.vfs
+ >>> (repo, content) = setup_repo()
+ >>> vfs = gbp.git.vfs.GitVfs(repo, 'HEAD')
+ >>> with vfs.open('foo.txt') as gf:
+ ... data = gf.readlines()
+ >>> data
+ ['al pha\\n', 'a\\n', 'b\\n', 'c']
+ """