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