Publishing R5 content (#72)
[platform/upstream/dldt.git] / model-optimizer / mo / front / caffe / proto / generate_caffe_pb2.py
1 # !/usr/bin/env python
2 """
3  Copyright (c) 2018 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 import argparse
18 import os
19 import shutil
20 import subprocess
21 import sys
22
23
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
30
31
32 def get_cli_parser():
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__)))
37     return parser
38
39
40 def build_proto(proto_file_path, python_path):
41     retcode, out, err = shell(['protoc', '-h'])
42     if retcode:
43         print(err)
44         return 1
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))
47         return 1
48     proto_path = os.path.split(proto_file_path)[0]
49     if not proto_path:
50         proto_path = os.getcwd()
51
52     proto_file = os.path.split(proto_file_path)[1]
53     command = ['protoc', proto_file, '--python_out={}'.format(python_path)]
54
55     retcode, out, err = shell(command, cwd=proto_path)
56
57     if retcode:
58         print('protoc exit with code {}'.format(retcode))
59         print('protoc out: {}'.format(out.decode().strip('\n')))
60         print('protoc error: {}'.format(err.decode()))
61     else:
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))
65     return retcode
66
67
68 if __name__ == "__main__":
69     if sys.version_info < (3, 0):
70         print('Python version should be of version 3.5 or newer')
71         sys.exit(1)
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))
77         sys.exit(1)
78     status = build_proto(proto_file_path, python_path)
79     exit(status)