github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/bin/protogen (about) 1 #!/usr/bin/env python3 2 # 3 # Copyright 2017 Intel Corporation 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # ------------------------------------------------------------------------------ 17 18 import os 19 import tempfile 20 from glob import glob 21 import re 22 import subprocess 23 import sys 24 25 26 try: 27 from grpc.tools.protoc import main as _protoc 28 except ImportError: 29 print("Error: grpc.tools.protoc not found") 30 exit(1) 31 32 33 JOIN = os.path.join 34 TOP_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) 35 36 37 def main(args=None): 38 if args is None: 39 args = sys.argv[1:] 40 41 # Generate protobuf classes for Python integration tests 42 protoc( 43 JOIN(TOP_DIR, "protos"), 44 "tests", 45 "sawtooth_sc_test/protobuf") 46 47 48 def protoc(src_dir, base_dir, pkg, language="python"): 49 if language == "python": 50 protoc_python(src_dir, base_dir, pkg) 51 52 53 def protoc_python(src_dir, base_dir, pkg): 54 # 1. Create output package directory 55 pkg_dir = JOIN(TOP_DIR, base_dir, pkg) 56 os.makedirs(pkg_dir, exist_ok=True) 57 58 # 2. 'touch' the __init__.py file if the output directory exists 59 init_py = JOIN(pkg_dir, "__init__.py") 60 if not os.path.exists(init_py): 61 with open(init_py, "w") as fd: 62 pass # Just need it to exist 63 64 # 3. Create a temp directory for building 65 with tempfile.TemporaryDirectory() as tmp_dir: 66 tmp_pkg_dir = JOIN(tmp_dir, pkg) 67 os.makedirs(tmp_pkg_dir) 68 69 # 4. Get a list of all .proto files to build 70 cwd = os.getcwd() 71 os.chdir(src_dir) 72 proto_files = glob("*.proto") 73 os.chdir(cwd) 74 75 # 5. Copy protos to temp dir and fix imports 76 for proto in proto_files: 77 src = JOIN(src_dir, proto) 78 dst = JOIN(tmp_pkg_dir, proto) 79 with open(src, encoding='utf-8') as fin: 80 with open(dst, "w", encoding='utf-8') as fout: 81 src_contents = fin.read() 82 fixed_contents = fix_import(src_contents, pkg) 83 fout.write(fixed_contents) 84 85 # 6. Compile protobuf files 86 _protoc([ 87 __file__, 88 "-I=%s" % tmp_dir, 89 "--python_out=%s" % JOIN(TOP_DIR, base_dir), 90 ] + glob("%s/*.proto" % tmp_pkg_dir)) 91 92 93 def fix_import(contents, pkg, sub_dir=False): 94 pattern = r'^import "(.*)\.proto\"' 95 if sub_dir: 96 template = r'import "%s/\1_pb2/\1.proto"' 97 else: 98 template = r'import "%s/\1.proto"' 99 100 return re.sub( 101 pattern, 102 lambda match: match.expand(template) % pkg, 103 contents, 104 flags=re.MULTILINE 105 ) 106 107 108 if __name__ == "__main__": 109 try: 110 main() 111 except KeyboardInterrupt: 112 exit(1)