ext_copy.py: fix #6234 also for branch as José and Richard gave his OK

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_1_6_X@31706 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Uwe Stöhr 2009-10-24 15:29:34 +00:00
parent 4c0db93e40
commit 3bfd4a66c2
2 changed files with 31 additions and 22 deletions

View File

@ -5,7 +5,7 @@
# This file is part of LyX, the document processor. # This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING. # Licence details can be found in the file COPYING.
# author Richard Heck # author Richard Heck, Alex Fernandez, Uwe Stöhr
# Full author contact details are available in file CREDITS # Full author contact details are available in file CREDITS
@ -23,16 +23,12 @@
# The -t argument determines the extension added, the default being "LyXconv". # The -t argument determines the extension added, the default being "LyXconv".
# If just . is given, no extension is added. # If just . is given, no extension is added.
# KNOWN BUG: This script is not aware of generated subdirectories. import getopt, os, shutil, sys
import os, sys, getopt
from lyxpreview_tools import error from lyxpreview_tools import error
def usage(prog_name): def usage(prog_name):
return "Usage: %s [-e extensions] [-t target extension] <from file> <to file>" % prog_name return "Usage: %s [-e extensions] [-t target extension] <from file> <to file>" % prog_name
def main(argv): def main(argv):
progname = argv[0] progname = argv[0]
@ -60,27 +56,26 @@ def main(argv):
if not os.path.isabs(to_dir): if not os.path.isabs(to_dir):
error("%s is not an absolute file name.\n%s" % to_dir, usage(progname)) error("%s is not an absolute file name.\n%s" % to_dir, usage(progname))
# try to create the output directory if it doesn't exist if not copy_all(from_dir, to_dir, exts):
if not os.path.isdir(to_dir): # some kind of failure
try: return 1
os.makedirs(to_dir) return 0
except:
error("Unable to create %s" % to_dir)
import shutil
# copy all matching files in from_dir to to_dir def copy_all(from_dir, to_dir, exts):
"Copy all matching files in from_dir to to_dir"
for file in os.listdir(from_dir): for file in os.listdir(from_dir):
if os.path.isdir(file): if os.path.isdir(os.path.join(from_dir, file)):
copy_all(os.path.join(from_dir, file), os.path.join(to_dir, file), exts)
continue continue
junk, ext = os.path.splitext(os.path.basename(file)) junk, ext = os.path.splitext(os.path.basename(file))
ext = ext.lower()[1:] #strip the leading dot ext = ext.lower()[1:] #strip the leading dot
try: # only create a directory and copy files when either
# if exts is empty we ignore it # exts is empty or when ext is in the exts list
# otherwise check if the extension is in the list if (exts) and (ext not in exts):
not exts or exts.index(ext) continue
except: if not create_dir(to_dir):
continue #not found return False
from_file = os.path.join(from_dir, file) from_file = os.path.join(from_dir, file)
to_file = os.path.join(to_dir, file) to_file = os.path.join(to_dir, file)
shutil.copyfile(from_file, to_file) shutil.copyfile(from_file, to_file)
@ -88,7 +83,18 @@ def main(argv):
shutil.copymode(from_file, to_file) shutil.copymode(from_file, to_file)
except: except:
pass pass
return 0 return True
def create_dir(new_dir):
"Try to create the output directory if it doesn't exist"
if not os.path.isdir(new_dir):
try:
os.makedirs(new_dir)
except:
error("Unable to create %s" % new_dir)
return False
return True
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv) main(sys.argv)

View File

@ -246,3 +246,6 @@ What's new
- Clarify that we require automake 1.8 (this was already the case, but - Clarify that we require automake 1.8 (this was already the case, but
nobody noticed). nobody noticed).
- The script ext_copy.py can now also copy files in subdirectories.
(useful for external converters)