1#!/usr/bin/env python3 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. 15import os 16import sys 17import shutil 18 19def replace_lite_option(target_path, protofilepath): 20 target_file_path = os.path.basename(protofilepath) 21 target_file_path = target_path + '/' + target_file_path.replace(".proto", "_standard.proto") 22 shutil.copyfile(protofilepath, target_file_path) 23 # replease lite flag, import file name, add package name 24 with open(target_file_path, 'r+') as content: 25 newcontent = content.read() 26 newcontent = newcontent.replace('option optimize_for = LITE_RUNTIME;\n', '') 27 newcontent = newcontent.replace('syntax = "proto3";', 'syntax = "proto3";\npackage ForStandard;') 28 newcontent = newcontent.replace('.proto";', '_standard.proto";') 29 content.seek(0, 0) 30 content.write(newcontent) 31 content.truncate() 32 content.close() 33 34def main(): 35 target_dir = sys.argv[1] 36 i = 2 37 while i < len(sys.argv): 38 proto_file_path = sys.argv[i] 39 replace_lite_option(target_dir, proto_file_path) 40 i += 1 41 42if __name__ == '__main__': 43 main() 44