def extract_version(string):
"""Try to extract a version from the given string and feed it into
distutils.version.StrictVersion, which is returned. If no version could be
extracted, a ValueError is raised.
If the given parameter is a path, only the last chunk, "base name", is
used."""
if os.sep in string: # it's a path
string = os.path.basename(string.rstrip(os.sep))
match = config.VERSION_PATTERN.search(string)
if not match:
raise ValueError("%s doesn't contain a version number to extract" %
string)
# remove None groups and join the fragments together with a dot to make
# StrictVersion happy
match = ''.join([m for m in match.groups() if m is not None])
if match.endswith('.'): # strip trailing dots
match = match.rstrip('.').lstrip('.')
return distutils.version.LooseVersion(match)
评论列表
文章目录