1#
2# Copyright (C) 2016 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#
16
17from vts.runners.host import const
18from vts.testcases.kernel.ltp.shell_environment.definitions.base_definitions import check_setup_cleanup
19
20
21class BinInPath(check_setup_cleanup.CheckSetupCleanup):
22    """Class for check existence of, make, and afterwards delete directories.
23
24    Attributes:
25        to_check: bool, whether or not to check the defined environment
26                  requirement. Default: True
27        to_setup: bool, whether or not to setup the defined environment
28                  requirement. Default: False
29        to_cleanup: bool, whether or not to cleanup the defined environment
30                    requirement if it is set up by this class. Default: False
31        _paths: list string, target directory paths
32        _failed_paths: list of string, paths that don't have desired permissions
33    """
34
35    def __init__(self,
36                 paths=None,
37                 to_check=True,
38                 to_setup=False,
39                 to_cleanup=False):
40        self._paths = paths
41        self._failed_paths = paths
42        self.to_check = to_check
43        self.to_setup = to_setup
44        self.to_cleanup = to_cleanup
45
46    def ValidateInputs(self):
47        """Validate input paths.
48
49        Check input path is not null or empty list or list containing
50        empty string. If input is a single path, it will
51        be converted to a single item list containing that path.
52        """
53        if not self._paths:
54            return False
55
56        self._paths = self.ToListLike(self._paths)
57
58        return all(self._paths)
59
60    def Check(self):
61        commands = ["which %s" % path for path in self._paths]
62        results = self.ExecuteShellCommand(commands)[const.EXIT_CODE]
63
64        self._failed_paths = [
65            path for path, fail in zip(self._paths, map(bool, results)) if fail
66        ]
67
68        if not self._failed_paths:
69            return True
70
71        self.note = "Some binary do not exist in path: %s" % self._failed_paths
72        return False
73