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 shutil 17import os 18import sys 19import argparse 20 21"""Add part replace test case 22 23Usage: test_set.py --add 24Add part repalce test case, and set build env. 25 26Usage: test_set.py --reset 27Remove part replace test case from oh build environment. 28""" 29 30 31def find_top(): 32 cur_dir = os.getcwd() 33 while cur_dir != "/": 34 build_scripts = os.path.join( 35 cur_dir, 'build/config/BUILDCONFIG.gn') 36 if os.path.exists(build_scripts): 37 return cur_dir 38 cur_dir = os.path.dirname(cur_dir) 39 40 41top_dir = find_top() 42 43product_config_json = "config.json" 44origin_component = "origin_component" 45component_a = "component_a" 46subsystem_config_json = "subsystem_config.json" 47subsystem_components_whitelist_json = "subsystem_compoents_whitelist.json" 48testpart = "testpart" 49 50product_dest_path = os.path.join(top_dir, "vendor/hihope/rk3568/config.json") 51subsystem_config_path = os.path.join(top_dir, "build/subsystem_config.json") 52subsystem_components_whitelist_path = os.path.join(top_dir, "build/subsystem_compoents_whitelist.json") 53testpart_dest_path = os.path.join(top_dir, "vendor/testpart") 54origin_component_path = os.path.join( 55 top_dir, "foundation/arkui/origin_component") 56component_a_path = os.path.join(top_dir, "foundation/arkui/component_a") 57 58 59def add_test_case(): 60 if os.path.exists("{}.backup".format(product_dest_path)): 61 raise Exception( 62 "you have modified the product's config.json, please use --reset to initialize oh env first!") 63 if os.path.exists("{}.backup".format(subsystem_config_path)): 64 raise Exception( 65 "you have modified the subsystem_config.json, please use --reset to initialize oh env first!") 66 67 if os.path.exists("{}.backup".format(subsystem_components_whitelist_path)): 68 raise Exception( 69 "you have modified the subsystem_compoents_whitelist.json, please use --reset to initialize oh env first!") 70 71 shutil.move(product_dest_path, "{}.backup".format(product_dest_path)) 72 shutil.move(subsystem_config_path, 73 "{}.backup".format(subsystem_config_path)) 74 shutil.move(subsystem_components_whitelist_path, 75 "{}.backup".format(subsystem_components_whitelist_path)) 76 77 shutil.copy(product_config_json, product_dest_path) 78 shutil.copy(subsystem_config_json, subsystem_config_path) 79 shutil.copy(subsystem_components_whitelist_json, subsystem_components_whitelist_path) 80 81 shutil.copytree(testpart, testpart_dest_path, 82 symlinks=False, ignore=None) 83 shutil.copytree(origin_component, origin_component_path, 84 symlinks=False, ignore=None) 85 shutil.copytree(component_a, component_a_path, 86 symlinks=False, ignore=None) 87 88 89def reset_env(): 90 if os.path.exists("{}.backup".format(product_dest_path)): 91 shutil.move("{}.backup".format(product_dest_path), product_dest_path) 92 93 if os.path.exists("{}.backup".format(subsystem_config_path)): 94 shutil.move("{}.backup".format( 95 subsystem_config_path), subsystem_config_path) 96 97 if os.path.exists("{}.backup".format(subsystem_components_whitelist_path)): 98 shutil.move("{}.backup".format( 99 subsystem_components_whitelist_path), subsystem_components_whitelist_path) 100 101 if os.path.exists(testpart_dest_path): 102 shutil.rmtree(testpart_dest_path) 103 104 if os.path.exists(origin_component_path): 105 shutil.rmtree(origin_component_path) 106 107 if os.path.exists(component_a_path): 108 shutil.rmtree(component_a_path) 109 110 111def main(): 112 parser = argparse.ArgumentParser() 113 parser.add_argument('--add', action='store_true', help="add test case") 114 parser.add_argument('--reset', action='store_true', 115 help="remove added test case, reset oh env") 116 args = parser.parse_args() 117 118 if args.add and args.reset: 119 return -1 120 if args.add: 121 add_test_case() 122 if args.reset: 123 reset_env() 124 return 0 125 126 127if __name__ == '__main__': 128 sys.exit(main()) 129