]> git.lyx.org Git - features.git/blob - lib/scripts/ext_copy.py
ext_copy.py: fix #6234 for trunk
[features.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, Alex Fernandez, Uwe Stöhr
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 import getopt, os, shutil, sys
27 from lyxpreview_tools import error
28
29 def usage(prog_name):
30     return "Usage: %s [-e extensions] [-t target extension] <from file> <to file>" % prog_name
31
32 def main(argv):
33     progname = argv[0]
34
35     exts = [] #list of extensions for which we're checking
36     targext = "LyXconv" #extension for target directory
37     opts, args = getopt.getopt(sys.argv[1:], "e:t:")
38     for o, v in opts:
39       if o == "-e":
40         exts = v.split(',')
41       if o == "-t":
42         targext = v
43
44     # input directory
45     if len(args) != 2:
46       error(usage(progname))
47     abs_from_file = args[0]
48     if not os.path.isabs(abs_from_file):
49       error("%s is not an absolute file name.\n%s" % abs_from_file, usage(progname))
50     from_dir = os.path.dirname(abs_from_file)
51
52     # output directory
53     to_dir = args[1]
54     if targext != '.':
55       to_dir += "." + targext
56     if not os.path.isabs(to_dir):
57       error("%s is not an absolute file name.\n%s" % to_dir, usage(progname))
58
59     return copy_all(from_dir, to_dir, exts) != True
60
61
62 def copy_all(from_dir, to_dir, exts):
63     "Copy all matching files in from_dir to to_dir"
64     for file in os.listdir(from_dir):
65       if os.path.isdir(os.path.join(from_dir, file)):
66         copy_all(os.path.join(from_dir, file), os.path.join(to_dir, file), exts)
67         continue
68       junk, ext = os.path.splitext(os.path.basename(file))
69       ext = ext.lower()[1:] #strip the leading dot
70       # only create a directory and copy files when either
71       # exts is empty or when ext is in the exts list
72       if (exts) and (ext not in exts):
73         continue
74       if not create_dir(to_dir):
75         return False
76       from_file = os.path.join(from_dir, file)
77       to_file  = os.path.join(to_dir, file)
78       shutil.copyfile(from_file, to_file)
79       try:
80         shutil.copymode(from_file, to_file)
81       except:
82         pass
83     return 0
84
85
86 def create_dir(new_dir):
87     "Try to create the output directory if it doesn't exist"
88     if not os.path.isdir(new_dir):
89       try:
90         os.makedirs(new_dir)
91       except:
92         error("Unable to create %s" % new_dir)
93         return False
94     return True
95
96 if __name__ == "__main__":
97     main(sys.argv)