• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2023 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18import os
19import json
20import tarfile
21import shutil
22import argparse
23
24
25def read_dependencies_file(hpm_cache_home):
26    # 构造文件路径
27    dependences_path = os.path.join(hpm_cache_home, "dependences.json")
28    try:
29        with open(dependences_path, 'r') as file:
30            dependencies = json.load(file)
31            return dependencies
32    except FileNotFoundError:
33        print(f"文件 {dependences_path} 未找到。")
34        return None
35    except json.JSONDecodeError:
36        print(f"文件 {dependences_path} 不是有效的 JSON 格式。")
37        return None
38    except Exception as e:
39        print(f"读取文件时发生错误: {e}")
40        return None
41
42
43def remove_directory(path):
44    try:
45        shutil.rmtree(path)
46        print(f"文件夹 {path} 已成功删除。")
47    except Exception as e:
48        print(f"删除文件夹时出错: {e}")
49
50
51def extract_tarfile(file_path, install_path):
52    try:
53        with tarfile.open(file_path, 'r:gz') as tar:
54            tar.extractall(path=install_path)
55        print(f"组件已成功安装到 {install_path}")
56    except tarfile.ReadError:
57        print(f"文件 {file_path} 不是有效的 tar.gz 格式。")
58    except Exception as e:
59        print(f"安装组件时发生错误: {e}")
60
61
62def find_and_install_component(packages_dir, dependencies, hpm_cache_home):
63    for component_name in dependencies.keys():
64        print(component_name)
65        # 构造文件名
66        version = dependencies[component_name]['version'].replace("-snapshot", "")
67        install_path = hpm_cache_home + dependencies[component_name]['installPath']
68        file_name = f"@ohos-{component_name}-{version}.tgz"
69        file_path = os.path.join(packages_dir, file_name)
70        if not os.path.exists(file_path):
71            print(f"文件 {file_path} 不存在。")
72            continue
73        # 检查 install_path 是否存在,如果存在直接删除
74        if os.path.exists(install_path):
75            remove_directory(install_path)
76        try:
77            # 解压文件
78            extract_tarfile(file_path, install_path)
79        except Exception as e:
80            print(f"安装组件时发生错误: {e}")
81        else:
82            print(f"文件夹 {install_path} 不存在。")
83
84
85def main():
86    parser = argparse.ArgumentParser(description="安装指定组件到指定目录")
87    parser.add_argument("--packages_dir", required=True, help="包目录路径")
88    args = parser.parse_args()
89    home_dir = os.path.expanduser("~")
90    hpm_cache_home = os.path.join(home_dir, ".hpm", ".hpmcache")
91    dependencies = read_dependencies_file(hpm_cache_home)
92    if dependencies:
93        find_and_install_component(args.packages_dir, dependencies, hpm_cache_home)
94
95
96if __name__ == "__main__":
97    main()
98