stakkr.services   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 90.24%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 64
ccs 37
cts 41
cp 0.9024
rs 10
c 0
b 0
f 0
wmc 13

4 Functions

Rating   Name   Duplication   Size   Complexity  
A update_package() 0 8 3
B install() 0 27 6
A _check_repo_exists() 0 4 2
A update_all() 0 7 2
1
# coding: utf-8
2 1
"""
3
Manage Stakkr Packages (extra packages)
4
"""
5
6 1
from git import Repo, exc
7 1
from requests import head, HTTPError
8
9 1
__github_url__ = 'https://github.com/stakkr-org'
10
11
12 1
def install(services_dir: str, package: str, name: str):
13
    """Install a specific service by cloning a repo"""
14 1
    from os.path import isdir
15 1
    from urllib.parse import urlparse
16
17 1
    url = '{}/services-{}.git'.format(__github_url__, package)
18 1
    if urlparse(package).scheme != '':
19 1
        url = package
20
21 1
    path = '{}/{}'.format(services_dir, name)
22 1
    try:
23 1
        _check_repo_exists(url)
24
25 1
        if isdir(path):
26 1
            msg = 'Package "{}" is already installed, updating'.format(package)
27 1
            update_package(path)
28 1
            return True, msg
29
30 1
        Repo.clone_from(url, path)
31
32 1
        return True, None
33 1
    except HTTPError as error:
34 1
        return False, "Can't add package: {}".format(str(error))
35
    except ImportError:
36
        return False, 'Make sure git is installed'
37
    except exc.GitCommandError as error:
38
        return False, "Couldn't clone {} ({})".format(url, error)
39
40
41 1
def update_all(services_dir: str):
42
    """Update all services by pulling"""
43 1
    from os import listdir
44
45 1
    for folder in listdir(services_dir):
46 1
        path = services_dir + '/' + folder
47 1
        update_package(path)
48
49
50 1
def update_package(path: str):
51
    """Update a single service withgit pull"""
52 1
    try:
53 1
        repo = Repo(path)
54 1
        if repo.remotes.origin.url.endswith('.git'):
55 1
            repo.remotes.origin.pull()
56 1
    except exc.InvalidGitRepositoryError:
57 1
        pass
58
59
60 1
def _check_repo_exists(repo: str):
61 1
    status_code = head(repo, allow_redirects=True).status_code
62 1
    if status_code != 200:
63
        raise HTTPError("{} is not a valid repo (status = {})".format(repo, status_code))
64