-# -*- coding: utf-8 -*-
-
# file lyxpak.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.
# author Enrico Forestieri
-# author Richard Heck
+# author Richard Kimberly Heck
# Full author contact details are available in file CREDITS
# 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"
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*)}.*$')
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)
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
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
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
# 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
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)
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:
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
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
incfiles.sort()
if topdir != '':
- os.chdir(unicode(topdir, 'utf-8'))
+ os.chdir(topdir)
# Create the archive
try:
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