• 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 run_command(cmd, verbose=None):
23    """Execute command `cmd`
24    :param cmd: Command to be executed.
25    :param verbose: Whether echo runtime infomation and output of command `cmd`.
26    :return output: The output of command `cmd`.
27    :return returncode: The return code of command `cmd`.
28    """
29    if verbose:
30        print("Running: {}".format(' '.join(cmd)))
31    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
32    output, _ = p.communicate()
33    if verbose:
34        print(output.decode().rstrip())
35    return output, p.returncode
36
37
38def package_installed(pkg_name):
39    """Check whether package `pkg_name` is installed or not.
40    Got package `pkg_name` installation state by executing `dpkg -s pkg_name`.
41    :param pkg_name: Package name to check.
42    :return r: Check result, 0 means installed, non-zero means not installed.
43    """
44    cmd = ['dpkg', '-s', pkg_name]
45    _, r = run_command(cmd)
46    return r
47
48
49def check_build_requried_packages(host_version,check=True):
50    """Check whether packages required by build process installed or not.
51    By parsing file `REPO_ROOT/build/scripts/build_package_list.json`.
52    Example content: `{"18.04":{"dep_package":["pkg1","pkg2",...]}, "20.04":{...}}`
53    Currently there are only lists for `Ubuntu 18.04` and `Ubuntu 20.04`.
54    :param host_version: OS version of the host.
55    :param check: Whether to prompt user of missing package.
56    :return _build_package_list: List of packages required by build process.
57    :return install_package_list: Packages installed.
58    :return uninstall_package_list: Packages missing.
59    """
60    cur_dir = os.getcwd()
61    build_package_json = os.path.join(cur_dir, 'build/scripts/build_package_list.json')
62    with open(build_package_json, 'r') as file:
63        file_json = json.load(file)
64        for _version in file_json.keys():
65            if host_version == _version or host_version.startswith(_version):
66                _build_package_list = file_json["{}".format(_version)]["dep_package"]
67    uninstall_package_list = []
68    for pkg in _build_package_list:
69        if package_installed(pkg):
70            if check:
71                print("\033[33m {} is not installed. please install it.\033[0m".
72                      format(pkg))
73            uninstall_package_list.append(pkg)
74    install_package_list = list(set(_build_package_list).difference(uninstall_package_list))
75    return _build_package_list,install_package_list,uninstall_package_list
76
77
78def check_os_version():
79    """Check OS type and version.
80    By parsing file `/etc/issue`.
81    :return -1: Retuern -1 if OS is not supported.
82    :return host_os: Host OS type, currently only `Ubuntu` supported.
83    :return host_version: Host OS version, currently only `18.04[.X]` or `20.04[.X]` supported.
84    """
85    available_os = ('Ubuntu')
86    available_releases = ('18.04', '20.04')
87    _os_info, _returncode = run_command(['cat', '/etc/issue'])
88    if _returncode != 0:
89        return -1
90
91    _os_info = _os_info.decode().rstrip().split()
92    host_os = _os_info[0]
93    host_version = _os_info[1]
94    if host_os not in available_os:
95        print("\033[33m OS {} is not supported for ohos build.\033[0m".format(
96            host_os))
97        return -1
98    version_available = False
99    for _version in available_releases:
100        if host_version == _version or host_version.startswith(_version):
101            version_available = True
102            break
103    if not version_available:
104        print("\033[33m OS version {} is not supported for ohos build.\033[0m".
105              format(host_version))
106        print("\033[33m Available OS version are {}.\033[0m".format(
107            ', '.join(available_releases)))
108        return -1
109    return host_os,host_version
110
111
112def main():
113    """Entrance function.
114    :return -1: Return -1 on error.
115    :return 0: Return 0 on success.
116    """
117    check_result = check_os_version()
118
119    if check_result == -1:
120        return -1
121
122    _, _, missing_packages = check_build_requried_packages(check_result[1], check=True)
123
124    if(len(missing_packages) == 0):
125        return 0
126    else:
127        print("\033[31m Missing dependencies, please check!\033[0m")
128        return -1
129
130
131if __name__ == '__main__':
132    sys.exit(main())
133