diff --git a/lib/Makefile.am b/lib/Makefile.am index 00b8174874..ae317d7ad3 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -884,7 +884,7 @@ dist_scripts_DATA = \ scripts/fen2ascii.py \ scripts/fig2pdftex.sh \ scripts/fig2pstex.sh \ - scripts/fig_copy.sh \ + scripts/fig_copy.py \ scripts/layout2layout.py \ scripts/legacy_lyxpreview2ppm.py \ scripts/listerrors \ diff --git a/lib/configure.py b/lib/configure.py index 8748943e85..42a773e0ba 100644 --- a/lib/configure.py +++ b/lib/configure.py @@ -456,7 +456,7 @@ def checkOtherEntries(): \print_spool_command "lpr"''', '']) # Add the rest of the entries (no checkProg is required) - addToRC(r'''\copier fig "sh $$s/scripts/fig_copy.sh $$i $$o" + addToRC(r'''\copier fig "python $$s/scripts/fig_copy.py $$i $$o" \copier pstex "python $$s/scripts/tex_copy.py $$i $$o $$l" \copier pdftex "python $$s/scripts/tex_copy.py $$i $$o $$l" ''') diff --git a/lib/scripts/fig_copy.py b/lib/scripts/fig_copy.py new file mode 100644 index 0000000000..3f8524c1c9 --- /dev/null +++ b/lib/scripts/fig_copy.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# -*- coding: iso-8859-15 -*- + +# file fig_copy.py +# This file is part of LyX, the document processor. +# Licence details can be found in the file COPYING. +# +# \author Angus Leeming +# \author Bo Peng +# +# Full author contact details are available in file CREDITS + +# Usage: +# fig_copy.py + +# This script will copy an XFIG .fig file "$1" to "$2". In the process, +# it will modify the contents of the .fig file so that the names of any +# picture files that are stored as relative paths are replaced +# with the absolute path. + +import os, sys + +if len(sys.argv) != 3: + print >> sys.stderr, "Usage: fig_copy.sh " + sys.exit(1) + +if not os.path.isfile(sys.argv[1]): + print >> sys.stderr, "Unable to read", sys.argv[1] + sys.exit(1) + +from_dir = os.path.split(os.path.realpath(sys.argv[1]))[0] +to_dir = os.path.split(os.path.realpath(sys.argv[2]))[0] + +# The work is trivial if "to" and "from" are in the same directory. +if from_dir == to_dir: + import shutil + try: + shutil.copy(sys.argv[1], sys.argv[2]) + except: + sys.exit(1) + sys.exit(0) + +# Ok, they're in different directories. The .fig file must be modified. +import re + +# We're looking for a line of text that defines an entry of +# type '2' (a polyline), subtype '5' (an external picture file). +# The line has 14 other data fields. +patternline = re.compile(r'^\s*2\s+5(\s+[0-9.+-]+){14}\s*$') +emptyline = re.compile(r'^\s*$') +commentline = re.compile(r'^\s*#.*$') +# we allow space in path name +figureline = re.compile(r'^(\s*[01]\s*)(\S[\S ]*)(\s*)$') + +input = open(sys.argv[1], 'r') +output = open(sys.argv[2], 'w') + +# path in the fig is relative to this path +os.chdir(from_dir) + +found = False +for line in input.xreadlines(): + if found and not emptyline.match(line) and not commentline.match(line): + # The contents of the final line containing the file name + # are ' X ', where X = 0 or 1. + # We extract filename and replace it with the absolute filename. + (pre, path, post) = figureline.match(line).groups() + line = pre + os.path.realpath(path) + post + found = False + elif patternline.match(line): + found = True + print >> output, line, + +input.close() +output.close() diff --git a/lib/scripts/fig_copy.sh b/lib/scripts/fig_copy.sh deleted file mode 100644 index ef4fbb7d6a..0000000000 --- a/lib/scripts/fig_copy.sh +++ /dev/null @@ -1,82 +0,0 @@ -#! /bin/sh - -# file fig_copy.sh -# This file is part of LyX, the document processor. -# Licence details can be found in the file COPYING. -# -# author Angus Leeming -# -# Full author contact details are available in file CREDITS - -# Usage: -# fig_copy.sh - -# This script will copy an XFIG .fig file "$1" to "$2". In the process, -# it will modify the contents of the .fig file so that the names of any -# picture files that are stored as relative paths are replaced -# with the absolute path. - -test $# -eq 2 || { - echo "Usage: fig_copy.sh " >&2 - exit 1 -} - - -test -r "$1" || { - echo "Unable to read $1" >&2 - exit 1 -} - - -# The work is trivial if "to" and "from" are in the same directory. -PRESENT_DIR=$PWD - -cd `dirname "$1"` || exit $? -FROM_DIR=$PWD -cd "$PRESENT_DIR" || exit $? - -cd `dirname "$2"` || exit $? -TO_DIR=$PWD -cd "$PRESENT_DIR" || exit $? - -test "$FROM_DIR" = "$TO_DIR" && { - 'cp' -f "$1" "$2" - exit $? -} - - -# Ok, they're in different directories. The .fig file must be modified. - -# WS is a space and a tab character. -WS=' ' - -TRANSFORMATION=" -# We're looking for a line of text that defines an entry of -# type '2' (a polyline), subtype '5' (an external picture file). -# The line has 14 other data fields. -/^[${WS}]*2[${WS}]\{1,\}5\([${WS}]\{1,\}[^${WS}]\{1,\}\)\{14\}/{ - -:loop -# If we're not on the last line, get the next line. -# It's this that defines the file itself. -$!{ -N - -# Does the new line contain any data? -# If not, loop -/\n[${WS}]*$/bloop - -# Does the new line contain only a comment? -# If so, loop -/\n[${WS}]*#[^\n]*$/bloop - -# The contents of the final line containing the file name -# are ' X ', where X = 0 or 1. -# If the file name does not begin with '/', then insert the absolute path. -# Note that this test will work even if the file name contains spaces. -s@\(.*\n[${WS}]*[01][${WS}]\{1,\}\)\([^/]\)@\1${FROM_DIR}/\2@ -} -}" - -sed "${TRANSFORMATION}" "$1" > "$2" -exit $?