Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/build_config/download_artifact.py
169674 views
1
#!/usr/bin/env python3
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file download_artifact.py
15
# @author Michael Behrisch
16
# @date 2024-05-03
17
18
import argparse
19
import io
20
import zipfile
21
22
import requests
23
24
25
def request(url, token):
26
if token:
27
return requests.get(url, headers={"Authorization": "Bearer " + token})
28
return requests.get(url)
29
30
31
def get_latest_artifact_url(options):
32
prefix = "%s/repos/%s/%s/actions/" % (options.api_url, options.owner, options.repository)
33
workflow_id = None
34
response = request(prefix + "workflows", options.token)
35
for workflow in response.json()['workflows']:
36
if workflow['name'] == options.workflow:
37
workflow_id = workflow['id']
38
if workflow_id is None:
39
raise RuntimeError("Workflow '%s' not found." % options.workflow)
40
41
workflow_run_ids = []
42
response = request("%sworkflows/%s/runs" % (prefix, workflow_id), options.token)
43
for workflow_run in response.json()['workflow_runs']:
44
if options.branch == "main" and workflow_run['head_branch'][:1] == "v":
45
# there seems to be no easy way to identify a tag so we take the first letter
46
workflow_run_ids.append(workflow_run['id'])
47
for workflow_run in response.json()['workflow_runs']:
48
if (workflow_run['status'] == "completed"
49
and (options.allow_failed or workflow_run['conclusion'] == "success")
50
and workflow_run['head_branch'] == options.branch):
51
workflow_run_ids.append(workflow_run['id'])
52
break
53
if not workflow_run_ids:
54
raise RuntimeError("No successful workflow run found in branch '%s'." % options.branch)
55
56
for workflow_run_id in workflow_run_ids:
57
response = request("%sruns/%s/artifacts" % (prefix, workflow_run_id), options.token)
58
for artifact in response.json()['artifacts']:
59
if artifact['name'].startswith(options.prefix):
60
yield "%sartifacts/%s/zip" % (prefix, artifact['id'])
61
62
63
if __name__ == "__main__":
64
ap = argparse.ArgumentParser()
65
ap.add_argument("--api-url", default="https://api.github.com")
66
ap.add_argument("--owner", default="eclipse-sumo")
67
ap.add_argument("--repository", default="sumo")
68
ap.add_argument("--workflow", default="windows-wheels")
69
ap.add_argument("--branch", default="main")
70
ap.add_argument("--token", help="GitHub authentication token")
71
ap.add_argument("--directory", help="output directory")
72
ap.add_argument("--prefix", default="libsumo-python-3.", help="prefix of the artifact zip file")
73
ap.add_argument("--allow-failed", action="store_true", default=False, help="download even if the build failed")
74
ap.add_argument("-v", "--verbose", action="store_true", default=False, help="tell me more")
75
options = ap.parse_args()
76
77
for artifact_url in get_latest_artifact_url(options):
78
response = request(artifact_url, options.token)
79
if response.status_code == 200:
80
with zipfile.ZipFile(io.BytesIO(response.content)) as zip:
81
zip.extractall(options.directory)
82
if options.verbose:
83
print(artifact_url, response)
84
85