<s> '''<STR_LIT>''' <EOL> import warnings <EOL> warnings . simplefilter ( '<STR_LIT:ignore>' , DeprecationWarning ) <EOL> import os , sys , tempfile , md5 <EOL> VERSION = '<STR_LIT>' <EOL> TEX_HEADER = r'''<STR_LIT>''' <EOL> TEX_FOOTER = r'''<STR_LIT>''' <EOL> verbose = False <EOL> class EApp ( Exception ) : pass <EOL> def print_stderr ( line ) : <EOL> sys . stderr . write ( line + os . linesep ) <EOL> def print_verbose ( line ) : <EOL> if verbose : <EOL> print_stderr ( line ) <EOL> def write_file ( filename , data , mode = '<STR_LIT:w>' ) : <EOL> f = open ( filename , mode ) <EOL> try : <EOL> f . write ( data ) <EOL> finally : <EOL> f . close ( ) <EOL> def read_file ( filename , mode = '<STR_LIT:r>' ) : <EOL> f = open ( filename , mode ) <EOL> try : <EOL> return f . read ( ) <EOL> finally : <EOL> f . close ( ) <EOL> def run ( cmd ) : <EOL> global verbose <EOL> if verbose : <EOL> cmd += '<STR_LIT>' <EOL> else : <EOL> cmd += '<STR_LIT>' % os . devnull <EOL> print_verbose ( '<STR_LIT>' % cmd ) <EOL> if os . system ( cmd ) : <EOL> raise EApp , '<STR_LIT>' % cmd <EOL> def latex2png ( infile , outfile , dpi , modified ) : <EOL> '''<STR_LIT>''' <EOL> outfile = os . path . abspath ( outfile ) <EOL> outdir = os . path . dirname ( outfile ) <EOL> if not os . path . isdir ( outdir ) : <EOL> raise EApp , '<STR_LIT>' % outdir <EOL> texfile = tempfile . mktemp ( suffix = '<STR_LIT>' , dir = os . path . dirname ( outfile ) ) <EOL> basefile = os . path . splitext ( texfile ) [ <NUM_LIT:0> ] <EOL> dvifile = basefile + '<STR_LIT>' <EOL> temps = [ basefile + ext for ext in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> skip = False <EOL> if infile == '<STR_LIT:->' : <EOL> tex = sys . stdin . read ( ) <EOL> if modified : <EOL> checksum = md5 . new ( tex ) . digest ( ) <EOL> md5_file = os . path . splitext ( outfile ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> if os . path . isfile ( md5_file ) and os . path . isfile ( outfile ) and checksum == read_file ( md5_file , '<STR_LIT:rb>' ) : <EOL> skip = True <EOL> else : <EOL> if not os . path . isfile ( infile ) : <EOL> raise EApp , '<STR_LIT>' % infile <EOL> tex = read_file ( infile ) <EOL> if modified and os . path . isfile ( outfile ) and os . path . getmtime ( infile ) <= os . path . getmtime ( outfile ) : <EOL> skip = True <EOL> if skip : <EOL> print_verbose ( '<STR_LIT>' % outfile ) <EOL> return <EOL> tex = '<STR_LIT>' % ( TEX_HEADER , tex . strip ( ) , TEX_FOOTER ) <EOL> print_verbose ( '<STR_LIT>' % tex ) <EOL> write_file ( texfile , tex ) <EOL> saved_pwd = os . getcwd ( ) <EOL> os . chdir ( outdir ) <EOL> try : <EOL> run ( '<STR_LIT>' % texfile ) <EOL> cmd = '<STR_LIT>' <EOL> if dpi : <EOL> cmd += '<STR_LIT>' % dpi <EOL> cmd += '<STR_LIT>' % ( outfile , dvifile ) <EOL> run ( cmd ) <EOL> finally : <EOL> os . chdir ( saved_pwd ) <EOL> for f in temps : <EOL> if os . path . isfile ( f ) : <EOL> print_verbose ( '<STR_LIT>' % f ) <EOL> os . remove ( f ) <EOL> if '<STR_LIT>' in locals ( ) : <EOL> print_verbose ( '<STR_LIT>' % md5_file ) <EOL> write_file ( md5_file , checksum , '<STR_LIT:wb>' ) <EOL> def usage ( msg = '<STR_LIT>' ) : <EOL> if msg : <EOL> print_stderr ( msg ) <EOL> print_stderr ( '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def main ( ) : <EOL> global verbose <EOL> dpi = None <EOL> outfile = None <EOL> modified = False <EOL> import getopt <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:version>' ] ) <EOL> for o , v in opts : <EOL> if o in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> print __doc__ <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if o == '<STR_LIT>' : <EOL> print ( '<STR_LIT>' % ( VERSION , ) ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if o == '<STR_LIT>' : dpi = v <EOL> if o == '<STR_LIT>' : outfile = v <EOL> if o == '<STR_LIT>' : modified = True <EOL> if o == '<STR_LIT>' : verbose = True <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> infile = args [ <NUM_LIT:0> ] <EOL> if dpi and not dpi . isdigit ( ) : <EOL> usage ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if outfile is None : <EOL> if infile == '<STR_LIT:->' : <EOL> usage ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> outfile = os . path . splitext ( infile ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> latex2png ( infile , outfile , dpi , modified ) <EOL> if infile == '<STR_LIT:->' : <EOL> sys . stdout . write ( '<STR_LIT:U+0020>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> try : <EOL> main ( ) <EOL> except SystemExit : <EOL> raise <EOL> except KeyboardInterrupt : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except Exception , e : <EOL> print_stderr ( "<STR_LIT>" % ( os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) , str ( e ) ) ) <EOL> sys . exit ( <NUM_LIT:1> ) </s>
