[Python-checkins] distutils2: savepoint

tarek.ziade python-checkins at python.org
Sun Jan 30 10:43:57 CET 2011


tarek.ziade pushed 59285ae1fc70 to distutils2:

http://hg.python.org/distutils2/rev/59285ae1fc70
changeset:   921:59285ae1fc70
parent:      852:e283946b09de
user:        Tarek Ziade <tarek at ziade.org>
date:        Sun Dec 26 17:11:20 2010 +0100
summary:
  savepoint

files:
  distutils2/index/dist.py
  distutils2/index/simple.py
  distutils2/install.py
  distutils2/run.py
  distutils2/version.py

diff --git a/distutils2/index/dist.py b/distutils2/index/dist.py
--- a/distutils2/index/dist.py
+++ b/distutils2/index/dist.py
@@ -376,6 +376,8 @@
         """
         predicate = get_version_predicate(requirements)
         releases = self.filter(predicate)
+        if len(releases) == 0:
+            return None
         releases.sort_releases(prefer_final, reverse=True)
         return releases[0]
 
diff --git a/distutils2/index/simple.py b/distutils2/index/simple.py
--- a/distutils2/index/simple.py
+++ b/distutils2/index/simple.py
@@ -14,6 +14,7 @@
 import logging
 import os
 
+from distutils2 import logger
 from distutils2.index.base import BaseClient
 from distutils2.index.dist import (ReleasesList, EXTENSIONS,
                                    get_infos_from_url, MD5_HASH)
@@ -167,6 +168,7 @@
         if predicate.name.lower() in self._projects and not force_update:
             return self._projects.get(predicate.name.lower())
         prefer_final = self._get_prefer_final(prefer_final)
+        logger.info('Reading info on PyPI about %s' % predicate.name)
         self._process_index_page(predicate.name)
 
         if predicate.name.lower() not in self._projects:
diff --git a/distutils2/install.py b/distutils2/install.py
--- a/distutils2/install.py
+++ b/distutils2/install.py
@@ -1,14 +1,15 @@
 from tempfile import mkdtemp
-import logging
 import shutil
 import os
 import errno
 import itertools
 
+from distutils2 import logger
 from distutils2._backport.pkgutil import get_distributions
 from distutils2.depgraph import generate_graph
 from distutils2.index import wrapper
 from distutils2.index.errors import ProjectNotFound, ReleaseNotFound
+from distutils2.version import get_version_predicate
 
 """Provides installations scripts.
 
@@ -72,10 +73,13 @@
 
     installed_dists, installed_files = [], []
     for d in dists:
+        logger.info('Installing %s %s' % (d.name, d.version))
         try:
             installed_files.extend(d.install(path))
             installed_dists.append(d)
         except Exception, e :
+            logger.info('Failed. %s' % str(e))
+
             for d in installed_dists:
                 d.uninstall()
             raise e
