From a60f37dab018ee827aef631f0e1646e720194655 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 29 Mar 2013 15:55:12 +0100 Subject: Introduce Source class so we don't have to expose all the details of Debian's different files and conventions. --- gbp/deb/format.py | 19 ++++++++++ gbp/deb/source.py | 75 ++++++++++++++++++++++++++++++++++++++ gbp/git/__init__.py | 1 + tests/15_test_DebianSource.py | 84 +++++++++++++++++++++++++++++++++++++++++++ tests/context.py | 2 +- tests/test_GitRepository.py | 3 +- tests/test_GitVfs.py | 10 +++--- tests/testutils.py | 1 - 8 files changed, 186 insertions(+), 9 deletions(-) create mode 100644 gbp/deb/source.py create mode 100644 tests/15_test_DebianSource.py diff --git a/gbp/deb/format.py b/gbp/deb/format.py index 8d6ac5e..9abba73 100644 --- a/gbp/deb/format.py +++ b/gbp/deb/format.py @@ -40,6 +40,7 @@ class DebianSourceFormat(object): ... DebianSourceFormatError: Cannot get source format from '1.0 broken' """ + format_file = 'debian/source/format' def _parse(self, content): parts = content.split() @@ -68,6 +69,9 @@ class DebianSourceFormat(object): """The 'type' (e.g. git, native)""" return self._type + def __str__(self): + return "%s (%s)" % (self._version, self._type) + @classmethod def parse_file(klass, filename): """ @@ -91,6 +95,21 @@ class DebianSourceFormat(object): with file(filename) as f: return klass(f.read()) + @classmethod + def from_content(klass, version, type, format_file=None): + """ + Write a format file from I{type} and I{format} at + I{format_file} + + @param version: the source package format version + @param type: the format type + @param format_file: the format file to create with + the above parameters + """ + format_file = format_file or klass.format_file + with file(klass.format_file, 'w') as f: + f.write("%s (%s)" % (version, type)) + return klass.parse_file(klass.format_file) if __name__ == "__main__": import doctest diff --git a/gbp/deb/source.py b/gbp/deb/source.py new file mode 100644 index 0000000..5d2a947 --- /dev/null +++ b/gbp/deb/source.py @@ -0,0 +1,75 @@ +# vim: set fileencoding=utf-8 : +# +# (C) 2013 Guido Günther +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +"""provides some debian source package related helpers""" + +import os +from gbp.deb.format import DebianSourceFormat +from gbp.deb.changelog import ChangeLog + +class FileVfs(object): + def __init__(self, dir): + """ + Access files in a unpaced Debian source package. + + @param dir: the toplevel of the source tree + @type dir: C{str} + """ + self._dir = dir + + def open(self, path, flags=None): + flags = flags or 'r' + return file(os.path.join(self._dir, path), flags) + +class DebianSourceError(Exception): + pass + +class DebianSource(object): + """ + A debianized source tree + + Querying/setting information in a debianized source tree + involves several files. This class provides a common interface. + """ + def __init__(self, vfs): + """ + @param vfs: a class that implemented GbpVFS interfacce or + a directory (which will used the DirGbpVFS class. + """ + if isinstance(vfs, basestring): + self._vfs = FileVfs(vfs) + else: + self._vfs = vfs + + def is_native(self): + """ + Whether this is a native debian package + """ + try: + ff = self._vfs.open('debian/source/format') + f = DebianSourceFormat(ff.read()) + return f.type == 'native' + except IOError as e: + pass # Fall back to changelog parsing + + try: + clf = self._vfs.open('debian/changelog') + cl = ChangeLog(clf.read()) + return cl.is_native() + except IOError as e: + raise DebianSourceError("Failed to determine source format: %s" % e) + + diff --git a/gbp/git/__init__.py b/gbp/git/__init__.py index cf15beb..55dc3a8 100644 --- a/gbp/git/__init__.py +++ b/gbp/git/__init__.py @@ -25,6 +25,7 @@ from gbp.git.errors import GitError from gbp.git.repository import GitRepository, GitRepositoryError from gbp.git.fastimport import FastImport from gbp.git.args import GitArgs +from gbp.git.vfs import GitVfs def rfc822_date_to_git(rfc822_date): diff --git a/tests/15_test_DebianSource.py b/tests/15_test_DebianSource.py new file mode 100644 index 0000000..4896b31 --- /dev/null +++ b/tests/15_test_DebianSource.py @@ -0,0 +1,84 @@ +# vim: set fileencoding=utf-8 : +# (C) 2013 Guido Günther +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +"""Test L{gbp.pq}""" + +from . import context + +import os +import testutils +from gbp.deb.source import DebianSource, DebianSourceError +from gbp.deb.format import DebianSourceFormat +from gbp.git.vfs import GitVfs + +class TestDebianSource(testutils.DebianGitTestRepo): + """Test L{gbp.deb.source}'s """ + + def setUp(self): + testutils.DebianGitTestRepo.setUp(self) + context.chdir(self.repo.path) + + def test_is_native_file_3_file(self): + """Test native package of format 3""" + source = DebianSource('.') + os.makedirs('debian/source') + self.assertRaises(DebianSourceError, + source.is_native) + + dsf = DebianSourceFormat.from_content("3.0", "native") + self.assertEqual(dsf.type, 'native') + self.assertTrue(source.is_native()) + + dsf = DebianSourceFormat.from_content("3.0", "quilt") + self.assertEqual(dsf.type, 'quilt') + self.assertFalse(source.is_native()) + + def test_is_native_fallback_file(self): + """Test native package without a debian/source/format file""" + source = DebianSource('.') + os.makedirs('debian/') + self.assertRaises(DebianSourceError, + source.is_native) + + with file('debian/changelog', 'w') as f: + f.write("""git-buildpackage (0.2.3) git-buildpackage; urgency=low + + * git doesn't like '~' in tag names so replace this with a dot when tagging + + -- Guido Guenther Mon, 2 Oct 2006 18:30:20 +0200 +""") + source = DebianSource('.') + self.assertTrue(source.is_native()) + + def _commit_format(self, version, format): + # Commit a format file to disk + if not os.path.exists('debian/source'): + os.makedirs('debian/source') + dsf = DebianSourceFormat.from_content(version, format) + self.assertEqual(dsf.type, format) + self.repo.add_files('.') + self.repo.commit_all('foo') + os.unlink('debian/source/format') + self.assertFalse(os.path.exists('debian/source/format')) + + def test_is_native_file_3_git(self): + """Test native package of format 3 from git""" + self._commit_format('3.0', 'native') + source = DebianSource(GitVfs(self.repo)) + self.assertTrue(source.is_native()) + + self._commit_format('3.0', 'quilt') + source = DebianSource(GitVfs(self.repo)) + self.assertFalse(source.is_native()) diff --git a/tests/context.py b/tests/context.py index 22a9e29..cc3e25d 100644 --- a/tests/context.py +++ b/tests/context.py @@ -29,7 +29,7 @@ def chdir(dir): def new_tmpdir(name): global _tmpdirs prefix='gbp_%s_' % name - tmpdir=TmpDir(prefix) + tmpdir = TmpDir(prefix) _tmpdirs.append(tmpdir) return tmpdir diff --git a/tests/test_GitRepository.py b/tests/test_GitRepository.py index a91ee06..5e11fad 100644 --- a/tests/test_GitRepository.py +++ b/tests/test_GitRepository.py @@ -13,7 +13,6 @@ This testcase creates several repositores: from . import context -import os import gbp.log gbp.log.setup(color=False, verbose=True) @@ -73,7 +72,7 @@ def test_add_files(): Properties tested: - L{gbp.git.GitRepository.head} - >>> import gbp.git, shutil + >>> import gbp.git, shutil, os >>> repo = gbp.git.GitRepository(repo_dir) >>> shutil.copy(os.path.join(repo.path, ".git/HEAD"), \ os.path.join(repo.path, "testfile")) diff --git a/tests/test_GitVfs.py b/tests/test_GitVfs.py index c4e2694..05ff545 100644 --- a/tests/test_GitVfs.py +++ b/tests/test_GitVfs.py @@ -4,12 +4,12 @@ Test L{gbp.git.GitVfs} """ -import os import gbp.log from . import context gbp.log.setup(color=False, verbose=True) +import gbp.git def test_read(): repo_dir = context.new_tmpdir(__name__) @@ -18,10 +18,10 @@ def test_read(): Methods tested: - L{gbp.git.GitVfs.open} - - L{gbp.git._File.readline} - - L{gbp.git._File.readlines} - - L{gbp.git._File.read} - - L{gbp.git._File.close} + - L{gbp.git.GitVfs._File.readline} + - L{gbp.git.GitVfs._File.readlines} + - L{gbp.git.GitVfs._File.read} + - L{gbp.git.GitVfs._File.close} >>> import os, gbp.git.vfs >>> repo = gbp.git.GitRepository.create(str(repo_dir)) diff --git a/tests/testutils.py b/tests/testutils.py index 112bb04..38e0ac3 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -3,7 +3,6 @@ from . import context import os -import shutil import unittest import gbp.log -- cgit v1.2.3