blob: 4a82c4f43cd996732aa7c7e1872e7bc98e6dcecd [file] [log] [blame]
tierno87858ca2018-10-08 16:30:15 +02001# -*- 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
tierno5c012612018-04-19 16:01:59 +020018import os
19import logging
garciadeblas2644b762021-03-24 09:21:01 +010020
tierno3054f782018-04-25 16:59:53 +020021# import tarfile
tierno5c012612018-04-19 16:01:59 +020022from http import HTTPStatus
23from shutil import rmtree
tierno3054f782018-04-25 16:59:53 +020024from osm_common.fsbase import FsBase, FsException
tierno5c012612018-04-19 16:01:59 +020025
26__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
27
28
29class FsLocal(FsBase):
garciadeblas2644b762021-03-24 09:21:01 +010030 def __init__(self, logger_name="fs", lock=False):
tierno1e9a3292018-11-05 18:18:45 +010031 super().__init__(logger_name, lock)
tierno5c012612018-04-19 16:01:59 +020032 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):
garciadeblas2644b762021-03-24 09:21:01 +010045 raise FsException(
46 "Invalid configuration param at '[storage]': path '{}' does not exist".format(
47 config["path"]
48 )
49 )
tierno5c012612018-04-19 16:01:59 +020050 except FsException:
51 raise
52 except Exception as e: # TODO refine
53 raise FsException(str(e))
54
55 def fs_disconnect(self):
56 pass # TODO
57
58 def mkdir(self, folder):
59 """
60 Creates a folder or parent object location
61 :param folder:
62 :return: None or raises and exception
63 """
64 try:
65 os.mkdir(self.path + folder)
tiernoc7ac30d2019-01-25 08:56:17 +000066 except FileExistsError: # make it idempotent
67 pass
tierno5c012612018-04-19 16:01:59 +020068 except Exception as e:
69 raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
70
tiernod4378aa2018-12-04 15:37:23 +000071 def dir_rename(self, src, dst):
72 """
73 Rename one directory name. If dst exist, it replaces (deletes) existing directory
74 :param src: source directory
75 :param dst: destination directory
76 :return: None or raises and exception
77 """
78 try:
79 if os.path.exists(self.path + dst):
80 rmtree(self.path + dst)
81
82 os.rename(self.path + src, self.path + dst)
83
84 except Exception as e:
85 raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
86
tierno5c012612018-04-19 16:01:59 +020087 def file_exists(self, storage, mode=None):
88 """
89 Indicates if "storage" file exist
90 :param storage: can be a str or a str list
91 :param mode: can be 'file' exist as a regular file; 'dir' exists as a directory or; 'None' just exists
92 :return: True, False
93 """
94 if isinstance(storage, str):
95 f = storage
96 else:
97 f = "/".join(storage)
98 if os.path.exists(self.path + f):
tiernoe458cd82020-02-12 10:46:51 +000099 if not mode:
100 return True
tierno5c012612018-04-19 16:01:59 +0200101 if mode == "file" and os.path.isfile(self.path + f):
102 return True
103 if mode == "dir" and os.path.isdir(self.path + f):
104 return True
105 return False
106
107 def file_size(self, storage):
108 """
109 return file size
110 :param storage: can be a str or a str list
111 :return: file size
112 """
113 if isinstance(storage, str):
114 f = storage
115 else:
116 f = "/".join(storage)
117 return os.path.getsize(self.path + f)
118
119 def file_extract(self, tar_object, path):
120 """
121 extract a tar file
122 :param tar_object: object of type tar
123 :param path: can be a str or a str list, or a tar object where to extract the tar_object
124 :return: None
125 """
126 if isinstance(path, str):
127 f = self.path + path
128 else:
129 f = self.path + "/".join(path)
130 tar_object.extractall(path=f)
131
132 def file_open(self, storage, mode):
133 """
134 Open a file
135 :param storage: can be a str or list of str
136 :param mode: file mode
137 :return: file object
138 """
139 try:
140 if isinstance(storage, str):
141 f = storage
142 else:
143 f = "/".join(storage)
144 return open(self.path + f, mode)
145 except FileNotFoundError:
garciadeblas2644b762021-03-24 09:21:01 +0100146 raise FsException(
147 "File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND
148 )
tierno5c012612018-04-19 16:01:59 +0200149 except IOError:
garciadeblas2644b762021-03-24 09:21:01 +0100150 raise FsException(
151 "File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST
152 )
tierno5c012612018-04-19 16:01:59 +0200153
154 def dir_ls(self, storage):
155 """
156 return folder content
157 :param storage: can be a str or list of str
158 :return: folder content
159 """
160 try:
161 if isinstance(storage, str):
162 f = storage
163 else:
164 f = "/".join(storage)
165 return os.listdir(self.path + f)
166 except NotADirectoryError:
garciadeblas2644b762021-03-24 09:21:01 +0100167 raise FsException(
168 "File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND
169 )
tierno5c012612018-04-19 16:01:59 +0200170 except IOError:
garciadeblas2644b762021-03-24 09:21:01 +0100171 raise FsException(
172 "File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST
173 )
tierno5c012612018-04-19 16:01:59 +0200174
175 def file_delete(self, storage, ignore_non_exist=False):
176 """
tiernod4378aa2018-12-04 15:37:23 +0000177 Delete storage content recursively
tierno5c012612018-04-19 16:01:59 +0200178 :param storage: can be a str or list of str
179 :param ignore_non_exist: not raise exception if storage does not exist
180 :return: None
181 """
tiernoc7ac30d2019-01-25 08:56:17 +0000182 try:
183 if isinstance(storage, str):
184 f = self.path + storage
185 else:
186 f = self.path + "/".join(storage)
187 if os.path.exists(f):
188 rmtree(f)
189 elif not ignore_non_exist:
garciadeblas2644b762021-03-24 09:21:01 +0100190 raise FsException(
191 "File {} does not exist".format(storage),
192 http_code=HTTPStatus.NOT_FOUND,
193 )
tiernoc7ac30d2019-01-25 08:56:17 +0000194 except (IOError, PermissionError) as e:
garciadeblas2644b762021-03-24 09:21:01 +0100195 raise FsException(
196 "File {} cannot be deleted: {}".format(f, e),
197 http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
198 )
David Garcia788b9d62020-01-20 13:21:06 +0100199
tiernob07e4ef2020-05-06 14:22:48 +0000200 def sync(self, from_path=None):
David Garcia788b9d62020-01-20 13:21:06 +0100201 pass # Not needed in fslocal
lloretgallegf296d2a2020-09-02 09:36:24 +0000202
203 def reverse_sync(self, from_path):
204 pass # Not needed in fslocal