|
1
|
|
|
from coala_decorators.decorators import generate_eq, generate_repr |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
@generate_eq("manager", "package", "version") |
|
5
|
|
|
@generate_repr() |
|
6
|
|
|
class PackageRequirement: |
|
7
|
|
|
""" |
|
8
|
|
|
This class helps keeping track of bear requirements. It should simply |
|
9
|
|
|
be appended to the REQUIREMENTS tuple inside the Bear class. |
|
10
|
|
|
|
|
11
|
|
|
Two ``PackageRequirements`` should always be equal if they have the same |
|
12
|
|
|
manager, package and version: |
|
13
|
|
|
|
|
14
|
|
|
>>> pr1 = PackageRequirement('pip', 'coala_decorators', '0.1.0') |
|
15
|
|
|
>>> pr2 = PackageRequirement('pip', 'coala_decorators', '0.1.0') |
|
16
|
|
|
>>> pr1 == pr2 |
|
17
|
|
|
True |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
def __init__(self, manager: str, package: str, version=""): |
|
21
|
|
|
""" |
|
22
|
|
|
Constructs a new ``PackageRequirement``. |
|
23
|
|
|
|
|
24
|
|
|
>>> pr = PackageRequirement('pip', 'colorama', '0.1.0') |
|
25
|
|
|
>>> pr.manager |
|
26
|
|
|
'pip' |
|
27
|
|
|
>>> pr.package |
|
28
|
|
|
'colorama' |
|
29
|
|
|
>>> pr.version |
|
30
|
|
|
'0.1.0' |
|
31
|
|
|
|
|
32
|
|
|
:param manager: A string with the name of the manager (pip, npm, etc). |
|
33
|
|
|
:param package: A string with the name of the package to be installed. |
|
34
|
|
|
:param version: A version string. Leave empty to specify latest version. |
|
35
|
|
|
""" |
|
36
|
|
|
self.manager = manager |
|
37
|
|
|
self.package = package |
|
38
|
|
|
self.version = version |
|
39
|
|
|
|
|
40
|
|
|
def check(self): |
|
41
|
|
|
""" |
|
42
|
|
|
Check if the requirement is satisfied. |
|
43
|
|
|
|
|
44
|
|
|
>>> PackageRequirement('pip', 'coala_decorators', '0.2.1').check() |
|
45
|
|
|
Traceback (most recent call last): |
|
46
|
|
|
... |
|
47
|
|
|
NotImplementedError |
|
48
|
|
|
|
|
49
|
|
|
:return: Returns True if satisfied, False if not. |
|
50
|
|
|
""" |
|
51
|
|
|
raise NotImplementedError |
|
52
|
|
|
|
|
53
|
|
|
@classmethod |
|
54
|
|
|
def multiple(cls, *args): |
|
55
|
|
|
""" |
|
56
|
|
|
Creates a tuple of multiple instances of a class. |
|
57
|
|
|
|
|
58
|
|
|
Should not be instances of ``PackageRequirement``, as this is an |
|
59
|
|
|
abstract class: |
|
60
|
|
|
|
|
61
|
|
|
>>> PackageRequirement.multiple(('pip', 'coala_decorators', '0.1.0'),) |
|
62
|
|
|
Traceback (most recent call last): |
|
63
|
|
|
... |
|
64
|
|
|
NotImplementedError |
|
65
|
|
|
|
|
66
|
|
|
It can only be used for requirements of the same manager. For example, |
|
67
|
|
|
consider a manager ``XYZRequirement`` that inherits from |
|
68
|
|
|
PackageRequirement. This subclass will have the manager set to XYZ: |
|
69
|
|
|
|
|
70
|
|
|
>>> class XYZRequirement(PackageRequirement): |
|
71
|
|
|
... manager = 'xyz' |
|
72
|
|
|
... def __init__(self, package, version=""): |
|
73
|
|
|
... PackageRequirement.__init__(self, package, version) |
|
74
|
|
|
|
|
75
|
|
|
This is the case where you would provide strings only, to specify the |
|
76
|
|
|
latest version automatically: |
|
77
|
|
|
|
|
78
|
|
|
>>> REQUIREMENTS = XYZRequirement.multiple( |
|
79
|
|
|
... "package1", "package2") |
|
80
|
|
|
|
|
81
|
|
|
And if you choose to mix them, specifying version for some and for some |
|
82
|
|
|
not: |
|
83
|
|
|
|
|
84
|
|
|
>>> REQUIREMENTS = XYZRequirement.multiple( |
|
85
|
|
|
... 'package1', ('package2', '2.0')) |
|
86
|
|
|
|
|
87
|
|
|
Lists are also valid arguments: |
|
88
|
|
|
|
|
89
|
|
|
>>> REQUIREMENTS = XYZRequirement.multiple( |
|
90
|
|
|
... ['package1', '1.0'],) |
|
91
|
|
|
|
|
92
|
|
|
In case you provide too many arguments into the tuple, an error will be |
|
93
|
|
|
raised: |
|
94
|
|
|
|
|
95
|
|
|
>>> REQUIREMENTS = XYZRequirement.multiple( |
|
96
|
|
|
... 'package1', ('package2', '2.0', 'package3')) |
|
97
|
|
|
Traceback (most recent call last): |
|
98
|
|
|
... |
|
99
|
|
|
TypeError: Too many elements provided. |
|
100
|
|
|
|
|
101
|
|
|
:param args: In the subclasses, the ``manager`` is already |
|
102
|
|
|
specified, so they hould be iterables with two |
|
103
|
|
|
elements: ``('packageName', 'version')`` or strings: |
|
104
|
|
|
``'packageName'`` if latest version is wanted. |
|
105
|
|
|
:return: A tuple containing instances of the subclass. |
|
106
|
|
|
:raises TypeError: In case the iterables contain more than two |
|
107
|
|
|
elements. |
|
108
|
|
|
""" |
|
109
|
|
|
if cls == PackageRequirement: |
|
110
|
|
|
raise NotImplementedError |
|
111
|
|
|
else: |
|
112
|
|
|
reqs = [] |
|
113
|
|
|
for requirement in args: |
|
114
|
|
|
if isinstance(requirement, str): |
|
115
|
|
|
reqs.append(cls(requirement)) |
|
116
|
|
|
elif len(requirement) == 2: |
|
117
|
|
|
name, version = requirement |
|
118
|
|
|
reqs.append(cls(name, version)) |
|
119
|
|
|
else: |
|
120
|
|
|
raise TypeError('Too many elements provided.') |
|
121
|
|
|
return tuple(reqs) |
|
122
|
|
|
|