Ticket #4244: setup-4244.patch

File setup-4244.patch, 6.3 KB (added by thijs, 4 years ago)
  • twisted/python/test/test_dist.py

     
    77
    88
    99import os
    10 import shutil
    1110import sys
    1211
    1312from distutils.core import Distribution
     
    1514from twisted.trial.unittest import TestCase
    1615
    1716from twisted.python import dist
    18 from twisted.python.dist import get_setup_args, ConditionalExtension
     17from twisted.python.dist import (get_setup_args, ConditionalExtension,
     18    build_scripts_twisted)
    1919from twisted.python.filepath import FilePath
    2020
    2121
     22
    2223class SetupTest(TestCase):
    2324    """
    2425    Tests for L{get_setup_args}.
     
    5758        self.assertEqual(ext.define_macros, [("whatever", 2), ("WIN32", 1)])
    5859
    5960
     61
    6062class GetExtensionsTest(TestCase):
    6163    """
    6264    Tests for L{dist.getExtensions}.
     
    195197        self.assertEqual(dist.getVersion("blat", base=self.dirname), "9.8.10")
    196198
    197199
     200
    198201class GetScriptsTest(TestCase):
    199202    """
    200203    Tests for L{dist.getScripts} which returns the scripts which should be
     
    295298
    296299
    297300
     301class DummyCommand:
     302    """
     303    A fake Command.
     304    """
     305    def __init__(self, **kwargs):
     306        for kw, val in kwargs.items():
     307            setattr(self, kw, val)
     308
     309    def ensure_finalized(self):
     310        pass
     311
     312
     313
     314class BuildScriptsTest(TestCase):
     315    """
     316    Tests for L{dist.build_scripts_twisted}.
     317    """
     318
     319    def setUp(self):
     320        self.source = FilePath(self.mktemp())
     321        self.target = FilePath(self.mktemp())
     322        self.source.makedirs()
     323        self.addCleanup(os.chdir, os.getcwd())
     324        os.chdir(self.source.path)
     325
     326
     327    def test_notWindows(self):
     328        """
     329        L{build_scripts_twisted} does not rename scripts on non-Windows
     330        platforms.
     331        """
     332        self.patch(os, "name", "twisted")
     333        built = self.buildScripts()
     334        for name in ['script1', 'script2.py', 'shell.sh']:
     335            self.assertTrue(name in built)
     336
     337
     338    def test_windows(self):
     339        """
     340        L{build_scripts_twisted} renames scripts so they end with '.py' on
     341        the Windows platform.
     342        """
     343        self.patch(os, "name", "nt")
     344        built = self.buildScripts()
     345        for name in ['script1.py', 'script2.py', 'shell.sh.py']:
     346            self.assertTrue(name in built)
     347
     348
     349    def buildScripts(self):
     350        """
     351        Writes 3 types of scripts and runs the L{build_scripts_twisted}
     352        command.
     353        """
     354        self.writeScript(self.source, "script1",
     355                          ("#! /usr/bin/env python2.3\n"
     356                           "# bogus script w/ Python sh-bang\n"
     357                           "pass\n"))
     358
     359        self.writeScript(self.source, "script2.py",
     360                        ("#!/usr/bin/python\n"
     361                         "# bogus script w/ Python sh-bang\n"
     362                         "pass\n"))
     363
     364        self.writeScript(self.source, "shell.sh",
     365                        ("#!/bin/sh\n"
     366                         "# bogus shell script w/ sh-bang\n"
     367                         "exit 0\n"))
     368
     369        expected = ['script1', 'script2.py', 'shell.sh']
     370        cmd = self.getBuildScriptsCmd(self.target,
     371                                     [self.source.child(fn).path
     372                                      for fn in expected])
     373        cmd.finalize_options()
     374        cmd.run()
     375
     376        return self.target.listdir()
     377
     378
     379    def getBuildScriptsCmd(self, target, scripts):
     380        """
     381        Create a distribution with a dummy command and wrap it in
     382        L{build_scripts_twisted}.
     383        """
     384        dist = Distribution()
     385        dist.scripts = scripts
     386        dist.command_obj["build"] = DummyCommand(
     387            build_scripts = target.path,
     388            force = 1,
     389            executable = sys.executable
     390        )
     391        return build_scripts_twisted(dist)
     392
     393
     394    def writeScript(self, dir, name, text):
     395        """
     396        Write the script to disk.
     397        """
     398        f = open(dir.child(name).path, "w")
     399        try:
     400            f.write(text)
     401        finally:
     402            f.close()
     403
     404
     405
    298406class FakeModule(object):
    299407    """
    300408    A fake module, suitable for dependency injection in testing.
     
    308416        """
    309417        self._attrs = attrs
    310418
     419
    311420    def __getattr__(self, name):
    312421        """
    313422        Gets an attribute of this fake module from its attrs.
  • twisted/python/dist.py

     
     1# -*- test-case-name: twisted.python.test.test_dist -*-
     2# Copyright (c) Twisted Matrix Laboratories.
     3# See LICENSE for details.
     4
    15"""
    26Distutils convenience functionality.
    37
     
    282286## Helpers and distutil tweaks
    283287
    284288class build_scripts_twisted(build_scripts.build_scripts):
    285     """Renames scripts so they end with '.py' on Windows."""
    286 
     289    """
     290    Renames scripts so they end with '.py' on Windows.
     291    """
    287292    def run(self):
    288293        build_scripts.build_scripts.run(self)
    289294        if not os.name == "nt":
    290295            return
    291296        for f in os.listdir(self.build_dir):
    292             fpath=os.path.join(self.build_dir, f)
     297            fpath = os.path.join(self.build_dir, f)
    293298            if not fpath.endswith(".py"):
    294299                try:
    295300                    os.unlink(fpath + ".py")
    296                 except EnvironmentError, e:
    297                     if e.args[1]=='No such file or directory':
     301                except EnvironmentError as e:
     302                    if e.args[1] == 'No such file or directory':
    298303                        pass
    299304                os.rename(fpath, fpath + ".py")
    300305
  • setup.py

     
    6767        try:
    6868            list(parse_requirements(requirements))
    6969        except:
    70             print """You seem to be running a very old version of setuptools.
     70            print("""You seem to be running a very old version of setuptools.
    7171This version of setuptools has a bug parsing dependencies, so automatic
    7272dependency resolution is disabled.
    73 """
     73""")
    7474        else:
    7575            setup_args['install_requires'] = requirements
    7676        setup_args['include_package_data'] = True