[Python-checkins] python/nondist/sandbox/setuptools pkg_resources.py, 1.6, 1.7

pje at users.sourceforge.net pje at users.sourceforge.net
Sun Apr 3 02:47:00 CEST 2005


Update of /cvsroot/python/python/nondist/sandbox/setuptools
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29068

Modified Files:
	pkg_resources.py 
Log Message:
Add a "Distribution" object that wraps a sys.path entry with metadata, and
can extract its name/version/pythonversion/platform if built from a .egg
filename.  Later, distributions will be able to add themselves to sys.path
and request that their dependencies be added as well.  Also, added some
real-life version test cases supplied by jemfinch.


Index: pkg_resources.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setuptools/pkg_resources.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- pkg_resources.py	2 Apr 2005 23:31:12 -0000	1.6
+++ pkg_resources.py	3 Apr 2005 00:46:56 -0000	1.7
@@ -17,7 +17,8 @@
     'register_loader_type', 'get_provider', 'IResourceProvider',
     'ResourceManager', 'iter_distributions', 'require', 'resource_string',
     'resource_stream', 'resource_filename', 'set_extraction_path',
-    'cleanup_resources', 'parse_requirements', 'parse_version'# 'glob_resources'
+    'cleanup_resources', 'parse_requirements', 'parse_version',
+    'Distribution', # 'glob_resources'
 ]
 
 import sys, os, zipimport, time, re
@@ -38,7 +39,6 @@
     loader = getattr(module, '__loader__', None)
     return _find_adapter(_provider_factories, loader)(module)
 
-
 class IResourceProvider:
 
     """An object that provides access to package resources"""
@@ -508,6 +508,12 @@
 VERSION  = re.compile(r"\s*(<=?|>=?|==|!=)\s*((\w|\.)+)").match  # version info
 COMMA    = re.compile(r"\s*,").match               # comma between items
 
+EGG_NAME = re.compile(
+    r"(?P<name>[^-]+)"
+    r"( -(?P<ver>[^-]+) (-py(?P<pyver>[^-]+) (-(?P<plat>.+))? )? )?",
+    re.VERBOSE | re.IGNORECASE
+).match
+
 component_re = re.compile(r'(\d+ | [a-z]+ | \.| -)', re.VERBOSE)
 replace = {'pre':'c', 'preview':'c','-':'final-','rc':'c'}.get
 
@@ -525,12 +531,6 @@
 
 
 
-
-
-
-
-
-
 def parse_version(s):
     """Convert a version string to a sortable key
 
@@ -572,6 +572,88 @@
 
 
 
+class Distribution(object):
+    """Wrap an actual or potential sys.path entry w/metadata"""
+    
+    def __init__(self,
+        path_str, metadata=None, name=None, version=None,
+        py_version=sys.version[:3]
+    ):
+        if name:
+            self.name = name
+        if version:
+            self.version = version
+
+        self.py_version = py_version
+        self.path = path_str
+        self.normalized_path = os.path.normpath(os.path.normcase(path_str))
+
+    def installed_on(self,path=None):
+        """Is this distro installed on `path`? (defaults to ``sys.path``)"""
+        if path is None:
+            path = sys.path
+        if self.path in path or self.normalized_path in path:
+            return True
+        for item in path:
+            normalized = os.path.normpath(os.path.normcase(item))
+            if normalized == self.normalized_path:
+                return True
+        return False
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+    #@classmethod
+    def from_filename(cls,filename,metadata=None):
+        name,version,py_version,platform = [None]*4
+        basename,ext = os.path.splitext(os.path.basename(filename))
+        if ext.lower()==".egg":
+            match = EGG_NAME(basename)
+            if match:
+                name,version,py_version,platform = match.group(
+                    'name','ver','pyver','plat'
+                )
+                if version and '_' in version:
+                    version = version.replace('_','-')
+        return cls(
+            filename,metadata,name=name,version=version,py_version=py_version
+        )
+    from_filename = classmethod(from_filename)
+
+    # These properties have to be lazy so that we don't have to load any
+    # metadata until/unless it's actually needed.  (i.e., some distributions
+    # may not know their name or version without loading PKG-INFO)
+
+    #@property
+    def key(self):
+        try:
+            return self._key
+        except AttributeError:
+            self._key = key = self.name.lower()
+            return key
+    key = property(key)
+
+    #@property
+    def parsed_version(self):
+        try:
+            return self._parsed_version
+        except AttributeError:
+            self._parsed_version = pv = parse_version(self.version)
+            return pv
+
+    parsed_version = property(parsed_version)
+    
+
 def parse_requirements(strs):
     """Yield ``Requirement`` objects for each specification in `strs`
 



More information about the Python-checkins mailing list