Debian Codegraph @

experiments/compression/corpus.py

raw
1 #!/usr/bin/env python
2
3 import getpass
4 import json
5 import pathlib
6 import subprocess
7 import sys
8 import time
9
10
11 def github_commits():
12 OAUTH_TOKEN = getpass.getpass("OAuth Token? ")
13 COMMIT_API = (
14 f'curl -H "Authorization: token {OAUTH_TOKEN}" '
15 f"https://api.github.com/repos/python-websockets/websockets/git/commits/:sha"
16 )
17
18 commits = []
19
20 head = subprocess.check_output(
21 "git rev-parse origin/main",
22 shell=True,
23 text=True,
24 ).strip()
25 todo = [head]
26 seen = set()
27
28 while todo:
29 sha = todo.pop(0)
30 commit = subprocess.check_output(COMMIT_API.replace(":sha", sha), shell=True)
31 commits.append(commit)
32 seen.add(sha)
33 for parent in json.loads(commit)["parents"]:
34 sha = parent["sha"]
35 if sha not in seen and sha not in todo:
36 todo.append(sha)
37 time.sleep(1) # rate throttling
38
39 return commits
40
41
42 def main(corpus):
43 data = github_commits()
44 for num, content in enumerate(reversed(data)):
45 (corpus / f"{num:04d}.json").write_bytes(content)
46
47
48 if __name__ == "__main__":
49 if len(sys.argv) < 2:
50 print(f"Usage: {sys.argv[0]} <directory>")
51 sys.exit(2)
52 main(pathlib.Path(sys.argv[1]))