<s> """<STR_LIT>""" <EOL> import subprocess <EOL> import os <EOL> import os . path <EOL> import re <EOL> import sys <EOL> from tempfile import mkstemp , mkdtemp , NamedTemporaryFile <EOL> from markmin2latex import markmin2latex <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def removeall ( path ) : <EOL> ERROR_STR = """<STR_LIT>""" <EOL> def rmgeneric ( path , __func__ ) : <EOL> try : <EOL> __func__ ( path ) <EOL> except OSError , ( errno , strerror ) : <EOL> print ERROR_STR % { '<STR_LIT:path>' : path , '<STR_LIT:error>' : strerror } <EOL> files = [ path ] <EOL> while files : <EOL> file = files [ <NUM_LIT:0> ] <EOL> if os . path . isfile ( file ) : <EOL> f = os . remove <EOL> rmgeneric ( file , os . remove ) <EOL> del files [ <NUM_LIT:0> ] <EOL> elif os . path . isdir ( file ) : <EOL> nested = os . listdir ( file ) <EOL> if not nested : <EOL> rmgeneric ( file , os . rmdir ) <EOL> del files [ <NUM_LIT:0> ] <EOL> else : <EOL> files = [ os . path . join ( file , x ) for x in nested ] + files <EOL> def latex2pdf ( latex , pdflatex = '<STR_LIT>' , passes = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> pdflatex = pdflatex <EOL> passes = passes <EOL> warnings = [ ] <EOL> tmpdir = mkdtemp ( ) <EOL> texfile = open ( tmpdir + '<STR_LIT>' , '<STR_LIT:wb>' ) <EOL> texfile . write ( latex ) <EOL> texfile . seek ( <NUM_LIT:0> ) <EOL> texfile . close ( ) <EOL> texfile = os . path . abspath ( texfile . name ) <EOL> for i in range ( <NUM_LIT:0> , passes ) : <EOL> logfd , logname = mkstemp ( ) <EOL> outfile = os . fdopen ( logfd ) <EOL> try : <EOL> ret = subprocess . call ( [ pdflatex , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , tmpdir , <EOL> texfile ] , <EOL> cwd = os . path . dirname ( texfile ) , stdout = outfile , <EOL> stderr = subprocess . PIPE ) <EOL> finally : <EOL> outfile . close ( ) <EOL> re_errors = re . compile ( '<STR_LIT>' , re . M ) <EOL> re_warnings = re . compile ( '<STR_LIT>' , re . M ) <EOL> flog = open ( logname ) <EOL> try : <EOL> loglines = flog . read ( ) <EOL> finally : <EOL> flog . close ( ) <EOL> errors = re_errors . findall ( loglines ) <EOL> warnings = re_warnings . findall ( loglines ) <EOL> os . unlink ( logname ) <EOL> pdffile = texfile . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> if os . path . isfile ( pdffile ) : <EOL> fpdf = open ( pdffile , '<STR_LIT:rb>' ) <EOL> try : <EOL> data = fpdf . read ( ) <EOL> finally : <EOL> fpdf . close ( ) <EOL> else : <EOL> data = None <EOL> removeall ( tmpdir ) <EOL> return data , warnings , errors <EOL> def markmin2pdf ( text , image_mapper = lambda x : None , extra = { } ) : <EOL> return latex2pdf ( markmin2latex ( text , image_mapper = image_mapper , extra = extra ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> import doctest <EOL> import markmin2html <EOL> if sys . argv [ <NUM_LIT:1> : <NUM_LIT:2> ] == [ '<STR_LIT>' ] : <EOL> data , warnings , errors = markmin2pdf ( markmin2html . __doc__ ) <EOL> if errors : <EOL> print '<STR_LIT>' + '<STR_LIT:\n>' . join ( errors ) <EOL> print '<STR_LIT>' + '<STR_LIT:\n>' . join ( warnings ) <EOL> else : <EOL> print data <EOL> elif len ( sys . argv ) > <NUM_LIT:1> : <EOL> fargv = open ( sys . argv [ <NUM_LIT:1> ] , '<STR_LIT:rb>' ) <EOL> try : <EOL> data , warnings , errors = markmin2pdf ( fargv . read ( ) ) <EOL> finally : <EOL> fargv . close ( ) <EOL> if errors : <EOL> print '<STR_LIT>' + '<STR_LIT:\n>' . join ( errors ) <EOL> print '<STR_LIT>' + '<STR_LIT:\n>' . join ( warnings ) <EOL> else : <EOL> print data <EOL> else : <EOL> doctest . testmod ( ) </s>
