Completed
Pull Request — master (#2408)
by Zatreanu
02:20
created

PipRequirement.is_installed()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
1
from coalib.bears.requirements.PackageRequirement import PackageRequirement
2
from coalib.misc.Shell import call_without_output
3
import sys
4
5
6
class PipRequirement(PackageRequirement):
7
    """
8
    This class is a subclass of ``PackageRequirement``, and helps specifying
9
    requirements from ``pip``, without using the manager name.
10
    """
11
12
    MANAGER = sys.executable + ' -m pip'
13
14
    def __init__(self, package, version=""):
15
        """
16
        Constructs a new ``PipRequirement``, using the ``PackageRequirement``
17
        constructor.
18
19
        >>> pr = PipRequirement('setuptools', '19.2')
20
        >>> pr.manager
21
        'pip'
22
        >>> pr.package
23
        'setuptools'
24
        >>> pr.version
25
        '19.2'
26
27
        :param package: A string with the name of the package to be installed.
28
        :param version: A version string. Leave empty to specify latest version.
29
        """
30
        PackageRequirement.__init__(self, 'pip', package, version)
31
32
    def install_command(self):
33
        """
34
        Creates the installation command for the instance of the class.
35
36
        :param return: A string with the installation command.
37
        """
38
        if self.version:
39
            return "{} install {}=={}".format(self.MANAGER, self.package,
40
                                              self.version)
41
        return "{} install {}".format(self.MANAGER, self.package)
42
43
    def is_installed(self):
44
        """
45
        Checks if the dependency is installed.
46
47
        :param return: True if dependency is installed, false otherwise.
48
        """
49
        return not call_without_output(('pip', 'show', self.package))
50