> This looks like it should work, but I haven't tested it. I don't have anything at the moment that > would use the new functionality.

The functionality is needed to be able to use external converters like Alex's eLyXer script. I therefore wait for Alex's test result before applying.

> That said, are we sure we always want to create all the directories that might happen to be there? > Might we check first to see if there are any files that need copying, and only then create the
> directory?

You are right. In case there is a subdirectory "images" that only contain JPEG images but the script should only copy PNG files, we create an empty folder. As you said this doesn't harm but can easily be avoided as done in the attached patch.

While implementing this, I found another bug in the script: The given list of file extensions was ignored because the "exts" list wasn't given to the "copy_all" routine.

Attached is a better patch for branch that fixes both issues.

regards Uwe
Index: ext_copy.py
===================================================================
--- ext_copy.py	(revision 31691)
+++ ext_copy.py	(working copy)
@@ -5,7 +5,7 @@
 # This file is part of LyX, the document processor.
 # 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
 
@@ -23,20 +23,17 @@
 # The -t argument determines the extension added, the default being "LyXconv".
 # If just . is given, no extension is added.
 
-# KNOWN BUG: This script is not aware of generated subdirectories.
-
-import os, sys, getopt
+import getopt, os, shutil, sys
 from lyxpreview_tools import error
 
-
 def usage(prog_name):
     return "Usage: %s [-e extensions] [-t target extension] <from file> <to file>" % prog_name
 
+exts = [] #list of extensions for which we're checking
 
 def main(argv):
     progname = argv[0]
 
-    exts = [] #list of extensions for which we're checking
     targext = "LyXconv" #extension for target directory
     opts, args = getopt.getopt(sys.argv[1:], "e:t:")
     for o, v in opts:
@@ -60,35 +57,37 @@
     if not os.path.isabs(to_dir):
       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 os.path.isdir(to_dir):
-      try:
-        os.makedirs(to_dir)
-      except:
-        error("Unable to create %s" % to_dir)
+    copy_all(from_dir, to_dir, exts)
+    return 0
 
-    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):
-      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
       junk, ext = os.path.splitext(os.path.basename(file))
       ext = ext.lower()[1:] #strip the leading dot
-      try:
-        # if exts is empty we ignore it
-        # otherwise check if the extension is in the list
-        not exts or exts.index(ext)
-      except:
-        continue #not found
+      # only create a directory and copy files when either
+      # exts is empty or when ext is in the exts list
+      if (exts) and (ext not in exts):
+        return
+      create_dir(to_dir)
       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)
       try:
         shutil.copymode(from_file, to_file)
       except:
         pass
-    return 0
 
+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)
+
 if __name__ == "__main__":
     main(sys.argv)

Reply via email to