]> git.lyx.org Git - lyx.git/blob - lib/configure.py
8748943e85e7fece11e5021e13d6d455ec14255a
[lyx.git] / lib / configure.py
1 #! /usr/bin/env python
2 #
3 # file configure.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6
7 # \author Bo Peng
8 # Full author contact details are available in file CREDITS.
9
10 # This is an experimental version of the configure script, written
11 # in Python. 
12
13 import sys, os, re, shutil, glob
14
15
16 def writeToFile(filename, lines, append = False):
17     " utility function: write or append lines to filename "
18     if append:
19         file = open(filename, 'a')
20     else:
21         file = open(filename, 'w')
22     file.write(lines)
23     file.close()
24
25
26 def addToRC(lines):
27     ''' utility function: shortcut for appending lines to outfile
28         add newline at the end of lines.
29     '''
30     if lines.strip() != '':
31         writeToFile(outfile, lines + '\n', append = True)
32
33
34 def removeFiles(filenames):
35     '''utility function: 'rm -f'
36         ignore errors when file does not exist, or is a directory.
37     '''
38     for file in filenames:
39         try:
40             os.remove(file)
41         except:
42             pass
43
44
45 def cmdOutput(cmd):
46     '''utility function: run a command and get its output as a string
47         cmd: command to run
48     '''
49     fout = os.popen(cmd)
50     output = fout.read()
51     fout.close()
52     return output.strip()
53
54
55 def setEnviron():
56     ''' I do not really know why this is useful, but we might as well keep it.
57         NLS nuisances.
58         Only set these to C if already set.  These must not be set unconditionally
59         because not all systems understand e.g. LANG=C (notably SCO).
60         Fixing LC_MESSAGES prevents Solaris sh from translating var values in `set'!
61         Non-C LC_CTYPE values break the ctype check.
62     '''
63     os.environ['LANG'] = os.getenv('LANG', 'C')
64     os.environ['LC'] = os.getenv('LC_ALL', 'C')
65     os.environ['LC_MESSAGE'] = os.getenv('LC_MESSAGE', 'C')
66     os.environ['LC_CTYPE'] = os.getenv('LC_CTYPE', 'C')
67
68
69 def createDirectories():
70     ''' Create the build directories if necessary '''
71     for dir in ['bind', 'clipart', 'doc', 'examples', 'images', 'kbd', \
72         'layouts', 'scripts', 'templates', 'ui' ]:
73         if not os.path.isdir( dir ):
74             try:
75                 os.mkdir( dir)
76             except:
77                 print "Failed to create directory ", dir
78                 sys.exit(1)
79
80
81 def checkTeXPaths():
82     ''' Determine the path-style needed by the TeX engine on Win32 (Cygwin) '''
83     windows_style_tex_paths = ''
84     if os.name == 'nt' or sys.platform == 'cygwin':
85         from tempfile import mkstemp
86         fd, tmpfname = mkstemp(suffix='.ltx')
87         # a wrapper file
88         wfd, wtmpfname = mkstemp(suffix='.ltx')
89         if os.name == 'nt':
90             inpname = tmpfname.replace('\\', '/')
91             wtmpfname = wtmpfname.replace('\\', '/')
92         else:
93             inpname = cmdOutput('cygpath -m ' + tmpfname)
94             wtmpfname = cmdOutput('cygpath -m ' + wtmpfname)
95         os.write(fd, r'''
96 \documentstyle{article}
97 \begin{document}\end{document}
98     ''')
99         os.close(fd)
100         os.write(wfd, r'\input{' + inpname.replace('~', '\\string~') + '}' )
101         os.close(wfd)
102         if cmdOutput('latex ' + wtmpfname).find('Error') != -1:
103             print "configure: TeX engine needs posix-style paths in latex files"
104             windows_style_tex_paths = 'false'
105         else:
106             print "configure: TeX engine needs windows-style paths in latex files"
107             windows_style_tex_paths = 'true'
108         tmpbname,ext = os.path.splitext(os.path.basename(tmpfname))
109         wtmpbname,ext = os.path.splitext(os.path.basename(wtmpfname))
110         removeFiles( [ tmpfname, wtmpfname, tmpbname + '.log', \
111             tmpbname + '.aux', wtmpbname + '.log', wtmpbname + '.aux' ] )
112     return windows_style_tex_paths
113
114
115 ## Searching some useful programs
116 def checkProg(description, progs, rc_entry = [], path = [], not_found = ''):
117     '''
118         This function will search a program in $PATH plus given path
119         If found, return directory and program name (not the options).
120
121         description: description of the program
122
123         progs: check programs, for each prog, the first word is used
124             for searching but the whole string is used to replace
125             %% for a rc_entry. So, feel free to add '$$i' etc for programs.
126
127         path: additional pathes
128
129         rc_entry: entry to outfile, can be
130             1. emtpy: no rc entry will be added
131             2. one pattern: %% will be replaced by the first found program,
132                 or '' if no program is found.
133             3. several patterns for each prog and not_found. This is used 
134                 when different programs have different usages. If you do not 
135                 want not_found entry to be added to the RC file, you can specify 
136                 an entry for each prog and use '' for the not_found entry.
137
138         not_found: the value that should be used instead of '' if no program
139             was found
140
141     '''
142     # one rc entry for each progs plus not_found entry
143     if len(rc_entry) > 1 and len(rc_entry) != len(progs) + 1:
144         print "rc entry should have one item or item for each prog and not_found."
145         sys.exit(2)
146     print 'checking for ' + description + '...'
147     ## print '(' + ','.join(progs) + ')',
148     for idx in range(len(progs)):
149         # ac_prog may have options, ac_word is the command name
150         ac_prog = progs[idx]
151         ac_word = ac_prog.split(' ')[0]
152         print '+checking for "' + ac_word + '"... ',
153         path = os.environ["PATH"].split(os.pathsep) + path
154         for ac_dir in path:
155             # check both ac_word and ac_word.exe (for windows system)
156             if os.path.isfile( os.path.join(ac_dir, ac_word) ) or \
157                 os.path.isfile( os.path.join(ac_dir, ac_word + ".exe") ):
158                 print ' yes'
159                 # write rc entries for this command
160                 if len(rc_entry) == 1:
161                     addToRC(rc_entry[0].replace('%%', ac_prog))
162                 elif len(rc_entry) > 1:
163                     addToRC(rc_entry[idx].replace('%%', ac_prog))
164                 return [ac_dir, ac_word]
165         # if not successful
166         print ' no'
167     # write rc entries for 'not found'
168     if len(rc_entry) > 0:  # the last one.
169         addToRC(rc_entry[-1].replace('%%', not_found))
170     return ['', not_found]
171
172
173 def checkViewer(description, progs, rc_entry = [], path = []):
174     ''' The same as checkProg, but for viewers and editors '''
175     return checkProg(description, progs, rc_entry, path, not_found = 'auto')
176
177
178 def checkLatex():
179     ''' Check latex, return lyx_check_config '''
180     # Find programs! Returned path is not used now
181     if ((os.name == 'nt' or sys.platform == 'cygwin') and
182             checkProg('DVI to DTL converter', ['dv2dt']) != ['', ''] and
183             checkProg('DTL to DVI converter', ['dt2dv']) != ['', '']):
184         # Windows only: DraftDVI
185         converter_entry = r'''\converter latex      dvi2       "%%"     "latex"
186 \converter dvi2       dvi        "python $$s/scripts/clean_dvi.py $$i $$o"      ""'''
187     else:
188         converter_entry = r'\converter latex      dvi        "%%"       "latex"'
189     path, LATEX = checkProg('a Latex2e program', ['pplatex $$i', 'latex $$i', 'latex2e $$i'],
190         rc_entry = [converter_entry])
191     # no latex
192     if LATEX != '':
193         # Check if latex is usable
194         writeToFile('chklatex.ltx', '''
195 \\nonstopmode\\makeatletter
196 \\ifx\\undefined\\documentclass\\else
197   \\message{ThisIsLaTeX2e}
198 \\fi
199 \\@@end
200 ''')
201         # run latex on chklatex.ltx and check result
202         if cmdOutput(LATEX + ' chklatex.ltx').find('ThisIsLaTeX2e') != -1:
203             # valid latex2e
204             return LATEX
205         else:
206             print "Latex not usable (not LaTeX2e) "
207         # remove temporary files
208         removeFiles(['chklatex.ltx', 'chklatex.log'])
209     return ''
210
211
212 def checkFormatEntries():  
213     ''' Check all formats (\Format entries) '''
214     checkViewer('a Tgif viewer and editor', ['tgif'],
215         rc_entry = [r'\Format tgif       obj     Tgif                   "" "%%" "%%"    ""'])
216     #
217     checkViewer('a FIG viewer and editor', ['xfig'],
218         rc_entry = [r'\Format fig        fig     FIG                    "" "%%" "%%"    ""'])
219     #
220     checkViewer('a Grace viewer and editor', ['xmgrace'],
221         rc_entry = [r'\Format agr        agr     Grace                  "" "%%" "%%"    ""'])
222     #
223     checkViewer('a FEN viewer and editor', ['xboard -lpf $$i -mode EditPosition'],
224         rc_entry = [r'\Format fen        fen     FEN                    "" "%%" "%%"    ""'])
225     #
226     path, iv = checkViewer('a raster image viewer', ['xv', 'kview', 'gimp'])
227     path, ie = checkViewer('a raster image editor', ['gimp'])
228     addToRC(r'''\Format bmp        bmp     BMP                    "" "%s"       "%s"    ""
229 \Format gif        gif     GIF                    "" "%s"       "%s"    ""
230 \Format jpg        jpg     JPEG                   "" "%s"       "%s"    ""
231 \Format pbm        pbm     PBM                    "" "%s"       "%s"    ""
232 \Format pgm        pgm     PGM                    "" "%s"       "%s"    ""
233 \Format png        png     PNG                    "" "%s"       "%s"    ""
234 \Format ppm        ppm     PPM                    "" "%s"       "%s"    ""
235 \Format tiff       tif     TIFF                   "" "%s"       "%s"    ""
236 \Format xbm        xbm     XBM                    "" "%s"       "%s"    ""
237 \Format xpm        xpm     XPM                    "" "%s"       "%s"    ""''' % \
238         (iv, ie, iv, ie, iv, ie, iv, ie, iv, ie, iv, ie, iv, ie, iv, ie, iv, ie, iv, ie) )
239     #
240     checkViewer('a text editor', ['xemacs', 'gvim', 'kedit', 'kwrite', 'kate', \
241         'nedit', 'gedit', 'notepad'],
242         rc_entry = [r'''\Format asciichess asc    "Plain text (chess output)"  "" ""    "%%"    ""
243 \Format asciiimage asc    "Plain text (image)"         "" ""    "%%"    ""
244 \Format asciixfig  asc    "Plain text (Xfig output)"   "" ""    "%%"    ""
245 \Format dateout    tmp    "date (output)"         "" "" "%%"    ""
246 \Format docbook    sgml    DocBook                B  "" "%%"    "document"
247 \Format docbook-xml xml   "Docbook (XML)"         "" "" "%%"    "document"
248 \Format literate   nw      NoWeb                  N  "" "%%"    "document"
249 \Format latex      tex    "LaTeX (plain)"         L  "" "%%"    "document"
250 \Format linuxdoc   sgml    LinuxDoc               x  "" "%%"    "document"
251 \Format pdflatex   tex    "LaTeX (pdflatex)"      "" "" "%%"    "document"
252 \Format text       txt    "Plain text"            a  "" "%%"    "document"
253 \Format textparagraph txt "Plain text (paragraphs)"    "" ""    "%%"    "document"''' ])
254     #
255     #checkProg('a Postscript interpreter', ['gs'],
256     #  rc_entry = [ r'\ps_command "%%"' ])
257     checkViewer('a Postscript previewer', ['gv', 'ghostview -swap', 'kghostview'],
258         rc_entry = [r'''\Format eps        eps     EPS                    "" "%%"       ""      ""
259 \Format ps         ps      Postscript             t  "%%"       ""      "document"'''])
260     #
261     checkViewer('a PDF previewer', ['acrobat', 'acroread', 'gv', 'ghostview', \
262                             'xpdf', 'kpdf', 'kghostview'],
263         rc_entry = [r'''\Format pdf        pdf    "PDF (ps2pdf)"          P  "%%"       ""      "document"
264 \Format pdf2       pdf    "PDF (pdflatex)"        F  "%%"       ""      "document"
265 \Format pdf3       pdf    "PDF (dvipdfm)"         m  "%%"       ""      "document"'''])
266     #
267     checkViewer('a DVI previewer', ['xdvi', 'kdvi'],
268         rc_entry = [r'\Format dvi        dvi     DVI                    D  "%%" ""      "document"'])
269     if ((os.name == 'nt' or sys.platform == 'cygwin') and
270             checkProg('DVI to DTL converter', ['dv2dt']) != ['', ''] and
271             checkProg('DTL to DVI converter', ['dt2dv']) != ['', '']):
272         # Windows only: DraftDVI
273         addToRC(r'\Format dvi2       dvi     DraftDVI               ""  ""      "document"')
274     #
275     checkViewer('a HTML previewer', ['mozilla file://$$p$$i', 'netscape'],
276         rc_entry = [r'\Format html       html    HTML                   H  "%%" ""      "document"'])
277     #
278     # entried that do not need checkProg
279     addToRC(r'''\Format date       ""     "date command"          "" "" ""      ""
280 \Format fax        ""      Fax                    "" "" ""      "document"
281 \Format lyx        lyx     LyX                    "" "" ""      ""
282 \Format lyx13x     lyx13  "LyX 1.3.x"             "" "" ""      "document"
283 \Format lyxpreview lyxpreview "LyX Preview"       "" "" ""      ""
284 \Format pdftex     pdftex_t PDFTEX                "" "" ""      ""
285 \Format program    ""      Program                "" "" ""      ""
286 \Format pstex      pstex_t PSTEX                  "" "" ""      ""
287 \Format sxw        sxw    "OpenOffice.Org Writer" O  "" ""      "document"
288 \Format word       doc    "MS Word"               W  "" ""      "document"
289 \Format wordhtml   html   "MS Word (HTML)"        "" ""        ""       "document"
290 ''')
291
292
293 def checkConverterEntries():
294     ''' Check all converters (\converter entries) '''
295     checkProg('the pdflatex program', ['pdflatex $$i'],
296         rc_entry = [ r'\converter pdflatex   pdf2       "%%"    "latex"' ])
297     
298     ''' If we're running LyX in-place then tex2lyx will be found in
299             ../src/tex2lyx. Add this directory to the PATH temporarily and
300             search for tex2lyx.
301             Use PATH to avoid any problems with paths-with-spaces.
302     '''
303     path_orig = os.environ["PATH"]
304     os.environ["PATH"] = os.path.join('..', 'src', 'tex2lyx') + \
305         os.pathsep + path_orig
306
307     checkProg('a LaTeX -> LyX converter', ['tex2lyx -f $$i $$o', \
308         'tex2lyx' +  version_suffix + ' -f $$i $$o' ],
309         rc_entry = [ r'\converter latex      lyx        "%%"    ""' ])
310
311     os.environ["PATH"] = path_orig
312
313     #
314     checkProg('a Noweb -> LyX converter', ['noweb2lyx' + version_suffix + ' $$i $$o'], path = ['./reLyX'],
315         rc_entry = [ r'\converter literate   lyx        "%%"    ""' ])
316     #
317     checkProg('a Noweb -> LaTeX converter', ['noweave -delay -index $$i > $$o'],
318         rc_entry = [ r'\converter literate   latex      "%%"    ""' ])
319     #
320     checkProg('a HTML -> LaTeX converter', ['html2latex $$i'],
321         rc_entry = [ r'\converter html       latex      "%%"    ""' ])
322     #
323     checkProg('a MSWord -> LaTeX converter', ['wvCleanLatex $$i $$o'],
324         rc_entry = [ r'\converter word       latex      "%%"    ""' ])
325     #
326     checkProg('a LaTeX -> MS Word converter', ["htlatex $$i 'html,word' 'symbol/!' '-cvalidate'"],
327         rc_entry = [ r'\converter latex      wordhtml   "%%"    ""' ])
328     #
329     checkProg('an OpenOffice.org -> LaTeX converter', ['w2l -clean $$i'],
330         rc_entry = [ r'\converter sxw        latex      "%%"    ""' ])
331     #
332     checkProg('an LaTeX -> OpenOffice.org LaTeX converter', ['oolatex $$i', 'oolatex.sh $$i'],
333         rc_entry = [ r'\converter latex      sxw        "%%"    "latex"' ])
334     #
335     checkProg('a PS to PDF converter', ['ps2pdf13 $$i $$o'],
336         rc_entry = [ r'\converter ps         pdf        "%%"    ""' ])
337     #
338     checkProg('a DVI to PS converter', ['dvips -o $$o $$i'],
339         rc_entry = [ r'\converter dvi        ps         "%%"    ""' ])
340     #
341     checkProg('a DVI to PDF converter', ['dvipdfmx -o $$o $$i', 'dvipdfm -o $$o $$i'],
342         rc_entry = [ r'\converter dvi        pdf3       "%%"    ""' ])
343     #
344     path, dvipng = checkProg('dvipng', ['dvipng'])
345     if dvipng == "dvipng":
346         addToRC(r'\converter lyxpreview png        "python $$s/scripts/lyxpreview2bitmap.py"    ""')
347     else:
348         addToRC(r'\converter lyxpreview png        ""   ""')
349     #  
350     checkProg('a fax program', ['kdeprintfax $$i', 'ksendfax $$i'],
351         rc_entry = [ r'\converter ps         fax        "%%"    ""'])
352     #
353     checkProg('a FIG -> EPS/PPM converter', ['fig2dev'],
354         rc_entry = [
355             r'''\converter fig        eps        "fig2dev -L eps $$i $$o"       ""
356 \converter fig        ppm        "fig2dev -L ppm $$i $$o"       ""
357 \converter fig        png        "fig2dev -L png $$i $$o"       ""''',
358             ''])
359     #
360     checkProg('a TIFF -> PS converter', ['tiff2ps $$i > $$o'],
361         rc_entry = [ r'\converter tiff       eps        "%%"    ""', ''])
362     #
363     checkProg('a TGIF -> EPS/PPM converter', ['tgif'],
364         rc_entry = [
365             r'''\converter tgif       eps        "tgif -stdout -print -color -eps $$i > $$o"    ""
366 \converter tgif       ppm        "tgif -stdout -print -color -ppm $$i > $$o"    ""
367 \converter tgif       png        "tgif -stdout -print -color -png $$i > $$o"    ""
368 \converter tgif       pdf        "tgif -stdout -print -color -pdf $$i > $$o"    ""''',
369             ''])
370     #
371     checkProg('a EPS -> PDF converter', ['epstopdf'],
372         rc_entry = [ r'\converter eps        pdf        "epstopdf --outfile=$$o $$i"    ""', ''])
373     #
374     checkProg('a Grace -> Image converter', ['gracebat'],
375         rc_entry = [
376             r'''\converter agr        eps        "gracebat -hardcopy -printfile $$o -hdevice EPS $$i 2>/dev/null"       ""
377 \converter agr        png        "gracebat -hardcopy -printfile $$o -hdevice PNG $$i 2>/dev/null"       ""
378 \converter agr        jpg        "gracebat -hardcopy -printfile $$o -hdevice JPEG $$i 2>/dev/null"      ""
379 \converter agr        ppm        "gracebat -hardcopy -printfile $$o -hdevice PNM $$i 2>/dev/null"       ""''',
380             ''])
381     #
382     checkProg('a LaTeX -> HTML converter', ['htlatex $$i', 'tth  -t -e2 -L$$b < $$i > $$o', \
383         'latex2html -no_subdir -split 0 -show_section_numbers $$i', 'hevea -s $$i'],
384         rc_entry = [ r'\converter latex      html       "%%"    "originaldir,needaux"' ])
385     #
386     # FIXME: no rc_entry? comment it out
387     # checkProg('Image converter', ['convert $$i $$o'])
388     #
389     # Entried that do not need checkProg
390     addToRC(r'''\converter lyxpreview ppm        "python $$s/scripts/lyxpreview2bitmap.py"      ""
391 \converter date       dateout    "date +%d-%m-%Y > $$o" ""
392 \converter docbook    docbook-xml "cp $$i $$o"  "xml"
393 \converter fen        asciichess "python $$s/scripts/fen2ascii.py $$i $$o"      ""
394 \converter fig        pdftex     "sh $$s/scripts/fig2pdftex.sh $$i $$o" ""
395 \converter fig        pstex      "sh $$s/scripts/fig2pstex.sh $$i $$o"  ""
396 \converter lyx        lyx13x     "python $$s/lyx2lyx/lyx2lyx -t 221 $$i > $$o"  ""
397 ''')
398
399
400 def checkLinuxDoc():
401     ''' Check linuxdoc '''
402     #
403     path, LINUXDOC = checkProg('SGML-tools 1.x (LinuxDoc)', ['sgml2lyx'],
404         rc_entry = [
405         r'''\converter linuxdoc   lyx        "sgml2lyx $$i"     ""
406 \converter linuxdoc   latex      "sgml2latex $$i"       ""
407 \converter linuxdoc   dvi        "sgml2latex -o dvi $$i"        ""
408 \converter linuxdoc   html       "sgml2html $$i"        ""''',
409         r'''\converter linuxdoc   lyx        "" ""
410 \converter linuxdoc   latex      ""     ""
411 \converter linuxdoc   dvi        ""     ""
412 \converter linuxdoc   html       ""     ""''' ])
413     if LINUXDOC != '':
414         return ('yes', 'true', '\\def\\haslinuxdoc{yes}')
415     else:
416         return ('no', 'false', '')
417
418
419 def checkDocBook():
420     ''' Check docbook '''
421     path, DOCBOOK = checkProg('SGML-tools 2.x (DocBook) or db2x scripts', ['sgmltools', 'db2dvi'],
422         rc_entry = [
423             r'''\converter docbook    dvi        "sgmltools -b dvi $$i" ""
424 \converter docbook    html       "sgmltools -b html $$i"        ""''',
425             r'''\converter docbook    dvi        "db2dvi $$i"   ""
426 \converter docbook    html       "db2html $$i"  ""''',
427             r'''\converter docbook    dvi        ""     ""
428 \converter docbook    html       ""     ""'''])
429     #
430     if DOCBOOK != '':
431         return ('yes', 'true', '\\def\\hasdocbook{yes}')
432     else:
433         return ('no', 'false', '')
434
435
436 def checkOtherEntries():
437     ''' entries other than Format and Converter '''
438     checkProg('a *roff formatter', ['groff', 'nroff'],
439         rc_entry = [
440             r'\ascii_roff_command "groff -t -Tlatin1 $$FName"',
441             r'\ascii_roff_command "tbl $$FName | nroff"',
442             r'\ascii_roff_command ""' ])
443     checkProg('ChkTeX', ['chktex -n1 -n3 -n6 -n9 -n22 -n25 -n30 -n38'],
444         rc_entry = [ r'\chktex_command "%%"' ])
445     checkProg('a spellchecker', ['ispell'],
446         rc_entry = [ r'\spell_command "%%"' ])
447     ## FIXME: OCTAVE is not used anywhere
448     # path, OCTAVE = checkProg('Octave', ['octave'])
449     ## FIXME: MAPLE is not used anywhere
450     # path, MAPLE = checkProg('Maple', ['maple'])
451     checkProg('a spool command', ['lp', 'lpr'],
452         rc_entry = [
453             r'''\print_spool_printerprefix "-d "
454 \print_spool_command "lp"''',
455             r'''\print_spool_printerprefix "-P",
456 \print_spool_command "lpr"''',
457             ''])
458     # Add the rest of the entries (no checkProg is required)
459     addToRC(r'''\copier    fig        "sh $$s/scripts/fig_copy.sh $$i $$o"
460 \copier    pstex      "python $$s/scripts/tex_copy.py $$i $$o $$l"
461 \copier    pdftex     "python $$s/scripts/tex_copy.py $$i $$o $$l"
462 ''')
463
464
465 def processLayoutFile(file, bool_docbook, bool_linuxdoc):
466     ''' process layout file and get a line of result
467         
468         Declear line are like this: (article.layout, scrbook.layout, svjog.layout)
469         
470         \DeclareLaTeXClass{article}
471         \DeclareLaTeXClass[scrbook]{book (koma-script)}
472         \DeclareLaTeXClass[svjour,svjog.clo]{article (Springer - svjour/jog)}
473
474         we expect output:
475         
476         "article" "article" "article" "false"
477         "scrbook" "scrbook" "book (koma-script)" "false"
478         "svjog" "svjour" "article (Springer - svjour/jog)" "false"
479     '''
480     classname = file.split(os.sep)[-1].split('.')[0]
481     # return ('LaTeX', '[a,b]', 'a', ',b,c', 'article') for \DeclearLaTeXClass[a,b,c]{article}
482     p = re.compile(r'\Declare(LaTeX|DocBook|LinuxDoc)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}')
483     for line in open(file).readlines():
484         res = p.search(line)
485         if res != None:
486             (classtype, optAll, opt, opt1, desc) = res.groups()
487             avai = {'LaTeX':'false', 'DocBook':bool_docbook, 'LinuxDoc':bool_linuxdoc}[classtype]
488             if opt == None:
489                 opt = classname
490             return '"%s" "%s" "%s" "%s"\n' % (classname, opt, desc, avai)
491     print "Layout file without \DeclearXXClass line. "
492     sys.exit(2)
493
494     
495 def checkLatexConfig(check_config, bool_docbook, bool_linuxdoc):
496     ''' Explore the LaTeX configuration '''
497     print 'checking LaTeX configuration... ',
498     # First, remove the files that we want to re-create
499     removeFiles(['textclass.lst', 'packages.lst', 'chkconfig.sed'])
500     #
501     if not check_config:
502         print ' default values'
503         print '+checking list of textclasses... '
504         tx = open('textclass.lst', 'w')
505         tx.write('''
506 # This file declares layouts and their associated definition files
507 # (include dir. relative to the place where this file is).
508 # It contains only default values, since chkconfig.ltx could not be run
509 # for some reason. Run ./configure if you need to update it after a
510 # configuration change.
511 ''')
512         # build the list of available layout files and convert it to commands
513         # for chkconfig.ltx
514         foundClasses = []
515         for file in glob.glob( os.path.join('layouts', '*.layout') ) + \
516             glob.glob( os.path.join(srcdir, 'layouts', '*.layout' ) ) :
517             # valid file?
518             if not os.path.isfile(file): 
519                 continue
520             # get stuff between /xxxx.layout .
521             classname = file.split(os.sep)[-1].split('.')[0]
522             #  tr ' -' '__'`
523             cleanclass = classname.replace(' ', '_')
524             cleanclass = cleanclass.replace('-', '_')
525             # make sure the same class is not considered twice
526             if foundClasses.count(cleanclass) == 0: # not found before
527                 foundClasses.append(cleanclass)
528                 tx.write(processLayoutFile(file, bool_docbook, bool_linuxdoc))
529         tx.close()
530         print '\tdone'
531     else:
532         print '\tauto'
533         removeFiles(['wrap_chkconfig.ltx', 'chkconfig.vars', \
534             'chkconfig.classes', 'chklayouts.tex'])
535         rmcopy = False
536         if not os.path.isfile( 'chkconfig.ltx' ):
537             shutil.copy( os.path.join(srcdir, 'chkconfig.ltx'),  'chkconfig.ltx' )
538             rmcopy = True
539         writeToFile('wrap_chkconfig.ltx', '%s\n%s\n\\input{chkconfig.ltx}\n' \
540             % (linuxdoc_cmd, docbook_cmd) )
541         # Construct the list of classes to test for.
542         # build the list of available layout files and convert it to commands
543         # for chkconfig.ltx
544         p1 = re.compile(r'\Declare(LaTeX|DocBook|LinuxDoc)Class')
545         testclasses = list()
546         for file in glob.glob( os.path.join('layouts', '*.layout') ) + \
547             glob.glob( os.path.join(srcdir, 'layouts', '*.layout' ) ) :
548             if not os.path.isfile(file):
549                 continue
550             classname = file.split(os.sep)[-1].split('.')[0]
551             for line in open(file).readlines():
552                 if p1.search(line) == None:
553                     continue
554                 if line[0] != '#':
555                     print "Wrong input layout file with line '" + line
556                     sys.exit(3)
557                 testclasses.append("\\TestDocClass{%s}{%s}" % (classname, line[1:].strip()))
558                 break
559         testclasses.sort()
560         cl = open('chklayouts.tex', 'w')
561         for line in testclasses:
562             cl.write(line + '\n')
563         cl.close()
564         #
565         # we have chklayouts.tex, then process it
566         for line in cmdOutput(LATEX + ' wrap_chkconfig.ltx').splitlines():
567             if re.match('^\+', line):
568                 print line
569         #
570         # currently, values in chhkconfig are only used to set
571         # \font_encoding
572         values = {}
573         for line in open('chkconfig.vars').readlines():
574             key, val = re.sub('-', '_', line).split('=')
575             val = val.strip()
576             values[key] = val.strip("'")
577         # chk_fontenc may not exist 
578         try:
579             addToRC(r'\font_encoding "%s"' % values["chk_fontenc"])
580         except:
581             pass
582         if rmcopy:   # remove the copied file
583             removeFiles( [ 'chkconfig.ltx' ] )
584
585
586 def createLaTeXConfig():
587     ''' create LaTeXConfig.lyx '''
588     # if chkconfig.sed does not exist (because LaTeX did not run),
589     # then provide a standard version.
590     if not os.path.isfile('chkconfig.sed'):
591         writeToFile('chkconfig.sed', 's!@.*@!???!g\n')
592     print "creating packages.lst"
593     # if packages.lst does not exist (because LaTeX did not run),
594     # then provide a standard version.
595     if not os.path.isfile('packages.lst'):
596         writeToFile('packages.lst', '''
597 ### This file should contain the list of LaTeX packages that have been
598 ### recognized by LyX. Unfortunately, since configure could not find
599 ### your LaTeX2e program, the tests have not been run. Run ./configure
600 ### if you need to update it after a configuration change.
601 ''')
602     print 'creating doc/LaTeXConfig.lyx'
603     #
604     # This is originally done by sed, using a
605     # tex-generated file chkconfig.sed
606     ##sed -f chkconfig.sed ${srcdir}/doc/LaTeXConfig.lyx.in
607     ##  >doc/LaTeXConfig.lyx
608     # Now, we have to do it by hand (python).
609     #
610     # add to chekconfig.sed
611     writeToFile('chkconfig.sed', '''s!@chk_linuxdoc@!%s!g
612 s!@chk_docbook@!%s!g
613     ''' % (chk_linuxdoc, chk_docbook) , append=True)
614     # process this sed file!!!!
615     lyxin = open( os.path.join(srcdir, 'doc', 'LaTeXConfig.lyx.in')).readlines()
616     # get the rules
617     p = re.compile(r's!(.*)!(.*)!g')
618     # process each sed replace.
619     for sed in open('chkconfig.sed').readlines():
620         if sed.strip() == '':
621             continue
622         try:
623             fr, to = p.match(sed).groups()
624             # if latex did not run, change all @name@ to '???'
625             if fr == '@.*@':
626                 for line in range(len(lyxin)):
627                     lyxin[line] = re.sub('@.*@', to, lyxin[line])
628             else:
629                 for line in range(len(lyxin)):
630                     lyxin[line] = lyxin[line].replace(fr, to)
631         except:  # wrong sed entry?
632             print "Wrong sed entry in chkconfig.sed: '" + sed + "'"
633             sys.exit(4)
634     # 
635     writeToFile( os.path.join('doc', 'LaTeXConfig.lyx'),
636         ''.join(lyxin))
637
638
639 def checkTeXAllowSpaces():
640     ''' Let's check whether spaces are allowed in TeX file names '''
641     tex_allows_spaces = 'false'
642     if lyx_check_config:
643         print "Checking whether TeX allows spaces in file names... ",
644         writeToFile('a b.tex', r'\message{working^^J}' )
645         # FIXME: the bsh version uses < /dev/null which is not portable.
646         # Can anyone confirm if this option (-interaction) is available
647         # at other flavor of latex as well? (MikTex/win, Web2C/linux are fine.) 
648         if ''.join(cmdOutput(LATEX + ' -interaction=nonstopmode "a b"')).find('working') != -1:
649             print 'yes'
650             tex_allows_spaces = 'true'
651         else:
652             print 'no'
653             tex_allows_spaces = 'false'
654         addToRC(r'\tex_allows_spaces ' + tex_allows_spaces)
655         removeFiles( [ 'a b.tex', 'a b.log', 'texput.log' ])
656
657
658 def removeTempFiles():
659     # Final clean-up
660     if not lyx_keep_temps:
661         removeFiles(['chkconfig.sed', 'chkconfig.vars',  \
662             'wrap_chkconfig.ltx', 'wrap_chkconfig.log', \
663             'chklayouts.tex', 'missfont.log', 
664             'chklatex.ltx', 'chklatex.log'])
665
666
667 if __name__ == '__main__':
668     lyx_check_config = True
669     outfile = 'lyxrc.defaults'
670     rc_entries = ''
671     lyx_keep_temps = False
672     version_suffix = ''
673     ## Parse the command line
674     for op in sys.argv[1:]:   # default shell/for list is $*, the options
675         if op in [ '-help', '--help', '-h' ]:
676             print '''Usage: configure [options]
677 Options:
678     --help                   show this help lines
679     --keep-temps             keep temporary files (for debug. purposes)
680     --without-latex-config   do not run LaTeX to determine configuration
681     --with-version-suffix=suffix suffix of binary installed files
682 '''
683             sys.exit(0)
684         elif op == '--without-latex-config':
685             lyx_check_config = False
686         elif op == '--keep-temps':
687             lyx_keep_temps = True
688         elif op[0:22] == '--with-version-suffix=':  # never mind if op is not long enough
689             version_suffix = op[22:]
690         else:
691             print "Unknown option", op
692             sys.exit(1)
693     #    
694     # check if we run from the right directory
695     srcdir = os.path.dirname(sys.argv[0])
696     if srcdir == '':
697         srcdir = '.'
698     if not os.path.isfile( os.path.join(srcdir, 'chkconfig.ltx') ):
699         print "configure: error: cannot find chkconfig.ltx script"
700         sys.exit(1)
701     setEnviron()
702     createDirectories()
703     windows_style_tex_paths = checkTeXPaths()
704     ## Write the first part of outfile
705     writeToFile(outfile, '''# This file has been automatically generated by LyX' lib/configure.py
706 # script. It contains default settings that have been determined by
707 # examining your system. PLEASE DO NOT MODIFY ANYTHING HERE! If you
708 # want to customize LyX, make a copy of the file LYXDIR/lyxrc as
709 # ~/.lyx/lyxrc and edit this file instead. Any setting in lyxrc will
710 # override the values given here.
711 ''')
712     # check latex
713     LATEX = checkLatex()
714     checkFormatEntries()
715     checkConverterEntries()
716     (chk_linuxdoc, bool_linuxdoc, linuxdoc_cmd) = checkLinuxDoc()
717     (chk_docbook, bool_docbook, docbook_cmd) = checkDocBook()
718     checkTeXAllowSpaces()
719     if windows_style_tex_paths != '':
720         addToRC(r'\tex_expects_windows_paths %s' % windows_style_tex_paths)
721     checkOtherEntries()
722     # --without-latex-config can disable lyx_check_config
723     checkLatexConfig( lyx_check_config and LATEX != '', bool_docbook, bool_linuxdoc)
724     createLaTeXConfig()
725     removeTempFiles()