gitlab-extract.py 2.83 KB
Newer Older
1
#!/usr/bin/env python3
2
import argparse, pprint, subprocess, sys
3
import requests
4
import parse_log
5

6
7
8
9
10
11
def last(it):
    r = first(it) # errors out if it is empty
    for i in it:
        r = i
    return r

12
13
14
15
16
17
18
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)
19
    return requests.get(url, headers={'PRIVATE-TOKEN': args.private_token})
20
21

# read command-line arguments
22
parser = argparse.ArgumentParser(description='Extract iris-coq build logs from GitLab')
23
24
parser.add_argument("-t", "--private-token",
                    dest="private_token", required=True,
25
                    help="The private token used to authenticate access.")
26
27
parser.add_argument("-s", "--server",
                    dest="server", default="https://gitlab.mpi-sws.org/",
28
                    help="The GitLab server to contact.")
29
parser.add_argument("-p", "--project",
30
                    dest="project", default="FP/iris-coq",
31
32
33
34
35
36
37
                    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.")
38
args = parser.parse_args()
39
log_file = sys.stdout if args.file == "-" else open(args.file, "a")
40

41
42
43
44
45
46
47
# determine commit, if missing
if args.commits is None:
    if args.file == "-":
        raise Exception("If you do not give explicit commits, you have to give a logfile so that we can determine the missing commits.")
    last_result = last(parse_log.parse(open(args.file, "r"), []))
    args.commits = "{}..origin/master".format(last_result.commit)

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

51
52
53
54
55
56
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")
57
for commit in reversed(commits.strip().split('\n')):
58
    print("Fetching {}...".format(commit))
59
60
61
62
63
64
65
66
    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
67
    build_times = requests.get("{}/builds/{}/artifacts/file/build-time.txt".format(project['web_url'], build['id']))
68
69
70
71
72
    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)