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