def _cleanup_and_exit(self, err_msg): """Clean up and exit with the error message. @param err_msg: the error message to print """ self._cleanup() print_and_exit(err_msg)
def _get_new_worksheet_by_title(self, worksheet_title, row_count=WORKSHEET_ROW_COUNT, col_count=WORKSHEET_COL_COUNT): """Create a new worksheet using the title. If the worksheet title already exists, using a new title name such as "Copy n of title", where n = 2, 3, ..., MAX_TITLE_DUP + 1 @param title: the worksheet title @param row_count: the number of rows in the worksheet @param col_count: the number of columns in the worksheet """ MAX_TITLE_DUP = 10 new_worksheet_title = worksheet_title for i in range(2, MAX_TITLE_DUP + 2): if not self._get_worksheet_entry_by_title(new_worksheet_title): break new_worksheet_title = 'Copy %d of %s' % (i, worksheet_title) self.worksheet_title = new_worksheet_title else: msg = 'Too many duplicate copies of the worksheet title: %s.' print_and_exit(msg % worksheet_title) # Add the new worksheet and get the worksheet_id. worksheet_entry = self.ss_client.add_worksheet(self.spreadsheet_key, new_worksheet_title, row_count, col_count) worksheet_id = worksheet_entry.get_worksheet_id() self.feed = gdata.spreadsheets.data.build_batch_cells_update( self.spreadsheet_key, worksheet_id) self.feed.add_set_cell(1, 1, SHEET_DESCRIPTION)
def _parse(): """Parse the command line options.""" parser = argparse.ArgumentParser( description='Play a raw data file and capture its image.') parser.add_argument('filename', help='a raw data file in mtplot format') parser.add_argument('-d', '--device', help='the device type (default: touchpad)', choices=['touchpad', 'touchscreen'], default='touchpad') args = parser.parse_args() # Get the touchpad/touchscreen device node from the device option is_ts = (args.device == 'touchscreen') args.device_node = TouchDevice.get_device_node(is_touchscreen=is_ts) if args.device_node is None: print_and_exit('Error: fail to get device node for %s.' % args.device) # Check the existence of the raw data file. if not os.path.isfile(args.filename): print_and_exit('Error: The file "%s" does not exist.' % args.filename) print '\nthe device node of the %s: %s\n' % (args.device, args.device_node) print 'the raw data file: %s\n' % args.filename return args
def _read_logs(self): """Read the result logs in the specified log directory.""" # Get logs in the log_dir or its sub-directories. log_filenames = glob.glob(os.path.join(self.log_dir, '*.log')) if not log_filenames: log_filenames = glob.glob(os.path.join(self.log_dir, '*', '*.log')) if not log_filenames: err_msg = 'Error: no log files in the test result directory: %s' print_and_exit(err_msg % self.log_dir) self.log_table = SimpleTable() self.fws = set() self.gestures = set() # fw_validators keeps track of the validators of every firmware self.fw_validators = defaultdict(set) for i, log_filename in enumerate(log_filenames): round_no = i if self.individual_round_flag else None self._add_round_log(log_filename, round_no) # Convert set to list below self.fws = sorted(list(self.fws)) self.gestures = sorted(list(self.gestures)) # Construct validators by taking the union of the validators of # all firmwares. self.validators = sorted(list(set.union(*self.fw_validators.values()))) for fw in self.fws: self.fw_validators[fw] = sorted(list(self.fw_validators[fw]))
def _save_device_description_file(self): """Save the device description file for future replay.""" filename = '%s.%s' % (self.board, self._get_device_ext()) filepath = os.path.join(self.log_dir, filename) if not self.touch_device.save_device_description_file( filepath, self.board): msg = 'Error: fail to save the device description file: %s' print_and_exit(msg % filepath)
def load(filename): """Load the log from the pickle file.""" try: with open(filename) as log_file: return pickle.load(log_file) except Exception, e: msg = 'Error in loading the log file (%s): %s' % (filename, e) print_and_exit(msg)
def _cleanup(self): """Remove the temporary directory that holds the gesture event files.""" if os.path.isdir(self.event_dir): print 'Removing tmp directory "%s" .... ' % self.event_dir try: shutil.rmtree(self.event_dir) except Exception as e: msg = 'Error in removing tmp directory ("%s"): %s' print_and_exit(msg % (self.event_dir, e))
def dump(self, filename): """Dump the log to the specified filename.""" try: with open(filename, 'w') as log_file: pickle.dump([self._fw, self._round_name, self._test_version, self._glogs], log_file) except Exception, e: msg = 'Error in dumping to the log file (%s): %s' % (filename, e) print_and_exit(msg)
def _get_board(self): """Get the board. If this is in replay mode, get the board from the replay directory. Otherwise, get the board name from current chromebook machine. """ replay_dir = self.options[OPTIONS.REPLAY] if replay_dir: self.board = firmware_utils.get_board_from_directory(replay_dir) if self.board is None: msg = 'Error: cannot get the board from the replay directory %s' print_and_exit(msg % replay_dir) else: self.board = firmware_utils.get_board() print ' board: %s' % self.board
def upload_to_gs(log_dir): """Upload the gesture event files specified in log_dir to Google cloud storage server. @param log_dir: the log directory of which the gesture event files are to be uploaded to Google cloud storage server """ # Set up gsutil package. # The board argument is used to locate the proper bucket directory gs = cros_gs.CrosGs(firmware_utils.get_board()) log_path = os.path.join(conf.log_root_dir, log_dir) if not os.path.isdir(log_path): print_and_exit('Error: the log path "%s" does not exist.' % log_path) print 'Uploading "%s" to %s ...\n' % (log_path, gs.bucket) try: gs.upload(log_path) except Exception, e: msg = 'Error in uploading event files in %s: %s.' print_and_exit(msg % (log_path, e))
def _get_device_description_file(self): """Get the device description file for replay purpose. Get the device description file only when it is in replay mode and the system DEVICE option is not specified. The priority to locate the device description file: (1) in the directory specified by the REPLAY option, (2) in the tests/device/ directory A device description file name looks like "link.touchpad" """ self.device_description_file = None # Replay without using the system device. So use a mocked device. if self.options[OPTIONS.REPLAY] and not self.options[OPTIONS.DEVICE]: device_ext = self._get_device_ext() board = self.board descriptions = [ # (1) Try to find the device description in REPLAY directory. (self.options[OPTIONS.REPLAY], '*.%s' % device_ext), # (2) Try to find the device description in tests/device/ ( conf.device_description_dir, '%s.%s' % (board, device_ext), ) ] for description_dir, description_pattern in descriptions: files = glob.glob( os.path.join(description_dir, description_pattern)) if files: self.device_description_file = files[0] break else: msg = 'Error: cannot find the device description file.' print_and_exit(msg) print ' device description file: %s' % self.device_description_file