Debian Codegraph @

scripts/first-run.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # This script is run the first time any action is performed after the repository
4 # is cloned. If you are reading this because the automatic initialization 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
14 BASE_OWNER = "googlefonts"
15 BASE_REPONAME = "googlefonts-project-template"
16 DUMMY_URL = "https://yourname.github.io/your-font-repository-name"
17
18
19 def repo_url(owner, name):
20 return f"https://github.com/{owner}/{name}"
21
22
23 def web_url(owner, name):
24 return f"https://{owner}.github.io/{name}"
25
26
27 def raw_url(owner, name):
28 return f"https://raw.githubusercontent.com/{owner}/{name}"
29
30
31 def touch():
32 open(".init.stamp", "w").close()
33
34
35 def lose(msg, e=None):
36 print(msg)
37 print("You will need to do the initialization steps manually.")
38 print("Read scripts/first-run.py for more instructions how to do this.")
39 if e:
40 print(
41 "\nHere's an additional error message which may help diagnose the problem."
42 )
43 raise e
44 sys.exit(1)
45
46
47 try:
48 my_repo_url = git.remote("get-url", "origin")
49 except Exception as e:
50 lose("Could not use git to find my own repository URL", e)
51
52 m = re.match(r"(?:https://github.com/|[email protected]:)(.*)/(.*)/?", str(my_repo_url))
53 if not m:
54 lose(
55 f"My git repository URL ({my_repo_url}) didn't look what I expected - are you hosting this on github?"
56 )
57
58 owner, reponame = m[1], m[2]
59
60 if owner == BASE_OWNER and reponame == BASE_REPONAME:
61 print("I am being run on the upstream repository (probably due to CI)")
62 print("All I'm going to do is create the touch file and quit.")
63 touch()
64 sys.exit()
65
66 # INITIALIZATION STEPS
67
68 # First, the README file contains URLs to pages in the `gh-pages` branch of the
69 # repo. When initially cloned, these URLs will point to the
70 # googlefonts/Unified-Font-Repository itself. But downstream users want links
71 # and badges about their own font, not ours! So any URLs need to be adjusted to
72 # refer to the end user's repository.
73
74 # We will also pin the dependencies so future builds are reproducible.
75
76 readme = open("README.md").read()
77
78 print(
79 "Fixing URLs:", web_url(BASE_OWNER, BASE_REPONAME), "->", web_url(owner, reponame)
80 )
81
82 readme = readme.replace(web_url(BASE_OWNER, BASE_REPONAME), web_url(owner, reponame))
83 # In the badges, the URLs to raw.githubusercontent.com are URL-encoded as they
84 # are passed to shields.io.
85 print(
86 "Fixing URLs:",
87 quote(raw_url(BASE_OWNER, BASE_REPONAME), safe=""),
88 "->",
89 quote(raw_url(owner, reponame), safe=""),
90 )
91 readme = readme.replace(
92 quote(raw_url(BASE_OWNER, BASE_REPONAME), safe=""),
93 quote(raw_url(owner, reponame), safe=""),
94 )
95
96 print(
97 "Fixing URLs:",
98 DUMMY_URL,
99 "->",
100 web_url(owner, reponame),
101 )
102 readme = readme.replace(
103 f"`{DUMMY_URL}`",
104 web_url(owner, reponame),
105 )
106
107 with open("README.md", "w") as fh:
108 fh.write(readme)
109
110 # Fix the OFL
111
112 ofl = open("OFL.txt").read()
113 ofl = ofl.replace(web_url(BASE_OWNER, BASE_REPONAME), web_url(owner, reponame))
114 ofl = ofl.replace("My Font", reponame.title())
115 ofl = ofl.replace("20**", str(datetime.date.today().year))
116 with open("OFL.txt", "w") as fh:
117 fh.write(ofl)
118
119 # Pin the dependencies
120 print("Pinning dependencies")
121 dependencies = subprocess.check_output(["pip", "freeze"])
122 with open("requirements.txt", "wb") as dependency_file:
123 dependency_file.write(dependencies)
124
125 # Finally, we add a "touch file" called ".init.stamp" to the repository which
126 # prevents this first-run process from being run again.
127 touch()