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-v1.8.1/" 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-generate-schema-headers-manually.patch', 21 '0002-generate-nnacl-simd-headers-manually.patch', 22 '0003-implement-mindir-module-and-support-nnrt-delegate.patch', 23 '0004-adapt-build-gn-and-provide-C-API-for-OHOS.patch', 24 '0005-mindir-add-custom-op.patch', 25 '0006-Support-converting-THIRDPARTY-model-in-MSLite.patch', 26 '0007-support-third-party-model-in-mslite-runtime.patch', 27 '0008-add-js-api.patch', 28 '0009-adapt-nnrt-v2_0.patch', 29 '0010-nnrt-delegate-supports-heterogeneous-predition.patch', 30 '0011-replace-memcpy-with-memcpy_s-in-mindir.patch', 31 '0012-add-secure-option.patch', 32 '0013-train-0001.patch', 33 '0014-train-0002.patch', 34 '0015-train-0003.patch', 35 '0016-fix-train-bug.patch', 36 '0017-support-build-training-lib.patch', 37 '0018-mindir-MaxPoolFusion-add-roundMode.patch', 38 '0019-change-nnrt-as-external_dep-in-mindir.patch', 39 '0020-add-ohos-js-callback-api-and-bugfix.patch', 40 '0021-js-namespace-bugfix.patch', 41 '0022-support-cross-compile-with-ohos-ndk.patch', 42 '0023-js-info-bugfix.patch', 43 '0024-js-info2-bugfix.patch', 44 '0025-js-callback-bugfix.patch', 45 '0026-js-buffer-add-bugfix.patch', 46 '0027-js-buffer-leakage-bugfix.patch', 47 '0028-modify-ext_dep-hilog.patch', 48 '0029-change-subsystem-name-to-thirdparty.patch', 49 '0030-fix-core-binding-in-ohos.patch', 50 '0031-backport-CVE-2023-2970.patch', 51 '0032-js-enable-enum.patch', 52 '0033-support-fp16-for-arm64-arch.patch', 53 '0034-splite-ndk-so.patch', 54 '0035-nnrt-delegate-support-metagraph.patch', 55 '0036-new-dynamic-quant-algorigthm-and-init-packed.patch', 56 '0037-npu-support-custom-model.patch', 57 '0038-adapt-nnrt-ext-deps.patch', 58 '0039-fix-double-destry-nnrt-mode.patch', 59 '0040-js-add-nnrt-device.patch', 60 '0041-fix-int8-bug.patch', 61 '0042-upgrade-openEuler-flatbuffers.patch', 62 ] 63 64 cwd = os.getcwd() 65 os.chdir(target_dir) 66 print('Change dir to', os.getcwd()) 67 os.system('git init .') 68 os.system('git add .; git commit -m "init"') 69 70 for patch in patches: 71 print('Applying ', patch, '...') 72 ret = os.system('git apply ' + patch_dir + '/' + patch) 73 if ret != 0: 74 raise Exception("Apply patch {} failed.".format(patch)) 75 os.system('git add .; git commit -m "auto-apply ' + patch + '"') 76 print('Done') 77 os.chdir(cwd) 78 79def create_status_file(out_src_path): 80 f = open(out_src_path + '/.status', 'w') 81 f.write('ok') 82 f.close 83 84 85def main_work(): 86 parser = argparse.ArgumentParser(description="mindspore build helper") 87 parser.add_argument('--in_zip_path') 88 parser.add_argument('--out_src_path') 89 parser.add_argument('--patch_dir') 90 args = vars(parser.parse_args()) 91 92 in_zip_path = os.path.realpath(args['in_zip_path']) 93 out_src_path = args['out_src_path'] 94 patch_dir = os.path.realpath(args['patch_dir']) 95 96 if os.path.exists(out_src_path): 97 shutil.rmtree(out_src_path) 98 99 os.mkdir(out_src_path) 100 out_src_path = os.path.realpath(out_src_path) 101 102 extract_source(in_zip_path, out_src_path) 103 104 do_patch(patch_dir, out_src_path + '/source/') 105 106 create_status_file(out_src_path) 107 108if __name__ == "__main__": 109 main_work() 110 111