]> git.lyx.org Git - features.git/blob - lib/scripts/docbook_copy.py
DocBook: add script to start LilyPond on the generated file.
[features.git] / lib / scripts / docbook_copy.py
1 # -*- coding: utf-8 -*-
2
3 # file docbook_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 Thibaut Cuvelier
8 #
9 # Full author contact details are available in file CREDITS
10
11 # Usage:
12 #   python docbook_copy.py lilypond_book_command in.docbook out.docbook
13 # This script copies the original DocBook file (directly produced by LyX) to the output DocBook file,
14 # potentially applying a post-processing step. For now, the only implemented post-processing step is
15 # LilyPond.
16 # /!\ The original file may be modified by this script!
17
18
19 import os
20 import shutil
21 import sys
22
23
24 def need_lilypond(file):
25     # Really tailored to the kind of output lilypond.module makes (in lib/layouts).
26     with open(file, 'r') as f:
27         return "language='lilypond'" in f.read()
28
29
30 def copy_docbook(args):
31     if len(args) != 4:
32         print('Exactly four arguments are expected, only %s found: %s.' % (len(args), args))
33         sys.exit(1)
34
35     # Parse the command line.
36     lilypond_command = args[1]
37     in_file = args[2]
38     out_file = args[3]
39
40     has_lilypond = lilypond_command != ""
41
42     # Apply LilyPond to the original file if available and needed.
43     if has_lilypond and need_lilypond(in_file):
44         # LilyPond requires that its input file has the .lyxml extension.
45         # Move the file, so that LilyPond doesn't have to erase the contents of the original file before
46         # writing the converted output.
47         in_lily_file = in_file.replace(".xml", ".lyxml")
48         shutil.move(in_file, in_lily_file)
49
50         # Start LilyPond on the copied file.
51         command = lilypond_command + ' --format=docbook ' + in_lily_file
52         if os.system(command) != 0:
53             print('Error from LilyPond')
54             sys.exit(1)
55
56         # Now, in_file should have the LilyPond-processed contents.
57
58     # Perform the final copy.
59     shutil.copyfile(in_file, out_file, follow_symlinks=False)
60
61
62 if __name__ == '__main__':
63     copy_docbook(sys.argv)