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