1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 19 20def remove_lite_option(target_path, protofilepath): 21 target_file_path = os.path.basename(protofilepath) 22 target_file_path = target_path + '/' + target_file_path.replace(".proto", "_standard.proto") 23 shutil.copyfile(protofilepath, target_file_path) 24 # replease lite flag, import file name, add package name 25 with open(target_file_path, 'r+') as content: 26 newcontent = content.read() 27 newcontent = newcontent.replace('option optimize_for = LITE_RUNTIME;\n', '') 28 newcontent = newcontent.replace('syntax = "proto3";', 'syntax = "proto3";\npackage ForStandard;') 29 newcontent = newcontent.replace('.proto";', '_standard.proto";') 30 content.seek(0, 0) 31 content.write(newcontent) 32 content.truncate() 33 content.close() 34 35 36def add_lite_option(target_path, protofilepath): 37 target_file_path = os.path.basename(protofilepath) 38 target_file_path = target_path + '/' + target_file_path.replace(".proto", "_lite.proto") 39 shutil.copyfile(protofilepath, target_file_path) 40 # replease lite flag, import file name, add package name 41 with open(target_file_path, 'r+') as content: 42 newcontent = content.read() 43 if newcontent.find('option optimize_for = LITE_RUNTIME') == -1: 44 newcontent = newcontent.replace('syntax = "proto3";', 45 'syntax = "proto3";\n\noption optimize_for = LITE_RUNTIME;') 46 newcontent = newcontent.replace('syntax = "proto3";', 'syntax = "proto3";\n\npackage LITE;') 47 newcontent = newcontent.replace('.proto";', '_lite.proto";') 48 content.seek(0, 0) 49 content.write(newcontent) 50 content.truncate() 51 content.close() 52 53 54def main(): 55 target_dir = sys.argv[1] 56 i = 2 57 while i < len(sys.argv): 58 proto_file_path = sys.argv[i] 59 remove_lite_option(target_dir, proto_file_path) 60 add_lite_option(target_dir, proto_file_path) 61 i += 1 62 63 64if __name__ == '__main__': 65 main() 66