X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog%2Fpub%2Futils%2Ffileutil.py;h=ce2a0a340b64a573543473adc446684ba1fcb1f6;hb=b16b4579ea80bf82fa497e4934b2bb8728845b58;hp=6ddfc72c84faeda8997162102729f849f71c6d77;hpb=5fd2ae6a4517980ce0f89247bb354220748ae67b;p=modeling%2Fetsicatalog.git diff --git a/catalog/pub/utils/fileutil.py b/catalog/pub/utils/fileutil.py index 6ddfc72..ce2a0a3 100644 --- a/catalog/pub/utils/fileutil.py +++ b/catalog/pub/utils/fileutil.py @@ -19,16 +19,25 @@ import traceback import urllib import zipfile - logger = logging.getLogger(__name__) def make_dirs(path): + """ + Make directories + :param path: + :return: + """ if not os.path.exists(path): os.makedirs(path, 0o777) def delete_dirs(path): + """ + Delete directories + :param path: + :return: + """ try: if os.path.exists(path): shutil.rmtree(path) @@ -38,6 +47,13 @@ def delete_dirs(path): def download_file_from_http(url, local_dir, file_name): + """ + Download file from http and save to local dir + :param url: + :param local_dir: + :param file_name: + :return: + """ local_file_name = os.path.join(local_dir, file_name) is_download_ok = False try: @@ -55,7 +71,16 @@ def download_file_from_http(url, local_dir, file_name): def unzip_file(zip_src, dst_dir, csar_path): + """ + Unzip the zip file to given path + :param zip_src: + :param dst_dir: + :param csar_path: + :return: + """ + logger.debug("unzip_file %s to %s.", zip_src, dst_dir) if os.path.exists(zip_src): + logger.debug("unzip_file %s.", zip_src) fz = zipfile.ZipFile(zip_src, 'r') for file in fz.namelist(): fz.extract(file, dst_dir) @@ -64,7 +89,29 @@ def unzip_file(zip_src, dst_dir, csar_path): return "" +def unzip_csar(zip_src, dst_dir): + """ + Unzip csar package + :param zip_src: + :param dst_dir: + :return: + """ + if os.path.exists(zip_src): + fz = zipfile.ZipFile(zip_src, 'r') + for file in fz.namelist(): + fz.extract(file, dst_dir) + return dst_dir + else: + logger.error("%s doesn't exist", zip_src) + return "" + + def unzip_csar_to_tmp(zip_src): + """ + Unzip csar package to temp path + :param zip_src: + :return: + """ dirpath = tempfile.mkdtemp() zip_ref = zipfile.ZipFile(zip_src, 'r') zip_ref.extractall(dirpath) @@ -72,7 +119,70 @@ def unzip_csar_to_tmp(zip_src): def get_artifact_path(vnf_path, artifact_file): + """ + Get the path of artifact file + :param vnf_path: + :param artifact_file: + :return: + """ for root, dirs, files in os.walk(vnf_path): if artifact_file in files: return os.path.join(root, artifact_file) return None + + +def end_with(_s_in, *suffix): + """ + Check if end with given suffix + :param _s_in: + :param suffix: + :return: + """ + array = map(_s_in.endswith, suffix) + if True in array: + return True + return False + + +def filter_files(search_path, suffix): + """ + Filter file by given suffix + :param search_path: + :param suffix: + :return: + """ + f_find = [] + file_list = os.listdir(search_path) + for file_item in file_list: + if end_with(file_item, suffix): + f_find.append(file_item) + return f_find + + +def recreate_dir(path): + """ + Recreate directory + :param path: + :return: + """ + if os.path.exists(path): + shutil.rmtree(path) + os.makedirs(path, mode=0o777) + + +def copy(src_file, dest_dir, new_file_name=None): + """ + Copy file to given dest dir + :param src_file: + :param dest_dir: + :param new_file_name: + :return: + """ + if not os.path.exists(dest_dir): + os.makedirs(dest_dir) + if new_file_name is None: + dst = os.path.join(dest_dir, os.path.basename(src_file)) + else: + dst = os.path.join(dest_dir, new_file_name) + shutil.copyfile(src_file, dst) + shutil.copymode(src_file, dst)