]> git.lyx.org Git - lyx.git/blob - lib/scripts/fig_copy.py
Document the IBus + Qt4 known issue (#9362)
[lyx.git] / lib / scripts / fig_copy.py
1 # -*- coding: utf-8 -*-
2
3 # file fig_copy.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6 #
7 # \author Angus Leeming
8 # \author Bo Peng
9 #
10 # Full author contact details are available in file CREDITS
11
12 # Usage:
13 # fig_copy.py <from file> <to file>
14
15 # This script will copy an XFIG .fig file "$1" to "$2". In the process,
16 # it will modify the contents of the .fig file so that the names of any
17 # picture files that are stored as relative paths are replaced
18 # with the absolute path.
19
20 import os, sys
21
22 if len(sys.argv) != 3:
23     print >> sys.stderr, "Usage: fig_copy.py <from file> <to file>"
24     sys.exit(1)
25
26 if not os.path.isfile(sys.argv[1]):
27     print >> sys.stderr, "Unable to read", sys.argv[1]
28     sys.exit(1)
29
30 from_dir = os.path.split(os.path.realpath(sys.argv[1]))[0]
31 to_dir = os.path.split(os.path.realpath(sys.argv[2]))[0]
32
33 # The work is trivial if "to" and "from" are in the same directory.
34 if from_dir == to_dir:
35     import shutil
36     try:
37         shutil.copy(sys.argv[1], sys.argv[2])
38     except:
39         sys.exit(1)
40     sys.exit(0)
41
42 # Ok, they're in different directories. The .fig file must be modified.
43 import re
44
45 # We're looking for a line of text that defines an entry of
46 # type '2' (a polyline), subtype '5' (an external picture file).
47 # The line has 14 other data fields.
48 patternline = re.compile(r'^\s*2\s+5(\s+[0-9.+-]+){14}\s*$')
49 emptyline   = re.compile(r'^\s*$')
50 commentline = re.compile(r'^\s*#.*$')
51 # we allow space in path name
52 figureline  = re.compile(r'^(\s*[01]\s*)(\S[\S ]*)(\s*)$')
53
54 input = open(sys.argv[1], 'r')
55 output = open(sys.argv[2], 'w')
56
57 # path in the fig is relative to this path
58 os.chdir(from_dir)
59
60 found = False
61 for line in input.xreadlines():
62     if found and not emptyline.match(line) and not commentline.match(line):
63         # The contents of the final line containing the file name
64         # are ' X <file name>', where X = 0 or 1.
65         # We extract filename and replace it with the absolute filename.
66         (pre, path, post) = figureline.match(line).groups()
67         line = pre + os.path.realpath(path) + post
68         found = False
69     elif patternline.match(line):
70         found = True
71     print >> output, line,
72
73 input.close()
74 output.close()