Caffe2 - Python API
A deep learning, cross platform ML framework
diagnose_protobuf.py
1 ## @package diagnose_protobuf
2 # Module scripts.diagnose_protobuf
3 """Diagnoses the current protobuf situation.
4 
5 Protocol buffer needs to be properly installed for Caffe2 to work, and
6 sometimes it is rather tricky. Specifically, we will need to have a
7 consistent version between C++ and python simultaneously. This is a
8 convenience script for one to quickly check if this is so on one's local
9 machine.
10 
11 Usage:
12  [set your environmental variables like PATH and PYTHONPATH]
13  python scripts/diagnose_protobuf.py
14 """
15 
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19 from __future__ import unicode_literals
20 import os
21 import re
22 from subprocess import Popen, PIPE
23 
24 # Get python protobuf version.
25 try:
26  import google.protobuf
27  python_version = google.protobuf.__version__
28  python_protobuf_installed = True
29 except ImportError:
30  print("DEBUG: cannot find python protobuf install.")
31  python_protobuf_installed = False
32 
33 if os.name == 'nt':
34  protoc_name = 'protoc.exe'
35 else:
36  protoc_name = 'protoc'
37 
38 try:
39  p = Popen([protoc_name, '--version'], stdout=PIPE, stderr=PIPE)
40  out, err = p.communicate()
41 except:
42  print('DEBUG: did not find protoc binary.')
43  print('DEBUG: out: ' + out)
44  print('DEBUG: err: ' + err)
45  native_protobuf_installed = False
46 else:
47  if p.returncode:
48  print('DEBUG: protoc returned a non-zero return code.')
49  print('DEBUG: out: ' + out)
50  print('DEBUG: err: ' + err)
51  native_protobuf_installed = False
52  else:
53  tmp = re.search(r'\d\.\d\.\d', out)
54  if tmp:
55  native_version = tmp.group(0)
56  native_protobuf_installed = True
57  else:
58  print('DEBUG: cannot parse protoc version string.')
59  print('DEBUG: out: ' + out)
60  native_protobuf_installed = False
61 
62 PYTHON_PROTOBUF_NOT_INSTALLED = """
63 You have not installed python protobuf. Protobuf is needed to run caffe2. You
64 can install protobuf via pip or conda (if you are using anaconda python).
65 """
66 
67 NATIVE_PROTOBUF_NOT_INSTALLED = """
68 You have not installed the protoc binary. Protoc is needed to compile Caffe2
69 protobuf source files. Depending on the platform you are on, you can install
70 protobuf via:
71  (1) Mac: using homebrew and do brew install protobuf.
72  (2) Linux: use apt and do apt-get install libprotobuf-dev
73  (3) Windows: install from source, or from the releases here:
74  https://github.com/google/protobuf/releases/
75 """
76 
77 VERSION_MISMATCH = """
78 Your python protobuf is of version {py_ver} but your native protoc version is of
79 version {native_ver}. This will cause the installation to produce incompatible
80 protobuf files. This is bad in general - consider installing the same version.
81 """.format(py_ver=python_version, native_ver=native_version)
82 
83 # Now, give actual recommendations
84 if not python_protobuf_installed:
85  print(PYTHON_PROTOBUF_NOT_INSTALLED)
86 
87 if not native_protobuf_installed:
88  print(NATIVE_PROTOBUF_NOT_INSTALLED)
89 
90 if python_protobuf_installed and native_protobuf_installed:
91  if python_version != native_version:
92  print(VERSION_MISMATCH)
93  else:
94  print('All looks good.')
95 
96 
97 
98