ae22c6ab1b3ef44e721c1520159100f52de84bb7
[osm/common.git] / osm_common / fslocal.py
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
18 import os
19 import tarfile
20 import zipfile
21 import logging
22
23 # import tarfile
24 from http import HTTPStatus
25 from shutil import rmtree
26 from osm_common.fsbase import FsBase, FsException
27
28 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
29
30
31 class FsLocal(FsBase):
32 def __init__(self, logger_name="fs", lock=False):
33 super().__init__(logger_name, lock)
34 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):
47 raise FsException(
48 "Invalid configuration param at '[storage]': path '{}' does not exist".format(
49 config["path"]
50 )
51 )
52 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)
68 except FileExistsError: # make it idempotent
69 pass
70 except Exception as e:
71 raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
72
73 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
89 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):
101 if not mode:
102 return True
103 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
121 def file_extract(self, compressed_object, path):
122 """
123 extract a tar file
124 :param compressed_object: object of type tar or zip
125 :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)
132
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)
139
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:
154 raise FsException(
155 "File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND
156 )
157 except IOError:
158 raise FsException(
159 "File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST
160 )
161
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:
175 raise FsException(
176 "File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND
177 )
178 except IOError:
179 raise FsException(
180 "File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST
181 )
182
183 def file_delete(self, storage, ignore_non_exist=False):
184 """
185 Delete storage content recursively
186 :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 """
190 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:
198 raise FsException(
199 "File {} does not exist".format(storage),
200 http_code=HTTPStatus.NOT_FOUND,
201 )
202 except (IOError, PermissionError) as e:
203 raise FsException(
204 "File {} cannot be deleted: {}".format(f, e),
205 http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
206 )
207
208 def sync(self, from_path=None):
209 pass # Not needed in fslocal
210
211 def reverse_sync(self, from_path):
212 pass # Not needed in fslocal