]> git.lyx.org Git - lyx.git/blob - lib/scripts/tex_copy.py
Remove profiling.py
[lyx.git] / lib / scripts / tex_copy.py
1 # file tex_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 Georg Baum
7
8 # Full author contact details are available in file CREDITS
9
10 # Usage:
11 # tex_copy.py <from file> <to file> <latex name>
12
13 # This script will copy a file <from file> to <to file>.
14 # <to file> is no exact copy of <from file>, but any occurrence of <basename>
15 # where <basename> is <from file> without directory and extension parts is
16 # replaced by <latex name> without extension.
17
18
19 import os, string, sys
20
21 from lyxpreview_tools import error
22
23
24 def usage(prog_name):
25     return "Usage: %s <from file> <to file> <latex name>" % prog_name
26
27
28 def main(argv):
29     # Parse and manipulate the command line arguments.
30     if len(argv) != 4:
31         error(usage(argv[0]))
32
33     # input file
34     abs_from_file = argv[1]
35     if not os.path.isabs(abs_from_file):
36         error("%s is no absolute file name.\n%s"\
37               % abs_from_file, usage(argv[0]))
38     from_dir, rel_from_file = os.path.split(abs_from_file)
39     from_base, from_ext = os.path.splitext(rel_from_file)
40
41     # output file
42     abs_to_file = argv[2]
43     if not os.path.isabs(abs_to_file):
44         error("%s is no absolute file name.\n%s"\
45               % abs_to_file, usage(argv[0]))
46     to_dir, rel_to_file = os.path.split(abs_to_file)
47
48     # latex file name
49     latex_file = argv[3]
50     latex_base, latex_ext = os.path.splitext(latex_file)
51
52     # convert strings to bytes since we are using binary files
53     from_base = from_base.encode()
54     latex_base = latex_base.encode()
55
56     # Read the input file and write the output file
57     if(not os.path.isfile(abs_from_file)):
58          error("%s is not a valid file.\n" % abs_from_file)
59     from_file = open(abs_from_file, 'rb')
60     to_file = open(abs_to_file, 'wb')
61     lines = from_file.readlines()
62     for line in lines:
63         to_file.write(line.replace(from_base, latex_base))
64     from_file.close()
65     to_file.close()
66
67     return 0
68
69
70 if __name__ == "__main__":
71     main(sys.argv)