Generate example package

This commit is contained in:
Stefano Rivera 2011-01-15 17:56:48 +02:00
parent 495d14b69d
commit b11d08f6b0
5 changed files with 156 additions and 32 deletions

View File

@ -1,17 +0,0 @@
Format: 3.0 (quilt)
Source: example
Binary: example
Architecture: all
Version: 1.0-1
Maintainer: Stefano Rivera <stefanor@ubuntu.com>
Standards-Version: 3.9.1
Build-Depends: debhelper (>= 7.0.50~)
Checksums-Sha1:
13c309f70155ab5be50553ce72f3344c48851013 170 example_1.0.orig.tar.gz
ae9dcd225000d24128b8eccbdc472abcfa9b60ee 1170 example_1.0-1.debian.tar.gz
Checksums-Sha256:
4908cb2ce47c9835fb74476ab5dba0298da7a56a389328b845d07575f5ea6276 170 example_1.0.orig.tar.gz
ca366dc1ae0efa1df6537f6882e87134a0409e1589ca18daf56d065beb9cfce3 1170 example_1.0-1.debian.tar.gz
Files:
8054fdf3653160e13d9539ca36c2e311 170 example_1.0.orig.tar.gz
8538e8b6cb8162d5c6886b18c132a1c8 1170 example_1.0-1.debian.tar.gz

Binary file not shown.

View File

@ -0,0 +1,133 @@
# example_package.py - Creates an example package
#
# Copyright (C) 2010-2011, Stefano Rivera <stefanor@ubuntu.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
import os.path
import shutil
import subprocess
import tempfile
import debian.debian_support
_base_pkg = {
'content': 'Boring file from upstream',
'debian/control':
"""Source: example
Section: misc
Priority: extra
Maintainer: Example <example@example.net>
Build-Depends: debhelper (>= 7.0.50~)
Standards-Version: 3.9.1
Package: example
Architecture: all
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: Example package for testing purposes
An example package used by the test suite. Useless.
""",
'debian/copyright':
"""Format: http://svn.debian.org/wsvn/dep/web/deps/dep5.mdwn?op=file&rev=152
Source: https://launchpad.net/ubuntu-dev-tools
Files: *
Copyright: 2010-2011, Stefano Rivera <stefanor@ubuntu.com>
License: ISC
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
""",
'debian/compat': '7',
'debian/rules':
"""#!/usr/bin/make -f
%:
\tdh $@
""",
'debian/source/format': '3.0 (quilt)',
'debian/source/local-options': 'abort-on-upstream-changes',
}
class ExamplePackage(object):
def __init__(self, source='example', version='1.0-1', workdir=None,
files=None):
self.source = source
self.version = debian.debian_support.Version(version)
self.pkg = dict(_base_pkg)
if files is not None:
self.pkg.update(files)
self.workdir = workdir or tempfile.mkdtemp(prefix='examplepkg')
self.srcdir = os.path.join(self.workdir, '%s-%s' % (source,
self.version.upstream_version))
def create_orig(self):
"Create .orig.tar.gz"
self._write_files(filter_=lambda fn: not fn.startswith('debian/'))
orig = '%s_%s.orig.tar.gz' % (self.source,
self.version.upstream_version)
subprocess.check_call(('tar', '-czf', orig,
os.path.basename(self.srcdir)),
cwd=self.workdir)
def changelog_entry(self, version=None, create=False):
"Add a changelog entry"
cmd = ['dch', '--noconf', '--package', self.source]
if create:
cmd.append('--create')
cmd += ['--newversion', version or self.version.full_version]
cmd.append('')
env = dict(os.environ)
env['DEBFULLNAME'] = 'Example'
env['DEBEMAIL'] = 'example@example.net'
subprocess.check_call(cmd, env=env, cwd=self.srcdir)
def create(self):
"Build source package"
self._write_files()
self.changelog_entry(create=True)
subprocess.check_call(('dpkg-buildpackage', '-rfakeroot', '-S',
'-uc', '-us'),
cwd=self.srcdir)
def cleanup(self):
"Remove workdir"
shutil.rmtree(self.workdir)
def pathname(self, fn):
"Return path to file in workdir"
return os.path.join(self.workdir, fn)
def _write_files(self, filter_=None):
"Write files from self.pkg into src pkg dir, if filter_(fn)"
if filter_ is None:
filter_ = lambda x: True
for fn, content in self.pkg.iteritems():
if not filter_(fn):
continue
pathname = os.path.join(self.srcdir, fn)
dirname = os.path.dirname(pathname)
if not os.path.exists(dirname):
os.makedirs(dirname)
with open(pathname, 'wb') as f:
f.write(content)

