-
Notifications
You must be signed in to change notification settings - Fork 2
/
gfautobuilder.py
100 lines (93 loc) · 3.8 KB
/
gfautobuilder.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
import glob
import json
import os
import re
import subprocess
import selectors
import git
from rich import progress
from rich.progress import Progress
from progress import GitRemoteProgress
repos = json.load(open("cache.json", "r"))
upstream_repos = len([r for r in repos.values() if r.get("has_upstream")])
gfr_repos = {k: v for k, v in repos.items() if v.get("is_gfr") or v.get("sources")}
failed = []
succeeded = []
with Progress(
progress.TimeElapsedColumn(),
progress.TextColumn("[progress.description]{task.description}"),
progress.BarColumn(),
progress.TextColumn("{task.completed}/{task.total}"),
progress.TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
progress.TimeRemainingColumn(),
) as progress:
buildall_task = progress.add_task("[green]Building Google Fonts", total=len(gfr_repos))
clone_task = progress.add_task("[yellow]Clone", total=100, visible=False)
if not os.path.isdir("build"):
os.mkdir("build")
for name, upstream in gfr_repos.items():
builddir = "build/"+name
project_url = "https://github.com/"+upstream["real_upstream"]
if not os.path.isdir(builddir):
progress.update(clone_task,description="[yellow] Cloning "+name+"...", completed=0, visible=True)
git.Repo.clone_from(
url=project_url,
to_path=builddir,
depth=1,
progress=GitRemoteProgress(progress, clone_task, name),
)
progress.update(clone_task, visible=False)
progress.update(buildall_task, advance=1)
build_task = progress.add_task("[green]Build " + name, total=1)
progress.console.print("[bright_black]Building " + name + "...")
os.chdir(builddir)
if upstream.get("sources"):
sources = upstream["sources"]
else:
sources = glob.glob("sources/*.y*l")
if not sources:
progress.console.print(f"[red]{name} has no sources!")
failed.append(name)
continue
sources = [sources[0]]
buildcmd = ["gftools-builder"] + sources
progress.console.print("[bright_black]Building " + " ".join(buildcmd) + "...")
process = subprocess.Popen(
buildcmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
sel = selectors.DefaultSelector()
sel.register(process.stdout, selectors.EVENT_READ)
sel.register(process.stderr, selectors.EVENT_READ)
ok = True
stdoutlines = []
stderrlines = []
while ok:
for key, val1 in sel.select():
line = key.fileobj.readline()
if not line:
ok = False
break
if key.fileobj is process.stdout and (
m := re.match(r"^\[(\d+)/(\d+)\]", line.decode("utf8"))
):
progress.update(
build_task, completed=int(m.group(1)), total=int(m.group(2))
)
elif key.fileobj is process.stderr:
stderrlines.append(line)
else:
stdoutlines.append(line)
rc = process.wait()
if rc != 0:
for line in stdoutlines:
progress.console.print(line.decode("utf-8"), end="")
for line in stderrlines:
progress.console.print("[red]" + line.decode("utf8"), end="")
failed.append(name)
progress.console.print("[red]Error building " + name)
else:
progress.console.print("[green]Built " + name)
succeeded.append(name)
os.chdir("../..")
progress.update(build_task, advance=1, visible=False)
progress.console.print("")