|
| 1 | +import os |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +import shutil |
| 5 | +import requests |
| 6 | +import sys |
| 7 | +import argparse |
| 8 | + |
| 9 | +DILIGENT_ENGINE_URL = 'https://github.com/DiligentGraphics/DiligentEngine.git' |
| 10 | +SOURCE_IMAGES = 'images' |
| 11 | +SOURCE_TEMPLATE = 'template' |
| 12 | +GITHUB_REPOSITORY = 'DiligentGraphics/DiligentEngine' |
| 13 | + |
| 14 | +def get_latest_successful_run_id(github_token): |
| 15 | + headers = {'Authorization': f'token {github_token}'} |
| 16 | + response = requests.get(f'https://api.github.com/repos/{GITHUB_REPOSITORY}/actions/runs', headers=headers) |
| 17 | + |
| 18 | + if response.status_code == 200: |
| 19 | + runs = response.json()['workflow_runs'] |
| 20 | + |
| 21 | + successful_emscripten_runs = [ |
| 22 | + run for run in runs |
| 23 | + if run['conclusion'] == 'success' and run['name'] == 'Emscripten build' and run['event'] == 'push' |
| 24 | + ] |
| 25 | + |
| 26 | + if successful_emscripten_runs: |
| 27 | + latest_successful_run = max(successful_emscripten_runs, key=lambda run: run['created_at']) |
| 28 | + return latest_successful_run['id'] |
| 29 | + else: |
| 30 | + print('No successful "Emscripten build" workflow runs triggered by "push" found.') |
| 31 | + return None |
| 32 | + else: |
| 33 | + print(f'Failed to fetch workflow runs: {response.content}') |
| 34 | + return None |
| 35 | + |
| 36 | +def download_artifact(artifact_name, run_id, github_token, destination_dir): |
| 37 | + headers = {'Authorization': f'token {github_token}'} |
| 38 | + response = requests.get(f'https://api.github.com/repos/{GITHUB_REPOSITORY}/actions/runs/{run_id}/artifacts', headers=headers) |
| 39 | + |
| 40 | + if response.status_code == 200: |
| 41 | + artifacts = response.json()['artifacts'] |
| 42 | + for artifact in artifacts: |
| 43 | + if artifact['name'] == artifact_name: |
| 44 | + download_url = artifact['archive_download_url'] |
| 45 | + artifact_response = requests.get(download_url, headers=headers, stream=True) |
| 46 | + |
| 47 | + total_size = int(artifact_response.headers.get('content-length', 0)) |
| 48 | + block_size = 1024 * 1024 |
| 49 | + downloaded_size = 0 |
| 50 | + |
| 51 | + if not os.path.exists(destination_dir): |
| 52 | + os.makedirs(destination_dir) |
| 53 | + |
| 54 | + zip_path = os.path.join(destination_dir, f'{artifact_name}.zip') |
| 55 | + with open(zip_path, 'wb') as f: |
| 56 | + for data in artifact_response.iter_content(block_size): |
| 57 | + f.write(data) |
| 58 | + downloaded_size += len(data) |
| 59 | + |
| 60 | + done = int(50 * downloaded_size / total_size) |
| 61 | + sys.stdout.write(f'\rDownloading: [{"=" * done}{" " * (50 - done)}] {downloaded_size * 100 // total_size}%') |
| 62 | + sys.stdout.flush() |
| 63 | + |
| 64 | + print(f'\nArtifact {artifact_name} downloaded successfully.') |
| 65 | + return True |
| 66 | + print(f'Artifact {artifact_name} not found.') |
| 67 | + else: |
| 68 | + print(f'Failed to fetch artifacts: {response.content}') |
| 69 | + return False |
| 70 | + |
| 71 | +def extract_zip(zip_path, extract_to): |
| 72 | + import zipfile |
| 73 | + with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 74 | + zip_ref.extractall(extract_to) |
| 75 | + os.remove(zip_path) |
| 76 | + |
| 77 | +def upload_artefacts(destination_dir): |
| 78 | + if not os.path.exists(destination_dir): |
| 79 | + os.makedirs(destination_dir) |
| 80 | + |
| 81 | + shutil.copytree(SOURCE_IMAGES, os.path.join(destination_dir, 'images'), dirs_exist_ok=True) |
| 82 | + for item in os.listdir(SOURCE_TEMPLATE): |
| 83 | + source_item = os.path.join(SOURCE_TEMPLATE, item) |
| 84 | + destination_item = os.path.join(destination_dir, item) |
| 85 | + |
| 86 | + if os.path.isdir(source_item): |
| 87 | + shutil.copytree(source_item, destination_item, dirs_exist_ok=True) |
| 88 | + else: |
| 89 | + shutil.copy2(source_item, destination_item) |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + parser = argparse.ArgumentParser(description='Download and manage GitHub Actions artifacts.') |
| 93 | + parser.add_argument('-t', '--github_token', help='GitHub token for authentication', required=True) |
| 94 | + parser.add_argument('-d', '--destination_dir', help='Destination directory for downloaded artifacts', required=True) |
| 95 | + args = parser.parse_args() |
| 96 | + |
| 97 | + github_token = args.github_token |
| 98 | + destination_dir = args.destination_dir |
| 99 | + |
| 100 | + run_id = get_latest_successful_run_id(github_token) |
| 101 | + if run_id: |
| 102 | + print(f"Download artifacts for Build ID: {run_id}") |
| 103 | + if download_artifact('WasmModules-DiligentGraphics.github.io', run_id, github_token, destination_dir): |
| 104 | + extract_zip(f'{destination_dir}/WasmModules-DiligentGraphics.github.io.zip', f'{destination_dir}/wasm-modules') |
| 105 | + upload_artefacts(destination_dir) |
| 106 | + else: |
| 107 | + print('Could not retrieve the latest run ID.') |
0 commit comments