Completed
Push — master ( b0ef4b...90b3ef )
by
unknown
01:51
created

JuliaRequirement   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 51
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A install_command() 0 12 1
A __init__() 0 17 1
A is_installed() 0 14 1
1
import shlex
2
3
from coalib.bears.requirements.PackageRequirement import PackageRequirement
4
from coalib.misc.Shell import call_without_output
5
6
from coala_utils.string_processing import escape
7
8
9
class JuliaRequirement(PackageRequirement):
10
    """
11
    This class is a subclass of ``PackageRequirement``, and helps specifying
12
    requirements from ``julia``, without using the manager name.
13
    """
14
15
    def __init__(self, package, version=""):
16
        """
17
        Constructs a new ``JuliaRequirement``, using the ``PackageRequirement``
18
        constructor.
19
20
        >>> pr = JuliaRequirement('Lint', '19.2')
21
        >>> pr.manager
22
        'julia'
23
        >>> pr.package
24
        'Lint'
25
        >>> pr.version
26
        '19.2'
27
28
        :param package: A string with the name of the package to be installed.
29
        :param version: A version string. Leave empty to specify latest version.
30
        """
31
        PackageRequirement.__init__(self, 'julia', package, version)
32
33
    def install_command(self):
34
        """
35
        Creates the installation command for the instance of the class.
36
37
        >>> JuliaRequirement('Lint').install_command()
38
        'julia -e \\'Pkg.add("Lint")\\''
39
40
        :return: A string with the installation command.
41
        """
42
        code = 'Pkg.add("{}")'.format(escape(self.package, '\\"'))
43
        args = ('julia', '-e', shlex.quote(code))
44
        return ' '.join(args)
45
46
    def is_installed(self):
47
        """
48
        Checks if the dependency is installed.
49
50
        :return: ``True`` if dependency is installed, ``False`` otherwise.
51
        """
52
        # We need to check explicitly if `nothing` is returned, as this happens
53
        # when the package is *registered*, but *not installed*. If it's not
54
        # even registered, julia will throw an exception which lets julia exit
55
        # with an error code different from 0.
56
        code = 'Pkg.installed("{}")==nothing?exit(1):exit(0)'.format(
57
            escape(self.package, '\\"'))
58
        args = ('julia', '-e', code)
59
        return not call_without_output(args)
60