-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathget_pypi_token.py
40 lines (35 loc) · 1.38 KB
/
get_pypi_token.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
#!/usr/bin/env python
# If PYPI_TOKEN is in the environment, output it to .secrets file
import os
PYPI_TOKEN = ""
# If the PYPI_TOKEN is in the .secrets file, and the user doesn't add the --update flag, we're done
if os.path.exists(".secret") and "--update" not in os.sys.argv:
with open(".secret", "r") as f:
lines = f.readlines()
for line in lines:
if line.startswith("PYPI_TOKEN"):
print("PYPI_TOKEN already in .secrets file, exiting")
exit(0)
# First check if the PYPI_TOKEN is in the environment
if "PYPI_TOKEN" in os.environ:
PYPI_TOKEN = os.environ["PYPI_TOKEN"]
# If not, check if it's in the .env file
elif os.path.exists(".env"):
with open(".env", "r") as f:
lines = f.readlines()
for line in lines:
if line.startswith("PYPI_TOKEN"):
PYPI_TOKEN = line.split("=")[1].strip()
# If we found the PYPI_TOKEN, write it to the .secret file, and delete the line if it's already there
if PYPI_TOKEN:
with open(".secret", "+tw") as f:
if os.path.exists(".secret"):
lines = f.readlines()
for line in lines:
if line.startswith("PYPI_TOKEN="):
lines.remove(line)
lines.append(f"PYPI_TOKEN={PYPI_TOKEN}")
f.writelines(lines)
else:
print("PYPI_TOKEN not found in environment or .env file")
exit(1)