1 # -*- coding: utf-8 -*-
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
7 # author Richard Heck, Alex Fernandez, Uwe Stöhr
9 # Full author contact details are available in file CREDITS
12 # ext_copy.py [-e ext1,ext2,...] <from file> <to file>
14 # This script is to be used as a "copier" script in the sense needed by
15 # the converter definitions. Given a <from file> and <to file>, it will copy
16 # all files in the directory in which from_file is found that have the
17 # extensions given in the -e argument, or all files in that directory if no
18 # such argument is given. So, for example, we can do:
19 # python ext_copy.py -e png,html,css /path/from/file.html /path/to/file.html
20 # and all html, png, and css files in /path/from/ will be copied to the
21 # (possibly new) directory /path/to/file.html.LyXconv/.
22 # The -t argument determines the extension added, the default being "LyXconv".
23 # If just . is given, no extension is added.
25 import getopt, os, shutil, sys
26 from lyxpreview_tools import error
29 return "Usage: %s [-e extensions] [-t target extension] <from file> <to file>" % prog_name
34 exts = [] #list of extensions for which we're checking
35 targext = "LyXconv" #extension for target directory
36 opts, args = getopt.getopt(sys.argv[1:], "e:t:")
45 error(usage(progname))
46 abs_from_file = args[0]
47 if not os.path.isabs(abs_from_file):
48 error("%s is not an absolute file name.\n%s" % abs_from_file, usage(progname))
49 from_dir = os.path.dirname(abs_from_file)
54 to_dir += "." + targext
55 if not os.path.isabs(to_dir):
56 error("%s is not an absolute file name.\n%s" % to_dir, usage(progname))
58 if not copy_all(from_dir, to_dir, exts):
59 # some kind of failure
64 def copy_all(from_dir, to_dir, exts):
65 "Copy all matching files in from_dir to to_dir"
66 for file in os.listdir(from_dir):
67 if os.path.isdir(os.path.join(from_dir, file)):
68 copy_all(os.path.join(from_dir, file), os.path.join(to_dir, file), exts)
70 junk, ext = os.path.splitext(os.path.basename(file))
71 ext = ext.lower()[1:] #strip the leading dot
72 # only create a directory and copy files when either
73 # exts is empty or when ext is in the exts list
74 if (exts) and (ext not in exts):
76 if not create_dir(to_dir):
78 from_file = os.path.join(from_dir, file)
79 to_file = os.path.join(to_dir, file)
80 shutil.copyfile(from_file, to_file)
82 shutil.copymode(from_file, to_file)
88 def create_dir(new_dir):
89 "Try to create the output directory if it doesn't exist"
90 if not os.path.isdir(new_dir):
94 error("Unable to create %s" % new_dir)
98 if __name__ == "__main__":