Go to the documentation of this file.00001
00002 """
00003 @file rs_general.py
00004 """
00005
00006 import os
00007 import subprocess
00008 import time
00009 import string
00010 import signal
00011 import commands
00012 import fileinput
00013 from PIL import Image
00014
00015
00016 BASE_LOG_DIR = (os.path.expanduser('~/.ros/log/'))
00017 LOG_FOLDER = os.listdir(BASE_LOG_DIR)
00018 LOG_FOLDER.sort(key=lambda fn: os.path.getmtime(BASE_LOG_DIR+fn)
00019 if os.path.isdir(BASE_LOG_DIR+fn) else 0)
00020 LOGFILE = BASE_LOG_DIR+LOG_FOLDER[-1] + '/rosout.log'
00021
00022
00023 def parse_camera_type(args):
00024 """parse args to get camera type
00025 @fn parse_camera_type
00026 @param args: all the arguments
00027 @return camera_type
00028 """
00029 argc = len(args)
00030 if argc == 0:
00031 return None
00032
00033 for i in range(0, argc):
00034 if args[i] == 'camera_type' and i+1 < argc:
00035 return args[i+1]
00036
00037
00038 def get_camera_params_and_values(args):
00039 """parse args to get all the camera parameters and paired values
00040 the args transferred from .test file, should remove 4 elements
00041 in args: start 1 - script name;
00042 last 3 - '--gtest_output', '--name', '_log';
00043 @fn parse_camera_type
00044 @param args: all the arguments
00045 @return param_dict: dictionary of camera params
00046 """
00047 param_dict = {}
00048 argc = len(args) - 4
00049 if argc == 0:
00050 return None
00051
00052 params = args[1:-3]
00053 for i in range(0, argc, 2):
00054 if i+1 < argc:
00055 param_dict.setdefault(params[i], params[i+1])
00056
00057 return param_dict
00058
00059
00060 def is_log_contains_keyword(log_file, keyword):
00061 """check if the keyword contains in log"""
00062 if (not os.path.exists(os.path.expanduser(log_file)) or
00063 keyword is None or
00064 keyword == ''):
00065 return False
00066 file = open(os.path.expanduser(log_file))
00067 for line in file:
00068 if line.find(keyword) != -1:
00069 file.close()
00070 return True
00071 file.close()
00072 return False
00073
00074
00075 def shell_cmd_timeout(cmd, timeout=0):
00076 """Execute shell command till timeout"""
00077 cmd_proc = subprocess.Popen(cmd, stderr=subprocess.STDOUT,
00078 stdout=subprocess.PIPE, shell=True,
00079 preexec_fn=os.setsid)
00080 pid = cmd_proc.pid
00081 pgid = os.getpgid(pid)
00082 if not cmd_proc:
00083 return -1, ''
00084 t_timeout, tick = timeout, 2
00085 while True:
00086 time.sleep(tick)
00087 ret = cmd_proc.poll()
00088 if ret is not None:
00089 break
00090 if t_timeout > 0:
00091 t_timeout -= tick
00092 if t_timeout <= 0:
00093 os.killpg(pgid, signal.SIGTERM)
00094 ret = -99999
00095 break
00096 return