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

pje at users.sourceforge.net pje at users.sourceforge.net
Sun Apr 3 01:31:32 CEST 2005


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

Modified Files:
	pkg_resources.py 
Log Message:
Add a simple version parser that combines the pre-release smarts of
distutils' StrictVersion, with the flexibility of LooseVersion.  It also
deals heuristically with common concepts like alpha/beta/candidate/rc
and pre/preview, as well as '-' and 'pl' branching schemes.


Index: pkg_resources.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setuptools/pkg_resources.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- pkg_resources.py	2 Apr 2005 02:43:20 -0000	1.5
+++ pkg_resources.py	2 Apr 2005 23:31:12 -0000	1.6
@@ -17,7 +17,7 @@
     'register_loader_type', 'get_provider', 'IResourceProvider',
     'ResourceManager', 'iter_distributions', 'require', 'resource_string',
     'resource_stream', 'resource_filename', 'set_extraction_path',
-    'cleanup_resources', 'parse_requirements', # 'glob_resources'
+    'cleanup_resources', 'parse_requirements', 'parse_version'# 'glob_resources'
 ]
 
 import sys, os, zipimport, time, re
@@ -508,8 +508,20 @@
 VERSION  = re.compile(r"\s*(<=?|>=?|==|!=)\s*((\w|\.)+)").match  # version info
 COMMA    = re.compile(r"\s*,").match               # comma between items
 
+component_re = re.compile(r'(\d+ | [a-z]+ | \.| -)', re.VERBOSE)
+replace = {'pre':'c', 'preview':'c','-':'final-','rc':'c'}.get
 
+def _parse_version_parts(s):
+    for part in component_re.split(s):
+        part = replace(part,part)
+        if not part or part=='.':
+            continue
+        if part[:1] in '0123456789':
+            yield part.zfill(8)    # pad for numeric comparison
+        else:
+            yield '*'+part
 
+    yield '*final'  # ensure that alpha/beta/candidate are before final
 
 
 
@@ -519,12 +531,41 @@
 
 
 
+def parse_version(s):
+    """Convert a version string to a sortable key
 
+    This is a rough cross between distutils' StrictVersion and LooseVersion;
+    if you give it versions that would work with StrictVersion, then it behaves
+    the same; otherwise it acts like a slightly-smarter LooseVersion.
 
+    The returned value will be a tuple of strings.  Numeric portions of the
+    version are padded to 8 digits so they will compare numerically, but
+    without relying on how numbers compare relative to strings.  Dots are
+    dropped, but dashes are retained.  Trailing zeros between alpha segments
+    or dashes are suppressed, so that e.g. 2.4.0 is considered the same as 2.4.
+    Alphanumeric parts are lower-cased.
+   
+    The algorithm assumes that strings like '-' and any alpha string > "final"
+    represents a "patch level".  So, "2.4-1" is assumed to be a branch or patch
+    of "2.4", and therefore "2.4.1" is considered newer than "2.4-1".
 
+    Strings like "a", "b", "c", "alpha", "beta", "candidate" and so on (that
+    come before "final" alphabetically) are assumed to be pre-release versions,
+    and so the version "2.4" is considered newer than "2.4a1".
 
-
-
+    Finally, to handle miscellaneous cases, the strings "pre", "preview", and
+    "rc" are treated as if they were "c", i.e. as though they were release
+    candidates, and therefore are not as new as a version string that does not
+    contain them.    
+    """
+    parts = []
+    for part in _parse_version_parts(s.lower()):
+        if part.startswith('*'):
+            # remove trailing zeros from each series of numeric parts
+            while parts and parts[-1]=='00000000':
+                parts.pop()
+        parts.append(part)
+    return tuple(parts)
 
 
 



More information about the Python-checkins mailing list