|
1
|
|
|
from coalib.bears.requirements.PackageRequirement import PackageRequirement |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class PythonRequirement(PackageRequirement): |
|
5
|
|
|
""" |
|
6
|
|
|
This class is a subclass of ``PackageRequirement``, and helps specifying |
|
7
|
|
|
requirements from ``pip``, without using the manager name. |
|
8
|
|
|
""" |
|
9
|
|
|
|
|
10
|
|
|
def __init__(self, package, version=""): |
|
11
|
|
|
""" |
|
12
|
|
|
Constructs a new ``PythonRequirement``, using the ``PackageRequirement`` |
|
13
|
|
|
constructor. |
|
14
|
|
|
|
|
15
|
|
|
>>> pr = PythonRequirement('setuptools', '19.2') |
|
16
|
|
|
>>> pr.manager |
|
17
|
|
|
'pip' |
|
18
|
|
|
>>> pr.package |
|
19
|
|
|
'setuptools' |
|
20
|
|
|
>>> pr.version |
|
21
|
|
|
'19.2' |
|
22
|
|
|
|
|
23
|
|
|
:param package: A string with the name of the package to be installed. |
|
24
|
|
|
:param version: A version string. Leave empty to specify latest version. |
|
25
|
|
|
""" |
|
26
|
|
|
PackageRequirement.__init__(self, 'pip', package, version) |
|
27
|
|
|
|
|
28
|
|
|
@classmethod |
|
29
|
|
|
def multiple(cls, *args): |
|
30
|
|
|
""" |
|
31
|
|
|
Creates a tuple of multiple ``PythonRequirements``. |
|
32
|
|
|
|
|
33
|
|
|
You should use the ``multiple`` method if you have more |
|
34
|
|
|
requirements from the same manager. This can receive both tuples of |
|
35
|
|
|
strings, in case you want a specific version, or a simple string, in |
|
36
|
|
|
case you want the latest version to be specified. |
|
37
|
|
|
|
|
38
|
|
|
This is the case where you would provide strings only, to specify the |
|
39
|
|
|
latest version automatically: |
|
40
|
|
|
|
|
41
|
|
|
>>> REQUIREMENTS = PythonRequirement.multiple( |
|
42
|
|
|
... 'coala_decorators', 'setuptools') |
|
43
|
|
|
|
|
44
|
|
|
And if you choose to mix them, specifying version for some and for some |
|
45
|
|
|
not: |
|
46
|
|
|
|
|
47
|
|
|
>>> REQUIREMENTS = PythonRequirement.multiple( |
|
48
|
|
|
... 'coala_decorators', ('setuptools', '19.2')) |
|
49
|
|
|
|
|
50
|
|
|
In case you provide too many arguments into the tuple, an error will be |
|
51
|
|
|
raised: |
|
52
|
|
|
|
|
53
|
|
|
>>> REQUIREMENTS = PythonRequirement.multiple( |
|
54
|
|
|
... 'coala_decorators', ('setuptools', '19.2', 'colorama')) |
|
55
|
|
|
Traceback (most recent call last): |
|
56
|
|
|
... |
|
57
|
|
|
TypeError: The tuple must have 2 elements. |
|
58
|
|
|
|
|
59
|
|
|
The same would happen in case you provide something different than a |
|
60
|
|
|
string or a tuple: |
|
61
|
|
|
|
|
62
|
|
|
>>> x = [1, 2, 3, 4] |
|
63
|
|
|
>>> REQUIREMENTS = PythonRequirement.multiple(x) |
|
64
|
|
|
Traceback (most recent call last): |
|
65
|
|
|
... |
|
66
|
|
|
TypeError: The arguments need to be tuples or strings. |
|
67
|
|
|
|
|
68
|
|
|
:param args: Should be tuples of strings: ``('packageName', |
|
69
|
|
|
'version')`` or strings: ``'packageName'`` if latest |
|
70
|
|
|
version is wanted. |
|
71
|
|
|
:return: A tuple containing ``PythonRequirements``. |
|
72
|
|
|
:raises TypeError: In case the tuples contain more or less than two |
|
73
|
|
|
elements. Also raised when arguments are neither |
|
74
|
|
|
tuples nor strings. |
|
75
|
|
|
""" |
|
76
|
|
|
reqs = [] |
|
77
|
|
|
for requirement in args: |
|
78
|
|
|
if isinstance(requirement, str): |
|
79
|
|
|
reqs.append(cls(requirement),) |
|
80
|
|
|
elif isinstance(requirement, tuple): |
|
81
|
|
|
try: |
|
82
|
|
|
name, version = requirement |
|
83
|
|
|
reqs.append(cls(name, version),) |
|
84
|
|
|
except ValueError: |
|
85
|
|
|
raise TypeError('The tuple must have 2 elements.') |
|
86
|
|
|
else: |
|
87
|
|
|
raise TypeError('The arguments need to be tuples or strings.') |
|
88
|
|
|
return tuple(reqs) |
|
89
|
|
|
|