import os
import json
import tarfile
import shutil
import argparse
def read_dependencies_file(hpm_cache_home):
dependences_path = os.path.join(hpm_cache_home, "dependences.json")
try:
with open(dependences_path, 'r') as file:
dependencies = json.load(file)
return dependencies
except FileNotFoundError:
print(f"文件 {dependences_path} 未找到。")
return None
except json.JSONDecodeError:
print(f"文件 {dependences_path} 不是有效的 JSON 格式。")
return None
except Exception as e:
print(f"读取文件时发生错误: {e}")
return None
def remove_directory(path):
try:
shutil.rmtree(path)
print(f"文件夹 {path} 已成功删除。")
except Exception as e:
print(f"删除文件夹时出错: {e}")
def extract_tarfile(file_path, install_path):
try:
with tarfile.open(file_path, 'r:gz') as tar:
tar.extractall(path=install_path)
print(f"组件已成功安装到 {install_path}")
except tarfile.ReadError:
print(f"文件 {file_path} 不是有效的 tar.gz 格式。")
except Exception as e:
print(f"安装组件时发生错误: {e}")
def find_and_install_component(packages_dir, dependencies, hpm_cache_home):
for component_name in dependencies.keys():
print(component_name)
version = dependencies[component_name]['version'].replace("-snapshot", "")
install_path = hpm_cache_home + dependencies[component_name]['installPath']
file_name = f"@ohos-{component_name}-{version}.tgz"
file_path = os.path.join(packages_dir, file_name)
if not os.path.exists(file_path):
print(f"文件 {file_path} 不存在。")
continue
if os.path.exists(install_path):
remove_directory(install_path)
try:
extract_tarfile(file_path, install_path)
except Exception as e:
print(f"安装组件时发生错误: {e}")
else:
print(f"文件夹 {install_path} 不存在。")
def main():
parser = argparse.ArgumentParser(description="安装指定组件到指定目录")
parser.add_argument("--packages_dir", required=True, help="包目录路径")
args = parser.parse_args()
home_dir = os.path.expanduser("~")
hpm_cache_home = os.path.join(home_dir, ".hpm", ".hpmcache")
dependencies = read_dependencies_file(hpm_cache_home)
if dependencies:
find_and_install_component(args.packages_dir, dependencies, hpm_cache_home)
if __name__ == "__main__":
main()