def substitution_offsets(path): """This returns offsets for substituting versions and names in the provided path. It is a helper for substitute_version(). """ # Get name and version offsets try: ver, vs, vl = parse_version_offset(path) name, ns, nl = parse_name_offset(path, ver) except UndetectableNameError: return (None, -1, -1, (), ver, vs, vl, (vs, )) except UndetectableVersionError: return (None, -1, -1, (), None, -1, -1, ()) # protect extensions like bz2 from getting inadvertently # considered versions. path = comp.strip_extension(path) # Construct a case-insensitive regular expression for the package name. name_re = '(%s)' % insensitize(name) # Split the string apart by things that match the name so that if the # name contains numbers or things that look like versions, we don't # accidentally substitute them with a version. name_parts = re.split(name_re, path) offsets = cumsum(name_parts, 0, len) name_offsets = offsets[1::2] ver_offsets = [] for i in xrange(0, len(name_parts), 2): vparts = re.split(ver, name_parts[i]) voffsets = cumsum(vparts, offsets[i], len) ver_offsets.extend(voffsets[1::2]) return (name, ns, nl, tuple(name_offsets), ver, vs, vl, tuple(ver_offsets))
def split_url_extension(path): """Some URLs have a query string, e.g.: 1. https://github.com/losalamos/CLAMR/blob/packages/PowerParser_v2.0.7.tgz?raw=true 2. http://www.apache.org/dyn/closer.cgi?path=/cassandra/1.2.0/apache-cassandra-1.2.0-rc2-bin.tar.gz 3. https://gitlab.kitware.com/vtk/vtk/repository/archive.tar.bz2?ref=v7.0.0 In (1), the query string needs to be stripped to get at the extension, but in (2) & (3), the filename is IN a single final query argument. This strips the URL into three pieces: ``prefix``, ``ext``, and ``suffix``. The suffix contains anything that was stripped off the URL to get at the file extension. In (1), it will be ``'?raw=true'``, but in (2), it will be empty. In (3) the suffix is a parameter that follows after the file extension, e.g.: 1. ``('https://github.com/losalamos/CLAMR/blob/packages/PowerParser_v2.0.7', '.tgz', '?raw=true')`` 2. ``('http://www.apache.org/dyn/closer.cgi?path=/cassandra/1.2.0/apache-cassandra-1.2.0-rc2-bin', '.tar.gz', None)`` 3. ``('https://gitlab.kitware.com/vtk/vtk/repository/archive', '.tar.bz2', '?ref=v7.0.0')`` """ prefix, ext, suffix = path, '', '' # Strip off sourceforge download suffix. # e.g. https://sourceforge.net/projects/glew/files/glew/2.0.0/glew-2.0.0.tgz/download match = re.search(r'(.*(?:sourceforge\.net|sf\.net)/.*)(/download)$', path) if match: prefix, suffix = match.groups() ext = comp.extension(prefix) if ext is not None: prefix = comp.strip_extension(prefix) else: prefix, suf = strip_query_and_fragment(prefix) ext = comp.extension(prefix) prefix = comp.strip_extension(prefix) suffix = suf + suffix if ext is None: ext = '' return prefix, ext, suffix
def split_url_extension(path): """Some URLs have a query string, e.g.: 1. https://github.com/losalamos/CLAMR/blob/packages/PowerParser_v2.0.7.tgz?raw=true 2. http://www.apache.org/dyn/closer.cgi?path=/cassandra/1.2.0/apache-cassandra-1.2.0-rc2-bin.tar.gz In (1), the query string needs to be stripped to get at the extension, but in (2), the filename is IN a single final query argument. This strips the URL into three pieces: prefix, ext, and suffix. The suffix contains anything that was stripped off the URL to get at the file extension. In (1), it will be '?raw=true', but in (2), it will be empty. e.g.: 1. ('https://github.com/losalamos/CLAMR/blob/packages/PowerParser_v2.0.7', '.tgz', '?raw=true') 2. ('http://www.apache.org/dyn/closer.cgi?path=/cassandra/1.2.0/apache-cassandra-1.2.0-rc2-bin', '.tar.gz', None) """ prefix, ext, suffix = path, '', '' # Strip off sourceforge download suffix. match = re.search(r'((?:sourceforge.net|sf.net)/.*)(/download)$', path) if match: prefix, suffix = match.groups() ext = comp.extension(prefix) if ext is not None: prefix = comp.strip_extension(prefix) else: prefix, suf = strip_query_and_fragment(prefix) ext = comp.extension(prefix) prefix = comp.strip_extension(prefix) suffix = suf + suffix if ext is None: ext = '' return prefix, ext, suffix
def substitution_offsets(path): """This returns offsets for substituting versions and names in the provided path. It is a helper for substitute_version(). """ # Get name and version offsets try: ver, vs, vl = parse_version_offset(path) name, ns, nl = parse_name_offset(path, ver) except UndetectableNameError: return (None, -1, -1, (), ver, vs, vl, (vs,)) except UndetectableVersionError: return (None, -1, -1, (), None, -1, -1, ()) # protect extensions like bz2 from getting inadvertently # considered versions. path = comp.strip_extension(path) # Construct a case-insensitive regular expression for the package name. name_re = '(%s)' % insensitize(name) # Split the string apart by things that match the name so that if the # name contains numbers or things that look like versions, we don't # accidentally substitute them with a version. name_parts = re.split(name_re, path) offsets = cumsum(name_parts, 0, len) name_offsets = offsets[1::2] ver_offsets = [] for i in xrange(0, len(name_parts), 2): vparts = re.split(ver, name_parts[i]) voffsets = cumsum(vparts, offsets[i], len) ver_offsets.extend(voffsets[1::2]) return (name, ns, nl, tuple(name_offsets), ver, vs, vl, tuple(ver_offsets))
def parse_version_string_with_indices(path): """Try to extract a version string from a filename or URL. This is taken largely from Homebrew's Version class.""" # Strip off sourceforge download stuffix. if re.search(r'((?:sourceforge.net|sf.net)/.*)/download$', path): path = os.path.dirname(path) # Strip archive extension path = comp.strip_extension(path) # Take basename to avoid including parent dirs in version name # Remember the offset of the stem in the full path. stem = os.path.basename(path) version_types = [ # GitHub tarballs, e.g. v1.2.3 (r'github.com/.+/(?:zip|tar)ball/v?((\d+\.)+\d+)$', path), # e.g. https://github.com/sam-github/libnet/tarball/libnet-1.1.4 (r'github.com/.+/(?:zip|tar)ball/.*-((\d+\.)+\d+)$', path), # e.g. https://github.com/isaacs/npm/tarball/v0.2.5-1 (r'github.com/.+/(?:zip|tar)ball/v?((\d+\.)+\d+-(\d+))$', path), # e.g. https://github.com/petdance/ack/tarball/1.93_02 (r'github.com/.+/(?:zip|tar)ball/v?((\d+\.)+\d+_(\d+))$', path), # e.g. https://github.com/hpc/lwgrp/archive/v1.0.1.tar.gz (r'github.com/[^/]+/[^/]+/archive/v?(\d+(?:\.\d+)*)\.tar\.gz$', path), # e.g. https://github.com/erlang/otp/tarball/OTP_R15B01 (erlang style) (r'[-_](R\d+[AB]\d*(-\d+)?)', path), # e.g. boost_1_39_0 (r'((\d+_)+\d+)$', stem), # e.g. foobar-4.5.1-1 # e.g. ruby-1.9.1-p243 (r'-((\d+\.)*\d\.\d+-(p|rc|RC)?\d+)(?:[-._](?:bin|dist|stable|src|sources))?$', stem), # e.g. lame-398-1 (r'-((\d)+-\d)', stem), # e.g. foobar-4.5.1 (r'-((\d+\.)*\d+)$', stem), # e.g. foobar-4.5.1b (r'-((\d+\.)*\d+([a-z]|rc|RC)\d*)$', stem), # e.g. foobar-4.5.0-beta1, or foobar-4.50-beta (r'-((\d+\.)*\d+-beta(\d+)?)$', stem), # e.g. foobar4.5.1 (r'((\d+\.)*\d+)$', stem), # e.g. foobar-4.5.0-bin (r'-((\d+\.)+\d+[a-z]?)[-._](bin|dist|stable|src|sources?)$', stem), # e.g. dash_0.5.5.1.orig.tar.gz (Debian style) (r'_((\d+\.)+\d+[a-z]?)[.]orig$', stem), # e.g. http://www.openssl.org/source/openssl-0.9.8s.tar.gz (r'-([^-]+(-alpha|-beta)?)', stem), # e.g. astyle_1.23_macosx.tar.gz (r'_([^_]+(_alpha|_beta)?)', stem), # e.g. http://mirrors.jenkins-ci.org/war/1.486/jenkins.war (r'\/(\d\.\d+)\/', path), # e.g. http://www.ijg.org/files/jpegsrc.v8d.tar.gz (r'\.v(\d+[a-z]?)', stem)] for vtype in version_types: regex, match_string = vtype[:2] match = re.search(regex, match_string) if match and match.group(1) is not None: version = match.group(1) start = path.index(version) return version, start, start+len(version) raise UndetectableVersionError(path)
"""This returns offsets for substituting versions and names in the provided path. It is a helper for substitute_version(). """ # Get name and version offsets try: ver, vs, vl = parse_version_offset(path) name, ns, nl = parse_name_offset(path, ver) except UndetectableNameError, e: return (None, -1, -1, (), ver, vs, vl, (vs,)) except UndetectableVersionError, e: return (None, -1, -1, (), None, -1, -1, ()) # protect extensions like bz2 from getting inadvertently # considered versions. ext = comp.extension(path) path = comp.strip_extension(path) # Construct a case-insensitive regular expression for the package name. name_re = '(%s)' % insensitize(name) # Split the string apart by things that match the name so that if the # name contains numbers or things that look like versions, we don't # accidentally substitute them with a version. name_parts = re.split(name_re, path) offsets = cumsum(name_parts, 0, len) name_offsets = offsets[1::2] ver_offsets = [] for i in xrange(0, len(name_parts), 2): vparts = re.split(ver, name_parts[i])
"""This returns offsets for substituting versions and names in the provided path. It is a helper for substitute_version(). """ # Get name and version offsets try: ver, vs, vl = parse_version_offset(path) name, ns, nl = parse_name_offset(path, ver) except UndetectableNameError, e: return (None, -1, -1, (), ver, vs, vl, (vs, )) except UndetectableVersionError, e: return (None, -1, -1, (), None, -1, -1, ()) # protect extensions like bz2 from getting inadvertently # considered versions. ext = comp.extension(path) path = comp.strip_extension(path) # Construct a case-insensitive regular expression for the package name. name_re = '(%s)' % insensitize(name) # Split the string apart by things that match the name so that if the # name contains numbers or things that look like versions, we don't # accidentally substitute them with a version. name_parts = re.split(name_re, path) offsets = cumsum(name_parts, 0, len) name_offsets = offsets[1::2] ver_offsets = [] for i in xrange(0, len(name_parts), 2): vparts = re.split(ver, name_parts[i])