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