forked from conda-forge/admin-requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive_feedstock.py
84 lines (70 loc) · 2.44 KB
/
archive_feedstock.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
import os
import sys
import time
from pathlib import Path
import requests
def raise_json_for_status(request):
try:
request.raise_for_status()
except Exception as exc:
exc.args = exc.args + (request.json(), )
raise exc.with_traceback(exc.__traceback__)
def archive_repo(owner, repo, archive=True, check_only=False):
headers = {
"X-GitHub-Api-Version": "2022-11-28",
"Accept": "application/vnd.github+json",
"User-Agent": "conda-forge/admin-requests",
"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}",
}
r = requests.get(
f"https://api.github.com/repos/{owner}/{repo}",
headers=headers,
)
raise_json_for_status(r)
target_status = f"{'' if archive else 'un'}archived"
data = r.json()
if data["archived"] == archive:
raise RuntimeError(f"{owner}/{repo} is already {target_status}!")
elif check_only:
return
r = requests.patch(
f"https://api.github.com/repos/{owner}/{repo}",
headers=headers,
json={"archived": archive}
)
raise_json_for_status(r)
def feedstocks(directory="archive"):
for path in Path(directory).glob("*.txt"):
if path.name == "example.txt":
continue
with open(path) as f:
for line in f:
line = line.strip()
if line and not line.startswith("#"):
yield line
def main(owner="conda-forge", check_only=False):
exceptions = []
for task in "archive", "unarchive":
seen = set()
for feedstock in feedstocks(task):
verb = "Archiving" if task == "archive" else "Unarchiving"
print(f"{verb} {owner}/{feedstock}-feedstock...", end=" ")
if feedstock in seen:
print("[SKIP]")
continue
seen.add(feedstock.lower())
try:
archive_repo(owner, f"{feedstock}-feedstock", archive=task == "archive", check_only=check_only)
except Exception as exc:
exceptions.append((feedstock, exc))
print("[!!]")
else:
print("[OK]")
time.sleep(0.5)
if exceptions:
raise Exception(*exceptions)
if __name__ == "__main__":
if len(sys.argv) != 2:
sys.exit("Usage: python archive_feedstock.py [check | archive]")
check_only = sys.argv[1] == "check"
main(owner="conda-forge", check_only=check_only)