1 # Copyright 2017 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
22 logger = logging.getLogger(__name__)
31 if not os.path.exists(path):
32 os.makedirs(path, 0o777)
35 def delete_dirs(path):
42 if os.path.exists(path):
44 except Exception as e:
45 logger.error(traceback.format_exc())
46 logger.error("Failed to delete %s:%s", path, e.args[0])
49 def download_file_from_http(url, local_dir, file_name):
51 Download file from http and save to local dir
57 local_file_name = os.path.join(local_dir, file_name)
58 is_download_ok = False
61 req = urllib.request.urlopen(url)
62 save_file = open(local_file_name, 'w')
63 save_file.write(req.read())
68 logger.error(traceback.format_exc())
69 logger.error("Failed to download %s to %s.", url, local_file_name)
70 return is_download_ok, local_file_name
73 def unzip_file(zip_src, dst_dir, csar_path):
75 Unzip the zip file to given path
81 logger.debug("unzip_file %s to %s.", zip_src, dst_dir)
82 if os.path.exists(zip_src):
83 logger.debug("unzip_file %s.", zip_src)
84 fz = zipfile.ZipFile(zip_src, 'r')
85 for file in fz.namelist():
86 fz.extract(file, dst_dir)
87 return os.path.join(dst_dir, csar_path)
92 def unzip_csar(zip_src, dst_dir):
99 if os.path.exists(zip_src):
100 fz = zipfile.ZipFile(zip_src, 'r')
101 for file in fz.namelist():
102 fz.extract(file, dst_dir)
105 logger.error("%s doesn't exist", zip_src)
109 def unzip_csar_to_tmp(zip_src):
111 Unzip csar package to temp path
115 dirpath = tempfile.mkdtemp()
116 zip_ref = zipfile.ZipFile(zip_src, 'r')
117 zip_ref.extractall(dirpath)
121 def get_artifact_path(vnf_path, artifact_file):
123 Get the path of artifact file
125 :param artifact_file:
128 for root, dirs, files in os.walk(vnf_path):
129 if artifact_file in files:
130 return os.path.join(root, artifact_file)
134 def end_with(_s_in, *suffix):
136 Check if end with given suffix
141 array = map(_s_in.endswith, suffix)
147 def filter_files(search_path, suffix):
149 Filter file by given suffix
155 file_list = os.listdir(search_path)
156 for file_item in file_list:
157 if end_with(file_item, suffix):
158 f_find.append(file_item)
162 def recreate_dir(path):
168 if os.path.exists(path):
170 os.makedirs(path, mode=0o777)
173 def copy(src_file, dest_dir, new_file_name=None):
175 Copy file to given dest dir
178 :param new_file_name:
181 if not os.path.exists(dest_dir):
182 os.makedirs(dest_dir)
183 if new_file_name is None:
184 dst = os.path.join(dest_dir, os.path.basename(src_file))
186 dst = os.path.join(dest_dir, new_file_name)
187 shutil.copyfile(src_file, dst)
188 shutil.copymode(src_file, dst)