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

stakkr.services   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 51
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
B install() 0 23 5
A update_package() 0 9 3
A update_all() 0 6 2
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