Home | History | Annotate | Download | only in definitions
      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 
     17 from vts.runners.host import const
     18 from vts.testcases.kernel.ltp.shell_environment.definitions.base_definitions import check_setup_cleanup
     19 
     20 
     21 class DirectoryExists(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         _permissions: list of int, desired permissions for each path
     34     """
     35 
     36     def __init__(self,
     37                  paths=None,
     38                  to_check=True,
     39                  to_setup=False,
     40                  to_cleanup=False):
     41         self._paths = paths
     42         self._failed_paths = paths
     43         self.to_check = to_check
     44         self.to_setup = to_setup
     45         self.to_cleanup = to_cleanup
     46 
     47     def ValidateInputs(self):
     48         """Validate input paths.
     49 
     50         Check input path is not null or empty list or list containing
     51         empty string. If input is a single path, it will
     52         be converted to a single item list containing that path.
     53         """
     54         if not self._paths:
     55             return False
     56 
     57         self._paths = self.ToListLike(self._paths)
     58 
     59         return all(self._paths)
     60 
     61     def Check(self):
     62         commands = ["ls %s" % path for path in self._paths]
     63         results = self.ExecuteShellCommand(commands)[const.EXIT_CODE]
     64 
     65         self._failed_paths = [
     66             path for path, fail in zip(self._paths, map(bool, results)) if fail
     67         ]
     68 
     69         if not self._failed_paths:
     70             return True
     71 
     72         self.note = "Some directories do not exist: %s" % self._failed_paths
     73         return False
     74 
     75     def Setup(self):
     76         commands = ["mkdir -p %s" % path for path in self._failed_paths]
     77         # TODO: perhaps store or print failed setup paths
     78         return not any(self.ExecuteShellCommand(commands)[const.EXIT_CODE])
     79 
     80     def Cleanup(self):
     81         commands = ["rm -rf %s" % path for path in self._failed_paths]
     82         return not any(self.ExecuteShellCommand(commands)[const.EXIT_CODE])
     83