[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[dak/master 1/3] Add new class UnpackedSource and a new test.



Signed-off-by: Torsten Werner <twerner@debian.org>
---
 daklib/contents.py                                 |   65 +++++++++++++++++++-
 tests/dbtest_contents.py                           |   20 ++++++-
 .../pool/main/h/hello/hello_2.2-1.debian.tar.gz    |  Bin 0 -> 462 bytes
 .../fixtures/ftp/pool/main/h/hello/hello_2.2-1.dsc |   13 ++++
 .../ftp/pool/main/h/hello/hello_2.2.orig.tar.gz    |  Bin 0 -> 147 bytes
 5 files changed, 95 insertions(+), 3 deletions(-)
 create mode 100644 tests/fixtures/ftp/pool/main/h/hello/hello_2.2-1.debian.tar.gz
 create mode 100644 tests/fixtures/ftp/pool/main/h/hello/hello_2.2-1.dsc
 create mode 100644 tests/fixtures/ftp/pool/main/h/hello/hello_2.2.orig.tar.gz

diff --git a/daklib/contents.py b/daklib/contents.py
index 4a0b3ae..a158e8f 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 158ec89..90fe496 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 0000000000000000000000000000000000000000..c185f1bb3473c1b973670eac778fe983e1a35691
GIT binary patch
literal 462
zcmb2|=3oE;CgwNOeZ3Dm2(%TSTQBk9(kkEY%JLG~niZcWOm2F<Me_22GI4XxgZ1y#
zTw9y=s&dU`KcqK%&bOQ8WkC<No%(u7TKJjAq6`Vy<&O=X+i<n^q|Es8n$y39eWE#I
z?%gM12X+T^Ev~<<wdQ|w{mG>MLba4PjLQFmf3Ek<F5mH|`hVd5bCDA5Q~tmFyI->U
z#B9Sq#}j|-u9utl<9T8Hf6f1om6_Q#f8Q2#_RjYX@3a5&=D!x2s(F0X|L33dRsNr|
zFK@m7bbf2kCwtcFkW&YISdtDtzmT<EI=c1yg<C>j-#vDmdG0&ox`&?OzEia~tT0$_
z*l=^Pk?A$fn7g%x<@5E#J|yMbUC<W3MM`(gth^aHed)Re+hUrlBkwd#n6UG%_&JM$
ziJvs43gzATdwzw4@9g@;(%Dxk{@%a&+rF><-~Wh9-|GV>1^iq7pfcl6ki@Ho`A?_j
z-Dx&!J;OFZaSh*&m1h}u+eYzNC5LWYRv^UUC;&n8rC1k(LjM1MDONJ+D-eCm3@5f6
KpU$AczyJW$1KJe;

literal 0
HcmV?d00001

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 0000000..f564ce4
--- /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 <me@somewhere.world>
+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 0000000000000000000000000000000000000000..f3fbc1831b4df9e60110b61446241d23595af2b6
GIT binary patch
literal 147
zcmb2|=3rQMt<#r@`RzqRu0sw2ZVxR_#W~!adH3YCCe7SEjjjr=0TIe^@xdBqjyg&l
z@3a3UEvv3I=XpMLZLV^6C;!pfC|$Yx-#tTYqd%oYsjNy3=G3z<*xPels;OuDx_P^-
uet&=YZ~3qA|IWMbzP-Klm$_yAMf=EL6_;Pk3`pR>8f)q4QY;J_3=9C4vpo|4

literal 0
HcmV?d00001

-- 
1.7.2.5



Reply to: