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 sys 17import subprocess 18 19 20def run_command(cmd, verbose=None): 21 if verbose: 22 print("Running: {}".format(' '.join(cmd))) 23 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 24 output, _ = p.communicate() 25 if verbose: 26 print(output.decode().rstrip()) 27 return output, p.returncode 28 29 30def package_installed(pkg_name): 31 cmd = ['dpkg', '-s', pkg_name] 32 _, r = run_command(cmd) 33 return r 34 35 36def check_build_repuried_packages(): 37 _build_package_list = [ 38 'binutils', 'flex', 'bison', 'git', 'build-essential', 'zip', 'curl', 39 'unzip', 'm4', 'wget', 'perl', 'bc' 40 ] 41 for pkg in _build_package_list: 42 if package_installed(pkg): 43 print("\033[33m {} is not installed. please install it.\033[0m". 44 format(pkg)) 45 sys.exit(1) 46 47 48def check_os_version(): 49 available_os = ('Ubuntu') 50 available_releases = ('18.04', '20.04') 51 _os_info, _returncode = run_command(['cat', '/etc/issue']) 52 if _returncode != 0: 53 return -1 54 55 _os_info = _os_info.decode().rstrip().split() 56 host_os = _os_info[0] 57 host_version = _os_info[1] 58 if host_os not in available_os: 59 print("\033[33m OS {} is not supported for ohos build.\033[0m".format( 60 host_os)) 61 return -1 62 version_available = False 63 for _version in available_releases: 64 if host_version == _version or host_version.startswith(_version): 65 version_available = True 66 break 67 if not version_available: 68 print("\033[33m OS version {} is not supported for ohos build.\033[0m". 69 format(host_version)) 70 print("\033[33m Avaliable OS version are {}.\033[0m".format( 71 ', '.join(available_releases))) 72 return -1 73 return 0 74 75 76def main(): 77 check_result = check_os_version() 78 if check_result != 0: 79 return 80 81 check_build_repuried_packages() 82 return 0 83 84 85if __name__ == '__main__': 86 sys.exit(main()) 87