|
| 1 | +import requests, zipfile, io, os |
| 2 | +from datetime import date |
| 3 | +import argparse |
| 4 | + |
| 5 | + |
| 6 | +def downloadArchives(github_token, owner, repo, workflow_file, dest_directory, numbers=2, branch="master", event="schedule", os_names=['ubuntu', 'macos'] ): |
| 7 | + #TODO Add OS parameter which is a list of OS we want to download the binaries from. This is to enable to download those from Windows |
| 8 | + |
| 9 | + HEADERS = { |
| 10 | + "Authorization": f"Bearer {github_token}", |
| 11 | + "Accept": "application/vnd.github.v3+json" |
| 12 | + } |
| 13 | + |
| 14 | + url = f'https://api.github.com/repos/{owner}/{repo}/actions/workflows/{workflow_file}/runs?per_page={numbers}&branch={branch}&event={event}&status=success' |
| 15 | + print(f'Requesting from url {url}') |
| 16 | + res = requests.get(url) |
| 17 | + JS = res.json() |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + absDestPath = os.path.abspath(dest_directory) |
| 22 | + |
| 23 | + if int(JS['total_count']) != numbers: |
| 24 | + raise ValueError("Not enough binaries found") |
| 25 | + |
| 26 | + cat = ['latest', 'previous'] |
| 27 | + if numbers>2: |
| 28 | + cat = [*cat, *( f'old_{i}' for i in range(numbers -2))] |
| 29 | + |
| 30 | + |
| 31 | + binaries_adress = [] |
| 32 | + binaries_JS = [] |
| 33 | + for i in range(numbers): |
| 34 | + binaries_adress.append(JS['workflow_runs'][i]['artifacts_url']) |
| 35 | + binaries_JS.append(requests.get(binaries_adress[i]).json()) |
| 36 | + for j in range(int(binaries_JS[i]['total_count'])): |
| 37 | + if 'binaries_' in binaries_JS[i]['artifacts'][j]['name']: |
| 38 | + osName = binaries_JS[i]['artifacts'][j]['name'].split('-')[1].split('_')[0] |
| 39 | + if osName in os_names: |
| 40 | + binaryName = '_'.join(binaries_JS[i]['artifacts'][j]['name'].split('-')[1].split('_')[:2]) |
| 41 | + binaryAdress = binaries_JS[i]['artifacts'][j]['archive_download_url'] |
| 42 | + binaryCreaterDate = date.fromisoformat(binaries_JS[i]['artifacts'][j]['updated_at'].split('T')[0]) |
| 43 | + binaryExpiredDate = date.fromisoformat(binaries_JS[i]['artifacts'][j]['expires_at'].split('T')[0]) |
| 44 | + |
| 45 | + extract_dir = f"{absDestPath}/{cat[i]}/{binaryName}" |
| 46 | + if not os.path.isdir(extract_dir): |
| 47 | + os.makedirs(extract_dir) |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + print(f' - Found {cat[i]} binaries for OS {osName} at adress {binaryAdress}') |
| 52 | + print(f' - Binaries are {(date.today() - binaryCreaterDate).days} days old and will expire in {(binaryExpiredDate - date.today()).days} days.') |
| 53 | + print(f' - Downloading ZIP...') |
| 54 | + r = requests.get(binaryAdress,headers=HEADERS) |
| 55 | + if r.ok : |
| 56 | + z = zipfile.ZipFile(io.BytesIO(r.content)) |
| 57 | + print(f' - Extracting it into {extract_dir}...') |
| 58 | + z.extractall(extract_dir) |
| 59 | + else: |
| 60 | + print(f'ERROR : Request returned with error code {r.status_code}') |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__" : |
| 65 | + parser = argparse.ArgumentParser() |
| 66 | + parser.add_argument('github_token') |
| 67 | + parser.add_argument('dest_directory') |
| 68 | + parser.add_argument('--os', nargs='+', default=['ubuntu', 'macos', 'windows']) |
| 69 | + parser.add_argument('-n', dest='numbers', default=2, type=int) |
| 70 | + parser.add_argument('-b', dest='branch', default='master') |
| 71 | + parser.add_argument('-e', dest='event', default='schedule') |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + downloadArchives(args.github_token, "sofa-framework", "sofa", "nightly-generate-binaries.yml", args.dest_directory, args.numbers, args.branch, args.event, args.os) |
| 75 | + |
0 commit comments