Commit 0adbac0f by Sébastien Eustace

Fix Dependency.to_pep_508() method

parent b6115051
...@@ -107,11 +107,11 @@ class Dependency: ...@@ -107,11 +107,11 @@ class Dependency:
requirement = f'{self.pretty_name}' requirement = f'{self.pretty_name}'
if isinstance(self.constraint, MultiConstraint): if isinstance(self.constraint, MultiConstraint):
requirement += ','.join( requirement += ' ({})'.format(','.join(
[str(c).replace(' ', '') for c in self.constraint.constraints] [str(c).replace(' ', '') for c in self.constraint.constraints]
) ))
else: else:
requirement += str(self.constraint).replace(' ', '') requirement += ' ({})'.format(str(self.constraint).replace(' ', ''))
# Markers # Markers
markers = [] markers = []
...@@ -119,21 +119,31 @@ class Dependency: ...@@ -119,21 +119,31 @@ class Dependency:
# Python marker # Python marker
if self.python_versions != '*': if self.python_versions != '*':
python_constraint = self.python_constraint python_constraint = self.python_constraint
marker = 'python_version'
if isinstance(python_constraint, MultiConstraint):
marker += ','.join(
[str(c).replace(' ', '') for c in python_constraint.constraints]
)
else:
marker += str(python_constraint).replace(' ', '')
markers.append(marker) markers.append(self._create_nested_marker('python_version', python_constraint))
if markers: if markers:
requirement += f'; {" and ".join(markers)}' requirement += f'; {" and ".join(markers)}'
return requirement return requirement
def _create_nested_marker(self, name, constraint):
if isinstance(constraint, MultiConstraint):
parts = []
for c in constraint.constraints:
parts.append(self._create_nested_marker(name, c))
glue = ' and '
if constraint.is_disjunctive():
parts = [f'({part})' for part in parts]
glue = ' or '
marker = glue.join(parts)
else:
marker = f'{name}{constraint.string_operator}"{constraint.version}"'
return marker
def activate(self): def activate(self):
""" """
Set the dependency as mandatory. Set the dependency as mandatory.
......
...@@ -42,6 +42,7 @@ class Constraint(BaseConstraint): ...@@ -42,6 +42,7 @@ class Constraint(BaseConstraint):
) )
self._operator = self._trans_op_str[operator] self._operator = self._trans_op_str[operator]
self._string_operator = operator
self._version = version self._version = version
@property @property
...@@ -53,6 +54,10 @@ class Constraint(BaseConstraint): ...@@ -53,6 +54,10 @@ class Constraint(BaseConstraint):
return self._operator return self._operator
@property @property
def string_operator(self):
return self._string_operator
@property
def version(self) -> str: def version(self) -> str:
return self._version return self._version
......
...@@ -39,9 +39,9 @@ def test_convert_dependencies(): ...@@ -39,9 +39,9 @@ def test_convert_dependencies():
get_dependency('C', '1.2.3'), get_dependency('C', '1.2.3'),
]) ])
main = [ main = [
'A>=1.0.0.0,<2.0.0.0', 'A (>=1.0.0.0,<2.0.0.0)',
'B>=1.0.0.0,<1.1.0.0', 'B (>=1.0.0.0,<1.1.0.0)',
'C==1.2.3.0', 'C (==1.2.3.0)',
] ]
extras = [] extras = []
...@@ -56,11 +56,11 @@ def test_convert_dependencies(): ...@@ -56,11 +56,11 @@ def test_convert_dependencies():
get_dependency('C', '1.2.3'), get_dependency('C', '1.2.3'),
]) ])
main = [ main = [
'B>=1.0.0.0,<1.1.0.0', 'B (>=1.0.0.0,<1.1.0.0)',
'C==1.2.3.0', 'C (==1.2.3.0)',
] ]
extras = [ extras = [
'A>=1.0.0.0,<2.0.0.0; python_version>=3.4.0.0,<4.0.0.0', 'A (>=1.0.0.0,<2.0.0.0); python_version>="3.4.0.0" and python_version<"4.0.0.0"',
] ]
assert result == (main, extras) assert result == (main, extras)
...@@ -82,7 +82,7 @@ def test_make_setup(): ...@@ -82,7 +82,7 @@ def test_make_setup():
'my_package.sub_pkg2' 'my_package.sub_pkg2'
] ]
assert ns['install_requires'] == [ assert ns['install_requires'] == [
'cleo>=0.6.0.0,<0.7.0.0' 'cleo (>=0.6.0.0,<0.7.0.0)'
] ]
assert ns['entry_points'] == { assert ns['entry_points'] == {
'console_scripts': ['my-script = my_package:main'] 'console_scripts': ['my-script = my_package:main']
......
...@@ -53,3 +53,18 @@ def test_accepts_fails_with_python_versions_mismatch(): ...@@ -53,3 +53,18 @@ def test_accepts_fails_with_python_versions_mismatch():
package.python_versions = '~3.5' package.python_versions = '~3.5'
assert not dependency.accepts(package) assert not dependency.accepts(package)
def test_to_pep_508():
dependency = Dependency('Django', '^1.23')
result = dependency.to_pep_508()
assert result == 'Django (>=1.23.0.0,<2.0.0.0)'
dependency = Dependency('Django', '^1.23')
dependency.python_versions = '~2.7 || ^3.6'
result = dependency.to_pep_508()
assert result == 'Django (>=1.23.0.0,<2.0.0.0); ' \
'(python_version>="2.7.0.0" and python_version<"2.8.0.0") ' \
'or (python_version>="3.6.0.0" and python_version<"4.0.0.0")'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment