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