Skip to content

Commit 4cb2db8

Browse files
authored
Merge pull request #848 from googlefonts/packager2
gftools packager: refactor
2 parents 8857696 + d9a8c45 commit 4cb2db8

File tree

16 files changed

+766
-2414
lines changed

16 files changed

+766
-2414
lines changed
Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Generates an upstream.yaml from a config.yaml and a GitHub release URL
1+
"""Generates a METADATA.pb from a config.yaml and a GitHub release URL
22
33
"""
44
import argparse
@@ -7,11 +7,13 @@
77
import yaml
88
import zipfile
99

10-
import gftools.packager
10+
from gftools.packager import load_metadata
11+
import gftools.fonts_public_pb2 as fonts_pb2
1112
from gftools.builder import GFBuilder
12-
from strictyaml import as_document
1313
from gftools.utils import download_file
1414
from fontTools.ttLib import TTFont
15+
import gftools.util.google_fonts as fonts
16+
from copy import deepcopy
1517

1618

1719
parser = argparse.ArgumentParser(description="Create an upstream.yaml for a family")
@@ -26,71 +28,84 @@ def get_family_name(config):
2628
return GFBuilder(config).get_family_name()
2729

2830

29-
def generate_upstream(config, url):
31+
def generate_metadata(config, url):
3032
repo = os.environ.get("GITHUB_REPOSITORY")
3133
if not repo:
3234
raise ValueError("Not being run from a GitHub action?")
3335
if "category" not in config:
3436
config["category"] = ["SANS_SERIF"]
3537

36-
upstream = {
37-
"name": get_family_name(config),
38-
"repository_url": os.environ["GITHUB_SERVER_URL"] + "/" + repo + ".git",
39-
"archive": url,
40-
"branch": "main",
41-
"category": config["category"],
42-
"build": "",
43-
"files": {},
44-
"designer": "Will be filled in",
45-
}
46-
return upstream
38+
metadata = fonts_pb2.FamilyProto()
39+
metadata.name = get_family_name(config)
40+
metadata.source.repository_url = os.environ["GITHUB_SERVER_URL"] + "/" + repo
41+
metadata.source.archive_url = url
42+
metadata.source.branch = "main"
43+
metadata.category.append(config["category"][0])
44+
metadata.designer = "Will be filled in"
45+
return metadata
4746

4847

49-
def update_file_list(upstream):
48+
def update_file_list(metadata):
5049
print("Downloading release archive...")
51-
upstream["files"] = {}
5250
with TemporaryDirectory() as tmp:
5351
archive_path = os.path.join(tmp, "archive.zip")
54-
download_file(upstream["archive"], archive_path)
52+
download_file(metadata.source.archive_url, archive_path)
5553
license_found = False
5654
description_found = False
5755
a_font = None
5856
with zipfile.ZipFile(archive_path, "r") as zip_ref:
5957
zip_ref.extractall(tmp)
58+
metadata.source.files.clear()
6059
for root, _, files in os.walk(tmp):
6160
for file in files:
6261
fullpath = os.path.join(root, file)
6362
relpath = os.path.relpath(fullpath, tmp)
6463
print(relpath)
64+
item = fonts_pb2.SourceFileProto()
6565
if file == "OFL.txt":
6666
license_found = True
67-
upstream["files"][relpath] = file
67+
item.source_file = relpath
68+
item.dest_file = file
69+
metadata.source.files.append(item)
6870
elif file == "DESCRIPTION.en_us.html":
6971
description_found = True
70-
upstream["files"][relpath] = file
72+
metadata["files"][relpath] = file
73+
item.source_file = relpath
74+
item.dest_file = file
75+
metadata.source.files.append(item)
7176
elif file == "ARTICLE.en_us.html":
72-
upstream["files"][relpath] = "article/"+file
77+
item.source_file = relpath
78+
item.dest_file = "article/"+file
79+
metadata.source.files.append(item)
7380
elif file.endswith("ttf"):
7481
if config.get("buildVariable", True):
7582
# Only add the file if it is the variable font
7683
if "[" in file:
77-
upstream["files"][relpath] = file
84+
item.source_file = relpath
85+
item.dest_file = file
86+
metadata.source.files.append(item)
7887
a_font = fullpath
7988
else:
8089
# Add statics
81-
upstream["files"][relpath] = file
90+
item.source_file = relpath
91+
item.dest_file = file
92+
metadata.source.files.append(item)
8293
a_font = fullpath
8394

