<s> USAGE = '''<STR_LIT>''' <EOL> __version__ = '<STR_LIT>' <EOL> __copyright__ = '<STR_LIT>' <EOL> import os , sys , re , difflib <EOL> if sys . platform [ : <NUM_LIT:4> ] == '<STR_LIT>' : <EOL> import cStringIO as StringIO <EOL> else : <EOL> import StringIO <EOL> import asciidocapi <EOL> BACKENDS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> BACKEND_EXT = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> def iif ( condition , iftrue , iffalse = None ) : <EOL> """<STR_LIT>""" <EOL> if iffalse is None : <EOL> if isinstance ( iftrue , basestring ) : <EOL> iffalse = '<STR_LIT>' <EOL> if type ( iftrue ) in ( int , float ) : <EOL> iffalse = <NUM_LIT:0> <EOL> if condition : <EOL> return iftrue <EOL> else : <EOL> return iffalse <EOL> def message ( msg = '<STR_LIT>' ) : <EOL> print >> sys . stderr , msg <EOL> def strip_end ( lines ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( len ( lines ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if not lines [ i ] : <EOL> del lines [ i ] <EOL> else : <EOL> break <EOL> def normalize_data ( lines ) : <EOL> """<STR_LIT>""" <EOL> result = [ s for s in lines if not s . startswith ( '<STR_LIT:#>' ) ] <EOL> strip_end ( result ) <EOL> return result <EOL> class AsciiDocTest ( object ) : <EOL> def __init__ ( self ) : <EOL> self . number = None <EOL> self . name = '<STR_LIT>' <EOL> self . title = '<STR_LIT>' <EOL> self . description = [ ] <EOL> self . source = None <EOL> self . options = [ ] <EOL> self . attributes = { } <EOL> self . backends = BACKENDS <EOL> self . datadir = None <EOL> self . disabled = False <EOL> def backend_filename ( self , backend ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( <EOL> os . path . normpath ( os . path . join ( self . datadir , self . name ) ) , <EOL> backend , <EOL> BACKEND_EXT [ backend ] ) <EOL> def parse ( self , lines , confdir , datadir ) : <EOL> """<STR_LIT>""" <EOL> self . __init__ ( ) <EOL> self . confdir = confdir <EOL> self . datadir = datadir <EOL> lines = Lines ( lines ) <EOL> while not lines . eol ( ) : <EOL> l = lines . read_until ( r'<STR_LIT>' ) <EOL> if l : <EOL> if not l [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:%>' ) : <EOL> if l [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT:!>' : <EOL> self . disabled = True <EOL> self . title = l [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] <EOL> else : <EOL> self . title = l [ <NUM_LIT:0> ] <EOL> self . description = l [ <NUM_LIT:1> : ] <EOL> continue <EOL> reo = re . match ( r'<STR_LIT>' , l [ <NUM_LIT:0> ] ) <EOL> if not reo : <EOL> raise ( ValueError , '<STR_LIT>' % l [ <NUM_LIT:0> ] ) <EOL> directive = reo . groupdict ( ) [ '<STR_LIT>' ] <EOL> data = normalize_data ( l [ <NUM_LIT:1> : ] ) <EOL> if directive == '<STR_LIT:source>' : <EOL> if data : <EOL> self . source = os . path . normpath ( os . path . join ( <EOL> self . confdir , os . path . normpath ( data [ <NUM_LIT:0> ] ) ) ) <EOL> elif directive == '<STR_LIT>' : <EOL> self . options = eval ( '<STR_LIT:U+0020>' . join ( data ) ) <EOL> for i , v in enumerate ( self . options ) : <EOL> if isinstance ( v , basestring ) : <EOL> self . options [ i ] = ( v , None ) <EOL> elif directive == '<STR_LIT>' : <EOL> self . attributes = eval ( '<STR_LIT:U+0020>' . join ( data ) ) <EOL> elif directive == '<STR_LIT>' : <EOL> self . backends = eval ( '<STR_LIT:U+0020>' . join ( data ) ) <EOL> elif directive == '<STR_LIT:name>' : <EOL> self . name = data [ <NUM_LIT:0> ] . strip ( ) <EOL> else : <EOL> raise ( ValueError , '<STR_LIT>' % l [ <NUM_LIT:0> ] ) <EOL> if not self . title : <EOL> self . title = self . source <EOL> if not self . name : <EOL> self . name = os . path . basename ( os . path . splitext ( self . source ) [ <NUM_LIT:0> ] ) <EOL> def is_missing ( self , backend ) : <EOL> """<STR_LIT>""" <EOL> return not os . path . isfile ( self . backend_filename ( backend ) ) <EOL> def is_missing_or_outdated ( self , backend ) : <EOL> """<STR_LIT>""" <EOL> return self . is_missing ( backend ) or ( <EOL> os . path . getmtime ( self . source ) <EOL> > os . path . getmtime ( self . backend_filename ( backend ) ) ) <EOL> def get_expected ( self , backend ) : <EOL> """<STR_LIT>""" <EOL> f = open ( self . backend_filename ( backend ) ) <EOL> try : <EOL> result = f . readlines ( ) <EOL> result = [ s . rstrip ( ) for s in result ] <EOL> finally : <EOL> f . close ( ) <EOL> return result <EOL> def generate_expected ( self , backend ) : <EOL> """<STR_LIT>""" <EOL> asciidoc = asciidocapi . AsciiDocAPI ( ) <EOL> asciidoc . options . values = self . options <EOL> asciidoc . attributes = self . attributes <EOL> infile = self . source <EOL> outfile = StringIO . StringIO ( ) <EOL> asciidoc . execute ( infile , outfile , backend ) <EOL> return outfile . getvalue ( ) . splitlines ( ) <EOL> def update_expected ( self , backend ) : <EOL> """<STR_LIT>""" <EOL> lines = self . generate_expected ( backend ) <EOL> if not os . path . isdir ( self . datadir ) : <EOL> print ( '<STR_LIT>' % self . datadir ) <EOL> os . mkdir ( self . datadir ) <EOL> f = open ( self . backend_filename ( backend ) , '<STR_LIT>' ) <EOL> try : <EOL> print ( '<STR_LIT>' % f . name ) <EOL> f . writelines ( [ s + os . linesep for s in lines ] ) <EOL> finally : <EOL> f . close ( ) <EOL> def update ( self , backend = None , force = False ) : <EOL> """<STR_LIT>""" <EOL> if backend is None : <EOL> backends = self . backends <EOL> else : <EOL> backends = [ backend ] <EOL> for backend in backends : <EOL> if force or self . is_missing_or_outdated ( backend ) : <EOL> self . update_expected ( backend ) <EOL> def run ( self , backend = None ) : <EOL> """<STR_LIT>""" <EOL> if backend is None : <EOL> backends = self . backends <EOL> else : <EOL> backends = [ backend ] <EOL> result = True <EOL> self . passed = self . failed = self . skipped = <NUM_LIT:0> <EOL> print ( '<STR_LIT>' % ( self . number , self . title ) ) <EOL> if self . source and os . path . isfile ( self . source ) : <EOL> print ( '<STR_LIT>' % self . source ) <EOL> for backend in backends : <EOL> fromfile = self . backend_filename ( backend ) <EOL> if not self . is_missing ( backend ) : <EOL> expected = self . get_expected ( backend ) <EOL> strip_end ( expected ) <EOL> got = self . generate_expected ( backend ) <EOL> strip_end ( got ) <EOL> lines = [ ] <EOL> for line in difflib . unified_diff ( got , expected , n = <NUM_LIT:0> ) : <EOL> lines . append ( line ) <EOL> if lines : <EOL> result = False <EOL> self . failed += <NUM_LIT:1> <EOL> lines = lines [ <NUM_LIT:3> : ] <EOL> print ( '<STR_LIT>' % ( backend , fromfile ) ) <EOL> message ( '<STR_LIT>' % fromfile ) <EOL> message ( '<STR_LIT>' ) <EOL> for line in lines : <EOL> message ( line ) <EOL> message ( ) <EOL> else : <EOL> self . passed += <NUM_LIT:1> <EOL> print ( '<STR_LIT>' % ( backend , fromfile ) ) <EOL> else : <EOL> self . skipped += <NUM_LIT:1> <EOL> print ( '<STR_LIT>' % ( backend , fromfile ) ) <EOL> else : <EOL> self . skipped += len ( backends ) <EOL> if self . source : <EOL> msg = '<STR_LIT>' % self . source <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> print ( msg ) <EOL> print ( '<STR_LIT>' ) <EOL> return result <EOL> class AsciiDocTests ( object ) : <EOL> def __init__ ( self , conffile ) : <EOL> """<STR_LIT>""" <EOL> self . conffile = os . path . normpath ( conffile ) <EOL> self . confdir = os . path . dirname ( self . conffile ) <EOL> self . datadir = self . confdir <EOL> self . tests = [ ] <EOL> self . globals = { } <EOL> f = open ( self . conffile ) <EOL> try : <EOL> lines = Lines ( f . readlines ( ) ) <EOL> finally : <EOL> f . close ( ) <EOL> first = True <EOL> while not lines . eol ( ) : <EOL> s = lines . read_until ( r'<STR_LIT>' ) <EOL> s = [ l for l in s if l ] <EOL> if len ( s ) > <NUM_LIT:1> : <EOL> if first and re . match ( r'<STR_LIT>' , s [ <NUM_LIT:0> ] ) : <EOL> self . globals = eval ( '<STR_LIT:U+0020>' . join ( normalize_data ( s [ <NUM_LIT:1> : ] ) ) ) <EOL> if '<STR_LIT>' in self . globals : <EOL> self . datadir = os . path . join ( <EOL> self . confdir , <EOL> os . path . normpath ( self . globals [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> test = AsciiDocTest ( ) <EOL> test . parse ( s [ <NUM_LIT:1> : ] , self . confdir , self . datadir ) <EOL> self . tests . append ( test ) <EOL> test . number = len ( self . tests ) <EOL> first = False <EOL> def run ( self , number = None , backend = None ) : <EOL> """<STR_LIT>""" <EOL> self . passed = self . failed = self . skipped = <NUM_LIT:0> <EOL> for test in self . tests : <EOL> if ( not test . disabled or number ) and ( not number or number == test . number ) and ( not backend or backend in test . backends ) : <EOL> test . run ( backend ) <EOL> self . passed += test . passed <EOL> self . failed += test . failed <EOL> self . skipped += test . skipped <EOL> if self . passed > <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' % self . passed ) <EOL> if self . failed > <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' % self . failed ) <EOL> if self . skipped > <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' % self . skipped ) <EOL> def update ( self , number = None , backend = None , force = False ) : <EOL> """<STR_LIT>""" <EOL> for test in self . tests : <EOL> if ( not test . disabled or number ) and ( not number or number == test . number ) : <EOL> test . update ( backend , force = force ) <EOL> def list ( self ) : <EOL> """<STR_LIT>""" <EOL> for test in self . tests : <EOL> print '<STR_LIT>' % ( test . number , iif ( test . disabled , '<STR_LIT:!>' ) , test . title ) <EOL> class Lines ( list ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lines ) : <EOL> super ( Lines , self ) . __init__ ( ) <EOL> self . extend ( [ s . rstrip ( ) for s in lines ] ) <EOL> self . pos = <NUM_LIT:0> <EOL> def eol ( self ) : <EOL> return self . pos >= len ( self ) <EOL> def read_until ( self , regexp ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> if not self . eol ( ) : <EOL> result . append ( self [ self . pos ] ) <EOL> self . pos += <NUM_LIT:1> <EOL> while not self . eol ( ) : <EOL> if re . match ( regexp , self [ self . pos ] ) : <EOL> break <EOL> result . append ( self [ self . pos ] ) <EOL> self . pos += <NUM_LIT:1> <EOL> return result <EOL> def usage ( msg = None ) : <EOL> if msg : <EOL> message ( msg + '<STR_LIT:\n>' ) <EOL> message ( USAGE ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import getopt <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> except getopt . GetoptError : <EOL> usage ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> conffile = os . path . join ( os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) , '<STR_LIT>' ) <EOL> force = False <EOL> for o , v in opts : <EOL> if o == '<STR_LIT>' : <EOL> force = True <EOL> if o in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> conffile = v <EOL> if not os . path . isfile ( conffile ) : <EOL> message ( '<STR_LIT>' % conffile ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> tests = AsciiDocTests ( conffile ) <EOL> cmd = args [ <NUM_LIT:0> ] <EOL> number = None <EOL> backend = None <EOL> for arg in args [ <NUM_LIT:1> : <NUM_LIT:3> ] : <EOL> try : <EOL> number = int ( arg ) <EOL> except ValueError : <EOL> backend = arg <EOL> if backend and backend not in BACKENDS : <EOL> message ( '<STR_LIT>' % backend ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if number is not None and number not in range ( <NUM_LIT:1> , len ( tests . tests ) + <NUM_LIT:1> ) : <EOL> message ( '<STR_LIT>' % number ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if cmd == '<STR_LIT>' : <EOL> tests . run ( number , backend ) <EOL> if tests . failed : <EOL> exit ( <NUM_LIT:1> ) <EOL> elif cmd == '<STR_LIT>' : <EOL> tests . update ( number , backend , force = force ) <EOL> elif cmd == '<STR_LIT:list>' : <EOL> tests . list ( ) <EOL> else : <EOL> usage ( '<STR_LIT>' % cmd ) </s>
