Completed
Pull Request — master (#2393)
by Zatreanu
02:13
created

PipRequirement   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 55
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A install_command() 0 11 1
A __init__() 0 17 1
A is_installed() 0 19 2
1
from coalib.bears.requirements.PackageRequirement import PackageRequirement
2
import subprocess
3
4
5
class PipRequirement(PackageRequirement):
6
    """
7
    This class is a subclass of ``PackageRequirement``, and helps specifying
8
    requirements from ``pip``, without using the manager name.
9
    """
10
11
    def __init__(self, package, version=""):
12
        """
13
        Constructs a new ``PipRequirement``, using the ``PackageRequirement``
14
        constructor.
15
16
        >>> pr = PipRequirement('setuptools', '19.2')
17
        >>> pr.manager
18
        'pip'
19
        >>> pr.package
20
        'setuptools'
21
        >>> pr.version
22
        '19.2'
23
24
        :param package: A string with the name of the package to be installed.
25
        :param version: A version string. Leave empty to specify latest version.
26
        """
27
        PackageRequirement.__init__(self, 'pip', package, version)
28
29
    def install_command(self):
30
        """
31
        Creates the installation command for the instance of the class.
32
33
        >>> pr = PipRequirement('setuptools', '19.2')
34
        >>> pr.install_command()
35
        'pip install setuptools==19.2'
36
37
        :param return: A string with the installation command.
38
        """
39
        return "pip install {}=={}".format(self.package, self.version)
40
41
    def is_installed(self):
42
        """
43
        Checks if the dependency is installed.
44
45
        >>> pr = PipRequirement('pip')
46
        >>> pr.is_installed()
47
        True
48
49
        >>> pr = PipRequirement('some_package')
50
        >>> pr.is_installed()
51
        False
52
53
        :param return: True if dependency is installed, false otherwise.
54
        """
55
        if subprocess.call(['pip', 'show', self.package],
56
                           stdout=subprocess.PIPE):
57
            return False
58
        else:
59
            return True
60