]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpak.py
Remove profiling.py
[lyx.git] / lib / scripts / lyxpak.py
index 2aaf2db42bc061c739fa3621c6e0d5c9c5723065..0d9151592ea84f049b5422d6f2ed4791cf547968 100755 (executable)
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-
 # file lyxpak.py
 # This file is part of LyX, the document processor.
 # Licence details can be found in the file COPYING.
 # a gzip compressed tar archive on *nix. This can be controlled by command
 # line options, however.
 
-from __future__ import print_function
 import gzip, os, re, sys
-from getopt import getopt
 from io import BytesIO
 import subprocess
 
-# Provide support for both python 2 and 3
-if sys.version_info[0] != 2:
-    def unicode(arg, enc):
-        return arg
-
 # The path to the current python executable. sys.executable may fail, so in
 # this case we revert to simply calling "python" from the path.
 PYTHON_BIN = sys.executable if sys.executable else "python"
@@ -35,9 +26,12 @@ running_on_windows = (os.name == 'nt')
 if running_on_windows:
     from shutil import copyfile
     from tempfile import NamedTemporaryFile
+    from lyxwin_getopt import getopt
+else:
+    from getopt import getopt
 
 # Pre-compiled regular expressions.
-re_lyxfile = re.compile(b"\.lyx$")
+re_lyxfile = re.compile(br"\.lyx$")
 re_input = re.compile(b'^(.*)\\\\(input|include){(\\s*)(.+)(\\s*)}.*$')
 re_ertinput = re.compile(b'^(input|include)({)(\\s*)(.+)(\\s*)}.*$')
 re_package = re.compile(b'^(.*)\\\\(usepackage){(\\s*)(.+)(\\s*)}.*$')
@@ -116,19 +110,19 @@ def gather_files(curfile, incfiles, lyx2lyx):
             try:
                 l2l_stdout = subprocess.check_output([PYTHON_BIN, lyx2lyx, tmp.name])
             except subprocess.CalledProcessError:
-                error('%s failed to convert "%s"' % (lyx2lyx, tostr(curfile)))
+                error(f'{lyx2lyx} failed to convert "{tostr(curfile)}"')
             os.unlink(tmp.name)
         else:
             try:
                 l2l_stdout = subprocess.check_output([PYTHON_BIN, lyx2lyx, curfile])
             except subprocess.CalledProcessError:
-                error('%s failed to convert "%s"' % (lyx2lyx, tostr(curfile)))
+                error(f'{lyx2lyx} failed to convert "{tostr(curfile)}"')
         if l2l_stdout.startswith(b"\x1f\x8b"):
             l2l_stdout = gzip.GzipFile("", "rb", 0, BytesIO(l2l_stdout)).read()
         elif running_on_windows:
             # For some unknown reason, there can be a spurious '\r' in the line
             # separators, causing spurious empty lines when calling splitlines.
-            l2l_stdout = l2l_stdout.replace('\r\r\n', '\r\n')
+            l2l_stdout = l2l_stdout.replace(b'\r\r\n', b'\r\n')
         lines = l2l_stdout.splitlines()
     else:
         input = gzopen(curfile)
@@ -166,9 +160,9 @@ def gather_files(curfile, incfiles, lyx2lyx):
             if not os.path.isabs(file):
                 file = os.path.join(curdir, file)
             file_exists = False
-            if not os.path.isdir(unicode(file, 'utf-8')):
+            if not os.path.isdir(file):
                 for ext in extlist:
-                    if os.path.exists(unicode(file + ext, 'utf-8')):
+                    if os.path.exists(file + ext):
                         file = file + ext
                         file_exists = True
                         break
@@ -191,7 +185,7 @@ def gather_files(curfile, incfiles, lyx2lyx):
                 file = file[9:]
             if not os.path.isabs(file):
                 file = os.path.join(curdir, file + b'.bst')
-            if os.path.exists(unicode(file, 'utf-8')):
+            if os.path.exists(file):
                 incfiles.append(abspath(file))
             i += 1
             continue
@@ -206,7 +200,7 @@ def gather_files(curfile, incfiles, lyx2lyx):
                     file = bibfiles[j] + b'.bib'
                 else:
                     file = os.path.join(curdir, bibfiles[j] + b'.bib')
