• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import logging
17import os
18
19from vts.proto import VtsReportMessage_pb2 as ReportMsg
20from vts.runners.host import keys
21from vts.utils.python.reporting import report_file_utils
22from vts.utils.python.systrace import systrace_controller
23from vts.utils.python.web import feature_utils
24
25
26class LogUploadingFeature(feature_utils.Feature):
27    """Feature object for log uploading functionality.
28
29    Attributes:
30        enabled: boolean, True if log uploading is enabled, False otherwise
31        web: (optional) WebFeature, object storing web feature util for test run
32        _report_file_util: report file util object for uploading logs
33    """
34
35    _TOGGLE_PARAM = keys.ConfigKeys.IKEY_ENABLE_LOG_UPLOADING
36    _REQUIRED_PARAMS = [
37        keys.ConfigKeys.IKEY_ANDROID_DEVICE,
38        keys.ConfigKeys.IKEY_LOG_UPLOADING_PATH,
39    ]
40    _OPTIONAL_PARAMS = [
41        keys.ConfigKeys.KEY_TESTBED_NAME,
42        keys.ConfigKeys.IKEY_LOG_UPLOADING_USE_DATE_DIRECTORY,
43        keys.ConfigKeys.IKEY_LOG_UPLOADING_URL_PREFIX,
44    ]
45
46    def __init__(self, user_params, web=None):
47        """Initializes the log uploading feature.
48
49        Args:
50            user_params: A dictionary from parameter name (String) to parameter value.
51            web: (optional) WebFeature, object storing web feature util for test run
52        """
53        self.ParseParameters(
54            toggle_param_name=self._TOGGLE_PARAM,
55            required_param_names=self._REQUIRED_PARAMS,
56            optional_param_names=self._OPTIONAL_PARAMS,
57            user_params=user_params)
58        self.web = web
59        logging.info("Log uploading enabled: %s", self.enabled)
60
61        if not self.enabled:
62            return
63
64        self._report_file_util = report_file_utils.ReportFileUtil(
65            flatten_source_dir=True,
66            use_destination_date_dir=getattr(
67                self, keys.ConfigKeys.IKEY_LOG_UPLOADING_USE_DATE_DIRECTORY,
68                True),
69            destination_dir=getattr(self,
70                                    keys.ConfigKeys.IKEY_LOG_UPLOADING_PATH),
71            url_prefix=getattr(
72                self, keys.ConfigKeys.IKEY_LOG_UPLOADING_URL_PREFIX, None))
73
74    def UploadLogs(self, prefix=None):
75        """Save test logs and add log urls to report message"""
76        if not self.enabled:
77            return
78
79        file_name_prefix = '%s_%s_' % (getattr(
80            self, keys.ConfigKeys.KEY_TESTBED_NAME, ''),
81                                       self.web.report_msg.start_timestamp)
82
83        def path_filter(path):
84            '''filter to exclude proto files in log uploading'''
85            return not path.endswith('_proto.msg')
86
87        urls = self._report_file_util.SaveReportsFromDirectory(
88            source_dir=logging.log_path,
89            file_name_prefix=file_name_prefix,
90            file_path_filters=path_filter)
91
92        if urls is None:
93            logging.error('Error happened when saving logs.')
94
95        if urls and self.web and self.web.enabled:
96            self.web.AddLogUrls(urls)
97