1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Telefonica S.A.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
21 from http
import HTTPStatus
22 from shutil
import rmtree
23 from osm_common
.fsbase
import FsBase
, FsException
25 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
28 class FsLocal(FsBase
):
30 def __init__(self
, logger_name
='fs', lock
=False):
31 super().__init
__(logger_name
, lock
)
35 return {"fs": "local", "path": self
.path
}
37 def fs_connect(self
, config
):
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("/"):
44 if not os
.path
.exists(self
.path
):
45 raise FsException("Invalid configuration param at '[storage]': path '{}' does not exist".format(
49 except Exception as e
: # TODO refine
50 raise FsException(str(e
))
52 def fs_disconnect(self
):
55 def mkdir(self
, folder
):
57 Creates a folder or parent object location
59 :return: None or raises and exception
62 os
.mkdir(self
.path
+ folder
)
63 except FileExistsError
: # make it idempotent
65 except Exception as e
:
66 raise FsException(str(e
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
)
68 def dir_rename(self
, src
, dst
):
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
76 if os
.path
.exists(self
.path
+ dst
):
77 rmtree(self
.path
+ dst
)
79 os
.rename(self
.path
+ src
, self
.path
+ dst
)
81 except Exception as e
:
82 raise FsException(str(e
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
)
84 def file_exists(self
, storage
, mode
=None):
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
91 if isinstance(storage
, str):
95 if os
.path
.exists(self
.path
+ f
):
98 if mode
== "file" and os
.path
.isfile(self
.path
+ f
):
100 if mode
== "dir" and os
.path
.isdir(self
.path
+ f
):
104 def file_size(self
, storage
):
107 :param storage: can be a str or a str list
110 if isinstance(storage
, str):
113 f
= "/".join(storage
)
114 return os
.path
.getsize(self
.path
+ f
)
116 def file_extract(self
, tar_object
, path
):
119 :param tar_object: object of type tar
120 :param path: can be a str or a str list, or a tar object where to extract the tar_object
123 if isinstance(path
, str):
126 f
= self
.path
+ "/".join(path
)
127 tar_object
.extractall(path
=f
)
129 def file_open(self
, storage
, mode
):
132 :param storage: can be a str or list of str
133 :param mode: file mode
137 if isinstance(storage
, str):
140 f
= "/".join(storage
)
141 return open(self
.path
+ f
, mode
)
142 except FileNotFoundError
:
143 raise FsException("File {} does not exist".format(f
), http_code
=HTTPStatus
.NOT_FOUND
)
145 raise FsException("File {} cannot be opened".format(f
), http_code
=HTTPStatus
.BAD_REQUEST
)
147 def dir_ls(self
, storage
):
149 return folder content
150 :param storage: can be a str or list of str
151 :return: folder content
154 if isinstance(storage
, str):
157 f
= "/".join(storage
)
158 return os
.listdir(self
.path
+ f
)
159 except NotADirectoryError
:
160 raise FsException("File {} does not exist".format(f
), http_code
=HTTPStatus
.NOT_FOUND
)
162 raise FsException("File {} cannot be opened".format(f
), http_code
=HTTPStatus
.BAD_REQUEST
)
164 def file_delete(self
, storage
, ignore_non_exist
=False):
166 Delete storage content recursively
167 :param storage: can be a str or list of str
168 :param ignore_non_exist: not raise exception if storage does not exist
172 if isinstance(storage
, str):
173 f
= self
.path
+ storage
175 f
= self
.path
+ "/".join(storage
)
176 if os
.path
.exists(f
):
178 elif not ignore_non_exist
:
179 raise FsException("File {} does not exist".format(storage
), http_code
=HTTPStatus
.NOT_FOUND
)
180 except (IOError, PermissionError
) as e
:
181 raise FsException("File {} cannot be deleted: {}".format(f
, e
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
)
183 def sync(self
, from_path
=None):
184 pass # Not needed in fslocal
186 def reverse_sync(self
, from_path
):
187 pass # Not needed in fslocal