<s> from __future__ import with_statement <EOL> import sys , platform , os , shutil , time , inspect , tempfile , doctest <EOL> class Tee ( object ) : <EOL> def __init__ ( self , _fd1 , _fd2 ) : <EOL> self . fd1 = _fd1 <EOL> self . fd2 = _fd2 <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> for toClose in ( self . fd1 , self . fd2 ) : <EOL> if toClose not in ( sys . stdout , sys . stderr , <EOL> sys . __stdout__ , sys . __stderr__ , None ) : <EOL> toClose . close ( ) <EOL> self . fd1 = self . fd2 = None <EOL> def write ( self , text ) : <EOL> self . fd1 . write ( text ) <EOL> self . fd2 . write ( text ) <EOL> def flush ( self ) : <EOL> self . fd1 . flush ( ) <EOL> self . fd2 . flush ( ) <EOL> try : <EOL> import nose <EOL> except ImportError , e : <EOL> print "<STR_LIT>" <EOL> raise e <EOL> if os . name == '<STR_LIT>' : <EOL> app_dir = os . environ [ '<STR_LIT>' ] <EOL> if int ( platform . version ( ) . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] ) > <NUM_LIT:5> : <EOL> app_dir = os . path . join ( app_dir , '<STR_LIT>' ) <EOL> else : <EOL> app_dir = os . path . join ( app_dir , '<STR_LIT>' ) <EOL> else : <EOL> app_dir = os . environ [ '<STR_LIT>' ] <EOL> if platform . system ( ) == '<STR_LIT>' : <EOL> app_dir = os . path . join ( app_dir , '<STR_LIT>' ) <EOL> else : <EOL> app_dir = os . path . join ( app_dir , '<STR_LIT>' ) <EOL> backup_dir = app_dir + '<STR_LIT>' <EOL> DELETE_BACKUP_ARG = '<STR_LIT>' <EOL> class RemoveBackupError ( Exception ) : pass <EOL> def nose_test ( module = None , extraArgs = None , pymelDir = None ) : <EOL> """<STR_LIT>""" <EOL> if pymelDir : <EOL> os . chdir ( pymelDir ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:5>' <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' <EOL> noseKwArgs = { } <EOL> noseArgv = "<STR_LIT>" . split ( ) <EOL> if module is None : <EOL> excludes = r'''<STR_LIT>''' . split ( ) <EOL> inGui = False <EOL> try : <EOL> import maya . cmds <EOL> inGui = not maya . cmds . about ( batch = <NUM_LIT:1> ) <EOL> except Exception : pass <EOL> if not inGui : <EOL> excludes . extend ( '<STR_LIT>' . split ( ) ) <EOL> noseArgv += [ '<STR_LIT>' , '<STR_LIT:|>' . join ( [ '<STR_LIT>' % x for x in excludes ] ) ] <EOL> if inspect . ismodule ( module ) : <EOL> noseKwArgs [ '<STR_LIT>' ] = module <EOL> elif module : <EOL> noseArgv . append ( module ) <EOL> if extraArgs is not None : <EOL> noseArgv . extend ( extraArgs ) <EOL> noseKwArgs [ '<STR_LIT>' ] = noseArgv <EOL> with DocTestPatcher ( ) : <EOL> print noseKwArgs <EOL> nose . main ( ** noseKwArgs ) <EOL> def backupAndTest ( extraNoseArgs ) : <EOL> if os . path . isdir ( backup_dir ) : <EOL> print "<STR_LIT>" % backup_dir <EOL> else : <EOL> print "<STR_LIT>" % ( app_dir , backup_dir ) <EOL> shutil . move ( app_dir , backup_dir ) <EOL> try : <EOL> nose_test ( extraArgs = extraNoseArgs ) <EOL> except Exception , e : <EOL> print e <EOL> finally : <EOL> try : <EOL> removeBackup ( ) <EOL> except RemoveBackupError : <EOL> print "<STR_LIT>" <EOL> os . spawnl ( os . P_NOWAIT , <EOL> sys . executable , os . path . basename ( sys . executable ) , <EOL> __file__ , DELETE_BACKUP_ARG ) <EOL> def removeBackup ( ) : <EOL> assert os . path . isdir ( backup_dir ) , "<STR_LIT>" % backup_dir <EOL> print "<STR_LIT>" , app_dir <EOL> tempdir = os . path . join ( tempfile . gettempdir ( ) , '<STR_LIT>' ) <EOL> if os . path . exists ( tempdir ) : <EOL> shutil . rmtree ( tempdir ) <EOL> try : <EOL> shutil . move ( app_dir , tempdir ) <EOL> except Exception , e : <EOL> print ( '<STR_LIT>' % <EOL> ( app_dir , tempdir , e ) ) <EOL> else : <EOL> try : <EOL> shutil . rmtree ( tempdir ) <EOL> except Exception , e : <EOL> print ( '<STR_LIT>' % <EOL> ( tempdir , backup_dir , e ) ) <EOL> else : <EOL> shutil . move ( backup_dir , app_dir ) <EOL> print "<STR_LIT>" <EOL> def removeBackupLoop ( retryTime = <NUM_LIT> , printFailure = False ) : <EOL> assert os . path . isdir ( backup_dir ) , "<STR_LIT>" % backup_dir <EOL> print "<STR_LIT>" , app_dir <EOL> lastException = None <EOL> start = time . time ( ) <EOL> while os . path . isdir ( app_dir ) : <EOL> try : <EOL> tempdir = os . path . join ( tempfile . gettempdir ( ) , '<STR_LIT>' ) <EOL> if os . path . exists ( tempdir ) : <EOL> shutil . rmtree ( tempdir ) <EOL> shutil . move ( app_dir , tempdir ) <EOL> shutil . rmtree ( tempdir ) <EOL> except Exception , e : <EOL> lastException = e <EOL> time . sleep ( <NUM_LIT> ) <EOL> else : <EOL> lastException = None <EOL> if time . time ( ) - start > retryTime : <EOL> break <EOL> if lastException is not None : <EOL> if printFailure : <EOL> print ( '<STR_LIT>' % <EOL> ( app_dir , backup_dir , lastException ) ) <EOL> raise RemoveBackupError <EOL> else : <EOL> shutil . move ( backup_dir , app_dir ) <EOL> print "<STR_LIT>" <EOL> class DocTestPatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __enter__ ( self ) : <EOL> self . set_from_module ( ) <EOL> self . set_wantFile ( ) <EOL> def set_from_module ( self ) : <EOL> self . orig_from_module = doctest . DocTestFinder . __dict__ [ '<STR_LIT>' ] <EOL> def _from_module ( docTestFinder_self , module , object ) : <EOL> """<STR_LIT>""" <EOL> if inspect . isfunction ( object ) : <EOL> if '<STR_LIT>' in module . __class__ . __name__ : <EOL> if module . __name__ == object . __module__ : <EOL> return True <EOL> return self . orig_from_module ( docTestFinder_self , module , object ) <EOL> doctest . DocTestFinder . _from_module = _from_module <EOL> def set_wantFile ( self ) : <EOL> import nose <EOL> import nose . plugins . doctests <EOL> self . orig_wantFile = nose . plugins . doctests . Doctest . __dict__ [ '<STR_LIT>' ] <EOL> def wantFile ( self , file ) : <EOL> """<STR_LIT>""" <EOL> if ( ( file . endswith ( '<STR_LIT>' ) or ( self . extension <EOL> and anyp ( file . endswith , self . extension ) ) ) <EOL> and ( not self . conf . exclude <EOL> or not filter ( None , <EOL> [ exc . search ( file ) <EOL> for exc in self . conf . exclude ] ) ) ) : <EOL> return True <EOL> return None <EOL> nose . plugins . doctests . Doctest . wantFile = wantFile <EOL> def __exit__ ( self , * args , ** kwargs ) : <EOL> doctest . DocTestFinder . _from_module = self . orig_from_module <EOL> if self . orig_wantFile is not None : <EOL> import nose . plugins . doctests <EOL> nose . plugins . doctests . Doctest . wantFile = self . orig_wantFile <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if DELETE_BACKUP_ARG not in sys . argv : <EOL> oldPath = os . getcwd ( ) <EOL> testsDir = os . path . dirname ( os . path . abspath ( sys . argv [ <NUM_LIT:0> ] ) ) <EOL> pymelRoot = os . path . dirname ( testsDir ) <EOL> noseArgs = sys . argv [ <NUM_LIT:1> : ] <EOL> os . chdir ( pymelRoot ) <EOL> pypath = os . environ [ '<STR_LIT>' ] . split ( os . pathsep ) <EOL> sys . path . append ( testsDir ) <EOL> pypath . append ( testsDir ) <EOL> sys . path . insert ( <NUM_LIT:0> , pymelRoot ) <EOL> pypath . insert ( <NUM_LIT:0> , pymelRoot ) <EOL> os . environ [ '<STR_LIT>' ] = os . pathsep . join ( pypath ) <EOL> nose_test ( extraArgs = noseArgs ) <EOL> os . chdir ( oldPath ) <EOL> else : <EOL> removeBackup ( ) </s>
