Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_invalid_versions(self, version):
with pytest.raises(InvalidVersion):
Version(version)
log.info("Did not find anything that looks like a version in the tag")
# as a last resort, let's try to convert underscores to dots, while stripping out
# any "alphanumeric_". many hg repos do this, e.g. PROJECT_1_2_3
parts = version.split('_')
if len(parts) >= 2 and parts[0].isalpha():
# gets list except first item, joins by dot
version = '.'.join(parts[1:])
try:
v = Version(version)
if not v.is_prerelease or pre_ok:
log.info("Parsed as Version OK")
log.info("String representation of version is {}.".format(v))
res = v
else:
log.info("Parsed as unwanted pre-release version: {}.".format(v))
except InvalidVersion:
log.info('Still not a valid version after applying underscores fix')
# apply --major filter
if res and major and not self.matches_major_filter(version, major):
log.info('{} is not under the desired major {}'.format(
version, major))
res = False
return res
if input_str is None or input_str == '':
return
if '-' in input_str:
versions_list = input_str.split('-')
if len(versions_list) > 2:
return
else:
try:
v_list = [Version(versions_list[0]), Version(versions_list[1])]
result[version_key] = v_list
result[operator_key] = '-'
return result
except InvalidVersion:
return
try:
result[version_key] = Version(input_str)
result[operator_key] = '='
return result
except InvalidVersion:
op_list = []
for op in valid_operators:
if find_str(input_str, op) == 0:
op_list.append(op)
op = max(op_list)
try:
result[version_key] = Version(input_str.replace(op, '', 1))
try:
v_list = [Version(versions_list[0]), Version(versions_list[1])]
result[version_key] = v_list
result[operator_key] = '-'
return result
except InvalidVersion:
return
try:
result[version_key] = Version(input_str)
result[operator_key] = '='
return result
except InvalidVersion:
op_list = []
for op in valid_operators:
if find_str(input_str, op) == 0:
op_list.append(op)
op = max(op_list)
try:
result[version_key] = Version(input_str.replace(op, '', 1))
result[operator_key] = op
except InvalidVersion:
return
return result
def safe_version(version):
"""
Convert an arbitrary string to a standard version string
"""
try:
# normalize the version
return str(packaging.version.Version(version))
except packaging.version.InvalidVersion:
version = version.replace(' ', '.')
return re.sub('[^A-Za-z0-9.]+', '-', version)
def _is_version(value, placeholder="x"):
if value.endswith(".{}".format(placeholder)):
value = value[: -(len(placeholder) + 1)]
try:
pv.Version(value)
return True
except pv.InvalidVersion:
return False
async def get_candidate_infos(
package_types: List[PackageType],
source: str,
session: ClientSession,
name: str,
) -> Optional[List[CandidateInfo]]:
base_metadata = await get_json_metadata(source, session, name, version=None)
if base_metadata is None:
return None
candidate_infos = []
for version_str, distributions in base_metadata['releases'].items():
try:
version = Version(version_str)
except InvalidVersion:
logger.info('Invalid version for %r: %s', name, version_str)
continue # Skip candidates without a valid version.
for distribution in distributions:
package_type = PackageType[distribution['packagetype']]
if package_type.name.startswith('bdist'):
filename = distribution['filename']
if not is_supported(filename):
logger.debug('Skipping unsupported bdist %s', filename)
continue
if package_type not in package_types:
logger.debug('Skipping package type %s for %s', package_type.name, name)
continue
raise FileNotFoundError(msg + str(prefer))
if path.exists():
python = self.get_by_path(path)
if python is not None:
return python
# looks like constraint
if set(prefer) & {'<', '>', '='}:
python = self.get_by_spec(RangeSpecifier(prefer))
if python is not None:
return python
# ok, let's try it like a version
try:
version = Version(prefer)
except InvalidVersion:
pass
else:
python = self.get_by_version(version)
if python is not None:
return python
# ok, let's try it like a name
python = self.get_by_name(prefer)
if python is not None:
return python
# no success
raise FileNotFoundError('cannot find interpretor: ' + str(prefer))
"""
Argument parser helper for --newer-than (-gt) option
:param value:
:type value:
:return:
:rtype:
"""
try:
# TODO use sanitize_version so that we can just pass tags as values
# help devel releases to be correctly identified
# https://www.python.org/dev/peps/pep-0440/#developmental-releases
value = re.sub('-devel$', '.dev0', value, 1)
# help post (patch) releases to be correctly identified (e.g. Magento 2.3.4-p2)
value = re.sub('-p(\\d+)$', '.post\\1', value, 1)
value = Version(value)
except InvalidVersion:
raise argparse.ArgumentTypeError("%s is an invalid version value" % value)
return value
def safe_version(version):
"""
Convert an arbitrary string to a standard version string
"""
try:
# normalize the version
return str(packaging.version.Version(version))
except packaging.version.InvalidVersion:
version = version.replace(' ', '.')
return re.sub('[^A-Za-z0-9.]+', '-', version)