[Python-checkins] distutils2: removed the installation to an empty path from the API and fixed a leaking
tarek.ziade
python-checkins at python.org
Sun Jan 30 10:45:09 CET 2011
tarek.ziade pushed 4070dc9752ef to distutils2:
http://hg.python.org/distutils2/rev/4070dc9752ef
changeset: 961:4070dc9752ef
user: Yannick Gingras <ygingras at ygingras.net>
date: Sat Jan 29 20:21:12 2011 -0500
summary:
removed the installation to an empty path from the API and fixed a leaking tempdir in the process
files:
distutils2/install.py
distutils2/tests/test_install.py
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,10 +128,6 @@
:param dists: distributions to install
:param path: base path to install distribution in
"""
- path_is_tmp = False
- if not path:
- path = mkdtemp()
- path_is_tmp = True
installed_dists, installed_files = [], []
for d in dists:
@@ -148,14 +142,11 @@
for d in installed_dists:
uninstall(d)
raise e
- finally:
- if path_is_tmp:
- shutil.rmtree(path)
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.
@@ -174,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
@@ -210,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):
@@ -379,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
@@ -331,9 +331,11 @@
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:
@@ -352,8 +354,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