1 | # Copyright (c) 2002 Douglas Gregor <doug.gregor -at- gmail.com> |
---|
2 | # |
---|
3 | # Distributed under the Boost Software License, Version 1.0. |
---|
4 | # (See accompanying file LICENSE_1_0.txt or copy at |
---|
5 | # http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | # This is a rewrite of setup_boostbook.sh in Python |
---|
8 | # It will work on Posix and Windows systems |
---|
9 | # The rewrite is not finished yet, so please don't use it |
---|
10 | # right now it is used only be release scripts |
---|
11 | |
---|
12 | # User configuration |
---|
13 | DOCBOOK_XSL_VERSION = "1.67.2" |
---|
14 | DOCBOOK_DTD_VERSION = "4.2" |
---|
15 | FOP_VERSION = "0.20.5" |
---|
16 | FOP_MIRROR = "http://mirrors.ibiblio.org/pub/mirrors/apache/xmlgraphics/fop/" |
---|
17 | SOURCEFORGE_MIRROR = "http://puzzle.dl.sourceforge.net" |
---|
18 | |
---|
19 | # No user configuration below this point------------------------------------- |
---|
20 | |
---|
21 | import os |
---|
22 | import re |
---|
23 | import sys |
---|
24 | import optparse |
---|
25 | import shutil |
---|
26 | |
---|
27 | sys.path.append( os.path.join( os.path.dirname( sys.modules[ __name__ ].__file__ ) |
---|
28 | , "../regression/xsl_reports/utils" ) ) |
---|
29 | |
---|
30 | import checked_system |
---|
31 | import urllib2 |
---|
32 | import tarfile |
---|
33 | import zipfile |
---|
34 | |
---|
35 | def accept_args( args ): |
---|
36 | parser = optparse.OptionParser() |
---|
37 | parser.add_option( "-t", "--tools", dest="tools", help="directory downloaded tools will be installed into. Optional. Used by release scripts to put the tools separately from the tree to be archived." ) |
---|
38 | parser.usage = "setup_boostbook [options]" |
---|
39 | |
---|
40 | ( options, args ) = parser.parse_args( args ) |
---|
41 | if options.tools is None: |
---|
42 | options.tools = os.getcwd() |
---|
43 | |
---|
44 | |
---|
45 | return options.tools |
---|
46 | |
---|
47 | def to_posix( path ): |
---|
48 | return path.replace( "\\", "/" ) |
---|
49 | |
---|
50 | def unzip( archive_path, result_dir ): |
---|
51 | z = zipfile.ZipFile( archive_path, 'r', zipfile.ZIP_DEFLATED ) |
---|
52 | for f in z.infolist(): |
---|
53 | print f.filename |
---|
54 | if not os.path.exists( os.path.join( result_dir, os.path.dirname( f.filename ) ) ): |
---|
55 | os.makedirs( os.path.join( result_dir, os.path.dirname( f.filename ) ) ) |
---|
56 | result = open( os.path.join( result_dir, f.filename ), 'wb' ) |
---|
57 | result.write( z.read( f.filename ) ) |
---|
58 | result.close() |
---|
59 | |
---|
60 | z.close() |
---|
61 | |
---|
62 | def gunzip( archive_path, result_dir ): |
---|
63 | tar = tarfile.open( archive_path, 'r:gz' ) |
---|
64 | for tarinfo in tar: |
---|
65 | tar.extract( tarinfo, result_dir ) |
---|
66 | tar.close() |
---|
67 | |
---|
68 | def http_get( file, url ): |
---|
69 | f = open( file, "wb" ) |
---|
70 | f.write( urllib2.urlopen( url ).read() ) |
---|
71 | f.close() |
---|
72 | |
---|
73 | def find_executable( executable_name, env_variable, test_args, error_message ): |
---|
74 | print "Looking for %s ..." % executable_name |
---|
75 | if os.environ.has_key( env_variable ): |
---|
76 | specified = os.environ[ env_variable ] |
---|
77 | print " Trying %s specified in env. variable %s" % ( specified, env_variable ) |
---|
78 | if os.path.exists( specified ): |
---|
79 | return specified.replace( "\\", "/" ) |
---|
80 | else: |
---|
81 | print "Cannot find %s specified in env. variable %s" % ( specified, env_variable ) |
---|
82 | |
---|
83 | rc = checked_system.system( [ "%s %s" % ( executable_name, test_args ) ] ) |
---|
84 | print "" |
---|
85 | if rc != 0: |
---|
86 | print error_message |
---|
87 | return None |
---|
88 | else: |
---|
89 | return executable_name.replace( "\\", "/" ) |
---|
90 | |
---|
91 | def adjust_user_config( config_file |
---|
92 | , docbook_xsl_dir |
---|
93 | , docbook_dtd_dir |
---|
94 | , xsltproc |
---|
95 | , doxygen |
---|
96 | , fop |
---|
97 | , java |
---|
98 | ): |
---|
99 | print "Modifying user-config.jam ..." |
---|
100 | r = [] |
---|
101 | using_boostbook = 0 |
---|
102 | eaten=0 |
---|
103 | lines = open( config_file, "r" ).readlines() |
---|
104 | for line in lines: |
---|
105 | if re.match( "^\s*using boostbook", line ): |
---|
106 | using_boostbook = 1 |
---|
107 | r.append( "using boostbook\n" ) |
---|
108 | r.append( " : %s\n" % docbook_xsl_dir ) |
---|
109 | r.append( " : %s\n" % docbook_dtd_dir ) |
---|
110 | r.append( " ; \n" ) |
---|
111 | eaten = 1 |
---|
112 | |
---|
113 | elif using_boostbook == 1 and re.match( ";", line ): |
---|
114 | using_boostbook = 2 |
---|
115 | elif using_boostbook == 1: |
---|
116 | eaten=1 |
---|
117 | elif re.match( "^\s*using xsltproc.*$", line ): |
---|
118 | eaten=1 |
---|
119 | elif re.match( "^\s*using doxygen.*$", line ): |
---|
120 | eaten=1 |
---|
121 | elif re.match( "^\s*using fop.*$", line ): |
---|
122 | eaten=1 |
---|
123 | else: |
---|
124 | if eaten == 0: |
---|
125 | r.append( line ) |
---|
126 | eaten=0 |
---|
127 | |
---|
128 | if using_boostbook==0: |
---|
129 | r.append( "using boostbook\n" ) |
---|
130 | r.append( " : %s\n" % docbook_xsl_dir ) |
---|
131 | r.append( " : %s\n" % docbook_dtd_dir ) |
---|
132 | r.append( " ;\n" ) |
---|
133 | |
---|
134 | r.append( "using xsltproc : %s ;\n" % xsltproc ) |
---|
135 | if doxygen is not None: |
---|
136 | r.append( "using doxygen : %s ;\n" % doxygen ) |
---|
137 | if fop is not None: |
---|
138 | print r.append( "using fop : %s : : %s ;\n" % ( fop, java ) ) |
---|
139 | |
---|
140 | open( config_file + ".tmp", "w" ).writelines( r ) |
---|
141 | try: |
---|
142 | os.rename( config_file + ".tmp", config_file ) |
---|
143 | except OSError, e: |
---|
144 | os.unlink( config_file ) |
---|
145 | os.rename( config_file + ".tmp", config_file ) |
---|
146 | |
---|
147 | |
---|
148 | def setup_docbook_xsl( tools_directory ): |
---|
149 | print "DocBook XSLT Stylesheets ..." |
---|
150 | DOCBOOK_XSL_TARBALL = os.path.join( tools_directory, "docbook-xsl-%s.tar.gz" % DOCBOOK_XSL_VERSION ) |
---|
151 | DOCBOOK_XSL_URL = "%s/sourceforge/docbook/%s" % ( SOURCEFORGE_MIRROR, os.path.basename( DOCBOOK_XSL_TARBALL ) ) |
---|
152 | |
---|
153 | if os.path.exists( DOCBOOK_XSL_TARBALL ): |
---|
154 | print " Using existing DocBook XSLT Stylesheets (version %s)." % DOCBOOK_XSL_VERSION |
---|
155 | else: |
---|
156 | print " Downloading DocBook XSLT Stylesheets version %s..." % DOCBOOK_XSL_VERSION |
---|
157 | print " from %s" % DOCBOOK_XSL_URL |
---|
158 | http_get( DOCBOOK_XSL_TARBALL, DOCBOOK_XSL_URL ) |
---|
159 | |
---|
160 | DOCBOOK_XSL_DIR = to_posix( os.path.join( tools_directory, "docbook-xsl-%s" % DOCBOOK_XSL_VERSION ) ) |
---|
161 | |
---|
162 | if not os.path.exists( DOCBOOK_XSL_DIR ): |
---|
163 | print " Expanding DocBook XSLT Stylesheets into %s..." % DOCBOOK_XSL_DIR |
---|
164 | gunzip( DOCBOOK_XSL_TARBALL, tools_directory ) |
---|
165 | print " done." |
---|
166 | |
---|
167 | return DOCBOOK_XSL_DIR |
---|
168 | |
---|
169 | def setup_docbook_dtd( tools_directory ): |
---|
170 | print "DocBook DTD ..." |
---|
171 | DOCBOOK_DTD_ZIP = to_posix( os.path.join( tools_directory, "docbook-xml-%s.zip" % DOCBOOK_DTD_VERSION ) ) |
---|
172 | DOCBOOK_DTD_URL = "http://www.oasis-open.org/docbook/xml/%s/%s" % ( DOCBOOK_DTD_VERSION, os.path.basename( DOCBOOK_DTD_ZIP ) ) |
---|
173 | if os.path.exists( DOCBOOK_DTD_ZIP ): |
---|
174 | print " Using existing DocBook XML DTD (version %s)." % DOCBOOK_DTD_VERSION |
---|
175 | else: |
---|
176 | print " Downloading DocBook XML DTD version %s..." % DOCBOOK_DTD_VERSION |
---|
177 | http_get( DOCBOOK_DTD_ZIP, DOCBOOK_DTD_URL ) |
---|
178 | |
---|
179 | DOCBOOK_DTD_DIR = to_posix( os.path.join( tools_directory, "docbook-dtd-%s" % DOCBOOK_DTD_VERSION ) ) |
---|
180 | if not os.path.exists( DOCBOOK_DTD_DIR ): |
---|
181 | print "Expanding DocBook XML DTD into %s... " % DOCBOOK_DTD_DIR |
---|
182 | unzip( DOCBOOK_DTD_ZIP, DOCBOOK_DTD_DIR ) |
---|
183 | print "done." |
---|
184 | |
---|
185 | return DOCBOOK_DTD_DIR |
---|
186 | |
---|
187 | def find_xsltproc(): |
---|
188 | return to_posix( find_executable( "xsltproc", "XSLTPROC", "--version" |
---|
189 | , "If you have already installed xsltproc, please set the environment\n" |
---|
190 | + "variable XSLTPROC to the xsltproc executable. If you do not have\n" |
---|
191 | + "xsltproc, you may download it from http://xmlsoft.org/XSLT/." ) ) |
---|
192 | |
---|
193 | def find_doxygen(): |
---|
194 | return to_posix( find_executable( "doxygen", "DOXYGEN", "--version" |
---|
195 | , "Warning: unable to find Doxygen executable. You will not be able to\n" |
---|
196 | + " use Doxygen to generate BoostBook documentation. If you have Doxygen,\n" |
---|
197 | + " please set the DOXYGEN environment variable to the path of the doxygen\n" |
---|
198 | + " executable." ) ) |
---|
199 | |
---|
200 | |
---|
201 | def find_java(): |
---|
202 | return to_posix( find_executable( "java", "JAVA", "-version" |
---|
203 | , "Warning: unable to find Java executable. You will not be able to\n" |
---|
204 | + " generate PDF documentation. If you have Java, please set the JAVA\n" |
---|
205 | + " environment variable to the path of the java executable." ) ) |
---|
206 | |
---|
207 | def setup_fop( tools_directory ): |
---|
208 | print "FOP ..." |
---|
209 | FOP_TARBALL = os.path.join( tools_directory, "fop-%s-bin.tar.gz" % FOP_VERSION ) |
---|
210 | FOP_URL = "%s/%s" % ( FOP_MIRROR, os.path.basename( FOP_TARBALL ) ) |
---|
211 | FOP_DIR = to_posix( "%s/fop-%s" % ( tools_directory, FOP_VERSION ) ) |
---|
212 | if sys.platform == 'win32': |
---|
213 | fop_driver = "fop.bat" |
---|
214 | else: |
---|
215 | fop_driver = "fop.sh" |
---|
216 | |
---|
217 | FOP = to_posix( os.path.join( FOP_DIR, fop_driver ) ) |
---|
218 | |
---|
219 | if os.path.exists( FOP_TARBALL ) : |
---|
220 | print " Using existing FOP distribution (version %s)." % FOP_VERSION |
---|
221 | else: |
---|
222 | print " Downloading FOP distribution version %s..." % FOP_VERSION |
---|
223 | http_get( FOP_TARBALL, FOP_URL ) |
---|
224 | |
---|
225 | if not os.path.exists( FOP_DIR ): |
---|
226 | print " Expanding FOP distribution into %s... " % FOP_DIR |
---|
227 | gunzip( FOP_TARBALL, tools_directory ) |
---|
228 | print " done." |
---|
229 | |
---|
230 | return FOP |
---|
231 | |
---|
232 | def find_user_config(): |
---|
233 | print "Looking for user-config.jam ..." |
---|
234 | JAM_CONFIG_OUT = os.path.join( os.environ[ "HOME" ], "user-config.jam" ) |
---|
235 | if os.path.exists( JAM_CONFIG_OUT ): |
---|
236 | JAM_CONFIG_IN ="user-config-backup.jam" |
---|
237 | print " Found user-config.jam in HOME directory (%s)" % JAM_CONFIG_IN |
---|
238 | shutil.copyfile( JAM_CONFIG_OUT, os.path.join( os.environ[ "HOME" ], "user-config-backup.jam" ) ) |
---|
239 | JAM_CONFIG_IN_TEMP="yes" |
---|
240 | print " Updating Boost.Jam configuration in %s... " % JAM_CONFIG_OUT |
---|
241 | return JAM_CONFIG_OUT |
---|
242 | elif os.environ.has_key( "BOOST_ROOT" ) and os.path.exists( os.path.join( os.environ[ "BOOST_ROOT" ], "tools/build/v2/user-config.jam" ) ): |
---|
243 | JAM_CONFIG_IN=os.path.join( os.environ[ "BOOST_ROOT" ], "tools/build/v2/user-config.jam" ) |
---|
244 | print " Found user-config.jam in BOOST_ROOT directory (%s)" % JAM_CONFIG_IN |
---|
245 | JAM_CONFIG_IN_TEMP="no" |
---|
246 | print " Writing Boost.Jam configuration to %s... " % JAM_CONFIG_OUT |
---|
247 | return JAM_CONFIG_IN |
---|
248 | return None |
---|
249 | |
---|
250 | def setup_boostbook( tools_directory ): |
---|
251 | print "Setting up boostbook tools..." |
---|
252 | print "-----------------------------" |
---|
253 | print "" |
---|
254 | |
---|
255 | DOCBOOK_XSL_DIR = setup_docbook_xsl( tools_directory ) |
---|
256 | DOCBOOK_DTD_DIR = setup_docbook_dtd( tools_directory ) |
---|
257 | XSLTPROC = find_xsltproc() |
---|
258 | DOXYGEN = find_doxygen() |
---|
259 | JAVA = find_java() |
---|
260 | |
---|
261 | FOP = None |
---|
262 | if JAVA is not None: |
---|
263 | print "Java is present." |
---|
264 | FOP = setup_fop( tools_directory ) |
---|
265 | |
---|
266 | user_config = find_user_config() |
---|
267 | |
---|
268 | # Find the input jamfile to configure |
---|
269 | |
---|
270 | if user_config is None: |
---|
271 | print "ERROR: Please set the BOOST_ROOT environment variable to refer to your" |
---|
272 | print "Boost installation or copy user-config.jam into your home directory." |
---|
273 | sys.exit() |
---|
274 | |
---|
275 | adjust_user_config( config_file = user_config |
---|
276 | , docbook_xsl_dir = DOCBOOK_XSL_DIR |
---|
277 | , docbook_dtd_dir = DOCBOOK_DTD_DIR |
---|
278 | , xsltproc = XSLTPROC |
---|
279 | , doxygen = DOXYGEN |
---|
280 | , fop = FOP |
---|
281 | , java = JAVA |
---|
282 | ) |
---|
283 | |
---|
284 | print "done." |
---|
285 | |
---|
286 | print "Done! Execute \"bjam --v2\" in a documentation directory to generate" |
---|
287 | print "documentation with BoostBook. If you have not already, you will need" |
---|
288 | print "to compile Boost.Jam." |
---|
289 | print "" |
---|
290 | print "WARNING FOR WIN32: please obtain a patched version of xsltproc " |
---|
291 | print "from http://engineering.meta-comm.com/xsltproc-win32.zip if you" |
---|
292 | print "are running BoostBook build on Win32 system (this patched version" |
---|
293 | print "solves the long-standing xsltproc problem with " |
---|
294 | print "creating directories for output files)." |
---|
295 | |
---|
296 | def main(): |
---|
297 | ( tools_directory ) = accept_args( sys.argv[ 1: ] ) |
---|
298 | setup_boostbook( tools_directory ) |
---|
299 | |
---|
300 | if __name__ == "__main__": |
---|
301 | main() |
---|
302 | |
---|
303 | |
---|