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