|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | + |
| 21 | +import sys, json, urllib.request, os, shutil, zipfile, tempfile |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +if len(sys.argv) != 3: |
| 25 | + print("Usage: ") |
| 26 | + print(" %s $WORKFLOW_RUN_ID $DEST_PATH" % sys.argv[0]) |
| 27 | + sys.exit(-1) |
| 28 | + |
| 29 | +if 'GITHUB_TOKEN' not in os.environ: |
| 30 | + print('You should have a GITHUB_TOKEN environment variable') |
| 31 | + sys.exit(-1) |
| 32 | + |
| 33 | +GITHUB_TOKEN = os.environ['GITHUB_TOKEN'] |
| 34 | + |
| 35 | +ACCEPT_HEADER = 'application/vnd.github+json' |
| 36 | +LIST_URL = 'https://api.github.com/repos/apache/pulsar-client-cpp/actions/runs/%d/artifacts' |
| 37 | + |
| 38 | +workflow_run_id = int(sys.argv[1]) |
| 39 | +dest_path = sys.argv[2] |
| 40 | + |
| 41 | +workflow_run_url = LIST_URL % workflow_run_id |
| 42 | +request = urllib.request.Request(workflow_run_url, |
| 43 | + headers={'Accept': ACCEPT_HEADER, 'Authorization': 'Bearer ' + GITHUB_TOKEN}) |
| 44 | +with urllib.request.urlopen(request) as response: |
| 45 | + data = json.loads(response.read().decode("utf-8")) |
| 46 | + for artifact in data['artifacts']: |
| 47 | + name = artifact['name'] |
| 48 | + url = artifact['archive_download_url'] |
| 49 | + |
| 50 | + print('Downloading %s from %s' % (name, url)) |
| 51 | + artifact_request = urllib.request.Request(url, |
| 52 | + headers={'Authorization': 'Bearer ' + GITHUB_TOKEN}) |
| 53 | + with urllib.request.urlopen(artifact_request) as response: |
| 54 | + tmp_zip = tempfile.NamedTemporaryFile(delete=False) |
| 55 | + try: |
| 56 | + # |
| 57 | + shutil.copyfileobj(response, tmp_zip) |
| 58 | + tmp_zip.close() |
| 59 | + |
| 60 | + dest_dir = os.path.join(dest_path, name) |
| 61 | + Path(dest_dir).mkdir(parents=True, exist_ok=True) |
| 62 | + with zipfile.ZipFile(tmp_zip.name, 'r') as z: |
| 63 | + z.extractall(dest_dir) |
| 64 | + finally: |
| 65 | + os.unlink(tmp_zip.name) |
| 66 | + |
| 67 | + |
| 68 | + |
0 commit comments