deploy_chrome: Make deploying from a GS tarball work again.

https://gerrit.chromium.org/gerrit/#/c/44561/ broke the functionality
for deploying from an uploaded Chrome tarball.  Make that work again, by
deploying only the /opt/google/chrome/* part of the tarball.

BUG=None
TEST=trybot, local deployment.

Change-Id: I492f4d191abd1bf06ecd22b6456d7e95868c6ca3
Reviewed-on: https://gerrit.chromium.org/gerrit/45782
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Ryan Cui <rcui@chromium.org>
Tested-by: Ryan Cui <rcui@chromium.org>
diff --git a/scripts/deploy_chrome.py b/scripts/deploy_chrome.py
index 213fa8f..69a51a6 100644
--- a/scripts/deploy_chrome.py
+++ b/scripts/deploy_chrome.py
@@ -411,6 +411,8 @@
   to the device.  Only the necessary Chrome build artifacts are put into the
   staging directory.
   """
+  osutils.SafeMakedirs(staging_dir)
+  os.chmod(staging_dir, 0755)
   if options.build_dir:
     with _StripBinContext(options) as strip_bin:
       strip_flags = (None if options.strip_flags is None else
@@ -428,9 +430,12 @@
 
     assert pkg_path
     logging.info('Extracting %s...', pkg_path)
-    osutils.SafeMakedirs(staging_dir)
-    cros_build_lib.DebugRunCommand(['tar', '-xpf', pkg_path], cwd=staging_dir)
-
+    # Extract only the ./opt/google/chrome contents, directly into the staging
+    # dir, collapsing the directory hierarchy.
+    cros_build_lib.DebugRunCommand(
+        ['tar', '--strip-components', '4', '--extract',
+         '--preserve-permissions', '--file', pkg_path, '.%s' % _CHROME_DIR],
+        cwd=staging_dir)
 
 def main(argv):
   options, args = _ParseCommandLine(argv)
@@ -446,6 +451,9 @@
     staging_dir = options.staging_dir
     if not staging_dir:
       staging_dir = os.path.join(tempdir, 'chrome')
+    else:
+      if os.path.exists(staging_dir) and os.listdir(staging_dir):
+        cros_build_lib.Die('Staging directory %s must be empty.' % staging_dir)
 
     deploy = DeployChrome(options, tempdir, staging_dir)
     try: