3 Copyright (c) 2018 Intel Corporation
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
24 def shell(cmd, env=None, cwd=None):
25 kwargs = dict(cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
26 print('Running: "{}"'.format(' '.join(cmd)))
27 p = subprocess.Popen(cmd, **kwargs)
28 (stdout, stderr) = p.communicate()
29 return p.returncode, stdout, stderr
33 parser = argparse.ArgumentParser()
34 parser.add_argument('--input_proto', required=True, help='Path to caffe.proto')
35 parser.add_argument('--output', help='Directory where output file are generated',
36 default=os.path.dirname(os.path.realpath(__file__)))
40 def build_proto(proto_file_path, python_path):
41 retcode, out, err = shell(['protoc', '-h'])
45 if not (os.path.exists(proto_file_path) and os.path.isfile(proto_file_path)):
46 print('File {} does not exist'.format(proto_file_path))
48 proto_path = os.path.split(proto_file_path)[0]
50 proto_path = os.getcwd()
52 proto_file = os.path.split(proto_file_path)[1]
53 command = ['protoc', proto_file, '--python_out={}'.format(python_path)]
55 retcode, out, err = shell(command, cwd=proto_path)
58 print('protoc exit with code {}'.format(retcode))
59 print('protoc out: {}'.format(out.decode().strip('\n')))
60 print('protoc error: {}'.format(err.decode()))
62 python_file = '{}_pb2.py'.format(proto_file.split('.')[0])
63 shutil.move(os.path.join(python_path, python_file), os.path.join(python_path, 'caffe_pb2.py'))
64 print('File {} was generated in: {}'.format('caffe_pb2.py', python_path))
68 if __name__ == "__main__":
69 if sys.version_info < (3, 0):
70 print('Python version should be of version 3.5 or newer')
72 argv = get_cli_parser().parse_args()
73 proto_file_path = argv.input_proto
74 python_path = argv.output
75 if not os.path.exists(python_path):
76 print("Output directory {} does not exist".format(python_path))
78 status = build_proto(proto_file_path, python_path)