Commit a60f37da authored by Guido Günther's avatar Guido Günther

Introduce Source class

so we don't have to expose all the details of Debian's different
files and conventions.
parent 8fd5ec31
......@@ -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
......
# vim: set fileencoding=utf-8 :
#
# (C) 2013 Guido Günther <agx@sigxcpu.org>
# 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)
......@@ -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):
......
# vim: set fileencoding=utf-8 :
# (C) 2013 Guido Günther <agx@sigxcpu.org>
# 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 <agx@sigxcpu.org> 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())
......@@ -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
......
......@@ -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"))
......
......@@ -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))
......
......@@ -3,7 +3,6 @@
from . import context
import os
import shutil
import unittest
import gbp.log
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment