]> git.lyx.org Git - features.git/blobdiff - lib/scripts/docbook2epub.py
ePub: refactor script as a series of small functions.
[features.git] / lib / scripts / docbook2epub.py
index 02d4083366802d08c6593364653c08f75fb04de0..28b7e4b9bb7cf3ca3af9729570afeb7783b8ce0b 100644 (file)
@@ -9,56 +9,78 @@
 # Full author contact details are available in file CREDITS
 
 # Usage:
-#   python docbook2epub.py in.docbook out.epub
+#   python docbook2epub.py java_binary in.docbook out.epub
 
 from __future__ import print_function
 
+# import glob  # Not powerful enough before Python 3.5.
 import os
 import shutil
 import sys
 import tempfile
 import zipfile
-import glob
 
-if __name__ == '__main__':
+
+def parse_arguments():
     if len(sys.argv) != 4:
         sys.exit(1)
     own_path, java_path, input, output = sys.argv
     script_folder = os.path.dirname(own_path) + '/../'
 
-    print('Generating ePub:')
+    print('Generating ePub with the following parameters:')
     print(own_path)
     print(input)
     print(output)
 
+    return java_path, input, output, script_folder
+
+
+def create_temporary_folder():
     output_dir = tempfile.mkdtemp().replace('\\', '/')
     print('Temporary output directory:')
     print(output_dir)
+    return output_dir
+
 
-    # Start the XSLT transformation.
+def start_xslt_transformation(input, output_dir, script_folder, java_path):
     xslt = script_folder + 'docbook/epub3/chunk.xsl'
     saxon_jar = script_folder + 'scripts/saxon6.5.5.jar'
     saxon_params = 'base.dir=%s' % output_dir
-    command = '"' + java_path + '" -jar "' + saxon_jar + '" ' + input + ' ' + xslt + ' ' + saxon_params
+    command = '"' + java_path + '" -jar "' + saxon_jar + '" "' + input + '" "' + xslt + '" "' + saxon_params + '"'
 
     print('XSLT style sheet to use:')
     print(xslt)
     print('Command to execute:')
     print(command)
 
-    if os.system('"' + command + '"') != 0:
+    quoted_command = command
+    if os.name == 'nt':
+        # On Windows, it is typical to have spaces in folder names, and that requires to wrap the whole command
+        # in quotes. On Linux, this might create errors when starting the command.
+        quoted_command = '"' + command + '"'
+    # This could be simplified by using subprocess.run, but this requires Python 3.5.
+
+    if os.system(quoted_command) != 0:
         print('docbook2epub fails')
         shutil.rmtree(output_dir, ignore_errors=True)
         sys.exit(1)
 
     print('Generated ePub contents.')
 
-    # TODO: Copy the assets to the OEBPS/images/.
 
-    # Create the actual ePub file.
+def create_zip_archive(output, output_dir):
     with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as zip:
-        for file in glob.glob(output_dir + '/**/*', recursive=True):
+        # Python 3.5 brings the `recursive` argument. For older versions, this trick is required...
+        # for file in glob.glob(output_dir + '/**/*', recursive=True):
+        for file in [os.path.join(dp, f) for dp, dn, filenames in os.walk(output_dir) for f in filenames]:
             zip.write(file, os.path.relpath(file, output_dir), compress_type=zipfile.ZIP_STORED)
 
     shutil.rmtree(output_dir)
     print('Generated ePub.')
+
+
+if __name__ == '__main__':
+    java_path, input, output, script_folder = parse_arguments()
+    output_dir = create_temporary_folder()
+    start_xslt_transformation(input, output_dir, script_folder, java_path)
+    create_zip_archive(output, output_dir)