diff --git a/archive.py b/archive.py index 21c4ba4a..4dfd64b6 100644 --- a/archive.py +++ b/archive.py @@ -33,25 +33,13 @@ from cmds.cmd_package.cmd_package_utils import is_windows, remove_folder -def unpack(archive_filename, path, package_info, package_name): - package_version = package_info['ver'] - package_temp_path = os.path.join(path, "package_temp") - try: - remove_folder(package_temp_path) - os.makedirs(package_temp_path) - except Exception as e: - logging.warning('Error message : {0}'.format(e)) - - logging.info("BSP packages path {0}".format(path)) - logging.info("BSP package temp path: {0}".format(package_temp_path)) - logging.info("archive filename : {0}".format(archive_filename)) - +def unpack(archive_filename, bsp_package_path, package_info, package_name): if ".tar.bz2" in archive_filename: arch = tarfile.open(archive_filename, "r:bz2") for tarinfo in arch: - arch.extract(tarinfo, path) + arch.extract(tarinfo, bsp_package_path) a = tarinfo.name - if not os.path.isdir(os.path.join(path, a)): + if not os.path.isdir(os.path.join(bsp_package_path, a)): if is_windows(): right_path = a.replace('/', '\\') else: @@ -64,9 +52,9 @@ def unpack(archive_filename, path, package_info, package_name): if ".tar.gz" in archive_filename: arch = tarfile.open(archive_filename, "r:gz") for tarinfo in arch: - arch.extract(tarinfo, path) + arch.extract(tarinfo, bsp_package_path) a = tarinfo.name - if not os.path.isdir(os.path.join(path, a)): + if not os.path.isdir(os.path.join(bsp_package_path, a)): if is_windows(): right_path = a.replace('/', '\\') else: @@ -75,60 +63,92 @@ def unpack(archive_filename, path, package_info, package_name): pkgsdb.save_to_database(a, archive_filename) arch.close() + if ".zip" in archive_filename: + if not handle_zip_package(archive_filename, bsp_package_path, package_name, package_info): + return False + + return True + + +def handle_zip_package(archive_filename, bsp_package_path, package_name, package_info): + package_version = package_info['ver'] + package_temp_path = os.path.join(bsp_package_path, "package_temp") + try: - if ".zip" in archive_filename: - flag = True - dir_name = "" - package_name_with_version = "" - - arch = zipfile.ZipFile(archive_filename, "r") - for item in arch.namelist(): - arch.extract(item, package_temp_path) - if not os.path.isdir(os.path.join(package_temp_path, item)): - if is_windows(): - right_path = item.replace('/', '\\') - else: - right_path = item - - # Gets the folder name and changed folder name only once - if flag: - dir_name = os.path.split(right_path)[0] - package_name_with_version = package_name + '-' + package_version - flag = False - - right_name_to_db = right_path.replace(dir_name, package_name_with_version, 1) - right_path = os.path.join("package_temp", right_path) - pkgsdb.save_to_database(right_name_to_db, archive_filename, right_path) - arch.close() + if remove_folder(package_temp_path): + os.makedirs(package_temp_path) + except Exception as e: + logging.warning('Error message : {0}'.format(e)) + + logging.info("BSP packages path {0}".format(bsp_package_path)) + logging.info("BSP package temp path: {0}".format(package_temp_path)) + logging.info("archive filename : {0}".format(archive_filename)) + + try: + flag = True + package_folder_name = "" + package_name_with_version = "" + arch = zipfile.ZipFile(archive_filename, "r") + for item in arch.namelist(): + arch.extract(item, package_temp_path) + if not os.path.isdir(os.path.join(package_temp_path, item)): + if is_windows(): + right_path = item.replace('/', '\\') + else: + right_path = item + + # Gets the folder name and changed folder name only once + if flag: + package_folder_name = os.path.split(right_path)[0] + package_name_with_version = package_name + '-' + package_version + flag = False + + right_name_to_db = right_path.replace(package_folder_name, package_name_with_version, 1) + right_path = os.path.join("package_temp", right_path) + pkgsdb.save_to_database(right_name_to_db, archive_filename, right_path) + arch.close() + + if not move_package_to_bsp_packages(package_folder_name, package_name, package_temp_path, package_version, + bsp_package_path): + return False except Exception as e: - # remove temp folder and archive file logging.warning('unpack error message : {0}'.format(e)) logging.warning('unpack {0} failed'.format(os.path.basename(archive_filename))) + # remove temp folder and archive file remove_folder(package_temp_path) os.remove(archive_filename) return False - # rename package folder name - package_name_with_version = package_name + '-' + package_version - rename_path = os.path.join(package_temp_path, package_name_with_version) + return True - logging.info("origin name: {0}".format(os.path.join(package_temp_path, dir_name))) - logging.info("rename name: {0}".format(rename_path)) +def move_package_to_bsp_packages(package_folder_name, package_name, package_temp_path, package_version, + bsp_packages_path): + """move package in temp folder to bsp packages folder.""" + origin_package_folder_path = os.path.join(package_temp_path, package_folder_name) + package_name_with_version = package_name + '-' + package_version + package_folder_in_temp = os.path.join(package_temp_path, package_name_with_version) + bsp_package_path = os.path.join(bsp_packages_path, package_name_with_version) + logging.info("origin name: {0}".format(origin_package_folder_path)) + logging.info("rename name: {0}".format(package_folder_in_temp)) + + result = True try: - os.rename(os.path.join(package_temp_path, dir_name), rename_path) + # rename package folder name to package name with version + os.rename(origin_package_folder_path, package_folder_in_temp) + + # if there is no specified version package in the bsp package path, + # then move package from package_folder_in_temp to bsp_package_path + if not os.path.isdir(bsp_package_path): + shutil.move(package_folder_in_temp, bsp_package_path) except Exception as e: - # remove temp folder and archive file logging.warning('{0}'.format(e)) + result = False + finally: + # must remove temp folder + remove_folder(package_temp_path) - if not os.path.isdir(os.path.join(path, package_name_with_version)): - # copy package to bsp packages path. - shutil.move(rename_path, os.path.join(path, package_name_with_version)) - - # remove temp folder - remove_folder(package_temp_path) - - return True + return result def package_integrity_test(path): diff --git a/cmds/cmd_package/cmd_package_update.py b/cmds/cmd_package/cmd_package_update.py index d11cbce5..47e7658c 100644 --- a/cmds/cmd_package/cmd_package_update.py +++ b/cmds/cmd_package/cmd_package_update.py @@ -149,9 +149,9 @@ def install_git_package(bsp_package_path, package_name, package_info, package_ur try: repo_path = os.path.join(bsp_package_path, package_name) repo_path = repo_path + '-' + package_info['ver'] - repo_path_full = '"' + repo_path + '"' + repo_name_with_version = '"' + repo_path + '"' - clone_cmd = 'git clone ' + package_url + ' ' + repo_path_full + clone_cmd = 'git clone ' + package_url + ' ' + repo_name_with_version logging.info(clone_cmd) execute_command(clone_cmd, cwd=bsp_package_path) @@ -180,10 +180,10 @@ def install_git_package(bsp_package_path, package_name, package_info, package_ur if need_using_mirror_download(env_config_file): if len(replace_list): for item in replace_list: - submod_dir_path = os.path.join(repo_path, item[2]) - if os.path.isdir(submod_dir_path): + submodule_path = os.path.join(repo_path, item[2]) + if os.path.isdir(submodule_path): cmd = 'git remote set-url origin ' + item[0] - execute_command(cmd, cwd=submod_dir_path) + execute_command(cmd, cwd=submodule_path) if need_using_mirror_download(env_config_file): if os.path.isfile(submodule_path): diff --git a/cmds/cmd_package/cmd_package_utils.py b/cmds/cmd_package/cmd_package_utils.py index 7821d0d7..0ae95bcd 100644 --- a/cmds/cmd_package/cmd_package_utils.py +++ b/cmds/cmd_package/cmd_package_utils.py @@ -31,6 +31,7 @@ import time import shutil import requests +import logging def execute_command(cmd_string, cwd=None, shell=True): @@ -186,9 +187,16 @@ def is_windows(): def remove_folder(folder_path): - if os.path.isdir(folder_path): - if is_windows(): - cmd = 'rd /s /q ' + folder_path - os.system(cmd) + try: + if os.path.isdir(folder_path): + if is_windows(): + cmd = 'rd /s /q ' + folder_path + os.system(cmd) + else: + shutil.rmtree(folder_path) + return True else: - shutil.rmtree(folder_path) + return True + except Exception as e: + logging.warning('Error message : {0}'.format(e)) + return False