From: Torsten Werner Date: Wed, 23 Mar 2011 12:40:25 +0000 (+0100) Subject: Add new class UnpackedSource and a new test. X-Git-Url: https://git.decadent.org.uk/gitweb/?a=commitdiff_plain;h=93e9bcb9ac5817c7ed6ff5603e43aa75ff0e3578;p=dak.git Add new class UnpackedSource and a new test. Signed-off-by: Torsten Werner --- diff --git a/daklib/contents.py b/daklib/contents.py index 4a0b3ae2..a158e8fc 100755 --- a/daklib/contents.py +++ b/daklib/contents.py @@ -29,7 +29,9 @@ from daklib.dbconn import * from daklib.config import Config from multiprocessing import Pool -from subprocess import Popen, PIPE +from shutil import rmtree +from subprocess import Popen, PIPE, check_call +from tempfile import mkdtemp import os.path @@ -313,3 +315,64 @@ def scan_helper(binary_id): ''' scanner = ContentsScanner(binary_id) scanner.scan() + + +class UnpackedSource(object): + ''' + UnpackedSource extracts a source package into a temporary location and + gives you some convinient function for accessing it. + ''' + def __init__(self, dscfilename): + ''' + The dscfilename is a name of a DSC file that will be extracted. + ''' + self.root_directory = os.path.join(mkdtemp(), 'root') + command = ('dpkg-source', '--no-copy', '--no-check', '-x', dscfilename, + self.root_directory) + # dpkg-source does not have a --quiet option + devnull = open(os.devnull, 'w') + check_call(command, stdout = devnull, stderr = devnull) + devnull.close() + + def get_root_directory(self): + ''' + Returns the name of the package's root directory which is the directory + where the debian subdirectory is located. + ''' + return self.root_directory + + def get_changelog_file(self): + ''' + Returns a file object for debian/changelog or None if no such file exists. + ''' + changelog_name = os.path.join(self.root_directory, 'debian', 'changelog') + try: + return open(changelog_name) + except IOError: + return None + + def get_all_filenames(self): + ''' + Returns an iterator over all filenames. The filenames will be relative + to the root directory. + ''' + skip = len(self.root_directory) + 1 + for root, _, files in os.walk(self.root_directory): + for name in files: + yield os.path.join(root[skip:], name) + + def cleanup(self): + ''' + Removes all temporary files. + ''' + if self.root_directory is None: + return + parent_directory = os.path.dirname(self.root_directory) + rmtree(parent_directory) + self.root_directory = None + + def __del__(self): + ''' + Enforce cleanup. + ''' + self.cleanup() diff --git a/tests/dbtest_contents.py b/tests/dbtest_contents.py index 158ec892..90fe4966 100755 --- a/tests/dbtest_contents.py +++ b/tests/dbtest_contents.py @@ -1,12 +1,13 @@ #!/usr/bin/env python -from db_test import DBDakTestCase +from db_test import DBDakTestCase, fixture from daklib.dbconn import * -from daklib.contents import ContentsWriter, ContentsScanner +from daklib.contents import ContentsWriter, ContentsScanner, UnpackedSource from os.path import normpath from sqlalchemy.exc import FlushError, IntegrityError +from subprocess import CalledProcessError import unittest class ContentsTestCase(DBDakTestCase): @@ -172,6 +173,21 @@ class ContentsTestCase(DBDakTestCase): self.assertEqual('usr/bin/hello', bin_contents_list[0].file) self.assertEqual('usr/share/doc/hello/copyright', bin_contents_list[1].file) + def test_unpack(self): + ''' + Tests the UnpackedSource class. + ''' + self.setup_poolfiles() + dscfilename = fixture('ftp/pool/' + self.file['hello_2.2-1.dsc'].filename) + unpacked = UnpackedSource(dscfilename) + self.assertTrue(len(unpacked.get_root_directory()) > 0) + self.assertEqual('hello (2.2-1) unstable; urgency=low\n', + unpacked.get_changelog_file().readline()) + all_filenames = set(unpacked.get_all_filenames()) + self.assertEqual(8, len(all_filenames)) + self.assertTrue('debian/rules' in all_filenames) + self.assertRaises(CalledProcessError, lambda: UnpackedSource('invalidname')) + def classes_to_clean(self): return [Override, Suite, BinContents, DBBinary, DBSource, Architecture, Section, \ OverrideType, Maintainer, Component, Priority, PoolFile] diff --git a/tests/fixtures/ftp/pool/main/h/hello/hello_2.2-1.debian.tar.gz b/tests/fixtures/ftp/pool/main/h/hello/hello_2.2-1.debian.tar.gz new file mode 100644 index 00000000..c185f1bb Binary files /dev/null and b/tests/fixtures/ftp/pool/main/h/hello/hello_2.2-1.debian.tar.gz differ diff --git a/tests/fixtures/ftp/pool/main/h/hello/hello_2.2-1.dsc b/tests/fixtures/ftp/pool/main/h/hello/hello_2.2-1.dsc new file mode 100644 index 00000000..f564ce44 --- /dev/null +++ b/tests/fixtures/ftp/pool/main/h/hello/hello_2.2-1.dsc @@ -0,0 +1,13 @@ +Format: 3.0 (quilt) +Source: hello +Version: 2.2-1 +Maintainer: Mr. Me +Checksums-Sha1: + 9613ac479ddb6bca7f3ec5436b27ab983733b963 147 hello_2.2.orig.tar.gz + 97cfabb792685ac19c1ddc03f7d4aa1022f626e1 462 hello_2.2-1.debian.tar.gz +Checksums-Sha256: + b041547e956f091a46030f133b6e47af15bc836771540118fec98d0913602ce0 147 hello_2.2.orig.tar.gz + da28e21cbae014b915abc8afc4be1c0b8e5148b78802dce815d5342e80cd52e7 462 hello_2.2-1.debian.tar.gz +Files: + cc4b081e2697fca88c87986b1cad905f 147 hello_2.2.orig.tar.gz + d7bdb277cbdbaad4ab700c6d5cee9b54 462 hello_2.2-1.debian.tar.gz diff --git a/tests/fixtures/ftp/pool/main/h/hello/hello_2.2.orig.tar.gz b/tests/fixtures/ftp/pool/main/h/hello/hello_2.2.orig.tar.gz new file mode 100644 index 00000000..f3fbc183 Binary files /dev/null and b/tests/fixtures/ftp/pool/main/h/hello/hello_2.2.orig.tar.gz differ