| 1 |
import glob |
| 2 |
import os |
| 3 |
import re |
| 4 |
import shutil |
| 5 |
import subprocess |
| 6 |
|
| 7 |
from diffenator2.html import build_index_page |
| 8 |
from gftools.utils import download_files_from_archive |
| 9 |
from github import Github |
| 10 |
|
| 11 |
|
| 12 |
def get_latest_release(family, user=None, repo=None): |
| 13 |
if not (user and repo): |
| 14 |
repo_url = ( |
| 15 |
subprocess.check_output(["git", "remote", "get-url", "origin"]) |
| 16 |
.decode("utf8") |
| 17 |
.strip() |
| 18 |
) |
| 19 |
url_split = repo_url.split("/") |
| 20 |
user, repo = url_split[3], url_split[4] |
| 21 |
|
| 22 |
g = Github(os.environ["GITHUB_TOKEN"]) |
| 23 |
repo = g.get_repo(user + "/" + repo) |
| 24 |
for release in repo.get_releases(): |
| 25 |
if release.draft: |
| 26 |
continue |
| 27 |
m = re.match(r"^(.*)-(v[\d.]+)", release.tag_name) |
| 28 |
if not m: |
| 29 |
print(f"Unparsable release {release.tag_name} in {user}/{repo}") |
| 30 |
continue |
| 31 |
thisfamily, version = m[1], m[2] |
| 32 |
if thisfamily != family: |
| 33 |
continue |
| 34 |
assets = release.get_assets() |
| 35 |
download_url = assets[0].browser_download_url |
| 36 |
return version, download_url |
| 37 |
return None, None |
| 38 |
|
| 39 |
|
| 40 |
if "GITHUB_TOKEN" not in os.environ: |
| 41 |
raise ValueError("GITHUB_TOKEN was not passed to the notoqa environment") |
| 42 |
os.environ["GH_TOKEN"] = os.environ["GITHUB_TOKEN"] |
| 43 |
|
| 44 |
outdir = os.path.join("out", "qa") |
| 45 |
os.makedirs(outdir, exist_ok=True) |
| 46 |
all_strings = None |
| 47 |
qa_strings = glob.glob("qa/*.txt") |
| 48 |
|
| 49 |
if qa_strings: |
| 50 |
all_strings = os.path.join(outdir, "all_strings.txt") |
| 51 |
with open(all_strings, "w") as out_file: |
| 52 |
for strings_file in qa_strings: |
| 53 |
with open(strings_file) as in_file: |
| 54 |
for line in in_file: |
| 55 |
out_file.write(line) |
| 56 |
|
| 57 |
for family in [os.path.basename(x) for x in glob.glob("fonts/*")]: |
| 58 |
previous_version, previous_url = get_latest_release(family) |
| 59 |
if not previous_version: |
| 60 |
print(f"No previous release for {family}, skipping") |
| 61 |
continue |
| 62 |
print(f"Testing {family} against {previous_version}") |
| 63 |
fonts_dir = os.path.join(outdir, "fonts") |
| 64 |
os.makedirs(fonts_dir, exist_ok=True) |
| 65 |
|
| 66 |
fonts_before_dir = os.path.join(outdir, "fonts_before") |
| 67 |
os.makedirs(fonts_before_dir, exist_ok=True) |
| 68 |
|
| 69 |
fonts_before = download_files_from_archive(previous_url, fonts_before_dir) |
| 70 |
variables_before = [ |
| 71 |
f for f in fonts_before if f.endswith(".ttf") and "unhinted/variable-ttf" in f |
| 72 |
] |
| 73 |
fonts_now = glob.glob(f"fonts/{family}/unhinted/ttf/*.ttf") |
| 74 |
variables_now = glob.glob(f"fonts/{family}/unhinted/variable-ttf/*.ttf") |
| 75 |
|
| 76 |
fonts_before = [f for f in fonts_before if f.endswith(".ttf") and "unhinted" in f] |
| 77 |
|
| 78 |
if variables_now and variables_before: |
| 79 |
|
| 80 |
fonts_now = variables_now |
| 81 |
fonts_before = variables_before |
| 82 |
|
| 83 |
print("Fonts before: ") |
| 84 |
for s in fonts_before: |
| 85 |
print(" * " + s) |
| 86 |
|
| 87 |
print("Fonts now: ") |
| 88 |
for s in fonts_now: |
| 89 |
print(" * " + s) |
| 90 |
|
| 91 |
if not fonts_now: |
| 92 |
print(f"No current fonts to compare for {family}!") |
| 93 |
continue |
| 94 |
if not fonts_before: |
| 95 |
print(f"No previous fonts to compare for {family}!") |
| 96 |
continue |
| 97 |
|
| 98 |
if len(fonts_before) == 1 and len(fonts_now) == 1: |
| 99 |
subprocess.run( |
| 100 |
[ |
| 101 |
"diffenator3", |
| 102 |
fonts_before[0], |
| 103 |
fonts_now[0], |
| 104 |
"--html", |
| 105 |
"--output", |
| 106 |
os.path.join(outdir, family), |
| 107 |
|
| 108 |
], |
| 109 |
check=True, |
| 110 |
) |
| 111 |
else: |
| 112 |
|
| 113 |
for before in fonts_before: |
| 114 |
before_bare = before.replace(os.path.join(outdir, "fonts_before"), "fonts") |
| 115 |
if before_bare in fonts_now: |
| 116 |
subprocess.run( |
| 117 |
[ |
| 118 |
"diffenator3", |
| 119 |
before, |
| 120 |
before_bare, |
| 121 |
"--html", |
| 122 |
"--output", |
| 123 |
os.path.join(outdir, family), |
| 124 |
|
| 125 |
], |
| 126 |
check=True, |
| 127 |
) |
| 128 |
os.rename( |
| 129 |
os.path.join(outdir, family, "diffenator.html"), |
| 130 |
os.path.join(outdir, family, f"{os.path.basename(before)}.html"), |
| 131 |
) |
| 132 |
else: |
| 133 |
print(f"Could not find a match for {before}") |
| 134 |
|
| 135 |
|
| 136 |
shutil.rmtree(fonts_before_dir) |
| 137 |
|
| 138 |
if glob.glob(outdir + "/*"): |
| 139 |
build_index_page(outdir) |