|
18 | 18 | # under the License.
|
19 | 19 | #
|
20 | 20 |
|
21 |
| -import sys, json, urllib.request, os, shutil, zipfile, tempfile |
| 21 | +import sys, requests, os, zipfile, tempfile |
22 | 22 | from pathlib import Path
|
23 | 23 |
|
24 | 24 | if len(sys.argv) != 3:
|
|
39 | 39 | dest_path = sys.argv[2]
|
40 | 40 |
|
41 | 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")) |
| 42 | +headers={'Accept': ACCEPT_HEADER, 'Authorization': 'Bearer ' + GITHUB_TOKEN} |
| 43 | + |
| 44 | +with requests.get(workflow_run_url, headers=headers) as response: |
| 45 | + response.raise_for_status() |
| 46 | + data = response.json() |
46 | 47 | for artifact in data['artifacts']:
|
47 | 48 | name = artifact['name']
|
48 | 49 | # Skip debug artifact
|
49 | 50 | if name.endswith("-Debug"):
|
50 | 51 | continue
|
| 52 | + dest_dir = os.path.join(dest_path, name) |
| 53 | + if name.find("windows") >= 0 and os.path.exists(dest_dir + ".tar.gz"): |
| 54 | + print(f'Skip downloading {name} since {dest_dir}.tar.gz exists') |
| 55 | + continue |
| 56 | + if os.path.exists(dest_dir) and \ |
| 57 | + (os.path.isfile(dest_dir) or len(os.listdir(dest_dir)) > 0): |
| 58 | + print(f'Skip downloading {name} since the directory exists') |
| 59 | + continue |
51 | 60 | url = artifact['archive_download_url']
|
52 | 61 |
|
53 | 62 | print('Downloading %s from %s' % (name, url))
|
54 |
| - artifact_request = urllib.request.Request(url, |
55 |
| - headers={'Authorization': 'Bearer ' + GITHUB_TOKEN}) |
56 |
| - with urllib.request.urlopen(artifact_request) as response: |
| 63 | + with requests.get(url, headers=headers, stream=True) as response: |
57 | 64 | tmp_zip = tempfile.NamedTemporaryFile(delete=False)
|
58 | 65 | try:
|
59 |
| - # |
60 |
| - shutil.copyfileobj(response, tmp_zip) |
| 66 | + for chunk in response.iter_content(chunk_size=8192): |
| 67 | + tmp_zip.write(chunk) |
61 | 68 | tmp_zip.close()
|
62 | 69 |
|
63 |
| - dest_dir = os.path.join(dest_path, name) |
64 | 70 | Path(dest_dir).mkdir(parents=True, exist_ok=True)
|
65 | 71 | with zipfile.ZipFile(tmp_zip.name, 'r') as z:
|
66 | 72 | z.extractall(dest_dir)
|
67 | 73 | finally:
|
68 | 74 | os.unlink(tmp_zip.name)
|
69 |
| - |
70 |
| - |
71 |
| - |
|
0 commit comments