Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module containing classes that wrap artifact downloads.""" |
| 6 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 7 | import os |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 8 | import re |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 9 | import shutil |
| 10 | import subprocess |
| 11 | |
| 12 | import gsutil_util |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 13 | import log_util |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 14 | |
| 15 | |
| 16 | # Names of artifacts we care about. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 17 | AUTOTEST_PACKAGE = 'autotest.tar' |
| 18 | AUTOTEST_ZIPPED_PACKAGE = 'autotest.tar.bz2' |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 19 | DEBUG_SYMBOLS = 'debug.tgz' |
| 20 | FIRMWARE_ARCHIVE = 'firmware_from_source.tar.bz2' |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 21 | IMAGE_ARCHIVE = 'image.zip' |
Vadim Bendebury | d369839 | 2012-12-27 18:21:32 -0800 | [diff] [blame] | 22 | ROOT_UPDATE = 'update.gz' |
| 23 | STATEFUL_UPDATE = 'stateful.tgz' |
| 24 | TEST_IMAGE = 'chromiumos_test_image.bin' |
| 25 | TEST_SUITES_PACKAGE = 'test_suites.tar.bz2' |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class ArtifactDownloadError(Exception): |
| 29 | """Error used to signify an issue processing an artifact.""" |
| 30 | pass |
| 31 | |
| 32 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 33 | class BuildArtifact(log_util.Loggable): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 34 | """Wrapper around an artifact to download from gsutil. |
| 35 | |
| 36 | The purpose of this class is to download objects from Google Storage |
| 37 | and install them to a local directory. There are two main functions, one to |
| 38 | download/prepare the artifacts in to a temporary staging area and the second |
| 39 | to stage it into its final destination. |
| 40 | """ |
| 41 | def __init__(self, gs_path, tmp_staging_dir, install_path, synchronous=False): |
| 42 | """Args: |
| 43 | gs_path: Path to artifact in google storage. |
| 44 | tmp_staging_dir: Temporary working directory maintained by caller. |
| 45 | install_path: Final destination of artifact. |
| 46 | synchronous: If True, artifact must be downloaded in the foreground. |
| 47 | """ |
| 48 | self._gs_path = gs_path |
| 49 | self._tmp_staging_dir = tmp_staging_dir |
| 50 | self._tmp_stage_path = os.path.join(tmp_staging_dir, |
| 51 | os.path.basename(self._gs_path)) |
| 52 | self._synchronous = synchronous |
| 53 | self._install_path = install_path |
| 54 | |
| 55 | if not os.path.isdir(self._tmp_staging_dir): |
| 56 | os.makedirs(self._tmp_staging_dir) |
| 57 | |
| 58 | if not os.path.isdir(os.path.dirname(self._install_path)): |
| 59 | os.makedirs(os.path.dirname(self._install_path)) |
| 60 | |
| 61 | def Download(self): |
| 62 | """Stages the artifact from google storage to a local staging directory.""" |
| 63 | gsutil_util.DownloadFromGS(self._gs_path, self._tmp_stage_path) |
| 64 | |
| 65 | def Synchronous(self): |
| 66 | """Returns False if this artifact can be downloaded in the background.""" |
| 67 | return self._synchronous |
| 68 | |
| 69 | def Stage(self): |
| 70 | """Moves the artifact from the tmp staging directory to the final path.""" |
| 71 | shutil.move(self._tmp_stage_path, self._install_path) |
| 72 | |
| 73 | def __str__(self): |
| 74 | """String representation for the download.""" |
| 75 | return '->'.join([self._gs_path, self._tmp_staging_dir, self._install_path]) |
| 76 | |
| 77 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 78 | class AUTestPayloadBuildArtifact(BuildArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 79 | """Wrapper for AUTest delta payloads which need additional setup.""" |
| 80 | def Stage(self): |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 81 | super(AUTestPayloadBuildArtifact, self).Stage() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 82 | |
| 83 | payload_dir = os.path.dirname(self._install_path) |
| 84 | # Setup necessary symlinks for updating. |
| 85 | os.symlink(os.path.join(os.pardir, os.pardir, TEST_IMAGE), |
| 86 | os.path.join(payload_dir, TEST_IMAGE)) |
| 87 | os.symlink(os.path.join(os.pardir, os.pardir, STATEFUL_UPDATE), |
| 88 | os.path.join(payload_dir, STATEFUL_UPDATE)) |
| 89 | |
| 90 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 91 | class TarballBuildArtifact(BuildArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 92 | """Wrapper around an artifact to download from gsutil which is a tarball.""" |
| 93 | |
| 94 | def _ExtractTarball(self, exclude=None): |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 95 | """Detects whether the tarball is compressed or not based on the file |
| 96 | extension and extracts the tarball into the install_path with optional |
| 97 | exclude path.""" |
| 98 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 99 | exclude_str = '--exclude=%s' % exclude if exclude else '' |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 100 | tarball = os.path.basename(self._tmp_stage_path) |
| 101 | |
| 102 | if re.search('.tar.bz2$', tarball): |
| 103 | compress_str = '--use-compress-prog=pbzip2' |
| 104 | else: |
| 105 | compress_str = '' |
| 106 | |
| 107 | cmd = 'tar xf %s %s %s --directory=%s' % ( |
| 108 | self._tmp_stage_path, exclude_str, compress_str, self._install_path) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 109 | msg = 'An error occurred when attempting to untar %s' % self._tmp_stage_path |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 110 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 111 | try: |
| 112 | subprocess.check_call(cmd, shell=True) |
| 113 | except subprocess.CalledProcessError, e: |
| 114 | raise ArtifactDownloadError('%s %s' % (msg, e)) |
| 115 | |
| 116 | def Stage(self): |
| 117 | """Changes directory into the install path and untars the tarball.""" |
| 118 | if not os.path.isdir(self._install_path): |
| 119 | os.makedirs(self._install_path) |
| 120 | |
| 121 | self._ExtractTarball() |
| 122 | |
| 123 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 124 | class AutotestTarballBuildArtifact(TarballBuildArtifact): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 125 | """Wrapper around the autotest tarball to download from gsutil.""" |
| 126 | |
| 127 | def Stage(self): |
| 128 | """Untars the autotest tarball into the install path excluding test suites. |
| 129 | """ |
| 130 | if not os.path.isdir(self._install_path): |
| 131 | os.makedirs(self._install_path) |
| 132 | |
| 133 | self._ExtractTarball(exclude='autotest/test_suites') |
| 134 | autotest_dir = os.path.join(self._install_path, 'autotest') |
| 135 | autotest_pkgs_dir = os.path.join(autotest_dir, 'packages') |
| 136 | if not os.path.exists(autotest_pkgs_dir): |
| 137 | os.makedirs(autotest_pkgs_dir) |
| 138 | |
| 139 | if not os.path.exists(os.path.join(autotest_pkgs_dir, 'packages.checksum')): |
| 140 | cmd = 'autotest/utils/packager.py upload --repository=%s --all' % ( |
| 141 | autotest_pkgs_dir) |
| 142 | msg = 'Failed to create autotest packages!' |
| 143 | try: |
| 144 | subprocess.check_call(cmd, cwd=self._tmp_staging_dir, |
| 145 | shell=True) |
| 146 | except subprocess.CalledProcessError, e: |
| 147 | raise ArtifactDownloadError('%s %s' % (msg, e)) |
| 148 | else: |
Gilad Arnold | f584313 | 2012-09-25 00:31:20 -0700 | [diff] [blame] | 149 | self._Log('Using pre-generated packages from autotest') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 150 | |
| 151 | # TODO(scottz): Remove after we have moved away from the old test_scheduler |
| 152 | # code. |
| 153 | cmd = 'cp %s/* %s' % (autotest_pkgs_dir, autotest_dir) |
| 154 | subprocess.check_call(cmd, shell=True) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 155 | |
| 156 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 157 | class DebugTarballBuildArtifact(TarballBuildArtifact): |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 158 | """Wrapper around the debug symbols tarball to download from gsutil.""" |
| 159 | |
| 160 | def _ExtractTarball(self): |
| 161 | """Extracts debug/breakpad from the tarball into the install_path.""" |
| 162 | cmd = 'tar xzf %s --directory=%s debug/breakpad' % ( |
| 163 | self._tmp_stage_path, self._install_path) |
| 164 | msg = 'An error occurred when attempting to untar %s' % self._tmp_stage_path |
| 165 | try: |
| 166 | subprocess.check_call(cmd, shell=True) |
| 167 | except subprocess.CalledProcessError, e: |
| 168 | raise ArtifactDownloadError('%s %s' % (msg, e)) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 169 | |
| 170 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 171 | class ZipfileBuildArtifact(BuildArtifact): |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 172 | """A downloadable artifact that is a zipfile. |
| 173 | |
| 174 | This class defines an extra public method for setting the list of files to be |
| 175 | extracted upon staging. Staging amounts to unzipping the desired files to the |
| 176 | install path. |
| 177 | |
| 178 | """ |
| 179 | |
| 180 | def __init__(self, gs_path, tmp_staging_dir, install_path, synchronous=False, |
| 181 | unzip_file_list=None): |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 182 | super(ZipfileBuildArtifact, self).__init__( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 183 | gs_path, tmp_staging_dir, install_path, synchronous) |
| 184 | self._unzip_file_list = unzip_file_list |
| 185 | |
| 186 | def _Unzip(self): |
| 187 | """Unzip files into the install path.""" |
| 188 | |
| 189 | cmd = 'unzip -o %s -d %s%s' % ( |
| 190 | self._tmp_stage_path, |
| 191 | os.path.join(self._install_path), |
| 192 | (' ' + ' '.join(self._unzip_file_list) |
| 193 | if self._unzip_file_list else '')) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 194 | self._Log('unzip command: %s' % cmd) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 195 | msg = 'An error occurred when attempting to unzip %s' % self._tmp_stage_path |
| 196 | |
| 197 | try: |
| 198 | subprocess.check_call(cmd, shell=True) |
| 199 | except subprocess.CalledProcessError, e: |
| 200 | raise ArtifactDownloadError('%s %s' % (msg, e)) |
| 201 | |
| 202 | def Stage(self): |
| 203 | """Unzip files into the install path.""" |
| 204 | if not os.path.isdir(self._install_path): |
| 205 | os.makedirs(self._install_path) |
| 206 | |
| 207 | self._Unzip() |