-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-fonts
executable file
·88 lines (78 loc) · 2.2 KB
/
install-fonts
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
#!/usr/bin/env python3
import argparse
import os
import shutil
import subprocess
import sys
import tempfile
description = "Install fonts from Github"
ap = argparse.ArgumentParser(description=description)
ap.add_argument(
"-n",
"--no-update-cache",
dest="update_cache",
action="store_false",
help="Don't update the font cache after installing fonts",
)
ap.add_argument(
"-d",
"--directory",
dest="directory",
metavar="dir",
help="Font installation directory",
)
ap.add_argument(
"-r",
"--root",
dest="root",
action="store_true",
help="Install fonts as root",
)
args = ap.parse_args()
if args.root and os.getuid() != 0:
sys.exit(subprocess.call(["sudo"] + sys.argv))
if not args.directory:
if os.geteuid() == 0:
args.directory = "/usr/local/share/fonts/truetype"
else:
args.directory = os.path.join(os.path.expanduser("~"), ".fonts")
fonts = {
"smkent": "https://github.com/smkent/fonts",
"powerline": "https://github.com/powerline/fonts",
}
def download_fonts(dir_name, url):
temp_dir = None
try:
temp_dir = tempfile.mkdtemp(prefix=os.path.basename(sys.argv[0]) + ".")
subprocess.check_call(["git", "clone", url, "--depth", "1", temp_dir])
font_files = (
subprocess.check_output(
[
"find",
temp_dir,
"-iname",
"*.[o,t]tf",
"-or",
"-name",
"*.pcf.gz",
]
)
.decode("utf-8")
.splitlines()
)
dest_dir = os.path.join(args.directory, dir_name)
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir, 0o755)
subprocess.check_call(["cp", "-v", "-t", dest_dir] + font_files)
except Exception as e:
print(
"Error installing fonts from {}: {}".format(url, str(e)),
file=sys.stderr,
)
finally:
if temp_dir and os.path.isdir(temp_dir):
shutil.rmtree(temp_dir)
for dir_name, url in fonts.items():
download_fonts(dir_name, url)
if args.update_cache:
subprocess.check_call(["fc-cache", "-vf"])