Debian Codegraph @

scripts/customize.py

raw
1 #!/usr/bin/env python3
2
3 # This script is run by the user using `make customize` after the repository
4 # is cloned. If you are reading this because `make customize` failed,
5 # skip down to the section headed "INITIALIZATION STEPS".
6
7 from sh import git
8 import datetime
9 import re
10 import sys
11 from urllib.parse import quote
12 import subprocess
13 import requests
14
15 BASE_OWNER = "googlefonts"
16 BASE_REPONAME = "googlefonts-project-template"
17 DUMMY_URL = "https://yourname.github.io/your-font-repository-name"
18 LATEST_OFL = "https://raw.githubusercontent.com/googlefonts/googlefonts-project-template/main/OFL.txt"
19
20
21 def repo_url(owner, name):
22 return f"https://github.com/{owner}/{name}"
23
24
25 def web_url(owner, name):
26 return f"https://{owner}.github.io/{name}"
27
28
29 def raw_url(owner, name):
30 return f"https://raw.githubusercontent.com/{owner}/{name}"
31
32
33 def lose(msg, e=None):
34 print(msg)
35 print("You will need to do the initialization steps manually.")
36 print("Read scripts/customize.py for more instructions how to do this.")
37 if e:
38 print(
39 "\nHere's an additional error message which may help diagnose the problem."
40 )
41 raise e
42 sys.exit(1)
43
44
45 try:
46 my_repo_url = git.remote("get-url", "origin")
47 except Exception as e:
48 lose("Could not use git to find my own repository URL", e)
49
50 m = re.match(r"(?:https://github.com/|[email protected]:)(.*)/(.*)/?", str(my_repo_url))
51 if not m:
52 lose(
53 f"My git repository URL ({my_repo_url}) didn't look what I expected - are you hosting this on github?"
54 )
55
56 owner, reponame = m[1], m[2]
57
58 if owner == BASE_OWNER and reponame == BASE_REPONAME:
59 print("I am being run on the upstream repository; don't do that")
60 sys.exit()
61
62 # INITIALIZATION STEPS
63
64 # First, the README file contains URLs to pages in the `gh-pages` branch of the
65 # repo. When initially cloned, these URLs will point to the
66 # googlefonts/Unified-Font-Repository itself. But downstream users want links
67 # and badges about their own font, not ours! So any URLs need to be adjusted to
68 # refer to the end user's repository.
69
70 # We will also pin the dependencies so future builds are reproducible.
71
72 readme = open("README.md").read()
73 ghpages_url = web_url(owner, reponame)
74 project_url = repo_url(owner, reponame)
75
76 print("Fixing URLs:", web_url(BASE_OWNER, BASE_REPONAME), "->", ghpages_url)
77
78 readme = readme.replace(web_url(BASE_OWNER, BASE_REPONAME), ghpages_url)
79 # In the badges, the URLs to raw.githubusercontent.com are URL-encoded as they
80 # are passed to shields.io.
81 readme = readme.replace(
82 quote(raw_url(BASE_OWNER, BASE_REPONAME), safe=""),
83 quote(raw_url(owner, reponame), safe=""),
84 )
85
86 print("Fixing URLs:", DUMMY_URL, "->", ghpages_url)
87 readme = readme.replace(f"`{DUMMY_URL}`", ghpages_url)
88
89 with open("README.md", "w") as fh:
90 fh.write(readme)
91
92 git.add("README.md")
93
94 # Fix the OFL
95 year = datetime.date.today().year
96 title = reponame.title()
97 copyright = f"Copyright {year} The {title} Project Authors ({project_url})\n"
98 print("Fetching the latest OFL..")
99 ofl = requests.get(LATEST_OFL).text.splitlines()
100 print("Writing an OFL for you")
101 print(copyright)
102 with open("OFL.txt", "w") as fh:
103 fh.write(copyright)
104 fh.write("\n".join(ofl[1:]))
105
106 git.add("OFL.txt")
107
108 # Pin the dependencies
109 print("Pinning dependencies")
110 dependencies = subprocess.check_output(["pip", "freeze"])
111 with open("requirements.txt", "wb") as dependency_file:
112 dependency_file.write(dependencies)
113 git.add("requirements.txt")
114
115 # Did anything change?
116 result = git.status("--porcelain")
117 if any(line.startswith("M ") for line in result.splitlines()):
118 git.commit("-m", "Customize repository")
119
120 print("Pushing changes to GitHub")
121 git.push()
122 else:
123 print("Nothing changed, no need to push")