8495
# If there was a "googlefonts/" directory in the release, just
8596
# use files in that directory.
86-
if any("googlefonts/" in x for x in upstream["files"].keys()):
87-
upstream["files"] = {str(k):str(v) for k,v in upstream["files"].items() if "googlefonts/" in str(k) or not ".ttf" in str(k) }
97+
if any("googlefonts/" in x.source_file for x in metadata.source.files):
98+
existing_files = deepcopy(metadata.source.files)
99+
metadata.source.files.clear()
100+
for item in existing_files:
101+
if "googlefonts/" in item.source_file or not ".ttf" in item.source_file:
102+
metadata.source.files.append(item)
88103

89104
if not license_found:
90105
raise ValueError(
91106
"No license file was found. Ensure OFL.txt is added the the release"
92107
)
93-
if not description_found and "Noto" not in upstream["name"]:
108+
if not description_found and "Noto" not in metadata.name:
94109
raise ValueError(
95110
"No description file was found. Ensure DESCRIPTION.en_us.html is added the the release"
96111
)
@@ -100,10 +115,10 @@ def update_file_list(upstream):
100115
raise ValueError("No font files were found. Is the release broken?")
101116

102117
designer = TTFont(a_font)["name"].getDebugName(9)
103-
if "Noto" in upstream["name"]:
104-
upstream["designer"] = "Google"
118+
if "Noto" in metadata.name:
119+
metadata.designer = "Google"
105120
elif designer:
106-
upstream["designer"] = designer
121+
metadata.designer = designer
107122

108123

109124
if __name__ == "__main__":
@@ -115,28 +130,18 @@ def update_file_list(upstream):
115130
open(args.config), Loader=yaml.FullLoader
116131
)
117132

118-
if os.path.isfile("upstream.yaml"):
119-
try:
120-
upstream = gftools.packager._upstream_conf_from_file(
121-
"upstream.yaml", yes=True, quiet=True
122-
)
123-
except Exception as e:
124-
raise ValueError("Something went wrong parsing upstream.yaml: " + str(e))
133+
if os.path.isfile("METADATA.pb"):
134+
metadata = load_metadata("METADATA.pb")
125135
else:
126136
try:
127-
upstream = as_document(
128-
generate_upstream(config, args.url),
129-
gftools.packager.upstream_yaml_schema,
130-
)
137+
metadata = generate_metadata(config, args.url)
131138
except Exception as e:
132139
raise ValueError(
133-
"Something went wrong generating upstream.yaml (bug in updateupstream): "
140+
"Something went wrong generating METADATA.pb (bug in updateupstream): "
134141
+ str(e)
135142
)
136143

137144
# Add archive URL
138-
upstream["archive"] = args.url
139-
update_file_list(upstream)
140-
141-
with open("upstream.yaml", "w") as upstream_yaml_file:
142-
upstream_yaml_file.write(upstream.as_yaml())
145+
metadata.source.archive_url = args.url
146+
update_file_list(metadata)
147+
fonts.WriteProto(metadata, "METADATA.pb")

Lib/gftools/builder/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
Any,
1616
MapCombined,
1717
)
18-
from gftools.packager import CATEGORIES
18+
19+
CATEGORIES = ['DISPLAY', 'SERIF', 'SANS_SERIF', 'HANDWRITING', 'MONOSPACE']
1920

2021

2122
BASE_SCHEMA = MapCombined(

Lib/gftools/gfgithub.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ def rest_url(self, path, **kwargs):
5656
)
5757
return base_url
5858

59+
def get_content(self, path, branch=None):
60+
if branch:
61+
url = self.rest_url(f"contents/{path}", ref=branch)
62+
else:
63+
url = self.rest_url(f"contents/{path}")
64+
headers = {
65+
"Accept": "application/vnd.github.v3.raw",
66+
"Authorization": f"bearer {self.gh_token}",
67+
}
68+
response = requests.get(url, headers=headers)
69+
response.raise_for_status()
70+
return response
71+
5972
def get_blob(self, file_sha):
6073
url = self.rest_url(f"git/blobs/{file_sha}")
6174
headers = {
@@ -66,6 +79,9 @@ def get_blob(self, file_sha):
6679
response.raise_for_status()
6780
return response
6881

82+
def get_latest_release(self):
83+
return self._get(self.rest_url("releases/latest"))
84+
6985
def open_prs(self, pr_head: str, pr_base_branch: str) -> typing.List:
7086
return self._get(
7187
self.rest_url("pulls", state="open", head=pr_head, base=pr_base_branch)

0 commit comments

Comments
 (0)