blob: ae22c6ab1b3ef44e721c1520159100f52de84bb7 [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
bravoffc5a6f82021-11-04 21:16:00 -030019import tarfile
20import zipfile
tierno5c012612018-04-19 16:01:59 +020021import logging
garciadeblas2644b762021-03-24 09:21:01 +010022
tierno3054f782018-04-25 16:59:53 +020023# import tarfile
tierno5c012612018-04-19 16:01:59 +020024from http import HTTPStatus
25from shutil import rmtree
tierno3054f782018-04-25 16:59:53 +020026from osm_common.fsbase import FsBase, FsException
tierno5c012612018-04-19 16:01:59 +020027
28__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
29
30
31class FsLocal(FsBase):
garciadeblas2644b762021-03-24 09:21:01 +010032 def __init__(self, logger_name="fs", lock=False):
tierno1e9a3292018-11-05 18:18:45 +010033 super().__init__(logger_name, lock)
tierno5c012612018-04-19 16:01:59 +020034 self.path = None
35
36 def get_params(self):
37 return {"fs": "local", "path": self.path}
38
39 def fs_connect(self, config):
40 try:
41 if "logger_name" in config:
42 self.logger = logging.getLogger(config["logger_name"])
43 self.path = config["path"]
44 if not self.path.endswith("/"):
45 self.path += "/"
46 if not os.path.exists(self.path):
garciadeblas2644b762021-03-24 09:21:01 +010047 raise FsException(
48 "Invalid configuration param at '[storage]': path '{}' does not exist".format(
49 config["path"]
50 )
51 )
tierno5c012612018-04-19 16:01:59 +020052 except FsException:
53 raise
54 except Exception as e: # TODO refine
55 raise FsException(str(e))
56
57 def fs_disconnect(self):
58 pass # TODO
59
60 def mkdir(self, folder):
61 """
62 Creates a folder or parent object location
63 :param folder:
64 :return: None or raises and exception
65 """
66 try:
67 os.mkdir(self.path + folder)
tiernoc7ac30d2019-01-25 08:56:17 +000068 except FileExistsError: # make it idempotent
69 pass
tierno5c012612018-04-19 16:01:59 +020070 except Exception as e:
71 raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
72
tiernod4378aa2018-12-04 15:37:23 +000073 def dir_rename(self, src, dst):
74 """
75 Rename one directory name. If dst exist, it replaces (deletes) existing directory
76 :param src: source directory
77 :param dst: destination directory
78 :return: None or raises and exception
79 """
80 try:
81 if os.path.exists(self.path + dst):
82 rmtree(self.path + dst)
83
84 os.rename(self.path + src, self.path + dst)
85
86 except Exception as e:
87 raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
88
tierno5c012612018-04-19 16:01:59 +020089 def file_exists(self, storage, mode=None):
90 """
91 Indicates if "storage" file exist
92 :param storage: can be a str or a str list
93 :param mode: can be 'file' exist as a regular file; 'dir' exists as a directory or; 'None' just exists
94 :return: True, False
95 """
96 if isinstance(storage, str):
97 f = storage
98 else:
99 f = "/".join(storage)
100 if os.path.exists(self.path + f):
tiernoe458cd82020-02-12 10:46:51 +0000101 if not mode:
102 return True
tierno5c012612018-04-19 16:01:59 +0200103 if mode == "file" and os.path.isfile(self.path + f):
104 return True
105 if mode == "dir" and os.path.isdir(self.path + f):
106 return True
107 return False
108
109 def file_size(self, storage):
110 """
111 return file size
112 :param storage: can be a str or a str list
113 :return: file size
114 """
115 if isinstance(storage, str):
116 f = storage
117 else:
118 f = "/".join(storage)
119 return os.path.getsize(self.path + f)
120
bravoffc5a6f82021-11-04 21:16:00 -0300121 def file_extract(self, compressed_object, path):
tierno5c012612018-04-19 16:01:59 +0200122 """
123 extract a tar file
bravoffc5a6f82021-11-04 21:16:00 -0300124 :param compressed_object: object of type tar or zip
tierno5c012612018-04-19 16:01:59 +0200125 :param path: can be a str or a str list, or a tar object where to extract the tar_object
126 :return: None
127 """
128 if isinstance(path, str):
129 f = self.path + path
130 else:
131 f = self.path + "/".join(path)
bravoffc5a6f82021-11-04 21:16:00 -0300132
133 if type(compressed_object) is tarfile.TarFile:
134 compressed_object.extractall(path=f)
135 elif (
136 type(compressed_object) is zipfile.ZipFile
137 ): # Just a check to know if this works with both tar and zip
138 compressed_object.extractall(path=f)
tierno5c012612018-04-19 16:01:59 +0200139
140 def file_open(self, storage, mode):
141 """
142 Open a file
143 :param storage: can be a str or list of str
144 :param mode: file mode
145 :return: file object
146 """
147 try:
148 if isinstance(storage, str):
149 f = storage
150 else:
151 f = "/".join(storage)
152 return open(self.path + f, mode)
153 except FileNotFoundError:
garciadeblas2644b762021-03-24 09:21:01 +0100154 raise FsException(
155 "File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND
156 )
tierno5c012612018-04-19 16:01:59 +0200157 except IOError:
garciadeblas2644b762021-03-24 09:21:01 +0100158 raise FsException(
159 "File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST
160 )
tierno5c012612018-04-19 16:01:59 +0200161
162 def dir_ls(self, storage):
163 """
164 return folder content
165 :param storage: can be a str or list of str
166 :return: folder content
167 """
168 try:
169 if isinstance(storage, str):
170 f = storage
171 else:
172 f = "/".join(storage)
173 return os.listdir(self.path + f)
174 except NotADirectoryError:
garciadeblas2644b762021-03-24 09:21:01 +0100175 raise FsException(
176 "File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND
177 )
tierno5c012612018-04-19 16:01:59 +0200178 except IOError:
garciadeblas2644b762021-03-24 09:21:01 +0100179 raise FsException(
180 "File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST
181 )
tierno5c012612018-04-19 16:01:59 +0200182
183 def file_delete(self, storage, ignore_non_exist=False):
184 """
tiernod4378aa2018-12-04 15:37:23 +0000185 Delete storage content recursively
tierno5c012612018-04-19 16:01:59 +0200186 :param storage: can be a str or list of str
187 :param ignore_non_exist: not raise exception if storage does not exist
188 :return: None
189 """
tiernoc7ac30d2019-01-25 08:56:17 +0000190 try:
191 if isinstance(storage, str):
192 f = self.path + storage
193 else:
194 f = self.path + "/".join(storage)
195 if os.path.exists(f):
196 rmtree(f)
197 elif not ignore_non_exist:
garciadeblas2644b762021-03-24 09:21:01 +0100198 raise FsException(
199 "File {} does not exist".format(storage),
200 http_code=HTTPStatus.NOT_FOUND,
201 )
tiernoc7ac30d2019-01-25 08:56:17 +0000202 except (IOError, PermissionError) as e:
garciadeblas2644b762021-03-24 09:21:01 +0100203 raise FsException(
204 "File {} cannot be deleted: {}".format(f, e),
205 http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
206 )
David Garcia788b9d62020-01-20 13:21:06 +0100207
tiernob07e4ef2020-05-06 14:22:48 +0000208 def sync(self, from_path=None):
David Garcia788b9d62020-01-20 13:21:06 +0100209 pass # Not needed in fslocal
lloretgallegf296d2a2020-09-02 09:36:24 +0000210
211 def reverse_sync(self, from_path):
212 pass # Not needed in fslocal