Passed
Push — master ( 13afb2...a9e5f8 )
by Emmanuel
05:18
created

stakkr.services.install()   B

Complexity

Conditions 5

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 19
nop 2
dl 0
loc 23
ccs 0
cts 19
cp 0
crap 30
rs 8.9833
c 0
b 0
f 0
1
# coding: utf-8
2
"""
3
Manage Stakkr Packages (extra packages)
4
"""
5
6
__github_url__ = 'https://github.com/stakkr-org'
7
8
9
def install(services_dir: str, package: str):
10
    from os.path import isdir
11
    from urllib.parse import urlparse
12
13
    url = '{}/services-{}.git'.format(__github_url__, package)
14
    if urlparse(package).scheme != '':
15
        url = package
16
17
    path = '{}/{}'.format(services_dir, package)
18
    if isdir(path):
19
        msg = 'Package "{}" is already installed, updating'.format(package)
20
        update_package(path)
21
        return True, msg
22
23
    try:
24
        from git import Repo, exc
25
        Repo.clone_from(url, path)
26
27
        return True, None
28
    except ImportError as error:
29
        return False, 'Make sure git is installed'
30
    except exc.GitCommandError as error:
31
        return False, "Couldn't clone {} ({})".format(url, error)
32
33
34
def update_all(services_dir: str):
35
    from os import listdir
36
37
    for folder in listdir(services_dir):
38
        path = services_dir + '/' + folder
39
        update_package(path)
40
41
42
def update_package(path: str):
43
    from git import Repo, exc
44
45
    try:
46
        repo = Repo(path)
47
        if repo.remotes.origin.url.startswith(__github_url__):
48
            repo.remotes.origin.pull()
49
    except exc.InvalidGitRepositoryError:
50
        pass
51