home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / command.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  4.1 KB  |  150 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os
  20. import logging
  21.  
  22. from checkbox.lib.process import Process
  23. from checkbox.lib.environ import (get_variables, add_variable, remove_variable,
  24.     get_paths, prepend_path, remove_path)
  25.  
  26.  
  27. SUCCESS = 0
  28. FAILURE = 1
  29. ALL_STATUS = [SUCCESS, FAILURE]
  30.  
  31.  
  32. class CommandResult(object):
  33.  
  34.     def __init__(self, command, status, stdout, stderr, start_time, end_time):
  35.         self.command = command
  36.         self.status = status
  37.         self.stdout = stdout
  38.         self.stderr = stderr
  39.         self.start_time = start_time
  40.         self.end_time = end_time
  41.  
  42.         self.if_exited = os.WIFEXITED(self.status)
  43.         self.if_signaled = os.WIFSIGNALED(self.status)
  44.         self.if_stopped = os.WIFSTOPPED(self.status)
  45.         self.if_continued = os.WIFCONTINUED(self.status)
  46.  
  47.     @property
  48.     def exit_status(self):
  49.         if not self.if_exited:
  50.             raise Exception, "Command not exited: %s" % self.command
  51.  
  52.         return os.WEXITSTATUS(self.status)
  53.  
  54.     @property
  55.     def term_signal(self):
  56.         if not self.if_signaled:
  57.             raise Exception, "Command not signaled: %s" % self.command
  58.  
  59.         return os.WTERMSIG(self.status)
  60.  
  61.     @property
  62.     def stop_signal(self):
  63.         if not self.if_stopped:
  64.             raise Exception, "Command not stopped: %s" % self.command
  65.  
  66.         return os.WSTOPSIG(self.status)
  67.  
  68.     @property
  69.     def duration(self):
  70.         if not self.end_time:
  71.             raise Exception, "Command timed out: %s" % self.command
  72.  
  73.         return self.end_time - self.start_time
  74.  
  75.  
  76. class Command(object):
  77.  
  78.     def __init__(self, command=None, timeout=None, paths=[], variables={}):
  79.         self._command = command
  80.         self._timeout = timeout
  81.         self._paths = paths
  82.         self._variables = variables
  83.  
  84.     def __str__(self):
  85.         return self.get_command() or ""
  86.  
  87.     def __call__(self, *args, **kwargs):
  88.         return self.execute(*args, **kwargs)
  89.  
  90.     def execute(self, *args, **kwargs):
  91.         command = self.get_command()
  92.         if command is None:
  93.             return
  94.  
  95.         self.pre_execute(*args, **kwargs)
  96.  
  97.         # Sanitize environment
  98.         env = get_variables()
  99.         env["PATH"] = ":".join(get_paths())
  100.  
  101.         logging.info("Running command: %s", command)
  102.         process = Process(command, env)
  103.         if process.read(self._timeout):
  104.             logging.info("Command timed out, killing process.")
  105.             process.kill()
  106.  
  107.         status = process.cleanup()
  108.         result = CommandResult(self, status, process.outdata, process.errdata,
  109.             process.starttime, process.endtime)
  110.  
  111.         return self.post_execute(result)
  112.  
  113.     def pre_execute(self, *args, **kwargs):
  114.         variables = self.get_variables()
  115.         for key, value in variables.items():
  116.             add_variable(key, value)
  117.  
  118.         paths = self.get_paths()
  119.         for path in paths:
  120.             prepend_path(path)
  121.  
  122.     def post_execute(self, result):
  123.         paths = self.get_paths()
  124.         for path in paths:
  125.             remove_path(path)
  126.  
  127.         variables = self.get_variables()
  128.         for key in variables.keys():
  129.             remove_variable(key)
  130.  
  131.         return result
  132.  
  133.     def add_path(self, path):
  134.         self._paths.append(path)
  135.  
  136.     def add_variable(self, key, value):
  137.         self._variables[key] = value
  138.  
  139.     def get_command(self):
  140.         return self._command
  141.  
  142.     def get_paths(self):
  143.         return self._paths
  144.  
  145.     def get_variable(self, name, default=None):
  146.         return self._variables.get(name, default)
  147.  
  148.     def get_variables(self):
  149.         return self._variables
  150.