Completed
Pull Request — master (#2397)
by Zatreanu
02:09
created

NpmRequirement   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 50
loc 50
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 17 17 1
A install_command() 15 15 2
A is_installed() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from coalib.bears.requirements.PackageRequirement import PackageRequirement
2
from coalib.misc.Shell import call_without_output
3
import platform
4
5
6 View Code Duplication
class NpmRequirement(PackageRequirement):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
    """
8
    This class is a subclass of ``PackageRequirement``, and helps specifying
9
    requirements from ``npm``, without using the manager name.
10
    """
11
12
    def __init__(self, package, version=""):
13
        """
14
        Constructs a new ``NpmRequirement``, using the ``PackageRequirement``
15
        constructor.
16
17
        >>> pr = NpmRequirement('ramllint', '6.2')
18
        >>> pr.manager
19
        'npm'
20
        >>> pr.package
21
        'ramllint'
22
        >>> pr.version
23
        '6.2'
24
25
        :param package: A string with the name of the package to be installed.
26
        :param version: A version string. Leave empty to specify latest version.
27
        """
28
        PackageRequirement.__init__(self, 'npm', package, version)
29
30
    def install_command(self):
31
        """
32
        Creates the installation command for the instance of the class.
33
34
        >>> NpmRequirement('alex', '2').install_command()
35
        'npm install alex@2'
36
37
        >>> NpmRequirement('alex').install_command()
38
        'npm install alex'
39
40
        :param return: A string with the installation command.
41
        """
42
        if self.version:
43
            return "npm install {}@{}".format(self.package, self.version)
44
        return "npm install {}".format(self.package)
45
46
    def is_installed(self):
47
        """
48
        Checks if the dependency is installed.
49
50
        :param return: True if dependency is installed, false otherwise.
51
        """
52
        cmd = ['npm', 'show', self.package]
53
        if platform.system() == 'Windows':
54
            cmd = ['cmd', '/c'] + cmd
55
        return not call_without_output(cmd)
56