summaryrefslogtreecommitdiff
path: root/.gitlab/rel_eng/fetch-gitlab-artifacts/fetch_gitlab.py
blob: 256a5cc5d42d589c45a39d1023d8fea7f536c461 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import logging
from pathlib import Path
import subprocess
import gitlab
import json

logging.basicConfig(level=logging.INFO)

def strip_prefix(s, prefix):
    if s.startswith(prefix):
        return s[len(prefix):]
    else:
        return None

def job_triple(job_name):
    bindists = {
        'release-x86_64-windows-release': 'x86_64-unknown-mingw32',
        'release-x86_64-windows-int_native-release': 'x86_64-unknown-mingw32-int_native',
        'release-x86_64-ubuntu20_04-release': 'x86_64-ubuntu20_04-linux',
        'release-x86_64-linux-fedora33-release+debug_info': 'x86_64-fedora33-linux-dwarf',
        'release-x86_64-linux-fedora33-release': 'x86_64-fedora33-linux',
        'release-x86_64-linux-fedora27-release': 'x86_64-fedora27-linux',
        'release-x86_64-linux-deb11-release': 'x86_64-deb11-linux',
        'release-x86_64-linux-deb10-release+debug_info': 'x86_64-deb10-linux-dwarf',
        'release-x86_64-linux-deb10-release': 'x86_64-deb10-linux',
        'release-x86_64-linux-deb9-release': 'x86_64-deb9-linux',
        'release-x86_64-linux-centos7-release': 'x86_64-centos7-linux',
        'release-x86_64-linux-alpine3_12-release+fully_static': 'x86_64-alpine3_12-linux-static',
        'release-x86_64-linux-alpine3_12-int_native-release+fully_static': 'x86_64-alpine3_12-linux-static-int_native',
        'release-x86_64-darwin-release': 'x86_64-apple-darwin',
        'release-i386-linux-deb9-release': 'i386-deb9-linux',
        'release-armv7-linux-deb10-release': 'armv7-deb10-linux',
        'release-aarch64-linux-deb10-release': 'aarch64-deb10-linux',
        'release-aarch64-darwin-release': 'aarch64-apple-darwin',

        'source-tarball': 'src',
        'package-hadrian-bootstrap-sources': 'hadrian-bootstrap-sources',
        'doc-tarball': 'docs',
        'hackage-doc-tarball': 'hackage_docs',
    }

    # Some bindists use the +no_split_sections transformer due to upstream
    # toolchain bugs.
    bindists.update({
        f'{k}+no_split_sections': v
        for k,v in bindists.items()
    })

    if job_name in bindists:
        return bindists[job_name]
    else:
        #return strip_prefix(job.name, 'validate-')
        return None

def fetch_artifacts(release: str, pipeline_id: int,
                    dest_dir: Path, gl: gitlab.Gitlab):
    dest_dir.mkdir(exist_ok=True)
    # Write the pipeline id into output directory
    with open(f"{dest_dir}/metadata.json", 'w') as out: json.dump({ "pipeline_id": pipeline_id }, out)

    proj = gl.projects.get('ghc/ghc')
    pipeline = proj.pipelines.get(pipeline_id)
    tmpdir = Path("fetch-gitlab")
    tmpdir.mkdir(exist_ok=True)
    for pipeline_job in pipeline.jobs.list(all=True):
        if len(pipeline_job.artifacts) == 0:
            logging.info(f'job {pipeline_job.name} ({pipeline_job.id}) has no artifacts')
            continue

        job = proj.jobs.get(pipeline_job.id)
        triple = job_triple(job.name)
        if triple is None:
            logging.info(f'ignoring {job.name}')
            continue

        #artifactZips = [ artifact
        #                 for artifact in job.artifacts
        #                 if artifact['filename'] == 'artifacts.zip' ]
        try:
            destdir = tmpdir / job.name
            zip_name = Path(f"{tmpdir}/{job.name}.zip")
            if not zip_name.exists() or zip_name.stat().st_size == 0:
                logging.info(f'downloading archive {zip_name} for job {job.name} (job {job.id})...')
                with open(zip_name, 'wb') as f:
                    job.artifacts(streamed=True, action=f.write)

            if zip_name.stat().st_size == 0:
                logging.info(f'artifact archive for job {job.name} (job {job.id}) is empty')
                continue


            subprocess.run(['unzip', '-bo', zip_name, '-d', destdir])
            bindist_files = list(destdir.glob('ghc*.tar.xz'))

            if job.name == 'source-tarball':
                for f in bindist_files:
                    dest = dest_dir / f.name
                    logging.info(f'extracted {job.name} to {dest}')
                    f.replace(dest)
            elif job.name == 'package-hadrian-bootstrap-sources':
                all_bootstrap_sources = destdir / 'hadrian-bootstrap-sources-all.tar.gz'
                dest = dest_dir / 'hadrian-bootstrap-sources'
                dest.mkdir()
                subprocess.run(['tar', '-xf', all_bootstrap_sources, '-C', dest])
                logging.info(f'extracted {job.name}/{all_bootstrap_sources} to {dest}')
            elif job.name == 'doc-tarball':
                dest = dest_dir / 'docs'
                dest.mkdir()
                doc_files = list(destdir.glob('*.tar.xz'))
                for f in doc_files:
                    subprocess.run(['tar', '-xf', f, '-C', dest])
                    logging.info(f'extracted docs {f} to {dest}')
                index_path = destdir / 'index.html'
                index_path.replace(dest / 'index.html')
            elif job.name == 'hackage-doc-tarball':
                dest = dest_dir / 'hackage_docs'
                logging.info(f'moved hackage_docs to {dest}')
                (destdir / 'hackage_docs').replace(dest)
            else:
                dest = dest_dir / f'ghc-{release}-{triple}.tar.xz'
                if dest.exists():
                    logging.info(f'bindist {dest} already exists')
                    continue
                if len(bindist_files) == 0:
                    logging.warn(f'Bindist does not exist')
                    continue
                bindist = bindist_files[0]
                logging.info(f'extracted {job.name} to {dest}')
                bindist.replace(dest)
        except Exception as e:
            logging.error(f'Error fetching job {job.name}: {e}')
            pass

def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--pipeline', '-p', required=True, type=int, help="pipeline id")
    parser.add_argument('--release', '-r', required=True, type=str, help="release name")
    parser.add_argument('--output', '-o', type=Path, default=Path.cwd(), help="output directory")
    parser.add_argument('--profile', '-P', default='haskell',
                        help='python-gitlab.cfg profile name')
    args = parser.parse_args()
    gl = gitlab.Gitlab.from_config(args.profile)
    fetch_artifacts(args.release, args.pipeline,
                    dest_dir=args.output, gl=gl)