[Python-checkins] distutils2: merged doc from Yannick

tarek.ziade python-checkins at python.org
Sun Jan 30 10:45:09 CET 2011


tarek.ziade pushed 50d8e9211704 to distutils2:

http://hg.python.org/distutils2/rev/50d8e9211704
changeset:   963:50d8e9211704
tag:         tip
parent:      957:8106777a8748
parent:      962:18cd862b069f
user:        Tarek Ziade <tarek at ziade.org>
date:        Sun Jan 30 10:44:37 2011 +0100
summary:
  merged doc from Yannick

files:
  

diff --git a/distutils2/install.py b/distutils2/install.py
--- a/distutils2/install.py
+++ b/distutils2/install.py
@@ -35,7 +35,7 @@
     """Raised when a conflict is detected"""
 
 
-def move_files(files, destination=None):
+def move_files(files, destination):
     """Move the list of files in the destination folder, keeping the same
     structure.
 
@@ -43,13 +43,11 @@
 
     :param files: a list of files to move.
     :param destination: the destination directory to put on the files.
-                        if not defined, create a new one, using mkdtemp
     """
-    if not destination:
-        destination = mkdtemp()
-
     for old in files:
-        new = '%s%s' % (destination, old)
+        # not using os.path.join() because basename() might not be
+        # unique in destination
+        new = "%s%s" % (destination, old)
 
         # try to make the paths.
         try:
@@ -119,7 +117,7 @@
         os.chdir(old_dir)
 
 
-def install_dists(dists, path=None):
+def install_dists(dists, path):
     """Install all distributions provided in dists, with the given prefix.
 
     If an error occurs while installing one of the distributions, uninstall all
@@ -130,8 +128,6 @@
     :param dists: distributions to install
     :param path: base path to install distribution in
     """
-    if not path:
-        path = mkdtemp()
 
     installed_dists, installed_files = [], []
     for d in dists:
@@ -146,10 +142,11 @@
             for d in installed_dists:
                 uninstall(d)
             raise e
+        
     return installed_files
 
 
-def install_from_infos(install=[], remove=[], conflicts=[], install_path=None):
+def install_from_infos(install_path=None, install=[], remove=[], conflicts=[]):
     """Install and remove the given distributions.
 
     The function signature is made to be compatible with the one of get_infos.
@@ -168,35 +165,42 @@
         4. Else, move the distributions to the right locations, and remove for
            real the distributions thats need to be removed.
 
-    :param install: list of distributions that will be installed.
+    :param install_path: the installation path where we want to install the
+                         distributions.
+    :param install: list of distributions that will be installed; install_path
+                    must be provided if this list is not empty.
     :param remove: list of distributions that will be removed.
     :param conflicts: list of conflicting distributions, eg. that will be in
                       conflict once the install and remove distribution will be
                       processed.
-    :param install_path: the installation path where we want to install the
-                         distributions.
     """
     # first of all, if we have conflicts, stop here.
     if conflicts:
         raise InstallationConflict(conflicts)
 
+    if install and not install_path:
+        raise ValueError("Distributions are to be installed but `install_path`"
+                         " is not provided.")
+
     # before removing the files, we will start by moving them away
     # then, if any error occurs, we could replace them in the good place.
     temp_files = {}  # contains lists of {dist: (old, new)} paths
+    temp_dir = None
     if remove:
+        temp_dir = tempfile.mkdtemp()
         for dist in remove:
             files = dist.get_installed_files()
-            temp_files[dist] = move_files(files)
+            temp_files[dist] = move_files(files, temp_dir)
     try:
         if install:
-            installed_files = install_dists(install, install_path)  # install to tmp first
-
+            installed_files = install_dists(install, install_path)
     except:
-        # if an error occurs, put back the files in the good place.
+        # if an error occurs, put back the files in the right place.
         for files in temp_files.values():
             for old, new in files:
                 shutil.move(new, old)
-
+        if temp_dir:
+            shutil.rmtree(temp_dir)
         # now re-raising
         raise
 
@@ -204,6 +208,8 @@
     for files in temp_files.values():
         for old, new in files:
             os.remove(new)
+    if temp_dir:
+        shutil.rmtree(temp_dir)
 
 
 def _get_setuptools_deps(release):
@@ -317,13 +323,13 @@
 
 def remove(project_name, paths=sys.path):
     """Removes a single project from the installation"""
-    tmp = tempfile.mkdtemp(prefix=project_name+'-uninstall')
     dist = get_distribution(project_name, paths=paths)
     if dist is None:
         raise DistutilsError('Distribution %s not found' % project_name)
     files = dist.get_installed_files(local=True)
     rmdirs = []
     rmfiles = []
+    tmp = tempfile.mkdtemp(prefix=project_name+'-uninstall')
 
     try:
         for file, md5, size in files:
@@ -339,8 +345,8 @@
                     rmfiles.append(file)
                 if dirname not in rmdirs:
                     rmdirs.append(dirname)
-    except OSError:
-        os.rmdir(tmp)
+    finally:
+        shutil.rmtree(tmp)
 
     for file in rmfiles:
         os.remove(file)
@@ -373,8 +379,8 @@
 
     install_path = get_config_var('base')
     try:
-        install_from_infos(info['install'], info['remove'], info['conflict'],
-                           install_path=install_path)
+        install_from_infos(install_path, 
+                           info['install'], info['remove'], info['conflict'])
 
     except InstallationConflict, e:
         projects = ['%s %s' % (p.name, p.version) for p in e.args[0]]
diff --git a/distutils2/tests/test_install.py b/distutils2/tests/test_install.py
--- a/distutils2/tests/test_install.py
+++ b/distutils2/tests/test_install.py
@@ -39,6 +39,11 @@
             for f in range(0,3):
                self._real_files.append(mkstemp())
 
+    def _unlink_installed_files(self):
+        if self._files:
+            for f in self._real_files:
+                os.unlink(f[1])
+
     def get_installed_files(self, **args):
         if self._files:
             return [f[1] for f in self._real_files]
@@ -331,14 +336,17 @@
             for i in range(0,2):
                 remove.append(ToInstallDist(files=True))
             to_install = [ToInstallDist(), ToInstallDist()]
+            temp_dir = self.mkdtemp()
 
             self.assertRaises(Exception, install.install_from_infos, 
-                    remove=remove, install=to_install)
+                              install_path=temp_dir, install=to_install, 
+                              remove=remove)
             # assert that the files are in the same place
             # assert that the files have been removed
             for dist in remove:
                 for f in dist.get_installed_files():
                     self.assertTrue(os.path.exists(f))
+                dist._unlink_installed_files()
         finally:
             install.install_dist = old_install_dist
             install.uninstall = old_uninstall
@@ -352,8 +360,7 @@
             install_path = "my_install_path"
             to_install = [ToInstallDist(), ToInstallDist()]
 
-            install.install_from_infos(install=to_install,
-                                             install_path=install_path)
+            install.install_from_infos(install_path, install=to_install)
             for dist in to_install:
                 install._install_dist.called_with(install_path)
         finally:

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


More information about the Python-checkins mailing list