• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import sys
18import subprocess
19import json
20
21
22def find_top():
23    cur_dir = os.getcwd()
24    while cur_dir != "/":
25        build_scripts = os.path.join(
26            cur_dir, 'build/scripts/build_package_list.json')
27        if os.path.exists(build_scripts):
28            return cur_dir
29        cur_dir = os.path.dirname(cur_dir)
30
31
32def run_command(cmd, verbose=None):
33    """Execute command `cmd`
34    :param cmd: Command to be executed.
35    :param verbose: Whether echo runtime infomation and output of command `cmd`.
36    :return output: The output of command `cmd`.
37    :return returncode: The return code of command `cmd`.
38    """
39    if verbose:
40        print("Running: {}".format(' '.join(cmd)))
41    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
42    output, _ = p.communicate()
43    if verbose:
44        print(output.decode().rstrip())
45    return output, p.returncode
46
47
48def package_installed(pkg_name):
49    """Check whether package `pkg_name` is installed or not.
50    Got package `pkg_name` installation state by executing `dpkg -s pkg_name`.
51    :param pkg_name: Package name to check.
52    :return r: Check result, 0 means installed, non-zero means not installed.
53    """
54    cmd = ['dpkg', '-s', pkg_name]
55    _, r = run_command(cmd)
56    return r
57
58
59def check_build_requried_packages(host_version, check=True):
60    """Check whether packages required by build process installed or not.
61    By parsing file `REPO_ROOT/build/scripts/build_package_list.json`.
62    Example content: `{"18.04":{"dep_package":["pkg1","pkg2",...]}, "20.04":{...}}`
63    Currently there are only lists for `Ubuntu 18.04` and `Ubuntu 20.04`.
64    :param host_version: OS version of the host.
65    :param check: Whether to prompt user of missing package.
66    :return _build_package_list: List of packages required by build process.
67    :return install_package_list: Packages installed.
68    :return uninstall_package_list: Packages missing.
69    """
70    cur_dir = find_top()
71    build_package_json = os.path.join(
72        cur_dir, 'build/scripts/build_package_list.json')
73    with open(build_package_json, 'r') as file:
74        file_json = json.load(file)
75        for _version in file_json.keys():
76            if host_version == _version or host_version.startswith(_version):
77                _build_package_list = file_json["{}".format(
78                    _version)]["dep_package"]
79    uninstall_package_list = []
80    for pkg in _build_package_list:
81        if package_installed(pkg):
82            if check:
83                print("\033[33m {} is not installed. please install it.\033[0m".
84                      format(pkg))
85            uninstall_package_list.append(pkg)
86    install_package_list = list(
87        set(_build_package_list).difference(uninstall_package_list))
88    return _build_package_list, install_package_list, uninstall_package_list
89
90
91def check_os_version():
92    """Check OS type and version.
93    By parsing file `/etc/issue`.
94    :return -1: Retuern -1 if OS is not supported.
95    :return host_os: Host OS type, currently only `Ubuntu` supported.
96    :return host_version: Host OS version, currently only `18.04[.X]`, `20.04[.X]` or `22.04[.X]` supported.
97    """
98    available_os = ('Ubuntu')
99    available_releases = ('18.04', '20.04', '22.04')
100    _os_info, _returncode = run_command(['cat', '/etc/issue'])
101    if _returncode != 0:
102        return -1
103
104    _os_info = _os_info.decode().rstrip().split()
105    host_os = _os_info[0]
106    host_version = _os_info[1]
107    if host_os not in available_os:
108        print("\033[33m OS {} is not supported for ohos build.\033[0m".format(
109            host_os))
110        return -1
111    version_available = False
112    for _version in available_releases:
113        if host_version == _version or host_version.startswith(_version):
114            version_available = True
115            break
116    if not version_available:
117        print("\033[33m OS version {} is not supported for ohos build.\033[0m".
118              format(host_version))
119        print("\033[33m Available OS version are {}.\033[0m".format(
120            ', '.join(available_releases)))
121        return -1
122    return host_os, host_version
123
124
125def main():
126    """Entrance function.
127    :return -1: Return -1 on error.
128    :return 0: Return 0 on success.
129    """
130    check_result = check_os_version()
131
132    if check_result == -1:
133        return -1
134
135    _, _, missing_packages = check_build_requried_packages(
136        check_result[1], check=True)
137
138    if (len(missing_packages) == 0):
139        return 0
140    else:
141        print("\033[31m Missing dependencies, please check!\033[0m")
142        return -1
143
144
145if __name__ == '__main__':
146    sys.exit(main())
147