This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fontpkggen
executable file
·232 lines (196 loc) · 6.73 KB
/
fontpkggen
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
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/python
import argparse
import os
import os.path
import re
import hashlib
import shutil
from enum import Enum
# for debug
# import json
parser = argparse.ArgumentParser(
description='TTF/OTF font PKGBUILD and .install generator')
parser.add_argument(
'-a',
'--auto',
help='Make [pkgname] directory, copy fonts, generate PKGBUILD',
action='store_true')
parser.add_argument('-f',
'--force',
help='Replace existing file',
action='store_true')
parser.add_argument('--pkgver',
help='Package version default=1.0',
default='1.0')
parser.add_argument('--pkgrel',
help='Package release number default=1',
default='1')
parser.add_argument('--pkgdesc',
help='Package description default=\'[pkgname] font\'')
parser.add_argument('pkgname', help='Package name')
parser.add_argument('fontfiles', help='TTF/OTF font files', nargs='+')
args = parser.parse_args()
class FontpkgError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class FontType(Enum):
ttf = 1
otf = 2
class FontpkgInfo:
def __init__(self, pkgname, pkgver, pkgrel, pkgdesc, fonts):
self.check_pkgname(pkgname)
self.pkgname = pkgname
self.check_pkgver(pkgver)
self.pkgver = pkgver
self.check_pkgrel(pkgrel)
self.pkgrel = pkgrel
if pkgdesc:
self.pkgdesc = pkgdesc
else:
self.pkgdesc = '{} font'.format(pkgname)
self.fonts = []
for i in fonts:
name = os.path.basename(i)
font_type = self.check_type(name)
path = os.path.abspath(i)
md5 = self.calc_md5(path)
self.fonts.append({'name': name,
'path': path,
'md5': md5,
'type': font_type})
self.ttf, self.otf = self.check_fonts(self.fonts)
def __repr__(self):
return '{}({})'.format(self.__class__, self.__dict__)
def check_pkgname(self, s):
if not re.compile(r'^[a-z0-9._@+-]*$').search(s.strip()):
raise FontpkgError('pkgname error')
def check_pkgver(self, s):
if not re.compile(r'^[a-z0-9._]*$').search(s.strip()):
raise FontpkgError('pkgver error')
def check_pkgrel(self, s):
if not re.compile(r'^[0-9]*$').search(s.strip()):
raise FontpkgError('pkgrel error')
def check_type(self, s):
if s.lower().endswith('ttf'):
return FontType.ttf
elif s.lower().endswith('otf'):
return FontType.otf
else:
raise FontpkgError('{} is not TTF or OTF font'.format(i))
def check_fonts(self, s):
ttf = False
otf = False
for i in s:
if i['type'] == FontType.ttf:
ttf = True
elif i['type'] == FontType.otf:
otf = True
else:
raise FontpkgError('{} is not TTF or OTF font'.format(i))
return (ttf, otf)
def calc_md5(self, font):
with open(font, 'rb') as f:
m = hashlib.md5()
while True:
d = f.read(128)
if not d:
break
m.update(d)
return m.hexdigest()
def gen_install(self):
install = '''
post_install() {
echo -n "Updating font cache... "
fc-cache >/dev/null -f
'''
if self.ttf:
install += '''
mkfontscale /usr/share/fonts/TTF
mkfontdir /usr/share/fonts/TTF
'''
if self.otf:
install += '''
mkfontscale /usr/share/fonts/OTF
mkfontdir /usr/share/fonts/OTF
'''
install += '''
echo "done"
}
post_upgrade() {
post_install
}
post_remove() {
post_install
}
'''
return install.lstrip()
def gen_pkgbuild(self):
pkgbuild = ''
pkgbuild += 'pkgname={}\n'.format(self.pkgname)
pkgbuild += 'pkgver={}\n'.format(self.pkgver)
pkgbuild += 'pkgrel={}\n'.format(self.pkgrel)
pkgbuild += "pkgdesc='{}'\n".format(self.pkgdesc)
pkgbuild += "arch=('any')\n"
pkgbuild += "depends=('fontconfig' 'xorg-font-utils')\n"
names = []
for i in self.fonts:
names.append(i['name'])
pkgbuild += 'source=({})\n'.format("'" + "' '".join(names) + "'")
pkgbuild += 'install=$pkgname.install\n\n'
pkgbuild += 'package() {\n'
if self.ttf:
pkgbuild += ' install -d "$pkgdir/usr/share/fonts/TTF"\n'
for i in self.fonts:
if i['type'] == FontType.ttf:
pkgbuild += ' install -m644 "$srcdir/{}" "$pkgdir/usr/share/fonts/TTF/"\n'.format(
i['name'])
if self.otf:
pkgbuild += ' install -d "$pkgdir/usr/share/fonts/OTF"\n'
for i in self.fonts:
if i['type'] == FontType.otf:
pkgbuild += ' install -m644 "$srcdir/{}" "$pkgdir/usr/share/fonts/OTF/"\n'.format(
i['name'])
pkgbuild += '}\n\n'
md5s = []
for i in self.fonts:
md5s.append(i['md5'])
pkgbuild += 'md5sums=({})\n'.format("'" + "'\n '".join(md5s) +
"'")
return pkgbuild
error_list = []
for i in args.fontfiles:
if not os.path.exists(i):
error_list.append(i)
if not len(error_list) == 0:
raise FontpkgError('cannot access ' + ', '.join(error_list) +
': No such file')
fi = FontpkgInfo(args.pkgname, args.pkgver, args.pkgrel, args.pkgdesc,
args.fontfiles)
# class FontpkgInfoJSONEncoder(json.JSONEncoder):
# def default(self, o):
# if isinstance(o, FontType):
# return str(o)
# return json.JSONEncoder.default(self, o)
# print(json.dumps(fi.__dict__, sort_keys=True, indent=4, cls=FontpkgInfoJSONEncoder))
if args.auto:
if os.path.exists(fi.pkgname):
raise FontpkgError(
'cannot create directory \'{}\': File exists'.format(fi.pkgname))
else:
os.mkdir(fi.pkgname)
os.chdir(fi.pkgname)
for i in fi.fonts:
shutil.copy(i['path'], '.')
install_file = '{}.install'.format(fi.pkgname)
if not args.force:
if os.path.exists(install_file):
raise FontpkgError('cannot create \'{}\': File exists'.format(
install_file))
if os.path.exists('PKGBUILD'):
raise FontpkgError('cannot create \'PKGBUILD\': File exists')
with open(install_file, 'w') as f:
f.write(fi.gen_install())
with open('PKGBUILD', 'w') as f:
f.write(fi.gen_pkgbuild())