]> git.lyx.org Git - lyx.git/blob - lib/scripts/TeXFiles.py
Move DrawStrategy enum to update_flags.h.
[lyx.git] / lib / scripts / TeXFiles.py
1 # file TeXFiles.py
2 # This file is part of LyX, the document processor.
3 # Licence details can be found in the file COPYING.
4
5 # \author Herbert Voß
6 # \author Jean-Marc Lasgouttes
7 # \author Jürgen Spitzmüller
8 # \author Bo Peng
9
10 # Full author contact details are available in file CREDITS.
11
12 # all files             -> without option
13 # TeX class files       -> option cls
14 # TeX style files       -> option sty
15 # bibtex style files    -> option bst
16 # bibtex database files -> option bib
17 # biblatex bibstyles    -> option bbx
18 # biblatex citestyles   -> option cbx
19 #
20 # with the help
21 # of kpsewhich and creates a
22 # bstFiles.lst, clsFiles.lst, styFiles.lst, bibFiles.lst,
23 # bbxFiles.lst, cbxFiles.lst
24 # without any parameter all files are created.
25 #
26 # Herbert Voss <voss@perce.org>
27 #
28 # Updates from Jean-Marc Lasgouttes.
29 #
30 # bib, bbx and cbx support added by Juergen Spitzmueller (v0.4)
31 #
32 # translated to python by Bo Peng, so that the script only 
33 # relies on python and kpsewhich (no shell command is used).
34
35
36 import os, sys, re
37
38 cls_stylefile = 'clsFiles.lst'
39 sty_stylefile = 'styFiles.lst'
40 bst_stylefile = 'bstFiles.lst'
41 bib_files = 'bibFiles.lst'
42 bbx_files = 'bbxFiles.lst'
43 cbx_files = 'cbxFiles.lst'
44
45 def cmdOutput(cmd):
46     '''utility function: run a command and get its output as a string
47         cmd: command to run
48     '''
49     fout = os.popen(cmd)
50     output = fout.read()
51     fout.close()
52     return output
53
54 # processing command line options
55 if len(sys.argv) > 1:
56     if sys.argv[1] in ['--help', '-help']:
57         print('''Usage: TeXFiles.py [-version | cls | sty | bst | bib | bbx| cbx ]
58             Default is without any Parameters,
59             so that all files will be created''')
60         sys.exit(0)
61     else:
62         types = sys.argv[1:]
63         for type in types:
64             if type not in ['cls', 'sty', 'bst', 'bib', 'bbx', 'cbx']:
65                 print('ERROR: unknown type', type)
66                 sys.exit(1)
67 else:
68     # if no parameter is specified, assume all
69     types = ['cls', 'sty', 'bst', 'bib', 'bbx', 'cbx']
70
71 #
72 # MS-DOS and MS-Windows define $COMSPEC or $ComSpec and use `;' to separate
73 # directories in path lists whereas Unix uses `:'.  Make an exception for
74 # Cygwin, where we could have either teTeX (using `:') or MikTeX (using `;').
75 # Create a variable that holds the right character to be used by the scripts.
76 path_sep = os.pathsep
77 if sys.platform == 'cygwin':
78     if ';' in cmdOutput('kpsewhich --show-path=.tex'):
79         path_sep = ';'
80     else:
81         path_sep = ':'
82
83 # process each file type
84 for type in types:
85     print("Indexing files of type", type)
86     if type == 'cls':
87         outfile = cls_stylefile
88         kpsetype = '.tex'
89     elif type == 'sty':
90         outfile = sty_stylefile
91         kpsetype = '.tex'
92     elif type == 'bst':
93         outfile = bst_stylefile
94         kpsetype = '.bst'
95     elif type == 'bib':
96         outfile = bib_files
97         kpsetype = '.bib'
98     elif type == 'bbx':
99         outfile = bbx_files
100         kpsetype = '.tex'
101     elif type == 'cbx':
102         outfile = cbx_files
103         kpsetype = '.tex'
104
105     dirs = cmdOutput('kpsewhich --show-path=' + kpsetype).replace('!!', '').strip()
106     # remove excessive //
107     dirs = re.sub('//+', '/', dirs)
108     
109     file_ext = '.' + type
110     out = open(outfile, 'w')
111     visited = set()
112     for dir in dirs.split(path_sep):
113         # for each valid directory
114         if not os.path.isdir(dir):
115             continue
116         # walk down the file hierarchy
117         for root,dirs,files in os.walk(dir, followlinks=True):
118             # prevent inifinite recursion
119             recurse = []
120             for dir in dirs:
121                 dirname = os.path.join(root, dir)
122                 dirname = os.path.realpath(dirname)
123                 dirname = os.path.normcase(dirname)
124                 if dirname not in visited:
125                     visited.add(dirname)
126                     recurse.append(dir)
127             dirs[:] = recurse
128             # check file type
129             for file in files:
130                 if len(file) > 4 and file[-4:] == file_ext:
131                     # force the use of / since miktex uses / even under windows
132                     print(root.replace('\\', '/') + '/' + file, file=out)
133     out.close()
134