1# Copyright 2017 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Provides class FolderMounter.
16
17The FolderMounter implements the abstract class BaseMounter. It can
18get files from a given folder. The  folder is usually the system/vendor folder
19of $OUT folder in an Android build environment.
20"""
21
22import logging
23import os
24
25from gsi_util.mounters import base_mounter
26
27
28class _FolderFileAccessor(base_mounter.BaseFileAccessor):
29
30  def __init__(self, folder_dir, path_prefix):
31    super(_FolderFileAccessor, self).__init__(path_prefix)
32    self._folder_dir = folder_dir
33
34  # override
35  def _handle_prepare_file(self, filename_in_storage):
36    filename = os.path.join(self._folder_dir, filename_in_storage)
37    logging.info('_FolderFileAccessor: Prepare file %s -> %s',
38                 filename_in_storage, filename)
39    if not os.path.isfile(filename):
40      logging.info('  File is not exist: %s', filename_in_storage)
41      return None
42    return base_mounter.MounterFile(filename)
43
44
45class FolderMounter(base_mounter.BaseMounter):
46  """Provides a file accessor which can access files in the given folder."""
47
48  def __init__(self, folder_dir, path_prefix):
49    super(FolderMounter, self).__init__()
50    self._folder_dir = folder_dir
51    self._path_prefix = path_prefix
52
53  # override
54  def _handle_mount(self):
55    return _FolderFileAccessor(self._folder_dir, self._path_prefix)
56