]> git.lyx.org Git - lyx.git/blob - lib/scripts/docbook2epub.py
28b7e4b9bb7cf3ca3af9729570afeb7783b8ce0b
[lyx.git] / lib / scripts / docbook2epub.py
1 # -*- coding: utf-8 -*-
2
3 # file docbook2epub.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6 #
7 # \author Thibaut Cuvelier
8 #
9 # Full author contact details are available in file CREDITS
10
11 # Usage:
12 #   python docbook2epub.py java_binary in.docbook out.epub
13
14 from __future__ import print_function
15
16 # import glob  # Not powerful enough before Python 3.5.
17 import os
18 import shutil
19 import sys
20 import tempfile
21 import zipfile
22
23
24 def parse_arguments():
25     if len(sys.argv) != 4:
26         sys.exit(1)
27     own_path, java_path, input, output = sys.argv
28     script_folder = os.path.dirname(own_path) + '/../'
29
30     print('Generating ePub with the following parameters:')
31     print(own_path)
32     print(input)
33     print(output)
34
35     return java_path, input, output, script_folder
36
37
38 def create_temporary_folder():
39     output_dir = tempfile.mkdtemp().replace('\\', '/')
40     print('Temporary output directory:')
41     print(output_dir)
42     return output_dir
43
44
45 def start_xslt_transformation(input, output_dir, script_folder, java_path):
46     xslt = script_folder + 'docbook/epub3/chunk.xsl'
47     saxon_jar = script_folder + 'scripts/saxon6.5.5.jar'
48     saxon_params = 'base.dir=%s' % output_dir
49     command = '"' + java_path + '" -jar "' + saxon_jar + '" "' + input + '" "' + xslt + '" "' + saxon_params + '"'
50
51     print('XSLT style sheet to use:')
52     print(xslt)
53     print('Command to execute:')
54     print(command)
55
56     quoted_command = command
57     if os.name == 'nt':
58         # On Windows, it is typical to have spaces in folder names, and that requires to wrap the whole command
59         # in quotes. On Linux, this might create errors when starting the command.
60         quoted_command = '"' + command + '"'
61     # This could be simplified by using subprocess.run, but this requires Python 3.5.
62
63     if os.system(quoted_command) != 0:
64         print('docbook2epub fails')
65         shutil.rmtree(output_dir, ignore_errors=True)
66         sys.exit(1)
67
68     print('Generated ePub contents.')
69
70
71 def create_zip_archive(output, output_dir):
72     with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as zip:
73         # Python 3.5 brings the `recursive` argument. For older versions, this trick is required...
74         # for file in glob.glob(output_dir + '/**/*', recursive=True):
75         for file in [os.path.join(dp, f) for dp, dn, filenames in os.walk(output_dir) for f in filenames]:
76             zip.write(file, os.path.relpath(file, output_dir), compress_type=zipfile.ZIP_STORED)
77
78     shutil.rmtree(output_dir)
79     print('Generated ePub.')
80
81
82 if __name__ == '__main__':
83     java_path, input, output, script_folder = parse_arguments()
84     output_dir = create_temporary_folder()
85     start_xslt_transformation(input, output_dir, script_folder, java_path)
86     create_zip_archive(output, output_dir)