-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskcluster_bins_download_aws.py
67 lines (54 loc) · 2.25 KB
/
taskcluster_bins_download_aws.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import os
import re
import boto3
from botocore.exceptions import NoCredentialsError
# GitHub API URL for latest releases
repo_owner = "taskcluster"
repo_name = "taskcluster"
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/latest"
# Directory to save the downloaded files
download_dir = "./downloads"
os.makedirs(download_dir, exist_ok=True)
# S3 bucket and path
s3_bucket = "ronin-puppet-package-repo"
s3_path = "macos/public/common/"
# Initialize S3 client
s3 = boto3.client('s3')
def download_file(url, save_path):
response = requests.get(url, stream=True)
with open(save_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
def upload_to_s3(file_path, s3_bucket, s3_key):
try:
s3.upload_file(file_path, s3_bucket, s3_key)
print(f"Uploaded {file_path} to s3://{s3_bucket}/{s3_key}")
except FileNotFoundError:
print(f"File not found: {file_path}")
except NoCredentialsError:
print("Credentials not available")
# Fetch the latest release from GitHub API
response = requests.get(url)
if response.status_code == 200:
release_data = response.json()
version = release_data['tag_name'].lstrip('v') # Strip 'v' from the version tag
# Look for assets with 'darwin' in the name and exclude '.tar.gz' files
for asset in release_data['assets']:
asset_name = asset['name']
if 'darwin' in asset_name and not asset_name.endswith('.tar.gz'):
# Replace 'darwin' with the version number
new_name = re.sub(r'darwin', version, asset_name)
# Replace 'insecure' with 'simple' in the file name
new_name = re.sub(r'insecure', 'simple', new_name)
# Build full file paths
download_url = asset['browser_download_url']
save_path = os.path.join(download_dir, new_name)
# Download and save the file
print(f"Downloading {asset_name} as {new_name}")
download_file(download_url, save_path)
# Upload the downloaded file to S3
s3_key = os.path.join(s3_path, new_name)
upload_to_s3(save_path, s3_bucket, s3_key)
else:
print(f"Failed to get release info: {response.status_code}")