-                if os.path.exists(unicode(file, 'utf-8')):
+                if os.path.exists(file):
                     incfiles.append(abspath(file))
                 j += 1
             i += 1
@@ -224,7 +218,10 @@ def find_lyx2lyx(progloc, path):
     # for $SOMEDIR/lyx2lyx/lyx2lyx.
     ourpath = os.path.dirname(abspath(progloc))
     (upone, discard) = os.path.split(ourpath)
-    tryit = os.path.join(upone, "lyx2lyx", "lyx2lyx")
+    if running_on_windows:
+        tryit = os.path.join(upone, b"lyx2lyx", b"lyx2lyx")
+    else:
+        tryit = os.path.join(upone, "lyx2lyx", "lyx2lyx")
     if os.access(tryit, os.X_OK):
         return tryit
 
@@ -233,7 +230,7 @@ def find_lyx2lyx(progloc, path):
     if "PATHEXT" in os.environ:
         extlist = extlist + os.environ["PATHEXT"].split(os.pathsep)
     lyx_exe, full_path = find_exe(["lyxc", "lyx"], extlist, path)
-    if lyx_exe == None:
+    if lyx_exe is None:
         error('Cannot find the LyX executable in the path.')
     try:
         cmd_stdout = subprocess.check_output([lyx_exe, '-version'], stderr=subprocess.STDOUT)
@@ -265,9 +262,12 @@ def main(args):
     ourprog = args[0]
 
     try:
-      (options, argv) = getopt(args[1:], "htzl:o:")
+        if running_on_windows:
+            (options, argv) = getopt(args[1:], b"htzl:o:")
+        else:
+            (options, argv) = getopt(args[1:], "htzl:o:")
     except:
-      error(usage(ourprog))
+        error(usage(ourprog))
 
     # we expect the filename to be left
     if len(argv) != 1:
@@ -278,24 +278,22 @@ def main(args):
     lyx2lyx = None
 
     for (opt, param) in options:
-      if opt == "-h":
-        print(usage(ourprog))
-        sys.exit(0)
-      elif opt == "-t":
-        makezip = False
-      elif opt == "-z":
-        makezip = True
-      elif opt == "-l":
-        lyx2lyx = param
-      elif opt == "-o":
-        outdir = param
-        if not os.path.isdir(unicode(outdir, 'utf-8')):
-          error('Error: "%s" is not a directory.' % outdir)
+        if opt == "-h":
+            print(usage(ourprog))
+            sys.exit(0)
+        elif opt == "-t":
+            makezip = False
+        elif opt == "-z":
+            makezip = True
+        elif opt == "-l":
+            lyx2lyx = param
+        elif opt == "-o":
+            outdir = param
+            if not os.path.isdir(outdir):
+                error('Error: "%s" is not a directory.' % outdir)
 
     lyxfile = argv[0]
-    if not running_on_windows:
-        lyxfile = unicode(lyxfile, sys.getfilesystemencoding()).encode('utf-8')
-    if not os.path.exists(unicode(lyxfile, 'utf-8')):
+    if not os.path.exists(lyxfile):
         error('File "%s" not found.' % tostr(lyxfile))
 
     # Check that it actually is a LyX document
@@ -320,7 +318,7 @@ def main(args):
 
     path = os.environ["PATH"].split(os.pathsep)
 
-    if lyx2lyx == None:
+    if lyx2lyx is None:
         lyx2lyx = find_lyx2lyx(ourprog, path)
 
     # Initialize the list with the specified LyX file and recursively
@@ -349,7 +347,7 @@ def main(args):
     incfiles.sort()
 
     if topdir != '':
-        os.chdir(unicode(topdir, 'utf-8'))
+        os.chdir(topdir)
 
     # Create the archive
     try:
@@ -390,10 +388,10 @@ if __name__ == "__main__":
         argc = c_int(0)
         argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))
         # unicode_argv[0] is the Python interpreter, so skip that.
-        argv = [argv_unicode[i].encode('utf-8') for i in xrange(1, argc.value)]
+        argv = [argv_unicode[i].encode('utf-8') for i in range(1, argc.value)]
         # Also skip option arguments to the Python interpreter.
         while len(argv) > 0:
-            if not argv[0].startswith("-"):
+            if not argv[0].startswith(b"-"):
                 break
             argv = argv[1:]
         sys.argv = argv