Debian Codegraph @

scripts/first-run.py

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