Set exit code on gclient getdep if rev is invalid

Add exit code if requested DEP is not found. This way, machines
don't need to parse stderr and make decision; instead they can use exit
code.

R=jojwang@google.com

Bug: 1454296
Change-Id: I73edda333342c2ebf78eb0e40b73cb87562989c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4608990
Commit-Queue: Joanna Wang <jojwang@chromium.org>
Commit-Queue: Josip Sokcevic <sokcevic@chromium.org>
Auto-Submit: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Joanna Wang <jojwang@chromium.org>
diff --git a/gclient.py b/gclient.py
index be074ae..6034617 100755
--- a/gclient.py
+++ b/gclient.py
@@ -3226,7 +3226,10 @@
 
 @metrics.collector.collect_metrics('gclient getdep')
 def CMDgetdep(parser, args):
-  """Gets revision information and variable values from a DEPS file."""
+  """Gets revision information and variable values from a DEPS file.
+
+  If key doesn't exist or is incorrectly declared, this script exits with exit
+  code 2."""
   parser.add_option('--var', action='append',
                     dest='vars', metavar='VAR', default=[],
                     help='Gets the value of a given variable.')
@@ -3271,7 +3274,11 @@
             % (name, package))
       print(gclient_eval.GetCIPD(local_scope, name, package))
     else:
-      print(gclient_eval.GetRevision(local_scope, name))
+      try:
+        print(gclient_eval.GetRevision(local_scope, name))
+      except KeyError as e:
+        print(repr(e), file=sys.stderr)
+        sys.exit(2)
 
 
 @metrics.collector.collect_metrics('gclient setdep')