import os
import sys
import shutil
def remove_lite_option(target_path, protofilepath):
target_file_path = os.path.basename(protofilepath)
target_file_path = target_path + '/' + target_file_path.replace(".proto", "_standard.proto")
shutil.copyfile(protofilepath, target_file_path)
with open(target_file_path, 'r+') as content:
newcontent = content.read()
newcontent = newcontent.replace('option optimize_for = LITE_RUNTIME;\n', '')
newcontent = newcontent.replace('syntax = "proto3";', 'syntax = "proto3";\npackage ForStandard;')
newcontent = newcontent.replace('.proto";', '_standard.proto";')
content.seek(0, 0)
content.write(newcontent)
content.truncate()
content.close()
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)
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()
def main():
target_dir = sys.argv[1]
i = 2
while i < len(sys.argv):
proto_file_path = sys.argv[i]
remove_lite_option(target_dir, proto_file_path)
add_lite_option(target_dir, proto_file_path)
i += 1
if __name__ == '__main__':
main()