View File

@ -30,28 +30,37 @@ import ubuntutools.archive
from ubuntutools.config import UDTConfig
from ubuntutools.logger import Logger
from ubuntutools.test import unittest
from ubuntutools.test.example_package import ExamplePackage
ex_pkg = ExamplePackage()
def setUpModule():
ex_pkg.create_orig()
ex_pkg.create()
def tearDownModule():
ex_pkg.cleanup()
class DscVerificationTestCase(mox.MoxTestBase, unittest.TestCase):
def setUp(self):
super(DscVerificationTestCase, self).setUp()
with open('test-data/example_1.0-1.dsc', 'rb') as f:
with open(ex_pkg.pathname('example_1.0-1.dsc'), 'rb') as f:
self.dsc = ubuntutools.archive.Dsc(f.read())
def tearDown(self):
super(DscVerificationTestCase, self).tearDown()
def test_good(self):
self.assertTrue(self.dsc.verify_file(
'test-data/example_1.0.orig.tar.gz'))
self.assertTrue(self.dsc.verify_file(
'test-data/example_1.0-1.debian.tar.gz'))
self.assertTrue(self.dsc.verify_file(ex_pkg.pathname(
'example_1.0.orig.tar.gz')))
self.assertTrue(self.dsc.verify_file(ex_pkg.pathname(
'example_1.0-1.debian.tar.gz')))
def test_missing(self):
self.assertFalse(self.dsc.verify_file(
'test-data/does.not.exist'))
self.assertFalse(self.dsc.verify_file(ex_pkg.pathname(
'does.not.exist')))
def test_bad(self):
fn = 'test-data/example_1.0.orig.tar.gz'
fn = ex_pkg.pathname('example_1.0.orig.tar.gz')
with open(fn, 'rb') as f:
data = f.read()
data = data[:-1] + chr(ord(data[-1]) ^ 8)
@ -96,8 +105,7 @@ class LocalSourcePackageTestCase(mox.MoxTestBase, unittest.TestCase):
if destname is None:
destname = os.path.basename(url)
return self.urlopen('file://'
+ os.path.join(os.path.abspath('test-data'),
destname))
+ os.path.abspath(ex_pkg.pathname(destname)))
def urlopen_file(self, filename):
"Wrapper for urlopen_proxy for named files"
@ -121,15 +129,15 @@ class LocalSourcePackageTestCase(mox.MoxTestBase, unittest.TestCase):
self.mox.ReplayAll()
pkg = self.SourcePackage('example', '1.0-1', 'main',
dscfile='test-data/example_1.0-1.dsc',
dscfile=ex_pkg.pathname('example_1.0-1.dsc'),
workdir=self.workdir)
pkg.pull()
pkg.unpack()
def test_verification(self):
shutil.copy2('test-data/example_1.0-1.dsc', self.workdir)
shutil.copy2('test-data/example_1.0.orig.tar.gz', self.workdir)
shutil.copy2('test-data/example_1.0-1.debian.tar.gz', self.workdir)
for fn in ('example_1.0-1.dsc', 'example_1.0.orig.tar.gz',
'example_1.0-1.debian.tar.gz'):
shutil.copy2(ex_pkg.pathname(fn), self.workdir)
with open(os.path.join(self.workdir, 'example_1.0-1.debian.tar.gz'),
'r+b') as f:
f.write('CORRUPTION')
@ -140,7 +148,7 @@ class LocalSourcePackageTestCase(mox.MoxTestBase, unittest.TestCase):
self.mox.ReplayAll()
pkg = self.SourcePackage('example', '1.0-1', 'main',
dscfile='test-data/example_1.0-1.dsc',
dscfile=ex_pkg.pathname('example_1.0-1.dsc'),
workdir=self.workdir)
pkg.pull()