mpdot/mpdot.py
changeset 4 468f4c8d3d5b
parent 2 932c358ece3e
equal deleted inserted replaced
3:d8fccb2cd802 4:468f4c8d3d5b
    43 import shutil
    43 import shutil
    44 import unittest
    44 import unittest
    45 #import xml
    45 #import xml
    46 from cStringIO import StringIO
    46 from cStringIO import StringIO
    47 
    47 
    48 __version__ = '0.1.3'
    48 __version__ = '0.1.4'
    49 
    49 
    50 """
    50 """
    51 Nokia XHTML conversion:
    51 Nokia XHTML conversion:
    52 $> ant ... -Dtranstype=xhtml.nokia -Dargs.input=INPUT_DITAMAP
    52 $> ant ... -Dtranstype=xhtml.nokia -Dargs.input=INPUT_DITAMAP
    53 Nokia Eclipse Help conversion:
    53 Nokia Eclipse Help conversion:
    60 CMD_PREFIX_OUTPUT   = "-Doutput.dir="
    60 CMD_PREFIX_OUTPUT   = "-Doutput.dir="
    61 CMD_PREFIX_TEMP     = "-Ddita.temp.dir="
    61 CMD_PREFIX_TEMP     = "-Ddita.temp.dir="
    62 CMD_PREFIX_NERVOUS  = "echo"
    62 CMD_PREFIX_NERVOUS  = "echo"
    63 #CMD_PREFIX_XSL      = "/xsl:plugins/cxxapiref/xsl/dita2xhtml.nokia.xsl "
    63 #CMD_PREFIX_XSL      = "/xsl:plugins/cxxapiref/xsl/dita2xhtml.nokia.xsl "
    64 
    64 
    65 DIR_DOT_COMPONENT   = 'dot_comp'
    65 BUILD_DIR           = 'build'
    66 DIR_DOT_SOURCE      = 'dot_src'
    66 DIR_DOT_COMPONENT   = os.path.join(BUILD_DIR, 'dot_comp')
    67 DIR_TOC_TMP         = 'dot_toc_tmp'
    67 DIR_TOC_TMP         = os.path.join(BUILD_DIR, 'dot_toc_tmp')
    68 DIR_DOT_TOC         = 'dot_toc'
    68 DIR_DOT_TOC         = os.path.join(BUILD_DIR, 'dot_toc')
    69 
    69 
    70 
    70 
    71 def invokeDot(theDitaMapPath, theDirOut, argList, isNervous):
    71 def invokeDot(theDitaMapPath, theDirOut, argList, isNervous):
    72     myCmdList = []
    72     myCmdList = []
    73     if isNervous:
    73     if isNervous:
   141         else:
   141         else:
   142             logging.error('Not copying. Results code %d directory: %s' % (c, d))
   142             logging.error('Not copying. Results code %d directory: %s' % (c, d))
   143 
   143 
   144 def execute(inToc, outDir, argList, numJobs=0, nervous=False):
   144 def execute(inToc, outDir, argList, numJobs=0, nervous=False):
   145     inDir = os.path.dirname(inToc)
   145     inDir = os.path.dirname(inToc)
   146     outDirCmpDot = os.path.join(outDir, DIR_DOT_COMPONENT)
   146     outDirCmpDot = os.path.join(os.path.dirname(outDir), DIR_DOT_COMPONENT)
   147     if not os.path.exists(outDirCmpDot):
   147     if not os.path.exists(outDirCmpDot):
   148         os.makedirs(outDirCmpDot)
   148         os.makedirs(outDirCmpDot)
   149     if numJobs >= 1:
   149     if numJobs >= 1:
   150         myPool = multiprocessing.Pool(processes=numJobs)
   150         myPool = multiprocessing.Pool(processes=numJobs)
   151     else:
   151     else:
   159             nervous,
   159             nervous,
   160         )
   160         )
   161         for t in genCompMapNames(inToc)
   161         for t in genCompMapNames(inToc)
   162     ]
   162     ]
   163     myResults = [r.get() for r in [myPool.apply_async(invokeDot, t) for t in myTaskS]]
   163     myResults = [r.get() for r in [myPool.apply_async(invokeDot, t) for t in myTaskS]]
   164     copyDirs(myResults, os.path.join(outDir, DIR_DOT_SOURCE))
   164     copyDirs(myResults, outDir)
   165     return myResults
   165     return myResults
   166 
   166 
   167 class DitamapLinkConverterError(Exception):
   167 class DitamapLinkConverterError(Exception):
   168     """ Raised if an invalid toc is input """
   168     """ Raised if an invalid toc is input """
   169 
   169 
   206         logging.debug('Writing file \"%s\"' % filepath)
   206         logging.debug('Writing file \"%s\"' % filepath)
   207         
   207         
   208         if root is not None:
   208         if root is not None:
   209             with open(filepath, 'w') as f:
   209             with open(filepath, 'w') as f:
   210                 f.write("""<?xml version="1.0" encoding="UTF-8"?>"""+'\n')
   210                 f.write("""<?xml version="1.0" encoding="UTF-8"?>"""+'\n')
   211                 f.write("""<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.5.0//EN" "dtd/cxxAPIMap.dtd" >"""+'\n')
   211                 f.write("""<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.6.0//EN" "dtd/cxxAPIMap.dtd" >"""+'\n')
   212                 f.write(etree.tostring(root))        
   212                 f.write(etree.tostring(root))        
   213                 f.close()
   213                 f.close()
   214     
   214     
   215     def _get_component_map_paths(self, tree):
   215     def _get_component_map_paths(self, tree):
   216         all_hrefs = []
   216         all_hrefs = []
   229             self._handle_map(component_map)
   229             self._handle_map(component_map)
   230         shutil.copyfile(self.toc_path, self.out_dir+os.sep+os.path.basename(self.toc_path))
   230         shutil.copyfile(self.toc_path, self.out_dir+os.sep+os.path.basename(self.toc_path))
   231 
   231 
   232 def publish_toc(toc_path, out_dir, argList, nervous):
   232 def publish_toc(toc_path, out_dir, argList, nervous):
   233     toc_name = os.path.basename(toc_path)
   233     toc_name = os.path.basename(toc_path)
   234     tmp_out =  os.path.join(out_dir, DIR_TOC_TMP)
   234     tmp_out =  os.path.join(os.path.dirname(out_dir), DIR_TOC_TMP)
   235     dlc = DitamapLinkConverter(toc_path, tmp_out)
   235     dlc = DitamapLinkConverter(toc_path, tmp_out)
   236     dlc.convert()
   236     dlc.convert()
   237     toc_to_publish = os.path.join(tmp_out, toc_name)
   237     toc_to_publish = os.path.join(tmp_out, toc_name)
   238     out = os.path.join(out_dir, DIR_DOT_TOC)
   238     out = os.path.join(os.path.dirname(out_dir), DIR_DOT_TOC)
   239     invokeDot(toc_to_publish, out, argList, nervous)
   239     invokeDot(toc_to_publish, out, argList, nervous)
   240     final_destination = os.path.join(out_dir, DIR_DOT_SOURCE)
   240     final_destination = out_dir
   241     if not os.path.exists(final_destination):
   241     if not os.path.exists(final_destination):
   242         os.makedirs(final_destination)
   242         os.makedirs(final_destination)
   243     try:
   243     for item in os.listdir(out):
   244         shutil.copy(os.path.join(out, 'index.html'), final_destination)
   244         item_path = os.path.join(out, item)
   245     except IOError, err:
   245         try:                                                                               
   246         logging.error('publish_toc(): %s' % str(err))
   246             if os.path.isdir(item_path):
       
   247                 if os.path.exists(os.path.join(final_destination, item)):           # The published toc's META-INF files etc.
       
   248                     shutil.rmtree(os.path.join(final_destination, item))            # will be the final ones in the publication,                
       
   249                     shutil.copytree(item_path, os.path.join(final_destination, item)) # any existing ones will be overwritten                                                
       
   250             else:
       
   251                 shutil.copy(item_path, final_destination)
       
   252             logging.info("Copied %s to %s" % (item_path, final_destination))            
       
   253         except IOError, err:
       
   254             logging.error("Couldn't copy %s to %s, error was '%s'" % (item_path, final_destination, str(err)))
       
   255             
       
   256 def clean(out_dir):
       
   257     if os.path.exists(out_dir):        
       
   258         shutil.rmtree(out_dir)
       
   259     if os.path.exists(os.path.join(os.path.dirname(out_dir), BUILD_DIR)):
       
   260         shutil.rmtree(os.path.join(os.path.dirname(out_dir), BUILD_DIR))                      
   247     
   261     
   248 def main():
   262 def main():
   249     usage = "usage: %prog [options] <DITA map> <output directory> -Doptions without the -D"
   263     usage = "usage: %prog [options] <DITA map> <output directory> -Doptions without the -D"
   250     parser = OptionParser(usage, version='%prog ' + __version__)
   264     parser = OptionParser(usage, version='%prog ' + __version__)
   251     parser.add_option(
   265     parser.add_option(
   275         parser.error("I need an output path.")
   289         parser.error("I need an output path.")
   276         return 1
   290         return 1
   277     # Dump out timestamp
   291     # Dump out timestamp
   278     print 'Start time: %s' % time.ctime()
   292     print 'Start time: %s' % time.ctime()
   279     execTime = time.clock()
   293     execTime = time.clock()
       
   294     clean(args[1])
   280     myResults = execute(args[0], args[1], args[2:], options.jobs, options.nervous)
   295     myResults = execute(args[0], args[1], args[2:], options.jobs, options.nervous)
   281     publish_toc(args[0], args[1], args[2:], options.nervous)
   296     publish_toc(args[0], args[1], args[2:], options.nervous)
   282     print 'Number of DITA maps processed: %d' % len(myResults)
   297     print 'Number of DITA maps processed: %d' % len(myResults)
   283     print 'End time: %s' % time.ctime()
   298     print 'End time: %s' % time.ctime()
   284     print 'Elapsed time: %8.3f (s)' % (time.clock()-execTime)
   299     print 'Elapsed time: %8.3f (s)' % (time.clock()-execTime)
   352         self._write_string_to_file(invalid_toc, invalid_toc_path)
   367         self._write_string_to_file(invalid_toc, invalid_toc_path)
   353         dlc = DitamapLinkConverter(invalid_toc_path, self.out_dir)
   368         dlc = DitamapLinkConverter(invalid_toc_path, self.out_dir)
   354         self.assertRaises(DitamapLinkConverterError, dlc.convert)
   369         self.assertRaises(DitamapLinkConverterError, dlc.convert)
   355            
   370            
   356 cmap = """<?xml version="1.0" encoding="UTF-8"?>
   371 cmap = """<?xml version="1.0" encoding="UTF-8"?>
   357 <!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.5.0//EN" "dtd/cxxAPIMap.dtd" >
   372 <!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.6.0//EN" "dtd/cxxAPIMap.dtd" >
   358 <cxxAPIMap id="GUID-0D9E5D45-5A07-302C-BEB3-2D0252214F2E" title="wlmplatform">
   373 <cxxAPIMap id="GUID-0D9E5D45-5A07-302C-BEB3-2D0252214F2E" title="wlmplatform">
   359     <cxxStructRef href="GUID-AE25CF37-B862-306B-B7B3-4A1226B83DA2.xml" navtitle="_SChannels" />
   374     <cxxStructRef href="GUID-AE25CF37-B862-306B-B7B3-4A1226B83DA2.xml" navtitle="_SChannels" />
   360     <cxxFileRef href="GUID-E1984316-685F-394E-B71A-9816E1495C1F.xml" navtitle="wlanerrorcodes.h" />
   375     <cxxFileRef href="GUID-E1984316-685F-394E-B71A-9816E1495C1F.xml" navtitle="wlanerrorcodes.h" />
   361     <cxxClassRef href="GUID-F795E994-BCB6-3040-872A-90F8ADFC75E7.xml" navtitle="MWlanMgmtNotifications" />
   376     <cxxClassRef href="GUID-F795E994-BCB6-3040-872A-90F8ADFC75E7.xml" navtitle="MWlanMgmtNotifications" />
   362 </cxxAPIMap>
   377 </cxxAPIMap>
   363 """
   378 """
   364                         # 
   379                         # 
   365 converted_cmap = """<?xml version="1.0" encoding="UTF-8"?>
   380 converted_cmap = """<?xml version="1.0" encoding="UTF-8"?>
   366 <!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.5.0//EN" "dtd/cxxAPIMap.dtd" >
   381 <!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.6.0//EN" "dtd/cxxAPIMap.dtd" >
   367 <cxxAPIMap id="GUID-0D9E5D45-5A07-302C-BEB3-2D0252214F2E" title="wlmplatform">
   382 <cxxAPIMap id="GUID-0D9E5D45-5A07-302C-BEB3-2D0252214F2E" title="wlmplatform">
   368     <cxxStructRef format="html" href="GUID-AE25CF37-B862-306B-B7B3-4A1226B83DA2.html" navtitle="_SChannels" scope="peer" />
   383     <cxxStructRef format="html" href="GUID-AE25CF37-B862-306B-B7B3-4A1226B83DA2.html" navtitle="_SChannels" scope="peer" />
   369     <cxxFileRef format="html" href="GUID-E1984316-685F-394E-B71A-9816E1495C1F.html" navtitle="wlanerrorcodes.h" scope="peer" />
   384     <cxxFileRef format="html" href="GUID-E1984316-685F-394E-B71A-9816E1495C1F.html" navtitle="wlanerrorcodes.h" scope="peer" />
   370     <cxxClassRef format="html" href="GUID-F795E994-BCB6-3040-872A-90F8ADFC75E7.html" navtitle="MWlanMgmtNotifications" scope="peer" />
   385     <cxxClassRef format="html" href="GUID-F795E994-BCB6-3040-872A-90F8ADFC75E7.html" navtitle="MWlanMgmtNotifications" scope="peer" />
   371 </cxxAPIMap>"""
   386 </cxxAPIMap>"""