utils.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import string
  2. import os
  3. class failure_exception:
  4. def __init__( self, rc ):
  5. self.rc_ = rc
  6. def __str__( self ):
  7. return "rc: %d" % self.rc_
  8. def system( commands ):
  9. if os.path.exists( "tmp.cmd" ):
  10. os.chmod( "tmp.cmd", 0777 )
  11. os.unlink( "tmp.cmd" )
  12. f = open( "tmp.cmd", "w" )
  13. f.write( string.join( commands, "\n" ) )
  14. f.close()
  15. rc = os.system( "tmp.cmd" )
  16. os.chmod( "tmp.cmd", 0777 )
  17. os.unlink( "tmp.cmd" )
  18. return rc
  19. def checked_system( commands, valid_return_codes = [ 0 ] ):
  20. rc = system( commands )
  21. if rc not in [ 0 ] + valid_return_codes: raise failure_exception( rc )
  22. return rc
  23. class step_controller:
  24. def __init__( self, start_step ):
  25. self.current_step_ = None;
  26. self.skip_to_step_ = start_step
  27. def start_step( self, step_name, start_message ):
  28. self.current_step_ = step_name
  29. if self.is_skipping( step_name ):
  30. print "[%s] Skipping." % step_name
  31. return 0
  32. else:
  33. self.skip_to_step_ = ""
  34. print "[%s] %s" % ( step_name, start_message )
  35. return 1
  36. def finish_step( self, step_name ):
  37. print "[%s] Finished" % step_name
  38. def is_skipping( self, step_name = None ):
  39. if step_name is None: step_name = self.current_step_
  40. return self.skip_to_step_ != "" and self.skip_to_step_ != step_name
粤ICP备19079148号