[Python-checkins] distutils2: Now deleting build-scripts directory before creating it. No more leftovers.

eric.araujo python-checkins at python.org
Wed May 16 07:07:27 CEST 2012


http://hg.python.org/distutils2/rev/463d3014ee4a
changeset:   1337:463d3014ee4a
parent:      1330:c74e6b9f1b5e
user:        Pierre Paul <info at pierre-paul.com>
date:        Mon May 14 08:36:06 2012 -0400
summary:
  Now deleting build-scripts directory before creating it. No more leftovers.

files:
  distutils2/command/build_scripts.py            |   1 +
  distutils2/command/cmd.py                      |  16 ++++-
  distutils2/tests/test_command_build_scripts.py |  35 ++++++++++
  3 files changed, 51 insertions(+), 1 deletions(-)


diff --git a/distutils2/command/build_scripts.py b/distutils2/command/build_scripts.py
--- a/distutils2/command/build_scripts.py
+++ b/distutils2/command/build_scripts.py
@@ -56,6 +56,7 @@
         ie. starts with "\#!" and contains "python"), then adjust the first
         line to refer to the current Python interpreter as we copy.
         """
+        self.rmpath(self.build_dir)
         self.mkpath(self.build_dir)
         outfiles = []
         for script in self.scripts:
diff --git a/distutils2/command/cmd.py b/distutils2/command/cmd.py
--- a/distutils2/command/cmd.py
+++ b/distutils2/command/cmd.py
@@ -5,7 +5,7 @@
 from distutils2 import util
 from distutils2 import logger
 from distutils2.errors import PackagingOptionError
-from distutils2._backport.shutil import copyfile, move, make_archive
+from distutils2._backport.shutil import copyfile, move, make_archive, rmtree
 
 
 class Command(object):
@@ -365,6 +365,20 @@
             return
         os.makedirs(name, mode)
 
+    def rmpath(self, name, dry_run=None):
+        if dry_run is None:
+            dry_run = self.dry_run
+        name = os.path.normpath(name)
+        if not os.path.isdir(name) or name == '':
+            return
+        if dry_run:
+            head = ''
+            for part in name.split(os.sep):
+                logger.info("removing directory %s%s", head, part)
+                head += part + os.sep
+            return
+        rmtree(name)
+
     def copy_file(self, infile, outfile,
                   preserve_mode=True, preserve_times=True, link=None, level=1):
         """Copy a file respecting dry-run and force flags.
diff --git a/distutils2/tests/test_command_build_scripts.py b/distutils2/tests/test_command_build_scripts.py
--- a/distutils2/tests/test_command_build_scripts.py
+++ b/distutils2/tests/test_command_build_scripts.py
@@ -123,6 +123,41 @@
         cmd.run()
 
         self.assertEqual(open(built).readline(), '#!pythonx\n')
+
+    def test_build_old_scripts_deleted(self):
+        source = self.mkdtemp()
+
+        expected = []
+        expected.append("script1.py")
+        self.write_script(source, "script1.py",
+                          ("#! /usr/bin/env python2.3\n"
+                           "# bogus script w/ Python sh-bang\n"
+                           "pass\n"))
+        expected.append("script2.py")
+        self.write_script(source, "script2.py",
+                          ("#!/usr/bin/python\n"
+                           "# bogus script w/ Python sh-bang\n"
+                           "pass\n"))
+
+        target = self.mkdtemp()
+        cmd = self.get_build_scripts_cmd(target,
+                                         [os.path.join(source, fn)
+                                          for fn in expected])
+        cmd.finalize_options()
+        cmd.run()
+
+        built = os.listdir(target)
+        for name in expected:
+            self.assertIn(name, built)
+
+        cmd = self.get_build_scripts_cmd(target, 
+                                        [os.path.join(source, 'script1.py')])
+        cmd.finalize_options()
+        cmd.run()
+
+        built = os.listdir(target)
+        self.assertIn('script1.py', built)
+        self.assertNotIn('script2.py', built)
         
 def test_suite():
     return unittest.makeSuite(BuildScriptsTestCase)

-- 
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list