summaryrefslogtreecommitdiff
path: root/src/verbs/sync.py
blob: 95c96f1ba9e8c65465c5316ec5b0c73d22ef7077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import requests

TEMP_DIR = "/tmp/xipkg"

def curl(url):
    r = requests.get(url)
    return r.status_code, r.text

def mkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)

def download_repo(output, url):
    pkg_list_url = url + "/packages.txt"

    status, response = curl(pkg_list_url)
    if status == 404:
        print("repo does not exist at", pkg_list_url)
    else:
        packages = response.split("\n")
        for package in packages:
            if len(package) > 0:
                pkg_url = url + "/" + package
                status, package_info = curl(pkg_url)

                if status == 200:
                    with open(os.path.join(output, package), "w") as file:
                        file.write(package_info)
                else:
                    print("package is missing at", pkg_url)

# have separate list and download methods for each scheme
def sync_package_infos(source_name, url, repos):

    source_dir = os.path.join(TEMP_DIR, source_name)

    scheme = url.split(":")[0]
    
    print(url)
    # TODO: add ftp
    if scheme.startswith("http"):
        sync_func = download_repo
    else:
        # Assume its a location on the file system
        sync_func = copy_repo

    for repo in repos:
        out = os.path.join(TEMP_DIR, repo)
        mkdir(out)
        sync_func(out, url + repo if url[-1] == "/" else f"/{repo}")

        
def sync(options, config):
    sources = config["sources"]
    repos = config["repos"]

    mkdir(TEMP_DIR)
    for source, url in sources.items():
        sync_package_infos(source, url, repos)
    print("Synced!")