Debian Codegraph @

texmf-dist/scripts/lilyglyphs/lily-rebuild-pdfs.py

raw
1 #!/usr/bin/env python
2
3 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 # %
5 # This file is part of the 'lilyglyphs' LaTeX package. %
6 # ========== %
7 # %
8 # https://github.com/openlilylib/lilyglyphs %
9 # http://www.openlilylib.org/lilyglyphs %
10 # %
11 # Copyright 2012-2020 Urs Liska and others, [email protected] %
12 # %
13 # 'lilyglyphs' is free software: you can redistribute it and/or modify %
14 # it under the terms of the LaTeX Project Public License, either %
15 # version 1.3 of this license or (at your option) any later version. %
16 # You may find the latest version of this license at %
17 # http://www.latex-project.org/lppl.txt %
18 # more information on %
19 # http://latex-project.org/lppl/ %
20 # and version 1.3 or later is part of all distributions of LaTeX %
21 # version 2005/12/01 or later. %
22 # %
23 # This work has the LPPL maintenance status 'maintained'. %
24 # The Current Maintainer of this work is Urs Liska (see above). %
25 # %
26 # This work consists of the files listed in the file 'manifest.txt' %
27 # which can be found in the 'license' directory. %
28 # %
29 # This program is distributed in the hope that it will be useful, %
30 # but WITHOUT ANY WARRANTY; without even the implied warranty of %
31 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. %
32 # %
33 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
35 # ########################################################################
36 # #
37 # rebuild-pdfs.py #
38 # #
39 # This program looks in the directories where the generated .ly files #
40 # and the generated pdf images are stored. #
41 # If it finds that there are missing pdfs it will recompile them #
42 # using LilyPond. #
43 # #
44 # The main purpose is to allow the creation of pdf images of glyphs #
45 # that already have been processed to LaTeX commands but whose PDF #
46 # files have been lost. #
47 # The other purpose is to update the set of PDF files to a new version #
48 # of Emmentaler or LilyPond without touching the definitions. #
49 # #
50 # ########################################################################
51
52 import os, sys, subprocess, argparse, lilyglyphs_common as lg
53
54
55 def main():
56 """Main walk through the program"""
57 print('rebuild-pdfs.py')
58 print('regenerate all pdf images that are not present (anymore)')
59 print('')
60
61 # Check if we are in a legal CWD and ensure a PDF subdir is present
62 check_paths()
63
64 # build a list of commands with missing PDF files
65 src_files = check_missing_pdfs()
66
67 # is there anything to be done at all?
68 if len(src_files) == 0:
69 print('')
70 print('No image files missing, nothing to be done.')
71 print('If you want to re-create pdfs, then delete them first')
72 sys.exit(0)
73 print('')
74 print('Found ' + str(len(src_files)) + ' missing file(s).')
75 for cmd in src_files:
76 print('- ' + cmd)
77
78 # compile all LilyPond files without matching pdf
79 lg.lily_files = src_files
80 lg.compile_lily_files()
81
82 # clean up directories
83 lg.cleanup_lily_files()
84
85 def check_missing_pdfs():
86 """Compares the list of LilyPond source and resulting PDF files.
87 Returns a list of LilyPond source file basenames
88 which don't have a corresponding PDF file"""
89 print('Reading file lists, counting missing pdf files')
90
91 # read existing .pdf files in lg.dir_pdfs
92 img_files = []
93 for entry in os.listdir(lg.dir_pdfs):
94 entry = os.path.join(lg.dir_pdfs, entry)
95 if os.path.isfile(entry):
96 path, name = os.path.split(entry)
97 basename, ext = os.path.splitext(name)
98 if ext == '.pdf':
99 img_files.append(basename)
100
101 # read existing .ly source files in in_dir
102 # and add them to the sources list if the image is missing
103 src_files = []
104 for entry in os.listdir(lg.dir_lysrc):
105 entry = os.path.join(lg.dir_lysrc, entry)
106 if os.path.isfile(entry):
107 path, name = os.path.split(entry)
108 basename, ext = os.path.splitext(name)
109 if ext == '.ly' and basename not in img_files:
110 src_files.append(basename)
111 return src_files
112
113 def check_paths():
114 """Checks if we're in the right CWD
115 and makes sure that there is a pdf output directory available"""
116
117 print('Checking directories ...')
118
119 # check the presence of the necessary subdirectories
120 ls = os.listdir('.')
121 if not 'generated_src' in ls:
122 print('No LilyPond source files directory found.')
123 print('Sorry, there is something wrong :-(')
124 print('Current working directory is: ' + os.getcwd())
125 print('Please consult the manual.')
126 sys.exit(2)
127 if not 'pdfs' in ls:
128 os.mkdir('pdfs')
129
130 print('... done')
131 print('')
132
133
134 # ####################################
135 # Finally launch the program
136 if __name__ == "__main__":
137 # parse command line arguments
138 parser = argparse.ArgumentParser(
139 description='Rebuild all pdfs missing in the \'pdfs\' subdiredtory',
140 parents=[lg.common_arguments])
141 args = parser.parse_args()
142 main()