1# Copyright 2017 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"""This throttler reduces result size by deleting files permanently.""" 6 7import os 8 9try: 10 from autotest_lib.client.bin.result_tools import throttler_lib 11 from autotest_lib.client.bin.result_tools import utils_lib 12except ImportError: 13 import throttler_lib 14 import utils_lib 15 16 17# Default threshold of file size in KB for a file to be qualified for deletion. 18DEFAULT_FILE_SIZE_THRESHOLD_BYTE = 1024 * 1024 19 20# Regex for file path that should not be deleted. 21NON_DELETABLE_FILE_PATH_PATTERNS = [ 22 '.*perf.data$', # Performance test data. 23 ] 24 25def _delete_file(file_info): 26 """Delete the given file and update the summary. 27 28 @param file_info: A ResultInfo object containing summary for the file to be 29 shrunk. 30 """ 31 utils_lib.LOG('Deleting file %s.' % file_info.path) 32 try: 33 os.remove(file_info.path) 34 except OSError as e: 35 utils_lib.LOG('Failed to delete file %s Error: %s' % 36 (file_info.path, e)) 37 38 # Update the trimmed_size in ResultInfo. 39 file_info.trimmed_size = 0 40 41 42def throttle(summary, max_result_size_KB, 43 file_size_threshold_byte=DEFAULT_FILE_SIZE_THRESHOLD_BYTE, 44 exclude_file_patterns=[]): 45 """Throttle the files in summary by trimming file content. 46 47 Stop throttling until all files are processed or the result size is already 48 reduced to be under the given max_result_size_KB. 49 50 @param summary: A ResultInfo object containing result summary. 51 @param max_result_size_KB: Maximum test result size in KB. 52 @param file_size_threshold_byte: Threshold of file size in byte for a file 53 to be qualified for deletion. All qualified files will be deleted, 54 until all files are processed or the result size is under the given 55 max_result_size_KB. 56 @param exclude_file_patterns: A list of regex pattern for files not to be 57 throttled. Default is an empty list. 58 """ 59 file_infos, _ = throttler_lib.sort_result_files(summary) 60 file_infos = throttler_lib.get_throttleable_files( 61 file_infos, 62 exclude_file_patterns + NON_DELETABLE_FILE_PATH_PATTERNS) 63 64 for info in file_infos: 65 if info.trimmed_size > file_size_threshold_byte: 66 _delete_file(info) 67 if throttler_lib.check_throttle_limit(summary, max_result_size_KB): 68 return 69