Ticket #5001: tap2deb-5001.patch

File tap2deb-5001.patch, 6.4 KB (added by Thijs Triemstra, 7 years ago)
  • twisted/scripts/tap2deb.py

     
     1# -*- test-case-name: twisted.scripts.test.test_tap2deb -*-
    12# Copyright (c) Twisted Matrix Laboratories.
    23# See LICENSE for details.
    34
    4 
    5 
    6 import sys, os, string, shutil
     5import os
     6import sys
     7import shutil
     8import subprocess
    79
    810from twisted.python import usage
    911
     12
    1013class MyOptions(usage.Options):
    1114    optFlags = [["unsigned", "u"]]
    1215    optParameters = [["tapfile", "t", "twistd.tap"],
     
    2730
    2831    def postOptions(self):
    2932        if not self["maintainer"]:
    30             raise usage.UsageError, "maintainer must be specified."
     33            raise usage.UsageError("maintainer must be specified.")
    3134
    3235
    3336type_dict = {
    34 'tap': 'file',
    35 'python': 'python',
    36 'source': 'source',
    37 'xml': 'xml',
     37    'tap': 'file',
     38    'python': 'python',
     39    'source': 'source',
     40    'xml': 'xml',
    3841}
    3942
    40 def save_to_file(file, text):
    41     f = open(file, 'w')
    42     f.write(text)
    43     f.close()
    4443
    4544
    46 def run():
     45def save_to_file(file, text):
     46    with open(file, 'w') as f:
     47        f.write(text)
     48
    4749
     50def run(options=None):
    4851    try:
    4952        config = MyOptions()
    50         config.parseOptions()
    51     except usage.error, ue:
     53        config.parseOptions(options)
     54    except usage.error as ue:
    5255        sys.exit("%s: %s" % (sys.argv[0], ue))
    5356
    5457    tap_file = config['tapfile']
    5558    base_tap_file = os.path.basename(config['tapfile'])
    5659    protocol = (config['protocol'] or os.path.splitext(base_tap_file)[0])
    57     deb_file = config['debfile'] or 'twisted-'+protocol
     60    deb_file = config['debfile'] or 'twisted-' + protocol
    5861    version = config['set-version']
    5962    maintainer = config['maintainer']
    6063    description = config['description'] or ('A Twisted-based server for %(protocol)s' %
    6164                                            vars())
    6265    long_description = config['long_description'] or 'Automatically created by tap2deb'
    6366    twistd_option = type_dict[config['type']]
    64     date = string.strip(os.popen('822-date').read())
     67    date = os.popen('date -R').read().strip()
    6568    directory = deb_file + '-' + version
    6669    python_version = '%s.%s' % sys.version_info[:2]
    6770
    6871    if os.path.exists(os.path.join('.build', directory)):
    6972        os.system('rm -rf %s' % os.path.join('.build', directory))
    70     os.makedirs(os.path.join('.build', directory, 'debian'))
     73    os.makedirs(os.path.join('.build', directory, 'debian', 'source'))
    7174
    7275    shutil.copy(tap_file, os.path.join('.build', directory))
    7376
     
    192195 %(long_description)s
    193196''' % vars())
    194197
     198    save_to_file(os.path.join('.build', directory, 'debian', 'compat'),
     199    '''\
     2007
     201''' % vars())
     202
    195203    save_to_file(os.path.join('.build', directory, 'debian', 'copyright'),
    196204    '''\
    197205This package was auto-debianized by %(maintainer)s on
     
    216224usr/share/%(deb_file)s
    217225''' % vars())
    218226
     227    save_to_file(os.path.join('.build', directory, 'debian', 'source',
     228                 'format'),
     229    '''\
     2303.0 (native)
     231''' % vars())
     232
    219233    save_to_file(os.path.join('.build', directory, 'debian', 'rules'),
    220234    '''\
    221235#!/usr/bin/make -f
     
    274288    os.chmod(os.path.join('.build', directory, 'debian', 'rules'), 0755)
    275289
    276290    os.chdir('.build/%(directory)s' % vars())
    277     os.system('dpkg-buildpackage -rfakeroot'+ ['', ' -uc -us'][config['unsigned']])
    278291
    279 if __name__ == '__main__':
    280     run()
     292    args = ["dpkg-buildpackage", "-rfakeroot"]
     293    if config['unsigned']:
     294        args = args + ['-uc', '-us']
     295
     296    #  build deb
     297    job = subprocess.Popen(args, stdout=subprocess.PIPE,
     298                           stderr=subprocess.STDOUT)
     299    stdout, _ = job.communicate()
    281300
  • twisted/scripts/test/test_tap2deb.py

     
     1# Copyright (c) Twisted Matrix Laboratories.
     2# See LICENSE for details.
     3
     4"""
     5Tests for L{twisted.scripts.tap2deb}.
     6"""
     7
     8import os
     9
     10from twisted.scripts import tap2deb
     11from twisted.python import usage
     12from twisted.python import procutils
     13from twisted.python.filepath import FilePath
     14
     15from twisted.trial.unittest import TestCase, SkipTest
     16
     17
     18
     19class TestTap2DEB(TestCase):
     20
     21    def setUp(self):
     22        self._checkForDebBuild()
     23
     24        self.maintainer = "Jane Doe <janedoe@example.com>"
     25        self.basedir = FilePath(self.mktemp())
     26        self.basedir.makedirs()
     27        self.addCleanup(os.chdir, os.getcwd())
     28        os.chdir(self.basedir.path)
     29
     30
     31    def _checkForDebBuild(self):
     32        """
     33        tap2deb requires dpkg-buildpackage; skip tests if dpkg-buildpackage
     34        is not present.
     35        """
     36        if not procutils.which("dpkg-buildpackage"):
     37            raise SkipTest("dpkg-buildpackage must be present to test tap2deb")
     38
     39
     40    def test_maintainerOption(self):
     41        """
     42        The C{--maintainer} option must be specified on the commandline or
     43        passed to L{tap2deb.run}.
     44        """
     45        config = tap2deb.MyOptions()
     46        self.assertRaises(usage.UsageError, config.parseOptions, [])
     47        self.assertRaises(SystemExit, tap2deb.run, [])
     48
     49
     50    def test_optionDefaults(self):
     51        """
     52        Commandline options should default to sensible values.
     53        """
     54        config = tap2deb.MyOptions()
     55        config.parseOptions(['--maintainer', self.maintainer])
     56
     57        self.assertEqual(config['tapfile'], 'twistd.tap')
     58        self.assertEqual(config['maintainer'], self.maintainer)
     59        self.assertEqual(config['protocol'], '')
     60        self.assertEqual(config['description'], '')
     61        self.assertEqual(config['long_description'], '')
     62        self.assertEqual(config['set-version'], '1.0')
     63        self.assertEqual(config['debfile'], None)
     64        self.assertEqual(config['type'], 'tap')
     65
     66
     67    def test_basicOperation(self):
     68        """
     69        Calling tap2deb should produce a DEB and DSC file.
     70        """
     71        # make a temporary .tap file
     72        tap = self.basedir.child("lemon.tap")
     73        tap.setContent("# Dummy .tap file")
     74
     75        # run
     76        args = ["--tapfile", tap.path, "--maintainer", self.maintainer]
     77        tap2deb.run(args)
     78
     79        build = tap.child('.build')
     80        for name in ['twisted-twistd_1.0_all.deb',
     81                     'twisted-twistd_1.0_all.dsc']:
     82            self.assertTrue(build.child(name).exists)
     83