]> Git Repo - pico-vscode.git/blob - scripts/genCache.py
Merge branch 'main' into main
[pico-vscode.git] / scripts / genCache.py
1 import json
2 import os
3 import sys
4
5 import requests
6 import js2py
7
8
9 dir_path = os.path.dirname(os.path.realpath(__file__))
10 os.chdir(f"{dir_path}/..")
11
12 # Extract js for variables
13 with open("dist/extension.cjs", "r") as f:
14     keep = False
15     txt = ""
16     for line in f.readlines():
17         if "const EXT_USER_AGENT =" in line:
18             keep = True
19         elif "getAuthorizationHeaders" in line:
20             keep = False
21         elif "const CURRENT_DATA_VERSION =" in line:
22             keep = True
23         
24         if keep:
25             txt += line
26
27         if "const CURRENT_DATA_VERSION =" in line:
28             keep = False
29
30 parsed = js2py.translate_js(txt)
31 with open("tmp.py", "w") as f:
32     f.write(parsed)
33 sys.path.append(os.getcwd())
34
35 import tmp
36
37 stuff = tmp.var.to_python()
38
39 # stuff.GithubRepository has conversions in both directions, so is twice the size
40 num_repos = 0
41 for _ in stuff.GithubRepository:
42     num_repos += 0.5
43 num_repos = int(num_repos)
44 assert num_repos == 5
45 print("Num repos", num_repos)
46
47
48 # Only provide data for these versions
49 versions = [
50     ["1.5.1", "2.0.0"],
51     ["v3.28.6", "v3.29.6"],
52     ["v1.12.1"],
53     ["v1.5.1-0", "v2.0.0-0"],
54     ["2.0.0"]
55 ]
56
57 headers = {
58     "X-GitHub-Api-Version": "2022-11-28",
59     "User-Agent": stuff.EXT_USER_AGENT,
60     "Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}"
61 }
62
63 ret = {}
64 for repo in range(num_repos):
65     ret[f"githubApiCache-{repo}-0"] = versions[repo]
66
67     for version in versions[repo]:
68         owner = stuff.ownerOfRepository(repo)
69         name = stuff.repoNameOfRepository(repo)
70         x = requests.get(
71             f"{stuff.GITHUB_API_BASE_URL}/repos/{owner}/{name}/releases/tags/{version}",
72             headers=headers
73         )
74         data = json.loads(x.content)
75         assets = []
76         for asset in data["assets"]:
77             assets.append({
78                 "id": asset["id"],
79                 "name": asset["name"],
80                 "browser_download_url": asset["browser_download_url"]
81             })
82         data = {
83             "assets": assets,
84             "assetsUrl": data["assets_url"]
85         }
86
87         ret[f"githubApiCache-{repo}-1-{version}"] = data
88         
89
90 for k, v in ret.items():
91     idx = int(k.split('-')[1])
92     print(f"{k} is {stuff.GithubRepository[idx]}")
93     if isinstance(v, list):
94         print(v)
95     else:
96         print(v["assets"][0])
97
98 with open(f"data/{stuff.CURRENT_DATA_VERSION}/github-cache.json", "w") as f:
99     json.dump(ret, f, indent=2)
This page took 0.029527 seconds and 4 git commands to generate.