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