]> git.lyx.org Git - lyx.git/blobdiff - lib/configure.py
configure.py: add support for Python 3 on Windows
[lyx.git] / lib / configure.py
index 2e28c070cd7b6ce9e99c987cf650f460846ff9d0..be47cea7a08252a0b0ccc61a186854204dd66678 100644 (file)
@@ -9,7 +9,7 @@
 # Full author contact details are available in file CREDITS.
 
 from __future__ import print_function
-import glob, logging, os, re, shutil, subprocess, sys, stat
+import glob, logging, os, re, shutil, subprocess, sys, stat, io
 
 # set up logging
 logging.basicConfig(level = logging.DEBUG,
@@ -196,19 +196,16 @@ def checkTeXPaths():
         from tempfile import mkstemp
         fd, tmpfname = mkstemp(suffix='.ltx')
         if os.name == 'nt':
-            from locale import getdefaultlocale
-            language, encoding = getdefaultlocale()
-            if encoding == None:
-                encoding = 'latin1'
+            encoding = sys.getfilesystemencoding()
             if sys.version_info[0] < 3:
                 inpname = shortPath(unicode(tmpfname, encoding)).replace('\\', '/')
             else:
-                inpname = shortPath(str(tmpfname, encoding)).replace('\\', '/')
+                inpname = shortPath(tmpfname).replace('\\', '/') 
         else:
             inpname = cmdOutput('cygpath -m ' + tmpfname)
         logname = os.path.basename(re.sub("(?i).ltx", ".log", inpname))
         inpname = inpname.replace('~', '\\string~')
-        os.write(fd, r'\relax')
+        os.write(fd, b'\\relax')
         os.close(fd)
         latex_out = cmdOutput(r'latex "\nonstopmode\input{%s}\makeatletter\@@end"'
                               % inpname)
@@ -686,6 +683,7 @@ def checkFormatEntries(dtl_tools):
 \Format eps3       eps    "EPS (cropped)"         "" "%%"      ""      "document"      ""
 \Format ps         ps      Postscript             t  "%%"      ""      "document,vector,menu=export"   "application/postscript"'''])
     # for xdg-open issues look here: http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg151818.html
+    # maybe use "bestApplication()" from https://github.com/jleclanche/python-mime
     # the MIME type is set for pdf6, because that one needs to be autodetectable by libmime
     checkViewer('a PDF previewer',
                 ['pdfview', 'kpdf', 'okular', 'qpdfview --unique',
@@ -1177,11 +1175,12 @@ def checkOtherEntries():
         rc_entry = [ r'\chktex_command "%%"' ])
     checkProgAlternatives('BibTeX or alternative programs',
         ['bibtex', 'bibtex8', 'biber'],
-        rc_entry = [ r'\bibtex_command "%%"' ],
+        rc_entry = [ r'\bibtex_command "automatic"' ],
         alt_rc_entry = [ r'\bibtex_alternatives "%%"' ])
-    checkProg('a specific Japanese BibTeX variant',
-        ['pbibtex', 'jbibtex', 'bibtex'],
-        rc_entry = [ r'\jbibtex_command "%%"' ])
+    checkProgAlternatives('a specific Japanese BibTeX variant',
+        ['pbibtex', 'upbibtex', 'jbibtex', 'bibtex', 'biber'],
+        rc_entry = [ r'\jbibtex_command "automatic"' ],
+        alt_rc_entry = [ r'\jbibtex_alternatives "%%"' ])
     checkProgAlternatives('available index processors',
         ['texindy', 'makeindex -c -q', 'xindy'],
         rc_entry = [ r'\index_command "%%"' ],
@@ -1350,7 +1349,11 @@ def checkLatexConfig(check_config, bool_docbook):
         classname = file.split(os.sep)[-1].split('.')[0]
         decline = ""
         catline = ""
-        for line in open(file).readlines():
+        if os.name == 'nt':
+            enco = sys.getfilesystemencoding()
+        else:
+            enco="utf8"
+        for line in io.open(file, encoding=enco).readlines():
             if not empty.match(line) and line[0] != '#':
                 if decline == "":
                     logger.warning("Failed to find valid \Declare line "
@@ -1542,6 +1545,158 @@ def processModuleFile(file, filename, bool_docbook):
             % (modname, filename, desc, pkgs, req, excl, catgy))
 
 
+def checkCiteEnginesConfig():
+  removeFiles(['lyxciteengines.lst', 'chkciteengines.tex'])
+
+  logger.info('+checking list of cite engines... ')
+  tx = open('lyxciteengines.lst', 'w')
+  tx.write('''## This file declares cite engines and their associated definition files.
+## It has been automatically generated by configure
+## Use "Options/Reconfigure" if you need to update it after a
+## configuration change.
+## "CiteEngineName" "filename" "CiteEngineType" "CiteFramework" "DefaultBiblio" "Description" "Packages"
+''')
+
+  # build the list of available modules
+  seen = []
+  # note that this searches the local directory first, then the
+  # system directory. that way, we pick up the user's version first.
+  for file in glob.glob( os.path.join('citeengines', '*.citeengine') ) + \
+      glob.glob( os.path.join(srcdir, 'citeengines', '*.citeengine' ) ) :
+      # valid file?
+      logger.info(file)
+      if not os.path.isfile(file):
+          continue
+
+      filename = file.split(os.sep)[-1]
+      filename = filename[:-11]
+      if seen.count(filename):
+          continue
+
+      seen.append(filename)
+      retval = processCiteEngineFile(file, filename, bool_docbook)
+      if retval != "":
+          tx.write(retval)
+  tx.close()
+  logger.info('\tdone')
+
+
+def processCiteEngineFile(file, filename, bool_docbook):
+    ''' process cite engines file and get a line of result
+
+        The top of a cite engine file should look like this:
+          #\DeclareLyXCiteEngine[LaTeX Packages]{CiteEngineName}
+          #DescriptionBegin
+          #...body of description...
+          #DescriptionEnd
+        We expect output:
+          "CiteEngineName" "filename" "CiteEngineType" "CiteFramework" "DefaultBiblio" "Description" "Packages"
+    '''
+    remods = re.compile(r'\DeclareLyXCiteEngine\s*(?:\[([^]]*?)\])?{(.*)}')
+    redbeg = re.compile(r'#+\s*DescriptionBegin\s*$')
+    redend = re.compile(r'#+\s*DescriptionEnd\s*$')
+    recet = re.compile(r'\s*CiteEngineType\s*(.*)')
+    redb = re.compile(r'\s*DefaultBiblio\s*(.*)')
+    resfm = re.compile(r'\s*CiteFramework\s*(.*)')
+
+    modname = desc = pkgs = cet = db = cfm = ""
+    readingDescription = False
+    descLines = []
+
+    for line in open(file).readlines():
+      if readingDescription:
+        res = redend.search(line)
+        if res != None:
+          readingDescription = False
+          desc = " ".join(descLines)
+          # Escape quotes.
+          desc = desc.replace('"', '\\"')
+          continue
+        descLines.append(line[1:].strip())
+        continue
+      res = redbeg.search(line)
+      if res != None:
+        readingDescription = True
+        continue
+      res = remods.search(line)
+      if res != None:
+          (pkgs, modname) = res.groups()
+          if pkgs == None:
+            pkgs = ""
+          else:
+            tmp = [s.strip() for s in pkgs.split(",")]
+            pkgs = ",".join(tmp)
+          continue
+      res = recet.search(line)
+      if res != None:
+        cet = res.group(1)
+        continue
+      res = redb.search(line)
+      if res != None:
+        db = res.group(1)
+        continue
+      res = resfm.search(line)
+      if res != None:
+        cfm = res.group(1)
+        continue
+
+    if modname == "":
+      logger.warning("Cite Engine File file without \DeclareLyXCiteEngine line. ")
+      return ""
+
+    if pkgs != "":
+        # this cite engine has some latex dependencies:
+        # append the dependencies to chkciteengines.tex,
+        # which is \input'ed by chkconfig.ltx
+        testpackages = list()
+        for pkg in pkgs.split(","):
+            if "->" in pkg:
+                # this is a converter dependency: skip
+                continue
+            if pkg.endswith(".sty"):
+                pkg = pkg[:-4]
+            testpackages.append("\\TestPackage{%s}" % (pkg,))
+        cm = open('chkciteengines.tex', 'a')
+        for line in testpackages:
+            cm.write(line + '\n')
+        cm.close()
+
+    return '"%s" "%s" "%s" "%s" "%s" "%s" "%s"\n' % (modname, filename, cet, cfm, db, desc, pkgs)
+
+
+def checkXTemplates():
+  removeFiles(['xtemplates.lst'])
+
+  logger.info('+checking list of external templates... ')
+  tx = open('xtemplates.lst', 'w')
+  tx.write('''## This file lists external templates.
+## It has been automatically generated by configure
+## Use "Options/Reconfigure" if you need to update it after a
+## configuration change.
+''')
+
+  # build the list of available templates
+  seen = []
+  # note that this searches the local directory first, then the
+  # system directory. that way, we pick up the user's version first.
+  for file in glob.glob( os.path.join('xtemplates', '*.xtemplate') ) + \
+      glob.glob( os.path.join(srcdir, 'xtemplates', '*.xtemplate' ) ) :
+      # valid file?
+      logger.info(file)
+      if not os.path.isfile(file):
+          continue
+
+      filename = file.split(os.sep)[-1]
+      if seen.count(filename):
+          continue
+
+      seen.append(filename)
+      if filename != "":
+          tx.write(filename + "\n")
+  tx.close()
+  logger.info('\tdone')
+
+
 def checkTeXAllowSpaces():
     ''' Let's check whether spaces are allowed in TeX file names '''
     tex_allows_spaces = 'false'
@@ -1584,15 +1739,15 @@ def removeTempFiles():
     # Final clean-up
     if not lyx_keep_temps:
         removeFiles(['chkconfig.vars', 'chklatex.ltx', 'chklatex.log',
-            'chklayouts.tex', 'chkmodules.tex', 'missfont.log',
-            'wrap_chkconfig.ltx', 'wrap_chkconfig.log'])
+            'chklayouts.tex', 'chkmodules.tex', 'chkciteengines.tex',
+            'missfont.log', 'wrap_chkconfig.ltx', 'wrap_chkconfig.log'])
 
 
 if __name__ == '__main__':
     lyx_check_config = True
     lyx_kpsewhich = True
     outfile = 'lyxrc.defaults'
-    lyxrc_fileformat = 20
+    lyxrc_fileformat = 21
     rc_entries = ''
     lyx_keep_temps = False
     version_suffix = ''
@@ -1664,6 +1819,8 @@ Format %i
     if lyx_kpsewhich:
         rescanTeXFiles()
     checkModulesConfig()
+    checkCiteEnginesConfig()
+    checkXTemplates()
     # --without-latex-config can disable lyx_check_config
     ret = checkLatexConfig(lyx_check_config and LATEX != '', bool_docbook)
     removeTempFiles()