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)
26 if sys.platform.startswith('linux') or sys.platform == 'darwin':
27 cmd = ['/bin/bash', '-c', "".join(cmd)]
29 kwargs.update({'shell': True})
30 print('Running: "{}"'.format(''.join(cmd)))
31 p = subprocess.Popen(cmd, **kwargs)
32 (stdout, stderr) = p.communicate()
33 return p.returncode, stdout, stderr
37 parser = argparse.ArgumentParser()
38 parser.add_argument('--input_proto', required=True, help='Path to caffe.proto')
39 parser.add_argument('--output', help='Directory where output file are generated',
40 default=os.path.dirname(os.path.realpath(__file__)))
44 def build_proto(proto_file_path, python_path):
45 retcode, out, err = shell('protoc -h')
49 if not (os.path.exists(proto_file_path) and os.path.isfile(proto_file_path)):
50 print('File {} does not exist'.format(proto_file_path))
52 if not os.path.exists(proto_file_path):
53 os.makedirs(python_path)
54 proto_path = os.path.split(proto_file_path)[0]
56 proto_path = os.getcwd()
58 proto_file = os.path.split(proto_file_path)[1]
59 command = 'protoc {} --python_out={}'.format(proto_file, python_path)
61 retcode, out, err = shell(command, cwd=proto_path)
64 print('protoc exit with code {}'.format(retcode))
65 print('protoc out: {}'.format(out.decode().strip('\n')))
66 print('protoc error: {}'.format(err.decode()))
68 python_file = '{}_pb2.py'.format(proto_file.split('.')[0])
69 shutil.move(os.path.join(python_path, python_file), os.path.join(python_path, 'caffe_pb2.py'))
70 print('File {} was generated in: {}'.format('caffe_pb2.py', python_path))
74 if __name__ == "__main__":
75 if sys.version_info < (3, 0):
76 print('Python version should be of version 3.5 or newer')
78 argv = get_cli_parser().parse_args()
79 proto_file_path = argv.input_proto
80 python_path = argv.output
81 status = build_proto(proto_file_path, python_path)