Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. """
  2. *
  3. * This file is part of rasdaman community.
  4. *
  5. * Rasdaman community is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Rasdaman community is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with rasdaman community. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Copyright 2003 - 2016 Peter Baumann / rasdaman GmbH.
  19. *
  20. * For more information please see <http://www.rasdaman.org>
  21. * or contact Peter Baumann via <baumann@rasdaman.com>.
  22. *
  23. """
  24.  
  25. import os
  26. import re
  27. import subprocess
  28. import sys
  29. from distutils.spawn import find_executable
  30.  
  31. # Find the Protocol Compiler.
  32. # TODO: Take windows cases into account
  33. if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
  34. protoc = os.environ['PROTOC']
  35. grpc_plugin = os.environ['GRPC_PYTHON_PLUGIN']
  36. elif os.path.exists('/usr/local/scripts/protoc'):
  37. protoc = "/usr/local/scripts/protoc"
  38. grpc_plugin = "/usr/local/scripts/grpc_python_plugin"
  39. elif os.path.exists('/usr/scripts/protoc'):
  40. protoc = "/usr/scripts/protoc"
  41. grpc_plugin = "/usr/scripts/grpc_python_plugin"
  42. else:
  43. protoc = find_executable("protoc")
  44. grpc_plugin = find_executable("grpc_python_plugin")
  45.  
  46.  
  47. def generate_proto(source, destination, proto_dir, stubs_dir, require=True):
  48. """
  49. Invokes the Protocol Compiler to generate a _pb2.py from the given
  50. .proto file. Does nothing if the output already exists and is newer than
  51. the input.
  52.  
  53. Args:
  54. destination: the _pb2 file path
  55. source: the proto file path
  56. """
  57.  
  58. if not require and not os.path.exists(source):
  59. sys.stderr.write("Source is not a valid file path")
  60. return
  61.  
  62. if not require and protoc is None:
  63. sys.stderr.write(
  64. "Can't find protoc. Make sure you've installed protocol buffers")
  65. return
  66.  
  67. if (not os.path.exists(destination) or
  68. (os.path.exists(source))):
  69. print("Generating %s..." % destination)
  70.  
  71. if not os.path.exists(source):
  72. sys.stderr.write("Can't find required file: %s\n" % source)
  73. sys.exit(-1)
  74.  
  75. """ if protoc is None:
  76. sys.stderr.write(
  77. "protoc is not installed nor found in ../src. Please compile it "
  78. "or install the binary package.\n")
  79. sys.exit(-1) """
  80.  
  81. """ protoc_command = ["python -m grpc.tools.protoc", "-I" + proto_dir, "--python_out=" + stubs_dir,
  82. "--grpc_out=" + stubs_dir,
  83. "--plugin=protoc-gen-grpc=" + grpc_plugin, source] """
  84.  
  85. command = "python -m grpc.tools.protoc --python_out={} --grpc_python_out={} --proto_path={} {}".format(stubs_dir, stubs_dir, proto_dir, source)
  86. print command
  87. os.system(command)
  88.  
  89. # if subprocess.call(protoc_command) != 0:
  90. # sys.exit(-1)
  91.  
  92.  
  93. def main(args=None):
  94. proto_list = ['client_rassrvr_service.proto', 'common_service.proto',
  95. 'rasmgr_client_service.proto']
  96. current_dir = os.path.dirname(os.path.realpath(__file__))
  97. proto_dir = current_dir + "/../../../rasnet/protomessages/"
  98. stubs_dir = current_dir + "/../rasdapy/stubs/"
  99.  
  100. for proto_file in proto_list:
  101. pb2_file = proto_file.replace(".proto", "_pb2.py")
  102. generate_proto(proto_dir + proto_file, stubs_dir + pb2_file, proto_dir,
  103. stubs_dir, require=True)
  104. f = open(stubs_dir + pb2_file, "r+b")
  105. f_content = f.read()
  106. f_content = re.sub(r"syntax='proto3',", r"#syntax='proto3'", f_content)
  107. f.seek(0)
  108. f.truncate()
  109. f.write(f_content)
  110. f.close()
  111.  
  112.  
  113. if __name__ == '__main__':
  114. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement