Debian Codegraph @

dh_makefont

raw symbols SCIP
1 #!/usr/bin/python3
2 #
3 # Copyright (C) 2012-2013 Vasudev Kamath <[email protected]>
4 # 2011-2012 Muneeb Shaikh <[email protected]>
5 # 2011-2012 Shravan Aras <[email protected]>
6 #
7 # License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl2.html>.
8
9 import os
10 import re
11 import argparse
12
13 __version__ = 0.3
14 ##########################################################################
15 # #
16 # Variables #
17 # #
18 ##########################################################################
19
20 required_files = [
21 "control",
22 "copyright",
23 "changelog",
24 "source",
25 "compat",
26 "rules",
27 "format",
28 ]
29
30 DEFAULT_MAINTAINER = (
31 "Debian Fonts Task Force <[email protected]>"
32 )
33
34 UPLOADERS = (
35 (os.environ.get("DEBFULLNAME") + " <" + os.environ.get("DEBEMAIL") + ">")
36 if "DEBEMAIL" in os.environ
37 else "#Please fill in your name and email"
38 )
39
40 PACKAGE = ""
41
42 version_string = """
43 dh_makefont - prepare Debian packaging for fonts, version {}
44
45 Copyright (C)
46 2012-2013 Vasudev Kamath <[email protected]>
47 2011-2012 Muneeb Shaikh <[email protected]>
48 2011-2012 Shravan Aras <[email protected]>
49 """.format(
50 __version__
51 )
52
53 GENERATES_TTF = False
54 GENERATES_OTF = False
55
56 font_reg_exp = re.compile(r"((.)*\.ttf|sfd|otf)") # RE to check
57 font_sfd_regx = re.compile(r"((.)*\.sfd)") # RE check font source file
58 generate_function = re.compile(r"Generate\(.*\)")
59
60 font_source = False
61
62 watch = """
63 version=3
64 #please put upstream url for watch expression
65 """
66
67
68 # This section of code parses the command line arguments.
69 arguments = argparse.ArgumentParser(
70 prog="dh_makefont",
71 description="dh_makefont",
72 formatter_class=argparse.RawTextHelpFormatter,
73 epilog="Font package helper",
74 )
75 arguments.add_argument(
76 "-c",
77 "--copyright",
78 help="""use <type> of license in copyright file
79 (apache|artistic|bsd|gpl|gpl2|gpl3|lgpl|lgpl2|
80 lgpl3|x11)""",
81 )
82 arguments.add_argument(
83 "-p",
84 "--package",
85 help="""force package name to be <name>
86 if name_version is given this will set version
87 number of package bypassing the directory
88 checking.""",
89 )
90 arguments.add_argument(
91 "--maint",
92 help="""Override default *Maintainer* which is
93 Debian Fonts Task Force
94 <[email protected]>""",
95 )
96 arguments.add_argument("foldername", help="Extracted folder containing fonts")
97
98 arguments.add_argument(
99 "-u",
100 "--upstream",
101 help="""Provide an upstream URL. If this value is provided
102 it will be substituted in control and copyright
103 file""",
104 )
105
106 arguments.add_argument(
107 "-v",
108 "--version",
109 action="version",
110 help="Show the version number of dh_makefont",
111 version=version_string,
112 )
113
114
115 ###########################################################################
116 # #
117 # Validation functions #
118 # #
119 ###########################################################################
120
121
122 def check_files():
123 """
124 This function checks if there is either .ttf .otf or .sfd
125 file in the given folder. If that file is present only then
126 we will continue after all this tool is for font packaging :)
127
128 `-return` 0 if one of the .sfd .ttf or .otf file is present
129 -1 other wise
130 """
131 global font_source
132 # We need to perform a directory traversal to find .ttf .otf or .sfd.
133 # Any one will do. [We are so kind aren't we]
134 for dirpath, dirnames, filenames in os.walk("."):
135 for filename in filenames:
136 if font_reg_exp.search(filename):
137 if font_sfd_regx.search(filename):
138 font_source = True
139 return 0
140 return -1 # No need to go into sub directories
141 return -1
142
143
144 def check_generatepe(filepath):
145 global GENERATES_TTF, GENERATES_OTF
146 with open(filepath) as fd:
147 content = fd.read()
148 if generate_function.search(content):
149 # We did find Generate function
150 for function in generate_function.findall(content):
151 if function.find(".ttf") != -1:
152 GENERATES_TTF = True
153 break
154 elif function.find(".otf") != -1:
155 GENERATES_OTF = True
156 break
157
158
159 ###########################################################################
160 # #
161 # Functions which edit the files inside debian/ folder of package #
162 # #
163 ###########################################################################
164
165
166 def update_control_file(args):
167 """
168 This function updates the control file which is generated by dh_make
169 1. Section is set to fonts
170 2. if Maintainer is not overridden by --maint DEFAULT_MAINTAINER
171 else use value given by user with --maint
172 3. Uploaders is added using DEBEMAIL and DEBFULLNAME environment
173 variable
174 4. Build-Depends will have fontforge dependency as a comment in
175 case upstream provides .sfd, user can uncomment it.
176 5. Architecture is changed to all
177 6. shlibs depends is removed from Depends
178
179 All remaining are kept as is and finally written back to control file
180 """
181 control_content = ""
182 with open("control", "rb") as fd:
183 contents = fd.read().split("\n")
184 for line in contents:
185 if line.startswith("Maintainer:"):
186 if args.maint:
187 control_content += "Maintainer: " + args.maint
188 else:
189 control_content += "Maintainer: " + DEFAULT_MAINTAINER
190 control_content += "\nUploaders: " + UPLOADERS
191 elif line.startswith("Build-Depends:"):
192 if font_source:
193 control_content += line + ", fontforge"
194 else:
195 control_content += line
196 elif line.startswith("Architecture:"):
197 control_content += "Architecture: all"
198 elif line.startswith("Section:"):
199 control_content += "Section: fonts"
200 elif line.startswith("Depends:"):
201 control_content += "Depends: ${misc:Depends}"
202 elif line.startswith("Homepage:") and args.upstream:
203 control_content += "Homepage: " + args.upstream
204 elif line.startswith("#Vcs-Git:") and not args.maint:
205 control_content += (
206 "#Vcs-Git:"
207 + "https://salsa.debian.org/debian/{}.git".format(PACKAGE)
208 )
209 elif line.startswith("#Vcs-Browser:") and not args.maint:
210 control_content += (
211 "Vcs-Browser: "
212 + "https://salsa.debian.org/debian/{}".format(PACKAGE)
213 )
214 else:
215 control_content += line
216
217 control_content += "\n"
218
219 with open("control", "wb") as fd:
220 fd.write(control_content)
221
222
223 def update_copyright_file(args):
224 """
225 `args`: Command line arguments
226 This function prepares copyright file in debian copyright-format 1.0.
227 """
228 copyright_content = ""
229 with open("copyright", "rb") as fd:
230 contents = fd.read()
231 contents = contents.split("\n")
232 for line in contents:
233 if line.startswith("Format:"):
234 copyright_content += (
235 "Format: "
236 "http://www.debian.org/doc/packaging-manuals/"
237 "copyright-format/1.0/"
238 )
239 elif line.startswith("Source:") and args.upstream:
240 copyright_content += "Source: " + args.upstream
241 else:
242 copyright_content += line
243
244 copyright_content += "\n"
245
246 with open("copyright", "wb") as fd:
247 fd.write(copyright_content)
248
249
250 def create_install_and_links():
251 """
252 This function writes install file it checks for availability of
253 ttf or otf file and writes a proper install file also if a font
254 config .conf file is available creates a links file to do proper
255 linking
256 """
257 global PACKAGE
258 install = ""
259 install_dir = PACKAGE.split("fonts-")[1]
260
261 for dirpath, dirnames, filenames in os.walk(".."):
262 if dirnames != "debian":
263 for filename in filenames:
264 if filename.endswith(".ttf"):
265 install += "./{} usr/share/fonts/truetype/{}/\n".format(
266 filename, install_dir
267 )
268 elif filename.endswith(".otf"):
269 install += "./{} usr/share/fonts/truetype/{}/\n".format(
270 filename, install_dir
271 )
272 elif filename.endswith(".sfd"):
273 if "generate.pe" in filenames:
274 check_generatepe(
275 os.path.join(
276 dirpath,
277 filenames[filenames.index("generate.pe")],
278 )
279 )
280 if GENERATES_TTF:
281 install += "./{} usr/share/fonts/truetype/{}/\n".format(
282 filename.replace("sfd", "ttf"), install_dir
283 )
284 elif GENERATES_OTF:
285 install += "./{} usr/share/fonts/opentype/{}/\n".format(
286 filename.replace("sfd", "otf"), install_dir
287 )
288 else:
289 print(
290 "\n*Unable to determine if source generates "
291 "TTF or OTF file. Please manually edit "
292 "the debian/install file*"
293 )
294 else:
295 print(
296 "\n*Unable to determine if source generates "
297 "TTF or OTF file.\nPlease manually edit "
298 "the debian/install file*"
299 )
300
301 elif filename.endswith(".conf"):
302 install += "./{} etc/fonts/conf.avail".format(filename)
303 print(
304 "\nFound a fontconfig configuration file. "
305 "Added it to debian/install"
306 )
307 with open("links", "w") as fd:
308 fd.write(
309 "etc/fonts/conf.avail/"
310 + filename
311 + " etc/fonts/conf.d/"
312 + filename
313 )
314 print(
315 "\nI've symlinked conf file in etc/fonts/conf.d"
316 ".\nPlease update fontconfig priority "
317 "appropriately"
318 )
319
320 with open("install", "w") as fd:
321 fd.write(install)
322
323
324 ###########################################################################
325 # #
326 # dh_make helper function #
327 # #
328 ###########################################################################
329
330
331 def call_dh_make(args):
332 """
333 `-args`: Arguments passed to this script this is of type NameSpace
334 class returned by argparse.parse_args
335 `-return`: Returns 0 like all other *nix utilities on success and non
336 zero value on error. Error should be documented in dh_make
337 manual
338
339 This function is a wrapper over the dh_make command
340 """
341 global PACKAGE
342
343 # Stores the final argument to be passed to dh_make
344 args_string = " --createorig"
345
346 if args.copyright:
347 args_string += " -c " + args.copyright
348 if args.package:
349 args_string += " -p " + args.package
350 PACKAGE = (
351 args.package.split("_")[0]
352 if args.package.find("_")
353 else args.package
354 )
355 else:
356 """
357 If user has not supplied -p or --package option which should be
358 passed to dh_make then we will do it ourselves rather than allowing
359 dh_make to decide
360
361 According to pkg-fonts policy font package should be named
362 `fonts-<foundry>-something` where foundry is optional.
363
364 1. We will check if folder name begins with fonts- if yes we will pass
365 it to dh_make with -p option.
366 2. If it doesn't we will create package name by appending fonts- to
367 `foldername`
368
369 Additionally foldername-version is changed to foldername_version which
370 is expected by dh_make as value for -p option
371
372 In case of any error dh_make will bark at you and not me :)
373 """
374
375 folder = ""
376 if args.foldername.count("-") > 1:
377 """
378 If we have more than one - in foldername that means foldername is
379 in format folder-name-version. So lets split on - and join first
380 2 part again on - and join last part with the latter with _ in
381 between
382
383 #FIXME: Bad English?
384 #FIXME: Any better way?
385 """
386 folder_split = args.foldername.split("-")
387 folder = ("-").join(folder_split[:-1]) + "_" + folder_split[-1]
388 elif args.foldername.count("-") == 1:
389 """
390 If there is only one - we are happy :)
391 """
392 folder = args.foldername.replace("-", "_")
393
394 if not folder.startswith("fonts-"):
395 args_string += " -p fonts-" + folder
396 folder = "fonts-" + folder
397 else:
398 args_string += " -p " + folder
399
400 PACKAGE = folder.split("_")[0] if folder.find("_") else folder
401
402 # Make a call to the system function.
403 return os.system("dh_make" + args_string)
404
405
406 ###########################################################################
407 # #
408 # Generic functions #
409 # #
410 ###########################################################################
411
412
413 def print_todo():
414 print(
415 """
416 **************************************************
417 TODO for the maintainers
418 **************************************************
419
420 1. Please add short and long description in debian/control file
421 2. Please add appropriate lines in debian/changelog
422 3. Please add DEP5 format URI in debian/copyright
423 4. Please add fonts copyright in debian/copyright
424 5. Check debian/install and debian/links if they exist
425 6. If font is built from source make sure font name and install target is
426 proper in debian/install
427 8. Please check Vcs-* field and modify accordingly, if maintaining under
428 pkg-fonts its preferred to leave as is.
429 7. Build the package and enjoy!
430 """
431 )
432
433
434 def main():
435 """
436 Main part of the script. Kept in separate function to have a good
437 readability
438 """
439 args = arguments.parse_args()
440
441 os.chdir(os.path.abspath(args.foldername))
442
443 if check_files():
444 print(
445 "I could not find .ttf, .sfd, .otf. "
446 "At least one should be present."
447 )
448 exit(256)
449
450 if call_dh_make(args):
451 print("dh_makefont: dh_make died so I die.")
452 exit(256)
453
454 # Lets get into Debian directory and work there
455 os.chdir(os.path.abspath("debian"))
456
457 # Remove unwanted files for font package
458 for dirpath, dirname, filenames in os.walk("."):
459 for filename in filenames:
460 if filename not in required_files:
461 os.remove(filename)
462
463 # Write a proper watch file
464 with open("watch", "w") as fd:
465 fd.write(watch)
466
467 # Fix control file
468 update_control_file(args)
469
470 # Fix copyright ile
471 update_copyright_file(args)
472
473 # Create install and links file
474 create_install_and_links()
475
476 # We are done tell packager what he needs to check
477 print_todo()
478
479
480 if __name__ == "__main__":
481 main()