A categorical programming language
Revision | 8b60dc0d6200a93008d69c118732d46bdc7a72d3 (tree) |
---|---|
Time | 2024-04-25 10:34:53 |
Author | Corbin <cds@corb...> |
Commiter | Corbin |
Get 2to3.py to build a v3 hive.
I haven't verified that the contents of the hive are correct, and
git-fsck is (unsurprisingly) very unhappy with me. I used a monkey patch
on dulwich to allow creation of refs with question marks in their names,
because I know it's legal on Linux and I don't care about other kernels.
@@ -1,21 +1,45 @@ | ||
1 | 1 | import json |
2 | +import os | |
2 | 3 | import sys |
4 | +import time | |
3 | 5 | |
4 | -import dulwich | |
6 | +from dulwich.repo import Repo | |
7 | +from dulwich.objects import Blob, Commit, Tree | |
5 | 8 | |
6 | -with open(sys.argv[-1]) as handle: d = json.load(handle) | |
9 | +# Monkey patch: Allow ? in ref names. | |
10 | +from dulwich import refs | |
11 | +refs.BAD_REF_CHARS = set(b"\177 ~^:*[") | |
12 | + | |
13 | +with open(sys.argv[-2]) as handle: d = json.load(handle) | |
14 | +repoPath = sys.argv[-1] | |
15 | +if os.path.exists(repoPath): repo = Repo(repoPath) | |
16 | +else: repo = Repo.init(repoPath, mkdir=True) | |
17 | + | |
18 | +# XXX implement following four functions, all should return object hash :str | |
7 | 19 | |
8 | 20 | def insertBlob(s): |
9 | - return "blob" | |
21 | + blob = Blob.from_string(s.encode("utf-8")) | |
22 | + repo.object_store.add_object(blob) | |
23 | + return blob.id | |
10 | 24 | |
11 | 25 | def insertTree(d): |
12 | - return "tree" | |
26 | + tree = Tree() | |
27 | + for k, v in d.items(): | |
28 | + tree.add(k.encode("utf-8"), 0o100644, v) | |
29 | + repo.object_store.add_object(tree) | |
30 | + return tree.id | |
13 | 31 | |
32 | +author = b"Corbin <cds@corbinsimpson.com>" | |
14 | 33 | def insertCommit(tree, message): |
15 | - return "commit" | |
16 | - | |
17 | -def addTag(t, obj): | |
18 | - return "tag" | |
34 | + commit = Commit() | |
35 | + commit.author = commit.committer = author | |
36 | + commit.author_time = commit.commit_time = int(time.time()) | |
37 | + commit.author_timezone = commit.commit_timezone = 0 | |
38 | + commit.encoding = b"UTF-8" | |
39 | + commit.message = message.encode("utf-8") | |
40 | + commit.tree = tree | |
41 | + repo.object_store.add_object(commit) | |
42 | + return commit.id | |
19 | 43 | |
20 | 44 | heap = [] |
21 | 45 | for row in d["heap"]: |
@@ -25,10 +49,14 @@ for row in d["heap"]: | ||
25 | 49 | tree = dict((("_%d" % i, heap[j]) for i, j in enumerate(row[1:]))) |
26 | 50 | tree["_con"] = insertBlob(row[0]) |
27 | 51 | heap.append(insertTree(tree)) |
52 | + if not (len(heap) % 100): print("h", end="", flush=True) | |
53 | +print() | |
28 | 54 | |
29 | -print("Heap is processed! Total expressions:", len(heap)) | |
30 | - | |
55 | +exprCount = 0 | |
31 | 56 | def insertExpr(expr): |
57 | + global exprCount | |
58 | + exprCount += 1 | |
59 | + if not (exprCount % 100): print("e", end="", flush=True) | |
32 | 60 | if isinstance(expr, str): return insertBlob(expr) |
33 | 61 | elif isinstance(expr, int): return insertBlob("_%d" % expr) |
34 | 62 | else: |
@@ -36,18 +64,21 @@ def insertExpr(expr): | ||
36 | 64 | tree = dict(("_%d" % i, j) for i, j in enumerate(exprs)) |
37 | 65 | return insertTree(tree) |
38 | 66 | |
39 | -for name, expr in d["jets"].items(): addTag("jets/" + name, insertExpr(expr)) | |
67 | +def refName(ns, s): return (("refs/%s/%s" % (ns, s)).encode("utf-8")) | |
40 | 68 | |
41 | -print("Jets are processed! Total jets:", len(d["jets"])) | |
69 | +for name, expr in d["jets"].items(): | |
70 | + repo.refs[refName("jets", name)] = insertExpr(expr) | |
42 | 71 | |
43 | 72 | for name, row in d["symbols"].items(): |
44 | 73 | pointer, trail = row |
45 | - addTag("local/" + name, insertCommit(heap[pointer], trail)) | |
46 | - | |
47 | -print("Symbols are processed! Total names:", len(d["symbols"])) | |
74 | + repo.refs[refName("local", name)] = insertCommit(heap[pointer], trail) | |
48 | 75 | |
49 | 76 | for name, row in d["templates"].items(): |
50 | 77 | expr, trail = row |
51 | - addTag("local/" + name, insertCommit(insertExpr(expr), trail)) | |
78 | + repo.refs[refName("local", name)] = insertCommit(insertExpr(expr), trail) | |
52 | 79 | |
80 | +print() | |
81 | +print("Heap is processed! Total expressions:", len(heap)) | |
53 | 82 | print("Templates are processed! Total names:", len(d["templates"])) |
83 | +print("Jets are processed! Total jets:", len(d["jets"])) | |
84 | +print("Symbols are processed! Total names:", len(d["symbols"])) |