forked from conda-forge/admin-requests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_reset.py
More file actions
190 lines (154 loc) · 5.79 KB
/
token_reset.py
File metadata and controls
190 lines (154 loc) · 5.79 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import sys
import glob
import requests
import subprocess
import tempfile
SMITHY_CONF = os.path.expanduser('~/.conda-smithy')
def get_token_reset_files():
return (
[f for f in glob.glob("token_reset/*") if f != "token_reset/example.txt"]
)
def feedstock_token_exists(organization, name):
r = requests.get(
"https://api.github.com/repos/%s/"
"feedstock-tokens/contents/tokens/%s.json" % (organization, name),
headers={"Authorization": "token %s" % os.environ["GITHUB_TOKEN"]},
)
if r.status_code != 200:
return False
else:
return True
def write_token(name, token):
with open(os.path.join(SMITHY_CONF, name + '.token'), 'w') as fh:
fh.write(token)
def delete_feedstock_token(org, feedstock_name):
with tempfile.TemporaryDirectory() as tmpdir:
subprocess.check_call(
"git clone https://${GITHUB_TOKEN}@github.com/conda-forge/"
"feedstock-tokens.git",
cwd=tmpdir,
shell=True,
)
subprocess.check_call(
"git remote set-url --push origin "
"https://${GITHUB_TOKEN}@github.com/conda-forge/"
"feedstock-tokens.git",
cwd=os.path.join(tmpdir, "feedstock-tokens"),
shell=True,
)
subprocess.check_call(
"git rm tokens/%s.json" % feedstock_name,
cwd=os.path.join(tmpdir, "feedstock-tokens"),
shell=True,
)
subprocess.check_call(
"git commit --allow-empty -am "
"'[ci skip] [skip ci] [cf admin skip] ***NO_CI*** removing "
"token for %s'" % feedstock_name,
cwd=os.path.join(tmpdir, "feedstock-tokens"),
shell=True,
)
subprocess.check_call(
"git pull",
cwd=os.path.join(tmpdir, "feedstock-tokens"),
shell=True,
)
subprocess.check_call(
"git push",
cwd=os.path.join(tmpdir, "feedstock-tokens"),
shell=True,
)
def reset_feedstock_token(name):
owner_info = ['--organization', 'conda-forge']
with tempfile.TemporaryDirectory() as tmpdir:
feedstock_dir = os.path.join(tmpdir, name + "-feedstock")
os.makedirs(feedstock_dir)
if feedstock_token_exists("conda-forge", name + "-feedstock"):
delete_feedstock_token("conda-forge", name + "-feedstock")
subprocess.check_call(
['conda', 'smithy', 'generate-feedstock-token',
'--feedstock_directory', feedstock_dir] + owner_info)
subprocess.check_call(
['conda', 'smithy', 'register-feedstock-token',
'--feedstock_directory', feedstock_dir] + owner_info)
subprocess.check_call(
['conda', 'smithy', 'rotate-binstar-token',
'--without-appveyor',
'--token_name', 'STAGING_BINSTAR_TOKEN'],
cwd=feedstock_dir)
def reset_feedstock_tokens_in_file(token_reset_file):
pkgs_to_do_again = []
with open(token_reset_file, "r") as fp:
for line in fp.readlines():
line = line.strip()
if line.startswith("#") or len(line) == 0:
continue
try:
reset_feedstock_token(line)
except Exception as e:
print(
"failed to reset token for '%s': %s" % (line, repr(e)),
flush=True,
)
pkgs_to_do_again.append(line)
if pkgs_to_do_again:
with open(token_reset_file, "w") as fp:
fp.write(
"# token reset failed for these packages - trying again later\n"
)
for pkg in pkgs_to_do_again:
fp.write(pkg + "\n")
subprocess.check_call(f"git add {token_reset_file}", shell=True)
subprocess.check_call(
f"git commit -m 'Keeping {token_reset_file} "
"after failed token reset'",
shell=True,
)
else:
subprocess.check_call(f"git rm {token_reset_file}", shell=True)
subprocess.check_call(
f"git commit -m 'Remove {token_reset_file} after token reset'",
shell=True,
)
subprocess.check_call("git show", shell=True)
def check_for_feedstocks_in_file(token_reset_file):
missing_feedstocks = []
with open(token_reset_file, "r") as fp:
for line in fp.readlines():
line = line.strip()
if line.startswith("#") or len(line) == 0:
continue
r = requests.get("https://github.com/conda-forge/%s-feedstock" % line)
if r.status_code != 200:
missing_feedstocks.append(line)
return missing_feedstocks
def main():
mode = sys.argv[1]
if mode == "reset":
if not os.path.exists(SMITHY_CONF):
os.makedirs(SMITHY_CONF, exist_ok=True)
for token_fname, token_name in [
("circle", "CIRCLE_TOKEN"),
("azure", "AZURE_TOKEN"),
("drone", "DRONE_TOKEN"),
("travis", "TRAVIS_TOKEN"),
("github", "GITHUB_TOKEN"),
("anaconda", "STAGING_BINSTAR_TOKEN"),
]:
if token_name in os.environ:
write_token(token_fname, os.environ[token_name])
token_reset_files = get_token_reset_files()
missing_feedstocks = []
for token_reset_file in token_reset_files:
print("working on file %s" % token_reset_file, flush=True)
if mode == "reset":
reset_feedstock_tokens_in_file(token_reset_file)
else:
missing_feedstocks.extend(
check_for_feedstocks_in_file(token_reset_file)
)
if missing_feedstocks:
raise RuntimeError("feedstocks %s could not be found!" % missing_feedstocks)
if __name__ == "__main__":
main()