]> git.lyx.org Git - lyx.git/blob - lib/scripts/ext_copy.py
Revert "DocBook: add new layout parameter DocBookWrapperMergeWithPrevious."
[lyx.git] / lib / scripts / ext_copy.py
1 # -*- coding: utf-8 -*-
2
3 # file ext_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 Richard Heck, Alex Fernandez, Uwe Stöhr
8
9 # Full author contact details are available in file CREDITS
10
11 # Usage:
12 # ext_copy.py [-d] [-e ext1,ext2,...] [-t target_ext] from_file to_file
13
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.
24 # If the -d option is given then the document directory /path/to/ will be used
25 # to copy the files no subdirectory will be created/used.
26
27 import getopt, os, shutil, sys
28 from lyxpreview_tools import error
29
30
31 def usage(prog_name):
32     msg = "Usage: %s [-d] [-e extensions] [-t target extension] from_file to_file"
33     return  msg % prog_name
34
35
36 def main(argv):
37     progname = argv[0]
38
39     exts = [] #list of extensions for which we're checking
40     use_doc_dir = False
41     targext = "LyXconv" #extension for target directory
42     opts, args = getopt.getopt(sys.argv[1:], "de:t:")
43     for o, v in opts:
44       if o == "-e":
45         exts = v.split(',')
46       if o == "-t":
47         targext = v
48       if o == "-d":
49         use_doc_dir = True
50
51     # input directory
52     if len(args) != 2:
53       error(usage(progname))
54     abs_from_file = args[0]
55     if not os.path.isabs(abs_from_file):
56       error("%s is not an absolute file name.\n%s" % abs_from_file, usage(progname))
57     from_dir = os.path.dirname(abs_from_file)
58
59     # output directory
60     if use_doc_dir:
61       to_dir = os.path.dirname(args[1])
62     else:
63       to_dir = args[1]
64       if targext != '.':
65         to_dir += "." + targext
66       if not os.path.isabs(to_dir):
67         error("%s is not an absolute file name.\n%s" % to_dir, usage(progname))
68
69     if not copy_all(from_dir, to_dir, exts):
70       # some kind of failure
71       return 1
72     return 0
73
74
75 def copy_all(from_dir, to_dir, exts):
76     "Copy all matching files in from_dir to to_dir"
77     for file in os.listdir(from_dir):
78       if os.path.isdir(os.path.join(from_dir, file)):
79         copy_all(os.path.join(from_dir, file), os.path.join(to_dir, file), exts)
80         continue
81       junk, ext = os.path.splitext(os.path.basename(file))
82       ext = ext.lower()[1:] #strip the leading dot
83       # only create a directory and copy files when either
84       # exts is empty or when ext is in the exts list
85       if (exts) and (ext not in exts):
86         continue
87       if not create_dir(to_dir):
88         return False
89       from_file = os.path.join(from_dir, file)
90       to_file  = os.path.join(to_dir, file)
91       shutil.copyfile(from_file, to_file)
92       try:
93         shutil.copymode(from_file, to_file)
94       except:
95         pass
96     return True
97
98
99 def create_dir(new_dir):
100     "Try to create the output directory if it doesn't exist"
101     if not os.path.isdir(new_dir):
102       try:
103         os.makedirs(new_dir)
104       except:
105         error("Unable to create %s" % new_dir)
106         return False
107     return True
108
109 if __name__ == "__main__":
110     main(sys.argv)