bug 605. New method at fslocal for rename folders
[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 logging
20 # import tarfile
21 from http import HTTPStatus
22 from shutil import rmtree
23 from osm_common.fsbase import FsBase, FsException
24
25 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
26
27
28 class FsLocal(FsBase):
29
30 def __init__(self, logger_name='fs', lock=False):
31 super().__init__(logger_name, lock)
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 dir_rename(self, src, dst):
67 """
68 Rename one directory name. If dst exist, it replaces (deletes) existing directory
69 :param src: source directory
70 :param dst: destination directory
71 :return: None or raises and exception
72 """
73 try:
74 if os.path.exists(self.path + dst):
75 rmtree(self.path + dst)
76
77 os.rename(self.path + src, self.path + dst)
78
79 except Exception as e:
80 raise FsException(str(e), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
81
82 def file_exists(self, storage, mode=None):
83 """
84 Indicates if "storage" file exist
85 :param storage: can be a str or a str list
86 :param mode: can be 'file' exist as a regular file; 'dir' exists as a directory or; 'None' just exists
87 :return: True, False
88 """
89 if isinstance(storage, str):
90 f = storage
91 else:
92 f = "/".join(storage)
93 if os.path.exists(self.path + f):
94 if mode == "file" and os.path.isfile(self.path + f):
95 return True
96 if mode == "dir" and os.path.isdir(self.path + f):
97 return True
98 return False
99
100 def file_size(self, storage):
101 """
102 return file size
103 :param storage: can be a str or a str list
104 :return: file size
105 """
106 if isinstance(storage, str):
107 f = storage
108 else:
109 f = "/".join(storage)
110 return os.path.getsize(self.path + f)
111
112 def file_extract(self, tar_object, path):
113 """
114 extract a tar file
115 :param tar_object: object of type tar
116 :param path: can be a str or a str list, or a tar object where to extract the tar_object
117 :return: None
118 """
119 if isinstance(path, str):
120 f = self.path + path
121 else:
122 f = self.path + "/".join(path)
123 tar_object.extractall(path=f)
124
125 def file_open(self, storage, mode):
126 """
127 Open a file
128 :param storage: can be a str or list of str
129 :param mode: file mode
130 :return: file object
131 """
132 try:
133 if isinstance(storage, str):
134 f = storage
135 else:
136 f = "/".join(storage)
137 return open(self.path + f, mode)
138 except FileNotFoundError:
139 raise FsException("File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND)
140 except IOError:
141 raise FsException("File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST)
142
143 def dir_ls(self, storage):
144 """
145 return folder content
146 :param storage: can be a str or list of str
147 :return: folder content
148 """
149 try:
150 if isinstance(storage, str):
151 f = storage
152 else:
153 f = "/".join(storage)
154 return os.listdir(self.path + f)
155 except NotADirectoryError:
156 raise FsException("File {} does not exist".format(f), http_code=HTTPStatus.NOT_FOUND)
157 except IOError:
158 raise FsException("File {} cannot be opened".format(f), http_code=HTTPStatus.BAD_REQUEST)
159
160 def file_delete(self, storage, ignore_non_exist=False):
161 """
162 Delete storage content recursively
163 :param storage: can be a str or list of str
164 :param ignore_non_exist: not raise exception if storage does not exist
165 :return: None
166 """
167
168 if isinstance(storage, str):
169 f = self.path + storage
170 else:
171 f = self.path + "/".join(storage)
172 if os.path.exists(f):
173 rmtree(f)
174 elif not ignore_non_exist:
175 raise FsException("File {} does not exist".format(storage), http_code=HTTPStatus.NOT_FOUND)