-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathinstall_savant_rs.py
executable file
·134 lines (112 loc) · 4.14 KB
/
install_savant_rs.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
import json
import os
import subprocess
import sys
from typing import AnyStr, Optional
from urllib.request import Request, urlopen
def print_usage():
"""Show usage information."""
print('Download and install savant_rs package from GitHub release.')
print(f'Usage: {sys.argv[0]} <tag> <download_dir>')
print(f'Example: {sys.argv[0]} 0.2.14 /tmp')
print('Environment variables:')
print(' GITHUB_TOKEN: GitHub personal access token')
print(' GITHUB_REPOSITORY: GitHub repo name, default insight-platform/savant-rs')
def gh_request(
repo: str, endpoint: str, content_type: str, token: Optional[str] = None
) -> AnyStr:
"""GitHub API request helper.
:param repo: Repository name (owner/repo).
:param endpoint: API endpoint.
:param content_type: Content type.
:param token: GitHub personal access token.
"""
url = f'https://api.github.com/repos/{repo}/{endpoint}'
headers = {
'Accept': content_type,
'X-GitHub-Api-Version': '2022-11-28',
}
if token is not None:
headers['Authorization'] = f'Bearer {token}'
req = Request(url, headers=headers)
res = urlopen(req)
return res.read()
def get_release_assets(tag: str, repo: str, token: Optional[str] = None) -> list:
"""Get the list of assets for a given release tag in a repository."""
release = json.loads(
gh_request(repo, f'releases/tags/{tag}', 'application/vnd.github+json', token)
)
return release.get('assets', [])
def download_asset(
asset: dict, path: str, repo: str, token: Optional[str] = None
) -> str:
"""Download an asset and save it to the specified path."""
data = gh_request(
repo, f'releases/assets/{asset["id"]}', 'application/octet-stream', token
)
asset_path = os.path.join(os.path.abspath(path), asset['name'])
os.makedirs(os.path.dirname(asset_path), exist_ok=True)
with open(asset_path, 'wb') as fp:
fp.write(data)
return asset_path
def install(package):
"""Install a package using pip."""
subprocess.check_call(
[sys.executable, '-m', 'pip', 'install', '--no-cache-dir', package]
)
def main():
python_short_version = f'cp{sys.version_info.major}{sys.version_info.minor}'
arch = os.uname().machine
if len(sys.argv) != 3:
print_usage()
sys.exit(1)
# get wheels in local_wheels
excludes = []
for name in os.listdir('local_wheels/savant_rs'):
print(f'Checking {name}')
if not name.endswith('.whl'):
print(f'Skipping {name} because it is not a .whl file.')
continue
if arch not in name:
print(
f'Skipping {name} because it does not relate to the target architecture {arch}.'
)
continue
if python_short_version not in name:
print(
f'Skipping {name} because it does not match Python version {python_short_version}.'
)
continue
print(f'Installing {name}')
install(os.path.join('local_wheels/savant_rs', name))
excludes.append(name)
gh_token = os.environ.get('GITHUB_TOKEN')
gh_repo = os.environ.get('GITHUB_REPOSITORY', 'insight-platform/savant-rs')
release_tag = sys.argv[1]
download_path = sys.argv[2]
assets = get_release_assets(release_tag, gh_repo, gh_token)
if not assets:
sys.exit(f'No assets found for tag {release_tag} in repository {gh_repo}.')
asset_path = None
for asset in assets:
name = asset['name']
if name in excludes:
continue
# if not name.startswith('savant_rs'):
# continue
if not name.endswith('.whl'):
continue
if arch not in name:
continue
if python_short_version not in name:
continue
asset_path = download_asset(asset, download_path, gh_repo, gh_token)
print(f'Downloaded {asset_path}.')
install(asset_path)
if asset_path is None and len(excludes) == 0:
sys.exit(
f'No savant_rs package found for tag {release_tag} in repository {gh_repo}.'
)
if __name__ == '__main__':
main()