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