forked from catppuccin/gtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
222 lines (177 loc) · 6.02 KB
/
install.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
import os
import zipfile
import argparse
import logging
import io
from typing import Optional
from pathlib import Path
from dataclasses import dataclass
from urllib.request import urlopen, Request
logger = logging.getLogger("catppuccin-gtk")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
formatter = logging.Formatter("[%(name)s] [%(levelname)s] - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
@dataclass
class InstallContext:
flavor: str
accent: str
dest: Path
link: bool
def build_info(self, include_url=True) -> str:
url = build_release_url(self)
info = f"""Installation info:
flavor: {self.flavor}
accent: {self.accent}
dest: {self.dest.absolute()}
link: {self.link}"""
if include_url:
info += f"\nremote_url: {url}"
return info
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"flavor",
type=str,
choices=["mocha", "frappe", "macchiato", "latte"],
help="Flavor of the theme to apply.",
)
parser.add_argument(
"accent",
type=str,
default="mauve",
choices=[
"rosewater",
"flamingo",
"pink",
"mauve",
"red",
"maroon",
"peach",
"yellow",
"green",
"teal",
"sky",
"sapphire",
"blue",
"lavender",
],
help="Accent of the theme.",
)
parser.add_argument(
"--from-artifact",
type=Path,
dest="from_artifact",
help="Install from an artifact instead of a mainline release, pass the artifact path (e.g 7bff2448a81e36bf3b0e03bfbd649bebe6973ec7-artifacts.zip)",
)
parser.add_argument(
"--dest",
"-d",
type=str,
dest="dest",
help="Destination of the files.",
)
parser.add_argument(
"--link",
help="Whether to add symlinks for libadwaita",
type=bool,
default=False,
action=argparse.BooleanOptionalAction,
)
return parser.parse_args()
def build_release_url(ctx: InstallContext) -> str:
repo_root = "https://github.com/catppuccin/gtk/releases/download"
release = "v1.0.3" # x-release-please-version
zip_name = f"catppuccin-{ctx.flavor}-{ctx.accent}-standard+default.zip"
return f"{repo_root}/{release}/{zip_name}"
def fetch_zip(url: str) -> Optional[zipfile.ZipFile]:
req = Request(url)
zip_file = None
logger.info("Starting download...")
with urlopen(req) as response:
logger.info(f"Response status: {response.status}")
zip_file = zipfile.ZipFile(io.BytesIO(response.read()))
logger.info("Download finished, zip is valid")
logger.info("Verifying download..")
first_bad_file = zip_file.testzip()
if first_bad_file is not None:
logger.error(f'Zip appears to be corrupt, first bad file is "{first_bad_file}"')
return None
logger.info("Download verified")
return zip_file
def add_libadwaita_links(ctx: InstallContext, rewrite: bool = False):
suffix = "light"
if ctx.flavor != "latte":
suffix = "dark"
dir_name = (
ctx.dest / f"catppuccin-{ctx.flavor}-{ctx.accent}-standard+default-{suffix}" / "gtk-4.0"
).absolute()
gtk4_dir = (Path(os.path.expanduser("~")) / ".config" / "gtk-4.0").absolute()
os.makedirs(gtk4_dir, exist_ok=True)
logger.info("Adding symlinks for libadwaita")
logger.info(f"Root: {dir_name}")
logger.info(f"Target: {gtk4_dir}")
try:
if rewrite:
os.remove(gtk4_dir / "assets")
os.remove(gtk4_dir / "gtk.css")
os.remove(gtk4_dir / "gtk-dark.css")
except FileNotFoundError:
logger.debug("Ignoring FileNotFound in symlink rewrite")
os.symlink(dir_name / "assets", gtk4_dir / "assets")
os.symlink(dir_name / "gtk.css", gtk4_dir / "gtk.css")
os.symlink(dir_name / "gtk-dark.css", gtk4_dir / "gtk-dark.css")
def install(ctx: InstallContext):
url = build_release_url(ctx)
logger.info(ctx.build_info())
zip_file = fetch_zip(url)
if zip_file is None:
return
logger.info("Extracting...")
zip_file.extractall(ctx.dest)
logger.info("Extraction complete")
if ctx.link:
add_libadwaita_links(ctx)
def install_from_artifact(ctx: InstallContext, artifact_path: Path):
# Working from a pull request, special case it
logger.info(f"Extracting artifact from '{artifact_path}'")
artifacts = zipfile.ZipFile(artifact_path)
logger.info("Verifying artifact...")
first_bad_file = artifacts.testzip()
if first_bad_file is not None:
logger.error(f'Zip appears to be corrupt, first bad file is "{first_bad_file}"')
return None
logger.info("Artifact verified")
logger.info(ctx.build_info(False))
# The zip, inside the artifacts, that we want to pull out
zip_name = f"catppuccin-{ctx.flavor}-{ctx.accent}-standard+default.zip"
logger.info(f"Pulling '{zip_name}' from the artifacts")
info = artifacts.getinfo(zip_name)
logger.info("Extracting the artifact...")
artifact = zipfile.ZipFile(io.BytesIO(artifacts.read(info)))
artifact.extractall(ctx.dest)
logger.info("Extraction complete")
if ctx.link:
logger.info("Adding links (with rewrite)")
add_libadwaita_links(ctx, True)
logger.info("Links added")
def main():
args = parse_args()
dest = Path(os.path.expanduser("~")) / ".local" / "share" / "themes"
os.makedirs(dest, exist_ok=True)
if args.dest:
dest = Path(args.dest)
ctx = InstallContext(
flavor=args.flavor, accent=args.accent, dest=dest, link=args.link
)
if args.from_artifact:
install_from_artifact(ctx, args.from_artifact)
return
install(ctx)
logger.info("Theme installation complete!")
try:
main()
except Exception as e:
logger.error("Something went wrong when installing the theme:", exc_info=e)