#!/usr/bin/python # -*- coding: utf-8 -*- import os import shutil import sys import sgmllib class MyParser(sgmllib.SGMLParser): "A simple parser class." def __init__(self, verbose=0): "Initialise an object, passing 'verbose' to the superclass." sgmllib.SGMLParser.__init__(self, verbose) self.hyperlinks = [] self.descriptions = [] self.depths = [] self.inside_a_element = 0 self.inside_li_element = 0 self.current_description = "" self.current_hyperlink = "" self.current_ul_depth = 0 self.current_li_data = "" def parse(self, string): "Parse the given string 'string'." self.feed(string) self.close() def start_a(self, attributes): "Process a hyperlink and its 'attributes'." self.current_hyperlink = "" self.current_description = "" self.inside_a_element = 1 for name, value in attributes: if name == "href": self.current_hyperlink = value def end_a(self): "Record the end of a hyperlink" self.inside_a_element = 0 def start_ul(self, attributes): "" self.current_ul_depth -= 1 def start_li(self, attributes): "
  • " self.inside_li_element = 1 self.current_description = "" self.current_hyperlink = "" def end_li(self): "
  • " self.inside_li_element = 0 if len(self.current_description) == 0: self.descriptions.append(self.current_li_data) self.hyperlinks.append(self.current_hyperlink) self.depths.append(self.current_ul_depth) def handle_data(self, data): "Handle the textual 'data'" if self.inside_a_element: self.descriptions.append(data) self.hyperlinks.append(self.current_hyperlink) self.depths.append(self.current_ul_depth) self.current_description = data if self.inside_li_element: self.current_li_data = data def get_xml(self, spacecount, prefix): "return the structure as xml" ret = "" basespaces = ' ' * spacecount for i, val in enumerate( self.descriptions ): depth = self.depths[i] nextdepth = self.depths[0] if i != len(self.descriptions) - 1: nextdepth = self.depths[i+1] end = '/>' if depth < nextdepth: end = '>' ref = "" if len(self.hyperlinks[i]) > 0: ref += ' ref="%s/%s"' % ( prefix, self.hyperlinks[i] ) spaces = basespaces + ' ' * depth ret += '%s
    nextdepth: spaces = basespaces + ' ' * (depth - 1) ret += '%s
    \n' % spaces depth -= 1 return ret def removeDir(dirname): "remove dir 'dirname' and all it's subdirectories" for name in os.listdir(dirname): filename = os.path.join(dirname, name) if not os.path.islink(filename) and os.path.isdir(filename): removeDir(filename) else: os.remove(filename) os.rmdir(dirname) return def main(): if len(sys.argv) < 4: print >> sys.stderr, "To few arguments: %s []" % sys.argv[0] sys.exit(1) srcdir = sys.argv[1] blddir = sys.argv[2] lang = sys.argv[3] files = sys.argv[4:] files.sort() if os.path.exists( blddir ): removeDir( blddir ) os.makedirs( blddir ) menu_toc = '' files_string = '' for filename in files: files_string += '%s\n' % filename if filename.endswith( 'menu.html' ): try: f = open( os.path.join( srcdir, filename ), 'r' ) except IOError: continue parser = MyParser() parser.parse( f.read() ) menu_toc = parser.get_xml(13, lang) qhp_file_src = os.path.join( srcdir, lang + '.qhp' ) qhp_file_bld = os.path.join( blddir, lang + '.qhp' ) infile = open( qhp_file_src, "r" ) outfile = open( qhp_file_bld, "w" ) for line in infile: line = line.replace( "@FILES@", files_string ).replace( "@MENU_TOC@", menu_toc ) outfile.write(line) infile.close() outfile.close() qhcp_file_src = os.path.join( srcdir, lang + '.qhcp' ) qhcp_file_bld = os.path.join( blddir, lang + '.qhcp' ) shutil.copy( qhcp_file_src, qhcp_file_bld ) for filename in files: src = os.path.join( srcdir, filename ) dst = os.path.join( blddir, filename ) if not os.path.exists( src ): print >> sys.stderr, "Source file %s does not exist - skipping" % src continue if ( os.path.exists( dst ) and os.path.isfile( dst ) ) and\ ( os.path.getsize( src ) == os.path.getsize( dst ) ) and\ ( os.stat( src ).st_mtime <= os.stat( dst ).st_mtime ): continue dstdir = os.path.dirname( dst ) if not os.path.isdir( dstdir ): if os.path.exists( dstdir ): os.remove( dstdir ) os.makedirs( dstdir ) shutil.copy( src, dst ) if __name__ == "__main__": main()