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