Telemetry-Autotest Integration: Run telemetry from Devservers.

As part of integrating telemetry into the autotest lab, we need the
devservers to extract and install telemetry for a given build.

BUG=chromium-os:37411
TEST=ran local devserver and through the rpc's downloaded the desired
build and made the rpc call to setup and stage telemetry and ensured
that it ran successfully.

Change-Id: I88cde49a177e484d8277369f6764e28a313dfd3e
Reviewed-on: https://gerrit.chromium.org/gerrit/43313
Reviewed-by: Chris Sosa <sosa@chromium.org>
Commit-Queue: Simran Basi <sbasi@chromium.org>
Reviewed-by: Simran Basi <sbasi@chromium.org>
Tested-by: Simran Basi <sbasi@chromium.org>
diff --git a/common_util.py b/common_util.py
index ac7f093..922b7e0 100644
--- a/common_util.py
+++ b/common_util.py
@@ -12,6 +12,7 @@
 import os
 import shutil
 import threading
+import subprocess
 
 import log_util
 
@@ -247,3 +248,41 @@
         lock = self._new_lock()
         self._dict[key] = lock
       return lock
+
+
+def ExtractTarball(tarball_path, install_path, files_to_extract=None,
+                   excluded_files=None):
+  """Extracts a tarball using tar.
+
+  Detects whether the tarball is compressed or not based on the file
+  extension and extracts the tarball into the install_path.
+
+  Args:
+    tarball_path: Path to the tarball to extract.
+    install_path: Path to extract the tarball to.
+    files_to_extract: String of specific files in the tarball to extract.
+    excluded_files: String of files to not extract.
+  """
+  # Deal with exclusions.
+  cmd = ['tar', 'xf', tarball_path, '--directory', install_path]
+
+  # Determine how to decompress.
+  tarball = os.path.basename(tarball_path)
+  if tarball.endswith('.tar.bz2'):
+    cmd.append('--use-compress-prog=pbzip2')
+  elif tarball.endswith('.tgz') or tarball.endswith('.tar.gz'):
+    cmd.append('--gzip')
+
+  if excluded_files:
+    for exclude in excluded_files:
+      cmd.extend(['--exclude', exclude])
+
+  if files_to_extract:
+    cmd.extend(files_to_extract)
+
+  try:
+    subprocess.check_call(cmd)
+  except subprocess.CalledProcessError, e:
+    raise CommonUtilError(
+        'An error occurred when attempting to untar %s:\n%s' %
+        (tarball_path, e))
\ No newline at end of file