1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: Use ark to execute workload test suite 18""" 19 20import sys 21import re 22import os 23 24HELP = """ 25python prerun_proc.py __VAR__=value ... default_js_scr.js 26 27lets assume that default_js_scr.js have next lines 28 29``` 30 const __MULTIPLIER__ = 1 31 let start1 = new Date().getTime(); 32 for (let i = 0; i < __MULTIPLIER__ * 10000; i++) { 33 SetAddSmi(); 34 } 35 print("__MULTIPLIER__ = " + __MULTIPLIER__) 36``` 37 38After execution 39``` 40python prerun_proc.py __MULTIPLIER__=3 ... default_js_scr.js 41``` 42 43you will see in default_js_scr.js 44``` 45 const __MULTIPLIER__ = 3 46 let start1 = new Date().getTime(); 47 for (let i = 0; i < __MULTIPLIER__ * 10000; i++) { 48 SetAddSmi(); 49 } 50 print("__MULTIPLIER__ = " + __MULTIPLIER__) 51``` 52 53So after executing you will see next line: 54``` 55__MULTIPLIER__ = 3 56``` 57 58""" 59 60 61def get_args(): 62 vars_to_replace = {} 63 script_name = "" 64 65 for arg in sys.argv[1:]: 66 if(re.search("^__.*__=.*$", arg)): 67 (var, val) = arg.split('=') 68 vars_to_replace[var] = val 69 elif(re.search("^.*.js$", arg)): 70 if(script_name != ""): 71 raise RuntimeError("More then on script to preprocess") 72 else: 73 script_name = arg 74 elif(arg == "-h" or arg == "--help"): 75 print(HELP) 76 exit(0) 77 else: 78 raise RuntimeError(f"{arg} is wrong argument, please look at help") 79 80 if(script_name == ""): 81 raise RuntimeError("There is no script to preprocess") 82 83 if(len(vars_to_replace) == 0): 84 raise RuntimeError("No vars to replace") 85 86 return script_name, vars_to_replace 87 88 89def main(): 90 script_name, vars_to_replace = get_args() 91 data = [] 92 93 status = os.stat(script_name) 94 fd = os.open(script_name, os.O_RDWR | os.O_CREAT, status.st_mode) 95 with os.fdopen(fd, 'r') as script_file: 96 data = script_file.readlines() 97 98 for i, _ in enumerate(data): 99 for var in vars_to_replace.items(): 100 if(re.search(f"const {var} =", data[i].strip())): 101 val = data.get(i) 102 data[i] = val[:val.find("=") + 1] + ' ' + vars_to_replace.get(var) + ';\n' 103 104 fd = os.open(script_name, os.O_RDWR | os.O_CREAT, status.st_mode) 105 with os.fdopen(fd, 'w') as script_file: 106 script_file.seek(0) 107 script_file.writelines(data) 108 script_file.truncate() 109 110if (__name__ == "__main__"): 111 main()