Projects STRLCPY GoReSym Commits a8967dae
🤬
  • ■ ■ ■ ■ ■ ■
    generatepackages.py
    1  -import http.client
    2  -import json
     1 +# Python >= 3.6, tested with Python 3.9.5
     2 +import requests
    3 3   
    4  -# this generates stdpackages.go from the go github source tree
    5  -def remove_prefix(text, prefix):
    6  - return text[text.startswith(prefix) and len(prefix):]
     4 +# https://docs.github.com/en/rest/reference/git#get-a-tree
     5 +API_URL = "https://api.github.com/repos/golang/go/git/trees"
     6 +DIR = "src"
     7 +OUTPUT_FILE = "stdpackages.go"
     8 +VAR_NAME = "standardPackages"
    7 9   
    8  -conn = http.client.HTTPSConnection("api.github.com")
    9  -payload = ''
    10  -headers = {'User-Agent': 'python'}
    11  -conn.request("GET", "/repos/golang/go/git/trees/master?recursive=0", payload, headers)
    12  -res = conn.getresponse()
    13  -txt = res.read().decode('utf-8')
    14  -data = json.loads(txt)
    15 10   
     11 +def get_tree(tree_sha):
     12 + url = f"{API_URL}/{tree_sha}"
     13 + print(f"Getting {url}")
     14 + r = requests.get(url)
     15 + r.raise_for_status()
     16 + return r.json()
    16 17   
    17  -with open('stdpackages.go', 'w') as f:
    18  - f.write("package main\nvar stdPkgs = []string{")
    19 18   
    20  - tree = data['tree']
    21  - for item in tree:
    22  - if item['type'] != 'tree':
    23  - continue
     19 +r = get_tree("master")
     20 +for leaf in r["tree"]:
     21 + if leaf["path"] == DIR:
     22 + sha = leaf["sha"]
     23 + break
    24 24   
    25  - path = item['path']
    26  - if not path.startswith('src'):
    27  - continue
     25 +r = get_tree(f"{sha}?recursive=1")
    28 26   
    29  - if path == "src":
    30  - continue
     27 +if r["truncated"]:
     28 + raise RuntimeError("Too many paths, needed to fetch one sub-tree at a time")
    31 29   
    32  - path = remove_prefix(path, 'src/')
    33  - f.write("\"{}\",".format(path))
     30 +# Use list instead of set to keep order
     31 +paths = [leaf["path"] for leaf in r["tree"] if leaf["type"] == "tree"]
     32 +# paths in the following format: {"path1", "path2", ...}
     33 +paths_str = '{"' + '", "'.join(paths) + '"}'
    34 34   
    35  - f.write("\"\"}")
     35 +print(f"Writing paths to {OUTPUT_FILE}")
     36 +with open(OUTPUT_FILE, "w") as f:
     37 + f.write(f"package main\n\nvar {VAR_NAME} = []string{paths_str}")
    36 38   
  • stdpackages.go
    Unable to diff as some line is too long.
Please wait...
Page is in error, reload to recover