-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
116 lines (88 loc) · 2.69 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import subprocess
all_dates = set()
lineages = {}
derivations = []
with open("history.txt", "rt") as f:
for line in f:
line = line.strip().replace("\\n", "\n")
if not line or line.startswith("#"):
continue
if line.startswith("$"):
lineages[line[1:]] = {}
lin = lineages[line[1:]]
continue
elif line.startswith("DERIVE "):
to, from_ = line[7:].split("~")
derivations.append((from_, to))
continue
space = line.find(" ")
date = line[:space]
label = line[space + 1:]
# print(date, ":", label)
all_dates.add(date)
lin[date] = label
# for y in range(2001, 2022):
# all_dates.add(f"{y}-01-01")
with open("history.dot", "wt") as f:
name_map = {}
f.write("""
digraph unix {
rankdir=BT;
{
node [shape=plaintext,fontsize=20,fontname="Roboto"];
""")
for date in sorted(all_dates):
if date != "today":
f.write(f' "{date}" ->\n')
f.write(''' "today";
}
''')
f.write("""
{
node [shape=box,style=filled,color=lightblue,fontsize=20,fontname="Roboto"];
""")
for lineage, contents in lineages.items():
defs = []
# entries = []
joins = []
invis_joins = []
prev = None
prev_visible = False
last_visible = None
dc = 0
for date in sorted(all_dates):
name = lineage + str(dc); dc += 1
if date in contents:
defs.append(f'{name} [label="{contents[date]}"]')
name_map[contents[date]] = name
# entries.append(f'"{label}"')
visible = True
else:
visible = False
if last_visible is not None and visible:
joins.append((last_visible, name))
prev = name
prev_visible = visible
if visible:
last_visible = name
f.write("\n".join(defs))
for a, b in joins:
f.write(f"{a} -> {b}[weight=4];\n")
f.write("\n")
for from_, to in derivations:
a = name_map[from_]
b = name_map[to]
f.write(f"{a} -> {b}[style=dashed, weight=1];\n")
f.write(' }\n')
for dc, date in enumerate(sorted(all_dates)):
f.write('{ rank=same; "' + date + '"')
for lineage, contents in lineages.items():
if date in contents:
name = lineage + str(dc)
f.write(f' "{name}"')
f.write(' }\n')
f.write('''
// }
}
''')
subprocess.check_call("dot -Tpng -o history.png history.dot".split(" "))