Add better error messages for missing dependencies.

If we run into issues importing well-known requirements, report them specially and guide
users on how to install those on their system.

Change-Id: Ic8e4107591198cec8f3744c728bbd23ad5580fba
Fixes: 38004607
Test: manual
diff --git a/tools/emulator/diagnostic_injector.py b/tools/emulator/diagnostic_injector.py
index eb9c51c..63e8f10 100755
--- a/tools/emulator/diagnostic_injector.py
+++ b/tools/emulator/diagnostic_injector.py
@@ -26,7 +26,25 @@
 
 import vhal_consts_2_1 as c
 
-from vhal_emulator import Vhal
+# vhal_emulator depends on a custom Python package that requires installation
+# give user guidance should the import fail
+try:
+    from vhal_emulator import Vhal
+except ImportError as e:
+    isProtobuf = False
+    pipTool = "pip%s" % ("3" if sys.version_info > (3,0) else "")
+    if hasattr(e, 'name'):
+        if e.name == 'google': isProtobuf = True
+    elif hasattr(e, 'message'):
+        if e.message.endswith('symbol_database'):
+            isProtobuf = True
+    if isProtobuf:
+        print('could not find protobuf.')
+        print('protobuf can be installed via "sudo %s install --upgrade protobuf"' % pipTool)
+        sys.exit(1)
+    else:
+        raise e
+
 from diagnostic_builder import DiagnosticEventBuilder
 
 class DiagnosticHalWrapper(object):
diff --git a/tools/emulator/vhal_const_generate.py b/tools/emulator/vhal_const_generate.py
index dd9e3e6..862c68f 100755
--- a/tools/emulator/vhal_const_generate.py
+++ b/tools/emulator/vhal_const_generate.py
@@ -23,6 +23,8 @@
 # to itself vhal_consts_x.y.py for any version of Vehicle HAL that it knows about
 # Those files can then be used with vhal_emulator.py as per that script's documentation
 
+from __future__ import print_function
+
 import datetime
 
 def printHeader(dest):
@@ -59,7 +61,24 @@
 parent_location = os.path.abspath(os.path.join(script_directory, '..'))
 sys.path.append(parent_location)
 
-from hidl_parser import parser
+# hidl_parser depends on a custom Python package that requires installation
+# give user guidance should the import fail
+try:
+    from hidl_parser import parser
+except ImportError as e:
+    isPly = False
+    pipTool = "pip%s" % ("3" if sys.version_info > (3,0) else "")
+    if hasattr(e, 'name'):
+        if e.name == 'ply': isPly = True
+    elif hasattr(e, 'message'):
+        if e.message.endswith('ply'): isPly = True
+    if isPly:
+        print('could not import ply.')
+        print('ply is available as part of an Android checkout in external/ply')
+        print('or it can be installed via "sudo %s install ply"' % pipTool)
+        sys.exit(1)
+    else:
+        raise e
 
 android_build_top = os.environ.get("ANDROID_BUILD_TOP", None)
 if android_build_top is not None:
diff --git a/tools/hidl_parser/parser.py b/tools/hidl_parser/parser.py
index 79a8608..3096804 100644
--- a/tools/hidl_parser/parser.py
+++ b/tools/hidl_parser/parser.py
@@ -25,6 +25,8 @@
 
 # It requires 'ply' (Python Lex/Yacc).
 
+from __future__ import print_function
+
 import ply
 
 tokens = ('package', 'import', 'enum', 'struct',