gitlab-extract.py 2.37 KB
Newer Older
1
#!/usr/bin/env python3
2
import argparse, pprint, subprocess, sys
3
4
5
6
7
8
9
10
11
import requests

def first(it):
    for i in it:
        return i
    raise Exception("The iterator is empty")

def req(path):
    url = '%s/api/v3/%s' % (args.server, path)
12
    return requests.get(url, headers={'PRIVATE-TOKEN': args.private_token})
13
14
15
16
17

# read command-line arguments
parser = argparse.ArgumentParser(description='Update and build a bunch of stuff')
parser.add_argument("-t", "--private-token",
                    dest="private_token", required=True,
18
                    help="The private token used to authenticate access.")
19
20
parser.add_argument("-s", "--server",
                    dest="server", default="https://gitlab.mpi-sws.org/",
21
                    help="The GitLab server to contact.")
22
parser.add_argument("-p", "--project",
23
                    dest="project", default="FP/iris-coq",
24
25
26
27
28
29
30
                    help="The name of the project on GitLab.")
parser.add_argument("-f", "--file",
                    dest="file", required=True,
                    help="Filename to store the load in.")
parser.add_argument("-c", "--commits",
                    dest="commits",
                    help="The commits to fetch. Default is everything since the most recent entry in the log file.")
31
args = parser.parse_args()
32
log_file = sys.stdout if args.file == "-" else open(args.file, "a")
33
34

projects = req("projects")
35
project = first(filter(lambda p: p['path_with_namespace'] == args.project, projects.json()))
36

37
38
39
40
41
42
if args.commits.find('..') >= 0:
    # a range of commits
    commits = subprocess.check_output(["git", "rev-list", args.commits]).decode("utf-8")
else:
    # a single commit
    commits = subprocess.check_output(["git", "rev-parse", args.commits]).decode("utf-8")
43
for commit in reversed(commits.strip().split('\n')):
44
    print("Fetching {}...".format(commit))
45
46
47
48
49
50
51
52
    builds = req("/projects/{}/repository/commits/{}/builds".format(project['id'], commit))
    if builds.status_code != 200:
        continue
    try:
        build = first(sorted(builds.json(), key = lambda b: -int(b['id'])))
    except Exception:
        # no build
        continue
53
    build_times = requests.get("{}/builds/{}/artifacts/file/build-time.txt".format(project['web_url'], build['id']))
54
55
56
57
58
    if build_times.status_code != 200:
        continue
    # Output in the log file format
    log_file.write("# {}\n".format(commit))
    log_file.write(build_times.text)