]> git.lyx.org Git - lyx.git/blob - lib/scripts/ext_copy.py
use "new" Lexer interface...
[lyx.git] / lib / scripts / ext_copy.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file ext_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 Richard Heck
9
10 # Full author contact details are available in file CREDITS
11
12 # Usage:
13 # ext_copy.py [-e ext1,ext2,...] <from file> <to file>
14
15 # This script is to be used as a "copier" script in the sense needed by
16 # the converter definitions. Given a <from file> and <to file>, it will copy
17 # all files in the directory in which from_file is found that have the 
18 # extensions given in the -e argument, or all files in that directory if no 
19 # such argument is given. So, for example, we can do:
20 #   python ext_copy.py -e png,html,css /path/from/file.html /path/to/file.html
21 # and all html, png, and css files in /path/from/ will be copied to the 
22 # (possibly new) directory /path/to/file.html.LyXconv/.
23 # The -t argument determines the extension added, the default being "LyXconv".
24 # If just . is given, no extension is added.
25
26 # KNOWN BUG: This script is not aware of generated subdirectories.
27
28 import os, sys, getopt
29 from lyxpreview_tools import error
30
31
32 def usage(prog_name):
33     return "Usage: %s [-e extensions] [-t target extension] <from file> <to file>" % prog_name
34
35
36 def main(argv):
37     progname = argv[0]
38
39     exts = [] #list of extensions for which we're checking
40     targext = "LyXconv" #extension for target directory
41     opts, args = getopt.getopt(sys.argv[1:], "e:t:")
42     for o, v in opts:
43       if o == "-e":
44         exts = v.split(',')
45       if o == "-t":
46         targext = v
47
48     # input directory
49     if len(args) != 2:
50       error(usage(progname))
51     abs_from_file = args[0]
52     if not os.path.isabs(abs_from_file):
53       error("%s is not an absolute file name.\n%s" % abs_from_file, usage(progname))
54     from_dir = os.path.dirname(abs_from_file)
55
56     # output directory
57     to_dir = args[1]
58     if targext != '.':
59       to_dir += "." + targext
60     if not os.path.isabs(to_dir):
61       error("%s is not an absolute file name.\n%s" % to_dir, usage(progname))
62
63     # try to create the output directory if it doesn't exist
64     if not os.path.isdir(to_dir):
65       try:
66         os.makedirs(to_dir)
67       except:
68         error("Unable to create %s" % to_dir)
69
70     import shutil
71
72     # copy all matching files in from_dir to to_dir
73     for file in os.listdir(from_dir):
74       if os.path.isdir(file):
75         continue
76       junk, ext = os.path.splitext(os.path.basename(file))
77       ext = ext.lower()[1:] #strip the leading dot
78       try:
79         # if exts is empty we ignore it
80         # otherwise check if the extension is in the list
81         not exts or exts.index(ext)
82       except:
83         continue #not found
84       from_file = os.path.join(from_dir, file)
85       to_file  = os.path.join(to_dir, file)
86       shutil.copyfile(from_file, to_file)
87       try:
88         shutil.copymode(from_file, to_file)
89       except:
90         pass
91     return 0
92
93 if __name__ == "__main__":
94     main(sys.argv)