]> git.lyx.org Git - lyx.git/blob - lib/scripts/TeXFiles.py
update layout files to format 101
[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 from __future__ import print_function
39 import os, sys, re
40
41 cls_stylefile = 'clsFiles.lst'
42 sty_stylefile = 'styFiles.lst'
43 bst_stylefile = 'bstFiles.lst'
44 bib_files = 'bibFiles.lst'
45 bbx_files = 'bbxFiles.lst'
46 cbx_files = 'cbxFiles.lst'
47
48 def cmdOutput(cmd):
49     '''utility function: run a command and get its output as a string
50         cmd: command to run
51     '''
52     fout = os.popen(cmd)
53     output = fout.read()
54     fout.close()
55     return output
56
57 # processing command line options
58 if len(sys.argv) > 1:
59     if sys.argv[1] in ['--help', '-help']:
60         print('''Usage: TeXFiles.py [-version | cls | sty | bst | bib | bbx| cbx ]
61             Default is without any Parameters,
62             so that all files will be created''')
63         sys.exit(0)
64     else:
65         types = sys.argv[1:]
66         for type in types:
67             if type not in ['cls', 'sty', 'bst', 'bib', 'bbx', 'cbx']:
68                 print('ERROR: unknown type', type)
69                 sys.exit(1)
70 else:
71     # if no parameter is specified, assume all
72     types = ['cls', 'sty', 'bst', 'bib', 'bbx', 'cbx']
73
74 #
75 # MS-DOS and MS-Windows define $COMSPEC or $ComSpec and use `;' to separate
76 # directories in path lists whereas Unix uses `:'.  Make an exception for
77 # Cygwin, where we could have either teTeX (using `:') or MikTeX (using `;').
78 # Create a variable that holds the right character to be used by the scripts.
79 path_sep = os.pathsep
80 if sys.platform == 'cygwin':
81     if ';' in cmdOutput('kpsewhich --show-path=.tex'):
82         path_sep = ';'
83     else:
84         path_sep = ':'
85
86 # process each file type
87 for type in types:
88     print("Indexing files of type", type)
89     if type == 'cls':
90         outfile = cls_stylefile
91         kpsetype = '.tex'
92     elif type == 'sty':
93         outfile = sty_stylefile
94         kpsetype = '.tex'
95     elif type == 'bst':
96         outfile = bst_stylefile
97         kpsetype = '.bst'
98     elif type == 'bib':
99         outfile = bib_files
100         kpsetype = '.bib'
101     elif type == 'bbx':
102         outfile = bbx_files
103         kpsetype = '.tex'
104     elif type == 'cbx':
105         outfile = cbx_files
106         kpsetype = '.tex'
107
108     dirs = cmdOutput('kpsewhich --show-path=' + kpsetype).replace('!!', '').strip()
109     # remove excessive //
110     dirs = re.sub('//+', '/', dirs)
111     
112     file_ext = '.' + type
113     out = open(outfile, 'w')
114     visited = set()
115     for dir in dirs.split(path_sep):
116         # for each valid directory
117         if not os.path.isdir(dir):
118             continue
119         # walk down the file hierarchy
120         for root,dirs,files in os.walk(dir, followlinks=True):
121             # prevent inifinite recursion
122             recurse = []
123             for dir in dirs:
124                 dirname = os.path.join(root, dir)
125                 dirname = os.path.realpath(dirname)
126                 dirname = os.path.normcase(dirname)
127                 if dirname not in visited:
128                     visited.add(dirname)
129                     recurse.append(dir)
130             dirs[:] = recurse
131             # check file type
132             for file in files:
133                 if len(file) > 4 and file[-4:] == file_ext:
134                     # force the use of / since miktex uses / even under windows
135                     print(root.replace('\\', '/') + '/' + file, file=out)
136     out.close()
137