[Python-checkins] distutils2: cleaned up the install module
tarek.ziade
python-checkins at python.org
Sun Jan 30 11:22:29 CET 2011
tarek.ziade pushed 66f2c2a50996 to distutils2:
http://hg.python.org/distutils2/rev/66f2c2a50996
changeset: 964:66f2c2a50996
tag: tip
user: Tarek Ziade <tarek at ziade.org>
date: Sun Jan 30 11:22:19 2011 +0100
summary:
cleaned up the install module
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
@@ -1,4 +1,11 @@
-from tempfile import mkdtemp
+"""Provides installations scripts.
+
+The goal of this script is to install a release from the indexes (eg.
+PyPI), including the dependencies of the releases if needed.
+
+It uses the work made in pkgutil and by the index crawlers to browse the
+installed distributions, and rely on the instalation commands to install.
+"""
import shutil
import os
import sys
@@ -17,14 +24,9 @@
from distutils2.errors import DistutilsError
from distutils2.version import get_version_predicate
-"""Provides installations scripts.
-The goal of this script is to install a release from the indexes (eg.
-PyPI), including the dependencies of the releases if needed.
-
-It uses the work made in pkgutil and by the index crawlers to browse the
-installed distributions, and rely on the instalation commands to install.
-"""
+__all__ = ['install_dists', 'install_from_infos', 'get_infos', 'remove',
+ 'install']
class InstallationException(Exception):
@@ -35,7 +37,7 @@
"""Raised when a conflict is detected"""
-def move_files(files, destination):
+def _move_files(files, destination):
"""Move the list of files in the destination folder, keeping the same
structure.
@@ -58,7 +60,7 @@
else:
raise e
os.rename(old, new)
- yield (old, new)
+ yield old, new
def _run_d1_install(archive_dir, path):
@@ -91,7 +93,7 @@
* copy the files in "path"
* determine if the distribution is distutils2 or distutils1.
"""
- where = dist.unpack(archive)
+ where = dist.unpack(path)
# get into the dir
archive_dir = None
@@ -117,7 +119,7 @@
os.chdir(old_dir)
-def install_dists(dists, path):
+def install_dists(dists, path, paths=sys.path):
"""Install all distributions provided in dists, with the given prefix.
If an error occurs while installing one of the distributions, uninstall all
@@ -127,26 +129,28 @@
:param dists: distributions to install
:param path: base path to install distribution in
+ :param paths: list of paths (defaults to sys.path) to look for info
"""
installed_dists, installed_files = [], []
- for d in dists:
- logger.info('Installing %s %s' % (d.name, d.version))
+ for dist in dists:
+ logger.info('Installing %s %s' % (dist.name, dist.version))
try:
- installed_files.extend(_install_dist(d, path))
- installed_dists.append(d)
- except Exception, e :
+ installed_files.extend(_install_dist(dist, path))
+ installed_dists.append(dist)
+ except Exception, e:
logger.info('Failed. %s' % str(e))
# reverting
- for d in installed_dists:
- uninstall(d)
+ for installed_dist in installed_dists:
+ _remove_dist(installed_dist, paths)
raise e
-
+
return installed_files
-def install_from_infos(install_path=None, install=[], remove=[], conflicts=[]):
+def install_from_infos(install_path=None, install=[], remove=[], conflicts=[],
+ paths=sys.path):
"""Install and remove the given distributions.
The function signature is made to be compatible with the one of get_infos.
@@ -173,6 +177,7 @@
:param conflicts: list of conflicting distributions, eg. that will be in
conflict once the install and remove distribution will be
processed.
+ :param paths: list of paths (defaults to sys.path) to look for info
"""
# first of all, if we have conflicts, stop here.
if conflicts:
@@ -190,10 +195,10 @@
temp_dir = tempfile.mkdtemp()
for dist in remove:
files = dist.get_installed_files()
- temp_files[dist] = move_files(files, temp_dir)
+ temp_files[dist] = _move_files(files, temp_dir)
try:
if install:
- installed_files = install_dists(install, install_path)
+ install_dists(install, install_path, paths)
except:
# if an error occurs, put back the files in the right place.
for files in temp_files.values():
@@ -271,9 +276,9 @@
# Get all the releases that match the requirements
try:
releases = index.get_releases(requirements)
- except (ReleaseNotFound, ProjectNotFound), e:
+ except (ReleaseNotFound, ProjectNotFound):
raise InstallationException('Release not found: "%s"' % requirements)
-
+
# Pick up a release, and try to get the dependency tree
release = releases.get_last(requirements, prefer_final=prefer_final)
@@ -290,6 +295,8 @@
else:
deps = metadata['requires_dist']
+ # XXX deps not used
+
distributions = itertools.chain(installed, [release])
depgraph = generate_graph(distributions)
@@ -321,6 +328,10 @@
infos[key].extend(new_infos[key])
+def _remove_dist(dist, paths=sys.path):
+ remove(dist.name, paths)
+
+
def remove(project_name, paths=sys.path):
"""Removes a single project from the installation"""
dist = get_distribution(project_name, paths=paths)
@@ -329,8 +340,7 @@
files = dist.get_installed_files(local=True)
rmdirs = []
rmfiles = []
- tmp = tempfile.mkdtemp(prefix=project_name+'-uninstall')
-
+ tmp = tempfile.mkdtemp(prefix=project_name + '-uninstall')
try:
for file, md5, size in files:
if os.path.isfile(file):
@@ -357,14 +367,6 @@
os.rmdir(dirname)
-
-def main(**attrs):
- if 'script_args' not in attrs:
- import sys
- attrs['requirements'] = sys.argv[1]
- get_infos(**attrs)
-
-
def install(project):
logger.info('Getting information about "%s".' % project)
try:
@@ -379,7 +381,7 @@
install_path = get_config_var('base')
try:
- install_from_infos(install_path,
+ install_from_infos(install_path,
info['install'], info['remove'], info['conflict'])
except InstallationConflict, e:
@@ -387,5 +389,12 @@
logger.info('"%s" conflicts with "%s"' % (project, ','.join(projects)))
+def _main(**attrs):
+ if 'script_args' not in attrs:
+ import sys
+ attrs['requirements'] = sys.argv[1]
+ get_infos(**attrs)
+
+
if __name__ == '__main__':
- main()
+ _main()
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
@@ -59,14 +59,14 @@
self._called_with = []
self._return_value = return_value
self._raise = raise_exception
-
+
def __call__(self, *args, **kwargs):
self.called = True
self._times_called = self._times_called + 1
self._called_with.append((args, kwargs))
iterable = hasattr(self._raise, '__iter__')
if self._raise:
- if ((not iterable and self._raise)
+ if ((not iterable and self._raise)
or self._raise[self._times_called - 1]):
raise Exception
return self._return_value
@@ -93,7 +93,7 @@
def get_installed_dists(dists):
- """Return a list of fake installed dists.
+ """Return a list of fake installed dists.
The list is name, version, deps"""
objects = []
for (name, version, deps) in dists:
@@ -210,7 +210,7 @@
])
# name, version, deps.
- already_installed = [('bacon', '0.1', []),
+ already_installed = [('bacon', '0.1', []),
('chicken', '1.1', ['bacon (0.1)'])]
output = install.get_infos("choxie", index=client, installed=
get_installed_dists(already_installed))
@@ -241,7 +241,7 @@
files = [os.path.join(path, '%s' % x) for x in range(1, 20)]
for f in files:
file(f, 'a+')
- output = [o for o in install.move_files(files, newpath)]
+ output = [o for o in install._move_files(files, newpath)]
# check that output return the list of old/new places
for f in files:
@@ -270,19 +270,19 @@
old_install_dist = install._install_dist
old_uninstall = getattr(install, 'uninstall', None)
- install._install_dist = MagicMock(return_value=[],
+ install._install_dist = MagicMock(return_value=[],
raise_exception=(False, True))
- install.uninstall = MagicMock()
+ install.remove = MagicMock()
try:
d1 = ToInstallDist()
d2 = ToInstallDist()
path = self.mkdtemp()
self.assertRaises(Exception, install.install_dists, [d1, d2], path)
self.assertTrue(install._install_dist.called_with(d1, path))
- self.assertTrue(install.uninstall.called)
+ self.assertTrue(install.remove.called)
finally:
install._install_dist = old_install_dist
- install.uninstall = old_uninstall
+ install.remove = old_uninstall
def test_install_dists_success(self):
@@ -327,7 +327,7 @@
old_install_dist = install._install_dist
old_uninstall = getattr(install, 'uninstall', None)
- install._install_dist = MagicMock(return_value=[],
+ install._install_dist = MagicMock(return_value=[],
raise_exception=(False, True))
install.uninstall = MagicMock()
try:
@@ -338,8 +338,8 @@
to_install = [ToInstallDist(), ToInstallDist()]
temp_dir = self.mkdtemp()
- self.assertRaises(Exception, install.install_from_infos,
- install_path=temp_dir, install=to_install,
+ self.assertRaises(Exception, install.install_from_infos,
+ 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
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list