| 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 | |
| 30 | def __init__(self, logger_name='fs'): |
| 31 | self.logger = logging.getLogger(logger_name) |
| 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) |
| 63 | except Exception as e: |
| 64 | raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR) |
| 65 | |
| 66 | def file_exists(self, storage, mode=None): |
| 67 | """ |
| 68 | Indicates if "storage" file exist |
| 69 | :param storage: can be a str or a str list |
| 70 | :param mode: can be 'file' exist as a regular file; 'dir' exists as a directory or; 'None' just exists |
| 71 | :return: True, False |
| 72 | """ |
| 73 | if isinstance(storage, str): |
| 74 | f = storage |
| 75 | else: |
| 76 | f = "/".join(storage) |
| 77 | if os.path.exists(self.path + f): |
| 78 | if mode == "file" and os.path.isfile(self.path + f): |
| 79 | return True |
| 80 | if mode == "dir" and os.path.isdir(self.path + f): |
| 81 | return True |
| 82 | return False |
| 83 | |
| 84 | def file_size(self, storage): |
| 85 | """ |
| 86 | return file size |
| 87 | :param storage: can be a str or a str list |
| 88 | :return: file size |
| 89 | """ |
| 90 | if isinstance(storage, str): |
| 91 | f = storage |
| 92 | else: |
| 93 | f = "/".join(storage) |
| 94 | return os.path.getsize(self.path + f) |
| 95 | |
| 96 | def file_extract(self, tar_object, path): |
| 97 | """ |
| 98 | extract a tar file |
| 99 | :param tar_object: object of type tar |
| 100 | :param path: can be a str or a str list, or a tar object where to extract the tar_object |
| 101 | :return: None |
| 102 | """ |
| 103 | if isinstance(path, str): |
| 104 | f = self.path + path |
| 105 | else: |
| 106 | f = self.path + "/".join(path) |
| 107 | tar_object.extractall(path=f) |
| 108 | |
| 109 | def file_open(self, storage, mode): |
| 110 | """ |
| 111 | Open a file |
| 112 | :param storage: can be a str or list of str |
| 113 | :param mode: file mode |
| 114 | :return: file object |
| 115 | """ |
| 116 | try: |
| 117 | if isinstance(storage, str): |
| 118 | f = storage |
| 119 | else: |
| 120 | f = "/".join(storage) |
| 121 | return open(self.path + f, mode) |
| 122 | except FileNotFoundError: |
| 123 | raise FsException("File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND) |
| 124 | except IOError: |
| 125 | raise FsException("File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST) |
| 126 | |
| 127 | def dir_ls(self, storage): |
| 128 | """ |
| 129 | return folder content |
| 130 | :param storage: can be a str or list of str |
| 131 | :return: folder content |
| 132 | """ |
| 133 | try: |
| 134 | if isinstance(storage, str): |
| 135 | f = storage |
| 136 | else: |
| 137 | f = "/".join(storage) |
| 138 | return os.listdir(self.path + f) |
| 139 | except NotADirectoryError: |
| 140 | raise FsException("File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND) |
| 141 | except IOError: |
| 142 | raise FsException("File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST) |
| 143 | |
| 144 | def file_delete(self, storage, ignore_non_exist=False): |
| 145 | """ |
| 146 | Delete storage content recursivelly |
| 147 | :param storage: can be a str or list of str |
| 148 | :param ignore_non_exist: not raise exception if storage does not exist |
| 149 | :return: None |
| 150 | """ |
| 151 | |
| 152 | if isinstance(storage, str): |
| 153 | f = self.path + storage |
| 154 | else: |
| 155 | f = self.path + "/".join(storage) |
| 156 | if os.path.exists(f): |
| 157 | rmtree(f) |
| 158 | elif not ignore_non_exist: |
| 159 | raise FsException("File {} does not exist".format(storage), http_code=HTTPStatus.NOT_FOUND) |