-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
pypi_lib.py
62 lines (47 loc) · 1.55 KB
/
pypi_lib.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
import subprocess
import os
from typing import List
import sys
from ray_release.aws import get_secret_token
AWS_SECRET_TEST_PYPI = "ray_ci_test_pypi_token"
AWS_SECRET_PYPI = "ray_ci_pypi_token"
bazel_workspace_dir = os.environ.get("BUILD_WORKSPACE_DIRECTORY", "")
def _check_pypi_env(pypi_env: str) -> None:
if pypi_env not in ["test", "prod"]:
raise ValueError(f"Invalid pypi_env: {pypi_env}")
def _get_pypi_url(pypi_env: str) -> str:
_check_pypi_env(pypi_env)
if pypi_env == "test":
return "https://test.pypi.org/legacy/"
return "https://upload.pypi.org/legacy/"
def _get_pypi_token(pypi_env: str) -> str:
_check_pypi_env(pypi_env)
if pypi_env == "test":
return get_secret_token(AWS_SECRET_TEST_PYPI)
return get_secret_token(AWS_SECRET_PYPI)
def _call_subprocess(command: List[str]):
subprocess.run(
command,
check=True,
)
def upload_wheels_to_pypi(pypi_env: str, directory_path: str) -> None:
directory_path = os.path.join(bazel_workspace_dir, directory_path)
pypi_url = _get_pypi_url(pypi_env)
pypi_token = _get_pypi_token(pypi_env)
wheels = os.listdir(directory_path)
for wheel in wheels:
wheel_path = os.path.join(directory_path, wheel)
cmd = [
sys.executable,
"-m",
"twine",
"upload",
"--repository-url",
pypi_url,
"--username",
"__token__",
"--password",
pypi_token,
wheel_path,
]
_call_subprocess(cmd)