|
1
|
|
|
from coalib.bears.requirements.PackageRequirement import PackageRequirement |
|
2
|
|
|
import shutil |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class GemRequirement(PackageRequirement): |
|
6
|
|
|
""" |
|
7
|
|
|
This class is a subclass of ``PackageRequirement``, and helps specifying |
|
8
|
|
|
requirements from ``gem``, without using the manager name. |
|
9
|
|
|
""" |
|
10
|
|
|
|
|
11
|
|
|
def __init__(self, package, version="", require=""): |
|
12
|
|
|
""" |
|
13
|
|
|
Constructs a new ``GemRequirement``, using the ``PackageRequirement`` |
|
14
|
|
|
constructor. |
|
15
|
|
|
|
|
16
|
|
|
>>> pr = GemRequirement('setuptools', '19.2', 'flag') |
|
17
|
|
|
>>> pr.manager |
|
18
|
|
|
'gem' |
|
19
|
|
|
>>> pr.package |
|
20
|
|
|
'setuptools' |
|
21
|
|
|
>>> pr.version |
|
22
|
|
|
'19.2' |
|
23
|
|
|
>>> pr.require |
|
24
|
|
|
'flag' |
|
25
|
|
|
|
|
26
|
|
|
:param package: A string with the name of the package to be installed. |
|
27
|
|
|
:param version: A version string. Leave empty to specify latest version. |
|
28
|
|
|
:param require: A string that specifies any additional flags, that |
|
29
|
|
|
would be used with ``require``. |
|
30
|
|
|
""" |
|
31
|
|
|
PackageRequirement.__init__(self, 'gem', package, version) |
|
32
|
|
|
self.require = require |
|
33
|
|
|
|
|
34
|
|
|
def install_command(self): |
|
35
|
|
|
""" |
|
36
|
|
|
Creates the installation command for the instance of the class. |
|
37
|
|
|
|
|
38
|
|
|
>>> pr = GemRequirement('scss_lint', '', 'false') |
|
39
|
|
|
>>> pr.install_command() |
|
40
|
|
|
'gem install "scss_lint", require: false' |
|
41
|
|
|
|
|
42
|
|
|
>>> pr = GemRequirement('rubocop') |
|
43
|
|
|
>>> pr.install_command() |
|
44
|
|
|
'gem install "rubocop"' |
|
45
|
|
|
|
|
46
|
|
|
:param return: A string with the installation command. |
|
47
|
|
|
""" |
|
48
|
|
|
if self.require: |
|
49
|
|
|
return 'gem install "{}", require: {}'.format(self.package, |
|
50
|
|
|
self.require) |
|
51
|
|
|
return 'gem install "{}"'.format(self.package) |
|
52
|
|
|
|
|
53
|
|
|
def is_installed(self): |
|
54
|
|
|
""" |
|
55
|
|
|
Checks if the dependency is installed. |
|
56
|
|
|
|
|
57
|
|
|
>>> pr = GemRequirement('rubocop') |
|
58
|
|
|
>>> pr.is_installed() |
|
59
|
|
|
True |
|
60
|
|
|
|
|
61
|
|
|
>>> pr = GemRequirement('some_package') |
|
62
|
|
|
>>> pr.is_installed() |
|
63
|
|
|
False |
|
64
|
|
|
|
|
65
|
|
|
:param return: True if dependency is installed, false otherwise. |
|
66
|
|
|
""" |
|
67
|
|
|
if shutil.which(self.package): |
|
68
|
|
|
return True |
|
69
|
|
|
else: |
|
70
|
|
|
return False |
|
71
|
|
|
|