home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / checkbox / command.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  5.5 KB  |  146 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import logging
  6. from checkbox.lib.process import Process
  7. from checkbox.lib.environ import get_variables, add_variable, remove_variable, get_paths, prepend_path, remove_path
  8. SUCCESS = 0
  9. FAILURE = 1
  10. ALL_STATUS = [
  11.     SUCCESS,
  12.     FAILURE]
  13.  
  14. class CommandResult(object):
  15.     
  16.     def __init__(self, command, status, stdout, stderr, start_time, end_time):
  17.         self.command = command
  18.         self.status = status
  19.         self.stdout = stdout
  20.         self.stderr = stderr
  21.         self.start_time = start_time
  22.         self.end_time = end_time
  23.         self.if_exited = os.WIFEXITED(self.status)
  24.         self.if_signaled = os.WIFSIGNALED(self.status)
  25.         self.if_stopped = os.WIFSTOPPED(self.status)
  26.         self.if_continued = os.WIFCONTINUED(self.status)
  27.  
  28.     
  29.     def exit_status(self):
  30.         if not self.if_exited:
  31.             raise Exception, 'Command not exited: %s' % self.command
  32.         self.if_exited
  33.         return os.WEXITSTATUS(self.status)
  34.  
  35.     exit_status = property(exit_status)
  36.     
  37.     def term_signal(self):
  38.         if not self.if_signaled:
  39.             raise Exception, 'Command not signaled: %s' % self.command
  40.         self.if_signaled
  41.         return os.WTERMSIG(self.status)
  42.  
  43.     term_signal = property(term_signal)
  44.     
  45.     def stop_signal(self):
  46.         if not self.if_stopped:
  47.             raise Exception, 'Command not stopped: %s' % self.command
  48.         self.if_stopped
  49.         return os.WSTOPSIG(self.status)
  50.  
  51.     stop_signal = property(stop_signal)
  52.     
  53.     def duration(self):
  54.         if not self.end_time:
  55.             raise Exception, 'Command timed out: %s' % self.command
  56.         self.end_time
  57.         return self.end_time - self.start_time
  58.  
  59.     duration = property(duration)
  60.  
  61.  
  62. class Command(object):
  63.     
  64.     def __init__(self, command = None, timeout = None, paths = [], variables = { }):
  65.         self._command = command
  66.         self._timeout = timeout
  67.         self._paths = paths
  68.         self._variables = variables
  69.  
  70.     
  71.     def __str__(self):
  72.         if not self.get_command():
  73.             pass
  74.         return ''
  75.  
  76.     
  77.     def __call__(self, *args, **kwargs):
  78.         return self.execute(*args, **kwargs)
  79.  
  80.     
  81.     def execute(self, *args, **kwargs):
  82.         command = self.get_command()
  83.         if command is None:
  84.             return None
  85.         self.pre_execute(*args, **kwargs)
  86.         env = get_variables()
  87.         env['PATH'] = ':'.join(get_paths())
  88.         logging.info('Running command: %s', command)
  89.         process = Process(command, env)
  90.         if process.read(self._timeout):
  91.             logging.info('Command timed out, killing process.')
  92.             process.kill()
  93.         
  94.         status = process.cleanup()
  95.         result = CommandResult(self, status, process.outdata, process.errdata, process.starttime, process.endtime)
  96.         return self.post_execute(result)
  97.  
  98.     
  99.     def pre_execute(self, *args, **kwargs):
  100.         variables = self.get_variables()
  101.         for key, value in variables.items():
  102.             add_variable(key, value)
  103.         
  104.         paths = self.get_paths()
  105.         for path in paths:
  106.             prepend_path(path)
  107.         
  108.  
  109.     
  110.     def post_execute(self, result):
  111.         paths = self.get_paths()
  112.         for path in paths:
  113.             remove_path(path)
  114.         
  115.         variables = self.get_variables()
  116.         for key in variables.keys():
  117.             remove_variable(key)
  118.         
  119.         return result
  120.  
  121.     
  122.     def add_path(self, path):
  123.         self._paths.append(path)
  124.  
  125.     
  126.     def add_variable(self, key, value):
  127.         self._variables[key] = value
  128.  
  129.     
  130.     def get_command(self):
  131.         return self._command
  132.  
  133.     
  134.     def get_paths(self):
  135.         return self._paths
  136.  
  137.     
  138.     def get_variable(self, name, default = None):
  139.         return self._variables.get(name, default)
  140.  
  141.     
  142.     def get_variables(self):
  143.         return self._variables
  144.  
  145.  
  146.