1#!/usr/bin/env python3 2 3import os 4import shutil 5import zipfile 6import argparse 7 8def extract_source(in_zip_path, out_src_path): 9 "depress source code form release package" 10 print('Extracting zipped release package...') 11 f = zipfile.ZipFile(in_zip_path, "r") 12 f.extractall(path=out_src_path) 13 old_src_dir = out_src_path + "/mindspore-v2.1.0/" 14 new_src_dir = out_src_path + "/source/" 15 os.rename(old_src_dir, new_src_dir) 16 print("Done extraction.") 17 18def do_patch(patch_dir, target_dir): 19 patches = [ 20 '0001-build-gn-c-api-for-OHOS.patch', 21 '0002-train-and-build.patch', 22 '0003-add-js-api.patch', 23 '0004-cross-compile-ndkso-fp16-nnrt-train_capi.patch', 24 '0005-micro-for-ohos.patch', 25 '0006-remove-lite-expression-fix-double-loadso.patch', 26 '0007-deobfuscator.patch', 27 '0008-upgrade-flatbuffers-fix_crash.patch', 28 '0009-npu-zero-copy.patch', 29 '0010-micro-dynamic-shape-support-discrete-value.patch', 30 '0011-fix-npu-infer-memory-leak-delete-liteGraph.patch', 31 '0012-fix-int8-bug.patch', 32 ] 33 34 cwd = os.getcwd() 35 os.chdir(target_dir) 36 print('Change dir to', os.getcwd()) 37 os.system('git init .') 38 os.system('git add .; git commit -m "init"') 39 40 for patch in patches: 41 print('Applying ', patch, '...') 42 ret = os.system('git apply ' + patch_dir + '/' + patch) 43 if ret != 0: 44 raise Exception("Apply patch {} failed.".format(patch)) 45 os.system('git add .; git commit -m "auto-apply ' + patch + '"') 46 print('Done') 47 os.chdir(cwd) 48 49def create_status_file(out_src_path): 50 f = open(out_src_path + '/.status', 'w') 51 f.write('ok') 52 f.close 53 54 55def main_work(): 56 parser = argparse.ArgumentParser(description="mindspore build helper") 57 parser.add_argument('--in_zip_path') 58 parser.add_argument('--out_src_path') 59 parser.add_argument('--patch_dir') 60 args = vars(parser.parse_args()) 61 62 in_zip_path = os.path.realpath(args['in_zip_path']) 63 out_src_path = args['out_src_path'] 64 patch_dir = os.path.realpath(args['patch_dir']) 65 66 if os.path.exists(out_src_path): 67 shutil.rmtree(out_src_path) 68 69 os.mkdir(out_src_path) 70 out_src_path = os.path.realpath(out_src_path) 71 72 extract_source(in_zip_path, out_src_path) 73 74 do_patch(patch_dir, out_src_path + '/source/') 75 76 create_status_file(out_src_path) 77 78 79if __name__ == "__main__": 80 main_work() 81 82