Home | History | Annotate | Download | only in firmware_TouchMTB
      1 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """Common utility functions that are not touch specific."""
      6 
      7 import logging
      8 import os
      9 import subprocess
     10 import sys
     11 import termios
     12 import tty
     13 
     14 
     15 def simple_system(cmd):
     16     """Replace autotest utils.system() locally.
     17 
     18     This method is grabbed from hardware_Trackpad and was written by truty.
     19     """
     20     ret = subprocess.call(cmd, shell=True)
     21     if ret:
     22         logging.warning('Command (%s) failed (ret=%s).', cmd, ret)
     23     return ret
     24 
     25 
     26 def simple_system_output(cmd):
     27     """Replace autotest utils.system_output() locally.
     28 
     29     This method is grabbed from hardware_Trackpad and was written by truty.
     30     """
     31     try:
     32         proc = subprocess.Popen(cmd, shell=True,
     33                                 stdout=subprocess.PIPE,
     34                                 stderr=subprocess.STDOUT)
     35         stdout, _ = proc.communicate()
     36     except Exception, e:
     37         logging.warning('Command (%s) failed (%s).', cmd, e)
     38     else:
     39         if proc.returncode:
     40             return None
     41         return stdout.strip()
     42 
     43 
     44 def getch():
     45     """Get a single character without typing ENTER."""
     46     fin = sys.stdin
     47     old_attrs = termios.tcgetattr(fin)
     48     tty.setraw(fin.fileno())
     49     try:
     50         char = fin.read(1)
     51     except ValueError:
     52         char = ''
     53     finally:
     54         termios.tcsetattr(fin, termios.TCSADRAIN, old_attrs)
     55     return char
     56 
     57 
     58 def program_exists(program):
     59     """Check if an executable program exists."""
     60     return os.system('which %s > /dev/null 2>&1' % program) == 0
     61 
     62 
     63 def print_and_exit(msg, exit_code=1):
     64     """Print a message and exit."""
     65     print msg
     66     sys.exit(exit_code)
     67 
     68 
     69 class Debug:
     70     """A simple class to print the debug message."""
     71     def __init__(self, debug_flag=False):
     72         self._debug_flag = debug_flag
     73 
     74     def print_msg(self, msg):
     75         """Print the message if _debug_flag is True."""
     76         if self._debug_flag:
     77             print msg
     78