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