Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/libversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
import re
import operator

HAS_SPECIFIER = False
try:
# Get 'SpecifierSet' from 'pip' package is the preferred method. It can match
# str like '==CPython' without fallback solution. Install it with
# 'pip install --ignore-blocklist pip' and delete the pip cmds in stash_extensions/bin
from pip._vendor.packaging.specifiers import SpecifierSet
HAS_SPECIFIER = True
except ImportError:
try:
# A good compromise is the 'packaging' package with 'pip install packaging'.
# 'SpecifierSet' from 'packaging' will raise an exception if matching str
# like '==CPython'. Fallback to old implementation in that case
from packaging.specifiers import SpecifierSet
HAS_SPECIFIER = True
except ImportError:
print("Fallback to default VersionSpecifier. To bypass some issues ")

# release type identifier -> release type priority (higher == better)
RELEASE_TYPE_PRIORITIES = {
None: 4, # no release type
Expand Down Expand Up @@ -275,6 +292,13 @@ def __init__(self, version_specs):
self.specs = [(VersionSpecifier.OPS[op], version) for (op, version) in version_specs]
self.str = str(version_specs)

self.specifier = None
if HAS_SPECIFIER:
try:
self.specifier = SpecifierSet(''.join(version_specs[0]))
except:
Copy link
Copy Markdown
Collaborator

@cclauss cclauss Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use bare except:, it also catches unexpected events like memory errors, interrupts, system exit, and so on.

pass

def __str__(self):
return self.str

Expand Down Expand Up @@ -323,6 +347,11 @@ def parse_requirement(requirement):
extras = []
else:
extras = extra_s.split(",")
elif "[" in name:
si = name.find("[")
extra_s = name[si + 1:-1]
name = name[:si]
extras = extra_s.split(",") if extra_s else []
else:
extras = []
splitted = specs_s.split(",")
Expand All @@ -347,6 +376,13 @@ def match(self, version):
:rtype: boolean
"""
# return all([op(Version.parse(version), Version.parse(ver)) for op, ver in self.specs])
if self.specifier is not None:
try:
return self.specifier.contains(version)
except:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use bare except:

# fallback to libversion implementation
pass

matches = True
for op, ver in self.specs:
try:
Expand Down