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