forked from GloriousEggroll/protonfixes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corefonts.py
executable file
·144 lines (114 loc) · 4.25 KB
/
corefonts.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
""" Utilities for getting the ms corefonts for protonfixes
"""
import os
import shutil
import subprocess
import urllib.request
from threading import Thread
from tempfile import NamedTemporaryFile as Temp
from .logger import log
#pylint: disable=W0106
COREFONTS_DIR = os.path.expanduser('~/.config/protonfixes/mscorefonts/')
FONTS_LIST = [
'andalemo.ttf', 'arial.ttf', 'arialbd.ttf',
'arialbi.ttf', 'ariali.ttf', 'ariblk.ttf',
'comic.ttf', 'comicbd.ttf', 'cour.ttf',
'courbd.ttf', 'courbi.ttf', 'couri.ttf',
'georgia.ttf', 'georgiab.ttf', 'georgiai.ttf',
'georgiaz.ttf', 'impact.ttf', 'times.ttf',
'timesbd.ttf', 'timesbi.ttf', 'timesi.ttf',
'trebuc.ttf', 'trebucbd.ttf', 'trebucbi.ttf',
'trebucit.ttf', 'verdana.ttf', 'verdanab.ttf',
'verdanai.ttf', 'verdanaz.ttf', 'webdings.ttf',
]
def check_corefonts():
""" Returns True if all corefonts are in cache, else returns False
"""
if not os.path.isdir(COREFONTS_DIR):
return False
installed = os.listdir(COREFONTS_DIR)
fonts = [False for x in FONTS_LIST if x not in installed]
if all(fonts):
return True
return False
def download_file(url, files):
""" Downloads a file at url and returns the adds the filename to files
"""
log.debug('Downloading ' + url)
with urllib.request.urlopen(url) as font:
with Temp(delete=False, prefix='font' + str(os.getpid())) as temp:
shutil.copyfileobj(font, temp)
files.append(temp)
def extract_cab(filename, dst_dir):
""" Extract a cab file filename to dst_dir using cabextract or bsdtar
"""
log.debug('Extracting ' + filename)
cabextract = shutil.which('cabextract')
bsdtar = shutil.which('bsdtar')
if cabextract:
# cabextract command to extract just ttf files
cabextract_cmd = [cabextract,
'-L', # make filenames lowercase
'-F', '*.ttf', # only extract ttf
'-d', dst_dir, # extract to dst_dir
filename,
]
cab = subprocess.Popen(cabextract_cmd)
cab.wait()
return True
if bsdtar:
# bsdtar command to extract just ttf files
bsdtar_cmd = [bsdtar,
'-C', dst_dir,
'-xf', filename,
'*.[Tt][Tt][Ff]', # just TTF files
]
cab = subprocess.Popen(bsdtar_cmd)
cab.wait()
return True
return False
def get_corefonts():
""" Downloads the ms-corefonts from the pushcx/corefonts mirror
"""
if check_corefonts():
return
urlbase = 'https://github.com/pushcx/corefonts/raw/master/'
urlsuffix = '32.exe'
fonts = ['andale', 'arial', 'arialb', 'comic', 'courie', 'georgi',
'impact', 'times', 'trebuc', 'verdan', 'webdin', ]
try:
os.makedirs(COREFONTS_DIR)
except FileExistsError:
log.debug('Directory exists: ' + COREFONTS_DIR)
urls = [urlbase + x + urlsuffix for x in fonts]
files = []
# download files in threads
dl_threads = [Thread(target=download_file, args=(u, files))
for u in urls]
[thread.start() for thread in dl_threads]
[thread.join() for thread in dl_threads]
# extract files with threads
ex_threads = [Thread(target=extract_cab, args=(f.name, COREFONTS_DIR))
for f in files]
[thread.start() for thread in ex_threads]
[thread.join() for thread in ex_threads]
# rename files to lowercase
[shutil.move(os.path.join(d, f), os.path.join(d, f).lower())
for d, _, files in os.walk(COREFONTS_DIR)
for f in files]
# cleanup temp files
[log.debug('Removing ' + f.name) for f in files]
[os.remove(f.name) for f in files]
def link_fonts(directory):
""" Make links to core fonts in directory
"""
log.info('Creating MS Core font links in ' + directory)
src = COREFONTS_DIR
dst = directory
link_list = [(os.path.join(d, f), os.path.join(dst, f))
for d, _, files in os.walk(src) for f in files]
for source_file, dest_file in link_list:
try:
os.symlink(source_file, dest_file)
except FileExistsError:
log.debug('Not overwriting ' + dest_file)