Set \origin correctly when upgrading docs

lyx2lyx did not yet know about /systemlyxdir/ and set \origin to the path
where my git tree lives instead. This path is not usable except on my machine,
so better write something more usable instead.
This is a special command line switch of lyx2lyx, so it does not interfere
with normal usage. I did not try to deduce the systemlyxdir from lyx2lyx to
be on the safe side.
This commit is contained in:
Georg Baum 2015-11-11 21:35:12 +01:00
parent 0b222f1b11
commit 1fc3d051b1
4 changed files with 34 additions and 9 deletions

View File

@ -16,7 +16,7 @@
import os, re, string, sys, subprocess, shutil
def convertdir(docdir, prefix, lyx2lyx):
def convertdir(docdir, prefix, lyx2lyx, systemlyxdir):
olddir = os.getcwd()
os.chdir(docdir)
for i in os.listdir("."):
@ -24,7 +24,7 @@ def convertdir(docdir, prefix, lyx2lyx):
if i != 'attic':
subdir = os.path.join(docdir, i)
subprefix = os.path.join(prefix, i)
convertdir(subdir, subprefix, lyx2lyx)
convertdir(subdir, subprefix, lyx2lyx, systemlyxdir)
continue
(base, ext) = os.path.splitext(i)
if ext != ".lyx":
@ -32,10 +32,10 @@ def convertdir(docdir, prefix, lyx2lyx):
old = i + ".old"
shutil.copy(i, old)
if sys.executable and sys.executable != '':
cmd = [sys.executable, lyx2lyx, old, '-o', i]
cmd = [sys.executable, lyx2lyx, old, '-s', systemlyxdir, '-o', i]
else:
# assume that python is in the path
cmd = [lyx2lyx, old, '-o', i]
cmd = [lyx2lyx, old, '-s', systemlyxdir, '-o', i]
sys.stderr.write('Converting %s\n' % os.path.join(prefix, i))
subprocess.call(cmd)
os.chdir(olddir)
@ -46,7 +46,8 @@ def main(argv):
toolsdir = os.path.dirname(argv[0])
docdir = os.path.abspath(os.path.join(toolsdir, '../../lib/doc'))
lyx2lyx = os.path.abspath(os.path.join(toolsdir, "../../lib/lyx2lyx/lyx2lyx"))
convertdir(docdir, '', lyx2lyx)
systemlyxdir = os.path.abspath(os.path.join(toolsdir, "../../lib"))
convertdir(docdir, '', lyx2lyx, systemlyxdir)
return 0

View File

@ -188,7 +188,8 @@ class LyX_base:
def __init__(self, end_format = 0, input = "", output = "", error = "",
debug = default_debug__, try_hard = 0, cjk_encoding = '',
final_version = "", language = "english", encoding = "auto"):
final_version = "", systemlyxdir = '', language = "english",
encoding = "auto"):
"""Arguments:
end_format: final format that the file should be converted. (integer)
@ -253,6 +254,7 @@ class LyX_base:
self.status = 0
self.encoding = encoding
self.language = language
self.systemlyxdir = systemlyxdir
def warning(self, message, debug_level= default_debug__):
@ -733,9 +735,10 @@ class File(LyX_base):
def __init__(self, end_format = 0, input = "", output = "", error = "",
debug = default_debug__, try_hard = 0, cjk_encoding = '',
final_version = ''):
final_version = '', systemlyxdir = ''):
LyX_base.__init__(self, end_format, input, output, error,
debug, try_hard, cjk_encoding, final_version)
debug, try_hard, cjk_encoding, final_version,
systemlyxdir)
self.read()

View File

@ -63,6 +63,8 @@ Copyright (C) 2011 The LyX Team, José Matos and Dekel Tsur""" % LyX.version__
help = "list all available formats and supported versions")
parser.add_option("-n", "--try-hard", action="store_true",
help = "try hard (ignore any convertion errors)")
parser.add_option("-s", "--systemlyxdir", dest= "systemlyxdir",
help= "LyX system directory for conversion from version 489 or older")
(options, args) = parser.parse_args()
if args:

View File

@ -1090,7 +1090,26 @@ def convert_origin(document):
if document.dir == "":
origin = "stdin"
else:
relpath = ''
if document.systemlyxdir and document.systemlyxdir != '':
try:
if os.path.isabs(document.dir):
absdir = os.path.normpath(document.dir)
else:
absdir = os.path.normpath(os.path.abspath(document.dir))
if os.path.isabs(document.systemlyxdir):
abssys = os.path.normpath(document.systemlyxdir)
else:
abssys = os.path.normpath(os.path.abspath(document.systemlyxdir))
relpath = os.path.relpath(absdir, abssys)
if relpath.find('..') == 0:
relpath = ''
except:
relpath = ''
if relpath == '':
origin = document.dir.replace('\\', '/') + '/'
else:
origin = os.path.join("/systemlyxdir", relpath).replace('\\', '/') + '/'
if os.name != 'nt':
origin = unicode(origin, sys.getfilesystemencoding())
document.header[i:i] = ["\\origin " + origin]