-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update-To-Latest-Versions.py
106 lines (80 loc) · 3.16 KB
/
Update-To-Latest-Versions.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import argparse
import json
import os
import zipfile
import requests
import shutil
import platform
operating_system = platform.system()
cwd = os.getcwd()
def get_releases_from_api(releases):
assets = []
for asset in releases["assets"]:
if operating_system == "Linux" and 'linux-x64' in asset['name']:
assets.append(asset)
if operating_system == "Windows" and 'win-x64' in asset['name']:
assets.append(asset)
return assets
def get_executables_for_matched_oeprating_system(repo_folder):
executables = []
for f in os.listdir(repo_folder):
if operating_system == 'Windows' and f.endswith(".exe"):
executables.append(f)
if operating_system == 'Linux' and not f.endswith(".pdb") and not f.endswith(".json") and not f.endswith(".config"):
executables.append(f)
return executables
def download_latest_release(repo, target_dir):
response = requests.get(
f'https://api.github.com/repos/CleverCodeCravers/{repo}/releases/latest')
release_info = response.json()
if "message" in release_info:
return
version_file = os.path.join(target_dir, 'versions.json')
if os.path.exists(version_file):
with open(version_file) as f:
versions = json.load(f)
if repo in versions and versions[repo] == release_info['tag_name']:
print(f'{repo} is already up to date')
return
assets = get_releases_from_api(release_info)
if not assets:
print(f'No assets found for {repo}')
return
for asset in assets:
zip_file = os.path.join(cwd, asset['name'])
repo_folder = os.path.join(cwd, zip_file.replace(".zip", ""))
with open(zip_file, 'wb') as f:
f.write(requests.get(asset['browser_download_url']).content)
with zipfile.ZipFile(zip_file, 'r') as zf:
os.mkdir(repo_folder)
zf.extractall(repo_folder)
os.remove(os.path.join(cwd, zip_file))
executing_file = get_executables_for_matched_oeprating_system(
repo_folder)
if not executing_file:
print(f'No exe files found in {zip_file}')
return
for exe in executing_file:
os.rename(os.path.join(repo_folder, exe),
os.path.join(target_dir, exe))
shutil.rmtree(repo_folder, ignore_errors=False, onerror=None)
if not os.path.exists(version_file):
versions = {}
versions[repo] = release_info['tag_name']
with open(version_file, 'w') as f:
json.dump(versions, f)
print(f'{repo} has been successfully updated')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-TargetDirectory', '--target_dir', required=True)
args = parser.parse_args()
target_dir = args.target_dir
if not os.path.exists(target_dir):
os.makedirs(target_dir)
response = requests.get(
'https://api.github.com/orgs/CleverCodeCravers/repos')
if "message" in response.json():
print(response.json()["message"])
exit()
for repo in response.json():
download_latest_release(repo["name"], target_dir)