941f65c1fff7df25c2ec0b79ffe02818cfa5e98d
[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)
26     if sys.platform.startswith('linux') or sys.platform == 'darwin':
27         cmd = ['/bin/bash', '-c', "".join(cmd)]
28     else:
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
34
35
36 def get_cli_parser():
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__)))
41     return parser
42
43
44 def build_proto(proto_file_path, python_path):
45     retcode, out, err = shell('protoc -h')
46     if retcode:
47         print(err)
48         return 1
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))
51         return 1
52     if not os.path.exists(proto_file_path):
53         os.makedirs(python_path)
54     proto_path = os.path.split(proto_file_path)[0]
55     if not proto_path:
56         proto_path = os.getcwd()
57
58     proto_file = os.path.split(proto_file_path)[1]
59     command = 'protoc {} --python_out={}'.format(proto_file, python_path)
60
61     retcode, out, err = shell(command, cwd=proto_path)
62
63     if retcode:
64         print('protoc exit with code {}'.format(retcode))
65         print('protoc out: {}'.format(out.decode().strip('\n')))
66         print('protoc error: {}'.format(err.decode()))
67     else:
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))
71     return retcode
72
73
74 if __name__ == "__main__":
75     if sys.version_info < (3, 0):
76         print('Python version should be of version 3.5 or newer')
77         sys.exit(1)
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)
82     exit(status)