| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2018 Telefonica S.A. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 18 | import os |
| 19 | import logging |
| tierno | 3054f78 | 2018-04-25 16:59:53 +0200 | [diff] [blame] | 20 | # import tarfile |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 21 | from http import HTTPStatus |
| 22 | from shutil import rmtree |
| tierno | 3054f78 | 2018-04-25 16:59:53 +0200 | [diff] [blame] | 23 | from osm_common.fsbase import FsBase, FsException |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 24 | |
| 25 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 26 | |
| 27 | |
| 28 | class FsLocal(FsBase): |
| 29 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 30 | def __init__(self, logger_name='fs', lock=False): |
| 31 | super().__init__(logger_name, lock) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 32 | self.path = None |
| 33 | |
| 34 | def get_params(self): |
| 35 | return {"fs": "local", "path": self.path} |
| 36 | |
| 37 | def fs_connect(self, config): |
| 38 | try: |
| 39 | if "logger_name" in config: |
| 40 | self.logger = logging.getLogger(config["logger_name"]) |
| 41 | self.path = config["path"] |
| 42 | if not self.path.endswith("/"): |
| 43 | self.path += "/" |
| 44 | if not os.path.exists(self.path): |
| 45 | raise FsException("Invalid configuration param at '[storage]': path '{}' does not exist".format( |
| 46 | config["path"])) |
| 47 | except FsException: |
| 48 | raise |
| 49 | except Exception as e: # TODO refine |
| 50 | raise FsException(str(e)) |
| 51 | |
| 52 | def fs_disconnect(self): |
| 53 | pass # TODO |
| 54 | |
| 55 | def mkdir(self, folder): |
| 56 | """ |
| 57 | Creates a folder or parent object location |
| 58 | :param folder: |
| 59 | :return: None or raises and exception |
| 60 | """ |
| 61 | try: |
| 62 | os.mkdir(self.path + folder) |
| tierno | c7ac30d | 2019-01-25 08:56:17 +0000 | [diff] [blame] | 63 | except FileExistsError: # make it idempotent |
| 64 | pass |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 65 | except Exception as e: |
| 66 | raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR) |
| 67 | |
| tierno | d4378aa | 2018-12-04 15:37:23 +0000 | [diff] [blame] | 68 | def dir_rename(self, src, dst): |
| 69 | """ |
| 70 | Rename one directory name. If dst exist, it replaces (deletes) existing directory |
| 71 | :param src: source directory |
| 72 | :param dst: destination directory |
| 73 | :return: None or raises and exception |
| 74 | """ |
| 75 | try: |
| 76 | if os.path.exists(self.path + dst): |
| 77 | rmtree(self.path + dst) |
| 78 | |
| 79 | os.rename(self.path + src, self.path + dst) |
| 80 | |
| 81 | except Exception as e: |
| 82 | raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR) |
| 83 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 84 | def file_exists(self, storage, mode=None): |
| 85 | """ |
| 86 | Indicates if "storage" file exist |
| 87 | :param storage: can be a str or a str list |
| 88 | :param mode: can be 'file' exist as a regular file; 'dir' exists as a directory or; 'None' just exists |
| 89 | :return: True, False |
| 90 | """ |
| 91 | if isinstance(storage, str): |
| 92 | f = storage |
| 93 | else: |
| 94 | f = "/".join(storage) |
| 95 | if os.path.exists(self.path + f): |
| 96 | if mode == "file" and os.path.isfile(self.path + f): |
| 97 | return True |
| 98 | if mode == "dir" and os.path.isdir(self.path + f): |
| 99 | return True |
| 100 | return False |
| 101 | |
| 102 | def file_size(self, storage): |
| 103 | """ |
| 104 | return file size |
| 105 | :param storage: can be a str or a str list |
| 106 | :return: file size |
| 107 | """ |
| 108 | if isinstance(storage, str): |
| 109 | f = storage |
| 110 | else: |
| 111 | f = "/".join(storage) |
| 112 | return os.path.getsize(self.path + f) |
| 113 | |
| 114 | def file_extract(self, tar_object, path): |
| 115 | """ |
| 116 | extract a tar file |
| 117 | :param tar_object: object of type tar |
| 118 | :param path: can be a str or a str list, or a tar object where to extract the tar_object |
| 119 | :return: None |
| 120 | """ |
| 121 | if isinstance(path, str): |
| 122 | f = self.path + path |
| 123 | else: |
| 124 | f = self.path + "/".join(path) |
| 125 | tar_object.extractall(path=f) |
| 126 | |
| 127 | def file_open(self, storage, mode): |
| 128 | """ |
| 129 | Open a file |
| 130 | :param storage: can be a str or list of str |
| 131 | :param mode: file mode |
| 132 | :return: file object |
| 133 | """ |
| 134 | try: |
| 135 | if isinstance(storage, str): |
| 136 | f = storage |
| 137 | else: |
| 138 | f = "/".join(storage) |
| 139 | return open(self.path + f, mode) |
| 140 | except FileNotFoundError: |
| 141 | raise FsException("File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND) |
| 142 | except IOError: |
| 143 | raise FsException("File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST) |
| 144 | |
| 145 | def dir_ls(self, storage): |
| 146 | """ |
| 147 | return folder content |
| 148 | :param storage: can be a str or list of str |
| 149 | :return: folder content |
| 150 | """ |
| 151 | try: |
| 152 | if isinstance(storage, str): |
| 153 | f = storage |
| 154 | else: |
| 155 | f = "/".join(storage) |
| 156 | return os.listdir(self.path + f) |
| 157 | except NotADirectoryError: |
| 158 | raise FsException("File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND) |
| 159 | except IOError: |
| 160 | raise FsException("File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST) |
| 161 | |
| 162 | def file_delete(self, storage, ignore_non_exist=False): |
| 163 | """ |
| tierno | d4378aa | 2018-12-04 15:37:23 +0000 | [diff] [blame] | 164 | Delete storage content recursively |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 165 | :param storage: can be a str or list of str |
| 166 | :param ignore_non_exist: not raise exception if storage does not exist |
| 167 | :return: None |
| 168 | """ |
| tierno | c7ac30d | 2019-01-25 08:56:17 +0000 | [diff] [blame] | 169 | try: |
| 170 | if isinstance(storage, str): |
| 171 | f = self.path + storage |
| 172 | else: |
| 173 | f = self.path + "/".join(storage) |
| 174 | if os.path.exists(f): |
| 175 | rmtree(f) |
| 176 | elif not ignore_non_exist: |
| 177 | raise FsException("File {} does not exist".format(storage), http_code=HTTPStatus.NOT_FOUND) |
| 178 | except (IOError, PermissionError) as e: |
| 179 | raise FsException("File {} cannot be deleted: {}".format(f, e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR) |