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 json 17import os 18import subprocess 19import hashlib 20import platform 21 22 23def find_top(): 24 cur_dir = os.getcwd() 25 while cur_dir != "/": 26 build_config_file = os.path.join( 27 cur_dir, 'build/config/BUILDCONFIG.gn') 28 if os.path.exists(build_config_file): 29 return cur_dir 30 cur_dir = os.path.dirname(cur_dir) 31 32# Read json file data 33def read_json_file(input_file): 34 if not os.path.exists(input_file): 35 print("file '{}' doesn't exist.".format(input_file)) 36 return None 37 38 data = None 39 try: 40 with open(input_file, 'r') as input_f: 41 data = json.load(input_f) 42 except json.decoder.JSONDecodeError: 43 print("The file '{}' format is incorrect.".format(input_file)) 44 raise 45 except: # noqa E722 46 print("read file '{}' failed.".format(input_file)) 47 raise 48 return data 49 50 51# Read file by line 52def read_file(input_file): 53 if not os.path.exists(input_file): 54 print("file '{}' doesn't exist.".format(input_file)) 55 return None 56 57 data = [] 58 try: 59 with open(input_file, 'r') as file_obj: 60 for line in file_obj.readlines(): 61 data.append(line.rstrip('\n')) 62 except: # noqa E722 63 print("read file '{}' failed".format(input_file)) 64 raise 65 return data 66 67 68# Write json file data 69def write_json_file(output_file, content, check_changes=False): 70 file_dir = os.path.dirname(os.path.abspath(output_file)) 71 if not os.path.exists(file_dir): 72 os.makedirs(file_dir, exist_ok=True) 73 74 if check_changes is True: 75 changed = __check_changes(output_file, content) 76 else: 77 changed = True 78 if changed is True: 79 with open(output_file, 'w') as output_f: 80 json.dump(content, output_f, sort_keys=True, indent=2) 81 82 83def __check_changes(output_file, content): 84 if os.path.exists(output_file) and os.path.isfile(output_file): 85 # file content md5 val 86 sha256_obj = hashlib.sha256() 87 sha256_obj.update(str(read_json_file(output_file)).encode()) 88 hash_value = sha256_obj.hexdigest() 89 # new content md5 val 90 sha256_obj_new = hashlib.sha256() 91 sha256_obj_new.update(str(content).encode()) 92 hash_value_new = sha256_obj_new.hexdigest() 93 if hash_value_new == hash_value: 94 return False 95 return True 96 97 98# Write file data 99def write_file(output_file, content): 100 code_dir = find_top() 101 os_name= platform.system().lower() 102 gn_exe = os.path.join(code_dir, f'prebuilts/build-tools/{os_name}-x86/bin/gn') 103 file_dir = os.path.dirname(os.path.abspath(output_file)) 104 if not os.path.exists(file_dir): 105 os.makedirs(file_dir, exist_ok=True) 106 107 with open(output_file, 'w') as output_f: 108 output_f.write(content) 109 if output_file.endswith('.gni') or output_file.endswith('.gn'): 110 # Call gn format to make the output gn file prettier. 111 cmd = [gn_exe, 'format'] 112 cmd.append(output_file) 113 subprocess.check_output(cmd) 114