Debian Codegraph @

extras/build_docs.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2008 Alexander Belchenko <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 """build_docs command for setup.py"""
21
22 from distutils.core import Command
23 from distutils.dep_util import newer
24 from distutils import log
25 import os
26 import sys
27
28
29 class build_docs(Command):
30
31 description = 'translate txt files from docs/ to html with docutils'
32
33 # List of options:
34 # - long name,
35 # - short name (None if no short name),
36 # - help string.
37 user_options = [('output-dir=', 'o', 'Directory for output html files'),
38 ('force', 'f', 'Force creation of html files'),
39 ('clean', None, 'Clean html files'),
40 ]
41 boolean_options = ['force', 'clean']
42
43 def initialize_options(self):
44 self.output_dir = None
45 self.force = None
46 self.clean = None
47
48 def finalize_options(self):
49 pass
50
51 def iter_txt_files(self, from_dir, out_dir=None):
52 """For each txt file yields 2 paths: source txt and target html."""
53 if out_dir is None:
54 out_dir = from_dir
55 if not from_dir.endswith(os.sep):
56 from_dir = from_dir + os.sep
57 n = len(from_dir)
58 for root, dirs, files in os.walk(from_dir):
59 for f in files:
60 if f.endswith('.txt'):
61 source = os.path.join(root, f)
62 relpath = source[n:]
63 target = os.path.join(out_dir, relpath[:-3]+'html')
64 yield source, target
65
66 def do_clean(self):
67 """Clean all generated html files"""
68 for source, target in self.iter_txt_files('docs', self.output_dir):
69 if os.path.isfile(target):
70 log.info('removing ' + target)
71 os.remove(target)
72
73 def run(self):
74 """Run rst2html for each txt file"""
75 if self.clean:
76 self.do_clean()
77 return
78
79 rst2html = ['rst2html.py']
80 if sys.platform == 'win32':
81 rst2html = ['python',
82 os.path.join(sys.prefix, 'Scripts', 'rst2html.py')]
83
84 for source, target in self.iter_txt_files('docs', self.output_dir):
85 if self.force or newer(source, target):
86 log.info('translating ' + source)
87 self.spawn(rst2html + [source, target])