<s> from docstructure import SITE_STRUCTURE , BASENAME_MAP <EOL> import os , shutil , re , sys , datetime <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> TARGET_FILE = "<STR_LIT>" <EOL> RST2LATEX_OPTIONS = "<STR_LIT:U+0020>" . join ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> htmlnsmap = { "<STR_LIT:h>" : "<STR_LIT>" } <EOL> replace_invalid = re . compile ( r'<STR_LIT>' ) . sub <EOL> replace_content = re . compile ( "<STR_LIT>" ) . sub <EOL> replace_epydoc_macros = re . compile ( r'<STR_LIT>' ) . sub <EOL> replace_rst_macros = re . compile ( r'<STR_LIT>' ) . sub <EOL> BASENAME_MAP = BASENAME_MAP . copy ( ) <EOL> BASENAME_MAP . update ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> DOCUMENT_CLASS = r"""<STR_LIT>""" <EOL> PYGMENTS_IMPORT = r"""<STR_LIT>""" <EOL> EPYDOC_IMPORT = r"""<STR_LIT>""" <EOL> def write_chapter ( master , title , filename ) : <EOL> filename = os . path . join ( os . path . dirname ( filename ) , <EOL> "<STR_LIT>" % os . path . basename ( filename ) ) <EOL> master . write ( r"""<STR_LIT>""" % ( title , filename , filename ) ) <EOL> def rest2latex ( script , source_path , dest_path ) : <EOL> command = ( '<STR_LIT>' % <EOL> ( sys . executable , script , RST2LATEX_OPTIONS , <EOL> source_path , dest_path ) ) <EOL> os . system ( command ) <EOL> def build_pygments_macros ( filename ) : <EOL> from pygments . formatters import LatexFormatter <EOL> text = LatexFormatter ( ) . get_style_defs ( ) <EOL> f = file ( filename , "<STR_LIT:w>" ) <EOL> f . write ( text ) <EOL> f . write ( '<STR_LIT:\n>' ) <EOL> f . close ( ) <EOL> def copy_epydoc_macros ( src , dest , existing_header_lines ) : <EOL> doc = file ( src , '<STR_LIT:r>' ) <EOL> out = file ( dest , "<STR_LIT:w>" ) <EOL> for line in doc : <EOL> if line . startswith ( '<STR_LIT>' ) or line . startswith ( '<STR_LIT>' ) or '<STR_LIT>' in line or '<STR_LIT>' in line : <EOL> break <EOL> if line . startswith ( '<STR_LIT:%>' ) or r'<STR_LIT>' in line or r'<STR_LIT>' in line or r'<STR_LIT>' in line : <EOL> continue <EOL> if line . startswith ( r'<STR_LIT>' ) : <EOL> if line in existing_header_lines : <EOL> continue <EOL> if '<STR_LIT>' in line : <EOL> line = line . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> out . write ( replace_epydoc_macros ( '<STR_LIT>' , line ) ) <EOL> out . close ( ) <EOL> doc . close ( ) <EOL> def noop ( input ) : <EOL> return input <EOL> counter_no = <NUM_LIT:0> <EOL> def tex_postprocess ( src_path , dest_path , want_header = False , process_line = noop ) : <EOL> """<STR_LIT>""" <EOL> title = '<STR_LIT>' <EOL> header = [ ] <EOL> add_header_line = header . append <EOL> global counter_no <EOL> counter_no = counter_no + <NUM_LIT:1> <EOL> counter_text = "<STR_LIT>" % counter_no <EOL> search_title = re . compile ( r'<STR_LIT>' ) . search <EOL> skipping = re . compile ( r'<STR_LIT>' ) . search <EOL> src = file ( src_path ) <EOL> dest = file ( dest_path , "<STR_LIT:w>" ) <EOL> src_text = src . read ( ) <EOL> src . close ( ) <EOL> title = search_title ( src_text ) <EOL> if title : <EOL> title = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , title . group ( <NUM_LIT:1> ) ) <EOL> iter_lines = iter ( src_text . splitlines ( ) ) <EOL> for l in iter_lines : <EOL> l = process_line ( l ) <EOL> if not l : <EOL> continue <EOL> if want_header : <EOL> add_header_line ( replace_rst_macros ( '<STR_LIT>' , l ) ) <EOL> if l . startswith ( "<STR_LIT>" ) : <EOL> break <EOL> for l in iter_lines : <EOL> l = process_line ( l ) <EOL> if skipping ( l ) : <EOL> continue <EOL> elif "<STR_LIT>" in l : <EOL> break <EOL> elif "<STR_LIT>" in l : <EOL> l = l . replace ( "<STR_LIT>" , counter_text ) <EOL> dest . write ( l + '<STR_LIT:\n>' ) <EOL> if not title : <EOL> raise Exception ( "<STR_LIT>" % src_path ) <EOL> return title , header <EOL> def publish ( dirname , lxml_path , release ) : <EOL> if not os . path . exists ( dirname ) : <EOL> os . mkdir ( dirname ) <EOL> book_title = "<STR_LIT>" % release <EOL> doc_dir = os . path . join ( lxml_path , '<STR_LIT>' ) <EOL> script = os . path . join ( doc_dir , '<STR_LIT>' ) <EOL> pubkey = os . path . join ( doc_dir , '<STR_LIT>' ) <EOL> shutil . copy ( pubkey , dirname ) <EOL> build_pygments_macros ( os . path . join ( dirname , '<STR_LIT>' ) ) <EOL> header = [ ] <EOL> titles = { } <EOL> replace_interdoc_hyperrefs = re . compile ( <EOL> r'<STR_LIT>' ) . sub <EOL> replace_docinternal_hyperrefs = re . compile ( <EOL> r'<STR_LIT>' ) . sub <EOL> replace_image_paths = re . compile ( <EOL> r'<STR_LIT>' ) . sub <EOL> def build_hyperref ( match ) : <EOL> basename , extension = match . groups ( ) <EOL> outname = BASENAME_MAP . get ( basename , basename ) <EOL> if '<STR_LIT:#>' in extension : <EOL> anchor = extension . split ( '<STR_LIT:#>' ) [ - <NUM_LIT:1> ] <EOL> return r"<STR_LIT>" % anchor <EOL> elif extension != '<STR_LIT:html>' : <EOL> return r'<STR_LIT>' % ( <EOL> outname , extension ) <EOL> else : <EOL> return r"<STR_LIT>" % outname <EOL> def fix_relative_hyperrefs ( line ) : <EOL> line = replace_image_paths ( r'<STR_LIT>' , line ) <EOL> if r'<STR_LIT>' not in line : <EOL> return line <EOL> line = replace_interdoc_hyperrefs ( build_hyperref , line ) <EOL> return replace_docinternal_hyperrefs ( r'<STR_LIT>' , line ) <EOL> for section , text_files in SITE_STRUCTURE : <EOL> for filename in text_files : <EOL> if filename . startswith ( '<STR_LIT:@>' ) : <EOL> continue <EOL> basename = os . path . splitext ( os . path . basename ( filename ) ) [ <NUM_LIT:0> ] <EOL> basename = BASENAME_MAP . get ( basename , basename ) <EOL> outname = basename + '<STR_LIT>' <EOL> outpath = os . path . join ( dirname , outname ) <EOL> path = os . path . join ( doc_dir , filename ) <EOL> print "<STR_LIT>" % outname <EOL> rest2latex ( script , path , outpath ) <EOL> final_name = os . path . join ( dirname , os . path . dirname ( outname ) , <EOL> "<STR_LIT>" % os . path . basename ( outname ) ) <EOL> title , hd = tex_postprocess ( outpath , final_name , <EOL> want_header = not header , <EOL> process_line = fix_relative_hyperrefs ) <EOL> if not header : <EOL> header = hd <EOL> titles [ outname ] = title <EOL> print "<STR_LIT>" <EOL> apidocsname = '<STR_LIT>' <EOL> apipath = os . path . join ( dirname , apidocsname ) <EOL> tex_postprocess ( apipath , os . path . join ( dirname , "<STR_LIT>" % apidocsname ) , <EOL> process_line = fix_relative_hyperrefs ) <EOL> copy_epydoc_macros ( apipath , os . path . join ( dirname , '<STR_LIT>' ) , <EOL> set ( header ) ) <EOL> print "<STR_LIT>" <EOL> find_version_title = re . compile ( <EOL> r'<STR_LIT>' ) . search <EOL> def fix_changelog ( line ) : <EOL> m = find_version_title ( line ) <EOL> if m : <EOL> line = "<STR_LIT>" % m . groups ( ) <EOL> else : <EOL> line = line . replace ( r'<STR_LIT>' , r'<STR_LIT>' ) <EOL> return line <EOL> chgname = '<STR_LIT>' % release <EOL> chgpath = os . path . join ( dirname , chgname ) <EOL> rest2latex ( script , <EOL> os . path . join ( lxml_path , '<STR_LIT>' ) , <EOL> chgpath ) <EOL> tex_postprocess ( chgpath , os . path . join ( dirname , "<STR_LIT>" % chgname ) , <EOL> process_line = fix_changelog ) <EOL> print "<STR_LIT>" % TARGET_FILE <EOL> master = file ( os . path . join ( dirname , TARGET_FILE ) , "<STR_LIT:w>" ) <EOL> for hln in header : <EOL> if hln . startswith ( r"<STR_LIT>" ) : <EOL> hln = DOCUMENT_CLASS + EPYDOC_IMPORT <EOL> elif hln . startswith ( r"<STR_LIT>" ) : <EOL> master . write ( PYGMENTS_IMPORT ) <EOL> elif hln . startswith ( r"<STR_LIT>" ) : <EOL> hln = replace_content ( <EOL> r'<STR_LIT>' % book_title , hln ) <EOL> elif hln . startswith ( r"<STR_LIT>" ) : <EOL> hln = replace_content ( <EOL> r'<STR_LIT>' % datetime . date . today ( ) . isoformat ( ) , hln ) <EOL> elif hln . startswith ( "<STR_LIT>" ) : <EOL> hln = replace_content ( <EOL> r'<STR_LIT>' % book_title , hln ) <EOL> master . write ( hln + '<STR_LIT:\n>' ) <EOL> master . write ( "<STR_LIT>" ) <EOL> master . write ( "<STR_LIT>" ) <EOL> for section , text_files in SITE_STRUCTURE : <EOL> master . write ( "<STR_LIT>" % section ) <EOL> for filename in text_files : <EOL> if filename . startswith ( '<STR_LIT:@>' ) : <EOL> continue <EOL> else : <EOL> basename = os . path . splitext ( os . path . basename ( filename ) ) [ <NUM_LIT:0> ] <EOL> basename = BASENAME_MAP . get ( basename , basename ) <EOL> outname = basename + '<STR_LIT>' <EOL> write_chapter ( master , titles [ outname ] , outname ) <EOL> master . write ( "<STR_LIT>" ) <EOL> master . write ( "<STR_LIT>" ) <EOL> write_chapter ( master , "<STR_LIT>" , chgname ) <EOL> write_chapter ( master , "<STR_LIT>" , apidocsname ) <EOL> master . write ( "<STR_LIT>" ) <EOL> master . write ( "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> publish ( sys . argv [ <NUM_LIT:1> ] , sys . argv [ <NUM_LIT:2> ] , sys . argv [ <NUM_LIT:3> ] ) </s>
