]> git.lyx.org Git - lyx.git/blob - lib/scripts/TeXFiles.py
Replace TeXFiles.sh by TeXFiles.py
[lyx.git] / lib / scripts / TeXFiles.py
1 #!/usr/bin/env python
2 # -*- coding: iso-8859-15 -*-
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     sye.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   # MikTeX's kpsewhich says "kpathsea emulation version x.x.x", whereas
77   # teTeX's simply "kpathsea version x.x.x".
78   if 'emulation' in cmdOutput('kpsewhich --version'):
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
99   dirs = cmdOutput('kpsewhich --show-path=' + kpsetype).replace('!!', '').strip()
100   # remove excessive //
101   dirs = re.sub('//+', '/', dirs)
102   
103   file_ext = '.' + type
104   out = open(outfile, 'w')
105   for dir in dirs.split(path_sep):
106     # for each valid directory
107     if os.path.isdir(dir):
108       # walk down the file hierarchy
109       for root,path,files in os.walk(dir):
110         # check file type
111         for file in files:
112           if len(file) > 4 and file[-4:] == file_ext:
113             # force the use of / since miktex uses / even under windows
114             print >> out, root.replace('\\', '/') + '/' + file
115   out.close()
116