<s> """<STR_LIT>""" <EOL> import subprocess <EOL> import doctest <EOL> import os <EOL> import sys <EOL> import shutil <EOL> import re <EOL> import cgi <EOL> import rfc822 <EOL> from cStringIO import StringIO <EOL> from paste . util import PySourceColor <EOL> here = os . path . abspath ( __file__ ) <EOL> paste_parent = os . path . dirname ( <EOL> os . path . dirname ( os . path . dirname ( here ) ) ) <EOL> def run ( command ) : <EOL> data = run_raw ( command ) <EOL> if data : <EOL> print ( data ) <EOL> def run_raw ( command ) : <EOL> """<STR_LIT>""" <EOL> proc = subprocess . Popen ( command , shell = True , <EOL> stderr = subprocess . STDOUT , <EOL> stdout = subprocess . PIPE , env = _make_env ( ) ) <EOL> data = proc . stdout . read ( ) <EOL> proc . wait ( ) <EOL> while data . endswith ( '<STR_LIT:\n>' ) or data . endswith ( '<STR_LIT:\r>' ) : <EOL> data = data [ : - <NUM_LIT:1> ] <EOL> if data : <EOL> data = '<STR_LIT:\n>' . join ( <EOL> [ l for l in data . splitlines ( ) if l ] ) <EOL> return data <EOL> else : <EOL> return '<STR_LIT>' <EOL> def run_command ( command , name , and_print = False ) : <EOL> output = run_raw ( command ) <EOL> data = '<STR_LIT>' % ( command , output ) <EOL> show_file ( '<STR_LIT>' , name , description = '<STR_LIT>' , <EOL> data = data ) <EOL> if and_print and output : <EOL> print ( output ) <EOL> def _make_env ( ) : <EOL> env = os . environ . copy ( ) <EOL> env [ '<STR_LIT>' ] = ( env . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> + '<STR_LIT::>' <EOL> + os . path . join ( paste_parent , '<STR_LIT>' ) <EOL> + '<STR_LIT::>' <EOL> + os . path . join ( paste_parent , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> env [ '<STR_LIT>' ] = ( env . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> + '<STR_LIT::>' <EOL> + paste_parent ) <EOL> return env <EOL> def clear_dir ( dir ) : <EOL> """<STR_LIT>""" <EOL> shutil . rmtree ( dir , True ) <EOL> def ls ( dir = None , recurse = False , indent = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> dir = dir or os . getcwd ( ) <EOL> fns = os . listdir ( dir ) <EOL> fns . sort ( ) <EOL> for fn in fns : <EOL> full = os . path . join ( dir , fn ) <EOL> if os . path . isdir ( full ) : <EOL> fn = fn + '<STR_LIT:/>' <EOL> print ( '<STR_LIT:U+0020>' * indent + fn ) <EOL> if os . path . isdir ( full ) and recurse : <EOL> ls ( dir = full , recurse = True , indent = indent + <NUM_LIT:2> ) <EOL> default_app = None <EOL> default_url = None <EOL> def set_default_app ( app , url ) : <EOL> global default_app <EOL> global default_url <EOL> default_app = app <EOL> default_url = url <EOL> def resource_filename ( fn ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( <EOL> os . path . dirname ( sys . testing_document_filename ) , <EOL> '<STR_LIT>' , <EOL> os . path . splitext ( os . path . basename ( sys . testing_document_filename ) ) [ <NUM_LIT:0> ] , <EOL> fn ) <EOL> def show ( path_info , example_name ) : <EOL> fn = resource_filename ( example_name + '<STR_LIT>' ) <EOL> out = StringIO ( ) <EOL> assert default_app is not None , ( <EOL> "<STR_LIT>" ) <EOL> url = default_url + path_info <EOL> out . write ( '<STR_LIT>' <EOL> % ( url , url ) ) <EOL> out . write ( '<STR_LIT>' ) <EOL> proc = subprocess . Popen ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' + path_info ] , <EOL> stderr = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , <EOL> env = _make_env ( ) ) <EOL> stdout , errors = proc . communicate ( ) <EOL> stdout = StringIO ( stdout ) <EOL> headers = rfc822 . Message ( stdout ) <EOL> content = stdout . read ( ) <EOL> for header , value in headers . items ( ) : <EOL> if header . lower ( ) == '<STR_LIT:status>' and int ( value . split ( ) [ <NUM_LIT:0> ] ) == <NUM_LIT:200> : <EOL> continue <EOL> if header . lower ( ) in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> if ( header . lower ( ) == '<STR_LIT>' <EOL> and value . startswith ( '<STR_LIT>' ) ) : <EOL> continue <EOL> out . write ( '<STR_LIT>' <EOL> % ( header , value ) ) <EOL> lines = [ l for l in content . splitlines ( ) if l . strip ( ) ] <EOL> for line in lines : <EOL> out . write ( line + '<STR_LIT:\n>' ) <EOL> if errors : <EOL> out . write ( '<STR_LIT>' <EOL> % errors ) <EOL> out . write ( '<STR_LIT>' ) <EOL> result = out . getvalue ( ) <EOL> if not os . path . exists ( fn ) : <EOL> f = open ( fn , '<STR_LIT:wb>' ) <EOL> f . write ( result ) <EOL> f . close ( ) <EOL> else : <EOL> f = open ( fn , '<STR_LIT:rb>' ) <EOL> expected = f . read ( ) <EOL> f . close ( ) <EOL> if not html_matches ( expected , result ) : <EOL> print ( '<STR_LIT>' % fn ) <EOL> print ( '<STR_LIT:->' * <NUM_LIT> ) <EOL> print ( expected ) <EOL> print ( '<STR_LIT:=>' * <NUM_LIT> ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT:->' * <NUM_LIT> ) <EOL> print ( result ) <EOL> def html_matches ( pattern , text ) : <EOL> regex = re . escape ( pattern ) <EOL> regex = regex . replace ( r'<STR_LIT>' , '<STR_LIT>' ) <EOL> regex = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , regex ) <EOL> regex = '<STR_LIT>' % regex <EOL> return re . search ( regex , text ) <EOL> def convert_docstring_string ( data ) : <EOL> if data . startswith ( '<STR_LIT:\n>' ) : <EOL> data = data [ <NUM_LIT:1> : ] <EOL> lines = data . splitlines ( ) <EOL> new_lines = [ ] <EOL> for line in lines : <EOL> if line . rstrip ( ) == '<STR_LIT:.>' : <EOL> new_lines . append ( '<STR_LIT>' ) <EOL> else : <EOL> new_lines . append ( line ) <EOL> data = '<STR_LIT:\n>' . join ( new_lines ) + '<STR_LIT:\n>' <EOL> return data <EOL> def create_file ( path , version , data ) : <EOL> data = convert_docstring_string ( data ) <EOL> write_data ( path , data ) <EOL> show_file ( path , version ) <EOL> def append_to_file ( path , version , data ) : <EOL> data = convert_docstring_string ( data ) <EOL> f = open ( path , '<STR_LIT:a>' ) <EOL> f . write ( data ) <EOL> f . close ( ) <EOL> if path . endswith ( '<STR_LIT>' ) : <EOL> pyc_file = path + '<STR_LIT:c>' <EOL> if os . path . exists ( pyc_file ) : <EOL> os . unlink ( pyc_file ) <EOL> show_file ( path , version , description = '<STR_LIT>' % path , <EOL> data = data ) <EOL> def show_file ( path , version , description = None , data = None ) : <EOL> ext = os . path . splitext ( path ) [ <NUM_LIT:1> ] <EOL> if data is None : <EOL> f = open ( path , '<STR_LIT:rb>' ) <EOL> data = f . read ( ) <EOL> f . close ( ) <EOL> if ext == '<STR_LIT>' : <EOL> html = ( '<STR_LIT>' <EOL> % PySourceColor . str2html ( data , PySourceColor . dark ) ) <EOL> else : <EOL> html = '<STR_LIT>' % cgi . escape ( data , <NUM_LIT:1> ) <EOL> html = '<STR_LIT>' % ( <EOL> description or path , html ) <EOL> write_data ( resource_filename ( '<STR_LIT>' % ( path , version ) ) , <EOL> html ) <EOL> def call_source_highlight ( input , format ) : <EOL> proc = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' % format ] , shell = False , <EOL> stdout = subprocess . PIPE ) <EOL> stdout , stderr = proc . communicate ( input ) <EOL> result = stdout <EOL> proc . wait ( ) <EOL> return result <EOL> def write_data ( path , data ) : <EOL> dir = os . path . dirname ( os . path . abspath ( path ) ) <EOL> if not os . path . exists ( dir ) : <EOL> os . makedirs ( dir ) <EOL> f = open ( path , '<STR_LIT:wb>' ) <EOL> f . write ( data ) <EOL> f . close ( ) <EOL> def change_file ( path , changes ) : <EOL> f = open ( os . path . abspath ( path ) , '<STR_LIT:rb>' ) <EOL> lines = f . readlines ( ) <EOL> f . close ( ) <EOL> for change_type , line , text in changes : <EOL> if change_type == '<STR_LIT>' : <EOL> lines [ line : line ] = [ text ] <EOL> elif change_type == '<STR_LIT>' : <EOL> lines [ line : text ] = [ ] <EOL> else : <EOL> assert <NUM_LIT:0> , ( <EOL> "<STR_LIT>" % change_type ) <EOL> f = open ( path , '<STR_LIT:wb>' ) <EOL> f . write ( '<STR_LIT>' . join ( lines ) ) <EOL> f . close ( ) <EOL> class LongFormDocTestParser ( doctest . DocTestParser ) : <EOL> """<STR_LIT>""" <EOL> _EXAMPLE_RE = re . compile ( r"""<STR_LIT>""" , re . MULTILINE | re . VERBOSE ) <EOL> def _parse_example ( self , m , name , lineno ) : <EOL> r"""<STR_LIT>""" <EOL> runner = m . group ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> indent = len ( m . group ( '<STR_LIT>' % runner ) ) <EOL> source_lines = m . group ( '<STR_LIT>' % runner ) . split ( '<STR_LIT:\n>' ) <EOL> if runner : <EOL> self . _check_prefix ( source_lines [ <NUM_LIT:1> : ] , '<STR_LIT:U+0020>' * indent , name , lineno ) <EOL> else : <EOL> self . _check_prompt_blank ( source_lines , indent , name , lineno ) <EOL> self . _check_prefix ( source_lines [ <NUM_LIT:2> : ] , '<STR_LIT:U+0020>' * indent + '<STR_LIT:.>' , name , lineno ) <EOL> if runner : <EOL> source = '<STR_LIT:\n>' . join ( [ sl [ indent : ] for sl in source_lines ] ) <EOL> else : <EOL> source = '<STR_LIT:\n>' . join ( [ sl [ indent + <NUM_LIT:4> : ] for sl in source_lines ] ) <EOL> if runner : <EOL> want = '<STR_LIT>' <EOL> exc_msg = None <EOL> else : <EOL> want = m . group ( '<STR_LIT>' ) <EOL> want_lines = want . split ( '<STR_LIT:\n>' ) <EOL> if len ( want_lines ) > <NUM_LIT:1> and re . match ( r'<STR_LIT>' , want_lines [ - <NUM_LIT:1> ] ) : <EOL> del want_lines [ - <NUM_LIT:1> ] <EOL> self . _check_prefix ( want_lines , '<STR_LIT:U+0020>' * indent , name , <EOL> lineno + len ( source_lines ) ) <EOL> want = '<STR_LIT:\n>' . join ( [ wl [ indent : ] for wl in want_lines ] ) <EOL> m = self . _EXCEPTION_RE . match ( want ) <EOL> if m : <EOL> exc_msg = m . group ( '<STR_LIT>' ) <EOL> else : <EOL> exc_msg = None <EOL> options = self . _find_options ( source , name , lineno ) <EOL> return source , options , want , exc_msg <EOL> def parse ( self , string , name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> string = string . expandtabs ( ) <EOL> min_indent = self . _min_indent ( string ) <EOL> if min_indent > <NUM_LIT:0> : <EOL> string = '<STR_LIT:\n>' . join ( [ l [ min_indent : ] for l in string . split ( '<STR_LIT:\n>' ) ] ) <EOL> output = [ ] <EOL> charno , lineno = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for m in self . _EXAMPLE_RE . finditer ( string ) : <EOL> output . append ( string [ charno : m . start ( ) ] ) <EOL> lineno += string . count ( '<STR_LIT:\n>' , charno , m . start ( ) ) <EOL> ( source , options , want , exc_msg ) = self . _parse_example ( m , name , lineno ) <EOL> if not self . _IS_BLANK_OR_COMMENT ( source ) : <EOL> output . append ( doctest . Example ( <EOL> source , want , exc_msg , <EOL> lineno = lineno , <EOL> indent = min_indent + len ( m . group ( '<STR_LIT>' ) or m . group ( '<STR_LIT>' ) ) , <EOL> options = options ) ) <EOL> lineno += string . count ( '<STR_LIT:\n>' , m . start ( ) , m . end ( ) ) <EOL> charno = m . end ( ) <EOL> output . append ( string [ charno : ] ) <EOL> return output <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if sys . argv [ <NUM_LIT:1> : ] and sys . argv [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> doctest . testmod ( ) <EOL> sys . exit ( ) <EOL> if not paste_parent in sys . path : <EOL> sys . path . append ( paste_parent ) <EOL> for fn in sys . argv [ <NUM_LIT:1> : ] : <EOL> fn = os . path . abspath ( fn ) <EOL> sys . testing_document_filename = fn <EOL> doctest . testfile ( <EOL> fn , module_relative = False , <EOL> optionflags = doctest . ELLIPSIS | doctest . REPORT_ONLY_FIRST_FAILURE , <EOL> parser = LongFormDocTestParser ( ) ) <EOL> new = os . path . splitext ( fn ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> assert new != fn <EOL> os . system ( '<STR_LIT>' % ( fn , new ) ) </s>
