2022-04-21 03:11:40 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2023-08-15 03:02:39 +00:00
|
|
|
# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
|
2022-04-21 03:11:40 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shutil
|
|
|
|
|
2023-05-30 08:11:21 +00:00
|
|
|
|
2023-12-08 01:37:35 +00:00
|
|
|
def remove_lite_option(target_path, protofilepath):
|
2022-04-21 03:11:40 +00:00
|
|
|
target_file_path = os.path.basename(protofilepath)
|
2023-11-29 09:45:09 +00:00
|
|
|
target_file_path = target_path + '/' + target_file_path.replace(".proto", "_standard.proto")
|
2022-04-21 03:11:40 +00:00
|
|
|
shutil.copyfile(protofilepath, target_file_path)
|
|
|
|
# replease lite flag, import file name, add package name
|
|
|
|
with open(target_file_path, 'r+') as content:
|
|
|
|
newcontent = content.read()
|
2023-11-29 09:45:09 +00:00
|
|
|
newcontent = newcontent.replace('option optimize_for = LITE_RUNTIME;\n', '')
|
|
|
|
newcontent = newcontent.replace('syntax = "proto3";', 'syntax = "proto3";\npackage ForStandard;')
|
|
|
|
newcontent = newcontent.replace('.proto";', '_standard.proto";')
|
2022-04-21 03:11:40 +00:00
|
|
|
content.seek(0, 0)
|
|
|
|
content.write(newcontent)
|
|
|
|
content.truncate()
|
|
|
|
content.close()
|
|
|
|
|
2023-12-02 02:09:01 +00:00
|
|
|
|
2023-12-08 01:37:35 +00:00
|
|
|
def add_lite_option(target_path, protofilepath):
|
|
|
|
target_file_path = os.path.basename(protofilepath)
|
|
|
|
target_file_path = target_path + '/' + target_file_path.replace(".proto", "_lite.proto")
|
|
|
|
shutil.copyfile(protofilepath, target_file_path)
|
|
|
|
# replease lite flag, import file name, add package name
|
|
|
|
with open(target_file_path, 'r+') as content:
|
|
|
|
newcontent = content.read()
|
|
|
|
if newcontent.find('option optimize_for = LITE_RUNTIME') == -1:
|
|
|
|
newcontent = newcontent.replace('syntax = "proto3";',
|
|
|
|
'syntax = "proto3";\n\noption optimize_for = LITE_RUNTIME;')
|
|
|
|
newcontent = newcontent.replace('syntax = "proto3";', 'syntax = "proto3";\n\npackage LITE;')
|
|
|
|
newcontent = newcontent.replace('.proto";', '_lite.proto";')
|
|
|
|
content.seek(0, 0)
|
|
|
|
content.write(newcontent)
|
|
|
|
content.truncate()
|
|
|
|
content.close()
|
|
|
|
|
|
|
|
|
2022-04-21 03:11:40 +00:00
|
|
|
def main():
|
2023-11-29 09:45:09 +00:00
|
|
|
target_dir = sys.argv[1]
|
|
|
|
i = 2
|
2022-04-21 03:11:40 +00:00
|
|
|
while i < len(sys.argv):
|
|
|
|
proto_file_path = sys.argv[i]
|
2023-12-08 01:37:35 +00:00
|
|
|
remove_lite_option(target_dir, proto_file_path)
|
|
|
|
add_lite_option(target_dir, proto_file_path)
|
2022-04-21 03:11:40 +00:00
|
|
|
i += 1
|
|
|
|
|
2023-12-08 01:37:35 +00:00
|
|
|
|
2022-04-21 03:11:40 +00:00
|
|
|
if __name__ == '__main__':
|
2023-12-08 01:37:35 +00:00
|
|
|
main()
|