Browse Source

add Package.is_universal() method

master
Niklas Rosenstein 1 year ago
parent
commit
741f61be18
No known key found for this signature in database GPG Key ID: 6D269B33D25F6C6
2 changed files with 45 additions and 0 deletions
  1. + 15
    - 0
      src/shore/model.py
  2. + 30
    - 0
      src/test/shore/util/test_package.py

+ 15
- 0
src/shore/model.py

@ -836,3 +836,18 @@ class Package(BaseObject):
author = '<Non-literal expression>'
return self.EntryMetadata(author, version)
def is_universal(self) -> bool:
"""
Checks if the package is a universal Python package (i.e. it is Python 2 and 3 compatible)
by testing the `$.requirements.python` version selector. If none is specified, the package
is also considered universal.
"""
if not self.requirements.python:
return True
# TODO (@NiklasRosenstein): This method of detecting if the version selector
# selects a Python 2 and 3 version is very suboptimal.
has_2 = re.search(r'\b2\b|\b2\.\b', str(self.requirements.python))
has_3 = re.search(r'\b3\b|\b3\.\b', str(self.requirements.python))
return bool(has_2 and has_3)

+ 30
- 0
src/test/shore/util/test_package.py

@ -0,0 +1,30 @@
from shore.model import Package, VersionSelector
def test_package_is_universal():
package = Package(name='test', description='')
assert not package.requirements.python
assert package.is_universal()
package.requirements.python = VersionSelector('2.7')
assert not package.is_universal()
package.requirements.python = VersionSelector('3.4')
assert not package.is_universal()
package.requirements.python = VersionSelector('>= 3.4')
assert not package.is_universal()
package.requirements.python = VersionSelector('^2.7 | ^3.3')
assert package.is_universal()
package.requirements.python = VersionSelector('>= 2.7 | >= 3.3')
assert package.is_universal()
package.requirements.python = VersionSelector('~2.7 | ~2.10')
assert not package.is_universal()
package.requirements.python = VersionSelector('2|3')
assert package.is_universal()

Loading…
Cancel
Save