@@ -155,12 +159,36 @@
     conflict.
     """
 
+
+    if not installed:
+        logger.info('Reading installed distributions')
+        installed = get_distributions(use_egg_info=True)
+
+    infos = {'install': [], 'remove': [], 'conflict': []}
+    # Is a compatible version of the project is already installed ?
+    predicate = get_version_predicate(requirements)
+    found = False
+    installed = list(installed)
+    for installed_project in installed:
+        # is it a compatible project ?
+        if predicate.name.lower() != installed_project.name.lower():
+            continue
+        found = True
+        logger.info('Found %s %s' % (installed_project.name,
+                                     installed_project.metadata.version))
+        if predicate.match(installed_project.metadata.version):
+            return infos
+
+
+
+        break
+
+    if not found:
+        logger.info('Project not installed.')
+
     if not index:
         index = wrapper.ClientWrapper()
 
-    if not installed:
-        installed = get_distributions(use_egg_info=True)
-
     # Get all the releases that match the requirements
     try:
         releases = index.get_releases(requirements)
@@ -170,28 +198,31 @@
     # Pick up a release, and try to get the dependency tree
     release = releases.get_last(requirements, prefer_final=prefer_final)
 
+    if release is None:
+        logger.info('Could not find a matching project')
+        return infos
+
     # Iter since we found something without conflicts
     metadata = release.fetch_metadata()
 
-    # Get the distributions already_installed on the system
-    # and add the one we want to install
 
     distributions = itertools.chain(installed, [release])
     depgraph = generate_graph(distributions)
 
     # Store all the already_installed packages in a list, in case of rollback.
-    infos = {'install': [], 'remove': [], 'conflict': []}
+
 
     # Get what the missing deps are
-    for dists in depgraph.missing.values():
+    for dists in depgraph.missing[release]:
         if dists:
-            logging.info("missing dependencies found, installing them")
+            logger.info("missing dependencies found, installing them")
             # we have missing deps
             for dist in dists:
                 _update_infos(infos, get_infos(dist, index, installed))
 
     # Fill in the infos
     existing = [d for d in installed if d.name == release.name]
+
     if existing:
         infos['remove'].append(existing[0])
         infos['conflict'].extend(depgraph.reverse_list[existing[0]])
@@ -214,5 +245,28 @@
         attrs['requirements'] = sys.argv[1]
     get_infos(**attrs)
 
+
+def install(project):
+    logger.info('Getting information about "%s".' % project)
+    try:
+        info = get_infos(project)
+    except InstallationException:
+        logger.info('Cound not find "%s".' % project)
+        return
+
+    if info['install'] == []:
+        logger.info('Nothing to install.')
+        return
+
+    #logger.info('Installing "%s" and its dependencies' % project)
+    try:
+        install_from_infos(info['install'], info['remove'], info['conflict'])
+
+    except InstallationConflict, e:
+
+        projects = ['%s %s' % (p.name, p.metadata.version) for p in e.args[0]]
+        logger.info('"%s" conflicts with "%s"' % (project, ','.join(projects)))
+
+
 if __name__ == '__main__':
     main()
diff --git a/distutils2/run.py b/distutils2/run.py
--- a/distutils2/run.py
+++ b/distutils2/run.py
@@ -1,7 +1,9 @@
 import os
 import sys
 from optparse import OptionParser
+import logging
 
+from distutils2 import logger
 from distutils2.util import grok_environment_error
 from distutils2.errors import (DistutilsSetupError, DistutilsArgError,
                                DistutilsError, CCompilerError)
@@ -9,6 +11,7 @@
 from distutils2 import __version__
 from distutils2._backport.pkgutil import get_distributions, get_distribution
 from distutils2.depgraph import generate_graph
+from distutils2.install import install
 
 # This is a barebones help message generated displayed when the user
 # runs the setup script with no arguments at all.  More useful help
@@ -114,8 +117,17 @@
     return dist
 
 
+def _set_logger():
+    logger.setLevel(logging.INFO)
+    sth = logging.StreamHandler(sys.stderr)
+    sth.setLevel(logging.INFO)
+    logger.addHandler(sth)
+    logger.propagate = 0
+
+
 def main():
     """Main entry point for Distutils2"""
+    _set_logger()
     parser = OptionParser()
     parser.disable_interspersed_args()
     parser.usage = '%prog [options] cmd1 cmd2 ..'
@@ -136,6 +148,14 @@
                   action="store_true", dest="fgraph", default=False,
                   help="Display the full graph for installed distributions.")
 
+    parser.add_option("-i", "--install",
+                  action="store", dest="install",
+                  help="Install a project.")
+
+    parser.add_option("-r", "--remove",
+                  action="store", dest="remove",
+                  help="Remove a project.")
+
     options, args = parser.parse_args()
     if options.version:
         print('Distutils2 %s' % __version__)
@@ -169,6 +189,10 @@
         print(graph)
         sys.exit(0)
 
+    if options.install is not None:
+        install(options.install)
+        sys.exit(0)
+
     if len(args) == 0:
         parser.print_help()
         sys.exit(0)
@@ -178,4 +202,5 @@
 
 
 if __name__ == '__main__':
+
     main()
diff --git a/distutils2/version.py b/distutils2/version.py
--- a/distutils2/version.py
+++ b/distutils2/version.py
@@ -323,7 +323,7 @@
 
 
 _PREDICATE = re.compile(r"(?i)^\s*([a-z_][\sa-zA-Z_-]*(?:\.[a-z_]\w*)*)(.*)")
-_VERSIONS = re.compile(r"^\s*\((.*)\)\s*$")
+_VERSIONS = re.compile(r"^\s*\((?P<versions>.*)\)\s*$|^\s*(?P<versions2>.*)\s*$")
 _PLAIN_VERSIONS = re.compile(r"^\s*(.*)\s*$")
 _SPLIT_CMP = re.compile(r"^\s*(<=|>=|<|>|!=|==)\s*([^\s,]+)\s*$")
 
@@ -358,14 +358,22 @@
 
         name, predicates = match.groups()
         self.name = name.strip()
-        predicates = predicates.strip()
-        predicates = _VERSIONS.match(predicates)
+        self.predicates = []
+        predicates = _VERSIONS.match(predicates.strip())
         if predicates is not None:
-            predicates = predicates.groups()[0]
-            self.predicates = [_split_predicate(pred.strip())
-                               for pred in predicates.split(',')]
-        else:
-            self.predicates = []
+            predicates = predicates.groupdict()
+            if predicates['versions'] is not None:
+                versions = predicates['versions']
+            else:
+                versions = predicates.get('versions2')
+
+            if versions is not None:
+                for version in versions.split(','):
+                    try:
+                        self.predicates.append(_split_predicate(version))
+                    except:
+                        pass
+                        #import pdb; pdb.set_trace()
 
     def match(self, version):
         """Check if the provided version matches the predicates."""

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


More information about the Python-checkins mailing list