Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Helper class for interacting with the Dev Server.""" |
| 6 | |
| 7 | import cherrypy |
| 8 | import distutils.version |
| 9 | import errno |
| 10 | import os |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 11 | import random |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 12 | import re |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 13 | import shutil |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 14 | import time |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 15 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 16 | import downloadable_artifact |
| 17 | import gsutil_util |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 18 | |
| 19 | AU_BASE = 'au' |
| 20 | NTON_DIR_SUFFIX = '_nton' |
| 21 | MTON_DIR_SUFFIX = '_mton' |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 22 | DEV_BUILD_PREFIX = 'dev' |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 23 | UPLOADED_LIST = 'UPLOADED' |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 24 | |
| 25 | class DevServerUtilError(Exception): |
| 26 | """Exception classes used by this module.""" |
| 27 | pass |
| 28 | |
| 29 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 30 | def ParsePayloadList(archive_url, payload_list): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 31 | """Parse and return the full/delta payload URLs. |
| 32 | |
| 33 | Args: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 34 | archive_url: The URL of the Google Storage bucket. |
| 35 | payload_list: A list filenames. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 36 | |
| 37 | Returns: |
| 38 | Tuple of 3 payloads URLs: (full, nton, mton). |
| 39 | |
| 40 | Raises: |
| 41 | DevServerUtilError: If payloads missing or invalid. |
| 42 | """ |
| 43 | full_payload_url = None |
| 44 | mton_payload_url = None |
| 45 | nton_payload_url = None |
| 46 | for payload in payload_list: |
| 47 | if '_full_' in payload: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 48 | full_payload_url = '/'.join([archive_url, payload]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 49 | elif '_delta_' in payload: |
| 50 | # e.g. chromeos_{from_version}_{to_version}_x86-generic_delta_dev.bin |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 51 | from_version, to_version = payload.split('_')[1:3] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 52 | if from_version == to_version: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 53 | nton_payload_url = '/'.join([archive_url, payload]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 54 | else: |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 55 | mton_payload_url = '/'.join([archive_url, payload]) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 56 | |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 57 | if not full_payload_url: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 58 | raise DevServerUtilError( |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 59 | 'Full payload is missing or has unexpected name format.', payload_list) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 60 | |
| 61 | return full_payload_url, nton_payload_url, mton_payload_url |
| 62 | |
| 63 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 64 | def IsAvailable(pattern_list, uploaded_list): |
| 65 | """Checks whether the target artifacts we wait for are available. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 66 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 67 | This method searches the uploaded_list for a match for every pattern |
| 68 | in the pattern_list. It aborts and returns false if no filename |
| 69 | matches a given pattern. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 70 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 71 | Args: |
| 72 | pattern_list: List of regular expression patterns to identify |
| 73 | the target artifacts. |
| 74 | uploaded_list: List of all uploaded files. |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 75 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 76 | Returns: |
| 77 | True if there is a match for every pattern; false otherwise. |
| 78 | """ |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 79 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 80 | # Pre-compile the regular expression patterns |
| 81 | compiled_patterns = [] |
| 82 | for p in pattern_list: |
| 83 | compiled_patterns.append(re.compile(p)) |
| 84 | |
| 85 | for pattern in compiled_patterns: |
| 86 | found = False |
| 87 | for filename in uploaded_list: |
| 88 | if re.search(pattern, filename): |
| 89 | found = True |
| 90 | break |
| 91 | if not found: |
| 92 | return False |
| 93 | |
| 94 | return True |
| 95 | |
| 96 | |
| 97 | def WaitUntilAvailable(to_wait_list, archive_url, err_str, timeout=600, |
| 98 | delay=10): |
| 99 | """Waits until all target artifacts are available in Google Storage or |
| 100 | until the request times out. |
| 101 | |
| 102 | This method polls Google Storage until all target artifacts are |
| 103 | available or until the timeout occurs. Because we may not know the |
| 104 | exact name of the target artifacts, the method accepts to_wait_list, a |
| 105 | list of filename patterns, to identify whether an artifact whose name |
| 106 | matches the pattern exists (e.g. use pattern '_full_' to search for |
| 107 | the full payload 'chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin'). |
| 108 | |
| 109 | Args: |
| 110 | to_wait_list: List of regular expression patterns to identify |
| 111 | the target artifacts. |
| 112 | archive_url: URL of the Google Storage bucket. |
| 113 | err_str: String to display in the error message. |
| 114 | |
| 115 | Returns: |
| 116 | The list of artifacts in the Google Storage bucket. |
| 117 | |
| 118 | Raises: |
| 119 | DevServerUtilError: If timeout occurs. |
| 120 | """ |
| 121 | |
| 122 | cmd = 'gsutil cat %s/%s' % (archive_url, UPLOADED_LIST) |
| 123 | msg = 'Failed to get a list of uploaded files.' |
| 124 | |
| 125 | deadline = time.time() + timeout |
| 126 | while time.time() < deadline: |
Yu-Ju Hong | 9278405 | 2012-08-22 13:08:54 -0700 | [diff] [blame] | 127 | uploaded_list = [] |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 128 | to_delay = delay + random.uniform(.5 * delay, 1.5 * delay) |
Yu-Ju Hong | 9278405 | 2012-08-22 13:08:54 -0700 | [diff] [blame] | 129 | try: |
| 130 | # Run "gsutil cat" to retrieve the list. |
| 131 | uploaded_list = gsutil_util.GSUtilRun(cmd, msg).splitlines() |
| 132 | except gsutil_util.GSUtilError: |
| 133 | # For backward compatibility, fallling back to use "gsutil ls" |
| 134 | # when the manifest file is not present. |
| 135 | cmd = 'gsutil ls %s/*' % archive_url |
| 136 | msg = 'Failed to list payloads.' |
| 137 | payload_list = gsutil_util.GSUtilRun(cmd, msg).splitlines() |
| 138 | for payload in payload_list: |
| 139 | uploaded_list.append(payload.rsplit('/', 1)[1]) |
| 140 | |
| 141 | # Check if all target artifacts are available. |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 142 | if IsAvailable(to_wait_list, uploaded_list): |
| 143 | return uploaded_list |
| 144 | cherrypy.log('Retrying in %f seconds...%s' % (to_delay, err_str)) |
| 145 | time.sleep(to_delay) |
| 146 | |
| 147 | raise DevServerUtilError('Missing %s for %s.' % (err_str, archive_url)) |
| 148 | |
| 149 | |
| 150 | def GatherArtifactDownloads(main_staging_dir, archive_url, build, build_dir, |
| 151 | timeout=600, delay=10): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 152 | """Generates artifacts that we mean to download and install for autotest. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 153 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 154 | This method generates the list of artifacts we will need for autotest. These |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 155 | artifacts are instances of downloadable_artifact.DownloadableArtifact. |
| 156 | |
| 157 | Note, these artifacts can be downloaded asynchronously iff |
| 158 | !artifact.Synchronous(). |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 159 | """ |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 160 | |
| 161 | # Wait up to 10 minutes for the full payload to be uploaded because we |
| 162 | # do not know the exact name of the full payload. |
| 163 | |
| 164 | # We also wait for 'autotest.tar' because we do not know what type of |
| 165 | # autotest tarballs (tar or tar.bz2) is available |
| 166 | # (crosbug.com/32312). This dependency can be removed once all |
| 167 | # branches move to the new 'tar' format. |
| 168 | to_wait_list = ['_full_', 'autotest.tar'] |
| 169 | err_str = 'full payload or autotest tarball' |
| 170 | uploaded_list = WaitUntilAvailable(to_wait_list, archive_url, err_str, |
| 171 | timeout=600) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 172 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 173 | # First we gather the urls/paths for the update payloads. |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 174 | full_url, nton_url, mton_url = ParsePayloadList(archive_url, uploaded_list) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 175 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 176 | full_payload = os.path.join(build_dir, downloadable_artifact.ROOT_UPDATE) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 177 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 178 | artifacts = [] |
| 179 | artifacts.append(downloadable_artifact.DownloadableArtifact(full_url, |
| 180 | main_staging_dir, full_payload, synchronous=True)) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 181 | |
| 182 | if nton_url: |
| 183 | nton_payload = os.path.join(build_dir, AU_BASE, build + NTON_DIR_SUFFIX, |
| 184 | downloadable_artifact.ROOT_UPDATE) |
| 185 | artifacts.append(downloadable_artifact.AUTestPayload(nton_url, |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 186 | main_staging_dir, nton_payload)) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 187 | |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 188 | if mton_url: |
| 189 | mton_payload = os.path.join(build_dir, AU_BASE, build + MTON_DIR_SUFFIX, |
| 190 | downloadable_artifact.ROOT_UPDATE) |
| 191 | artifacts.append(downloadable_artifact.AUTestPayload( |
| 192 | mton_url, main_staging_dir, mton_payload)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 193 | |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 194 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 195 | # Gather information about autotest tarballs. Use autotest.tar if available. |
| 196 | if downloadable_artifact.AUTOTEST_PACKAGE in uploaded_list: |
| 197 | autotest_url = '%s/%s' % (archive_url, |
| 198 | downloadable_artifact.AUTOTEST_PACKAGE) |
| 199 | else: |
| 200 | # Use autotest.tar.bz for backward compatibility. This can be |
| 201 | # removed once all branches start using "autotest.tar" |
| 202 | autotest_url = '%s/%s' % (archive_url, |
| 203 | downloadable_artifact.AUTOTEST_ZIPPED_PACKAGE) |
| 204 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 205 | # Next we gather the miscellaneous payloads. |
| 206 | stateful_url = archive_url + '/' + downloadable_artifact.STATEFUL_UPDATE |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 207 | test_suites_url = (archive_url + '/' + |
| 208 | downloadable_artifact.TEST_SUITES_PACKAGE) |
| 209 | |
| 210 | stateful_payload = os.path.join(build_dir, |
| 211 | downloadable_artifact.STATEFUL_UPDATE) |
| 212 | |
| 213 | artifacts.append(downloadable_artifact.DownloadableArtifact( |
| 214 | stateful_url, main_staging_dir, stateful_payload, synchronous=True)) |
| 215 | artifacts.append(downloadable_artifact.AutotestTarball( |
| 216 | autotest_url, main_staging_dir, build_dir)) |
| 217 | artifacts.append(downloadable_artifact.Tarball( |
| 218 | test_suites_url, main_staging_dir, build_dir, synchronous=True)) |
| 219 | return artifacts |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 220 | |
| 221 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 222 | def GatherSymbolArtifactDownloads(temp_download_dir, archive_url, staging_dir, |
| 223 | timeout=600, delay=10): |
| 224 | """Generates debug symbol artifacts that we mean to download and stage. |
| 225 | |
| 226 | This method generates the list of artifacts we will need to |
| 227 | symbolicate crash dumps that occur during autotest runs. These |
| 228 | artifacts are instances of downloadable_artifact.DownloadableArtifact. |
| 229 | |
| 230 | This will poll google storage until the debug symbol artifact becomes |
| 231 | available, or until the 10 minute timeout is up. |
| 232 | |
| 233 | @param temp_download_dir: the tempdir into which we're downloading artifacts |
| 234 | prior to staging them. |
| 235 | @param archive_url: the google storage url of the bucket where the debug |
| 236 | symbols for the desired build are stored. |
| 237 | @param staging_dir: the dir into which to stage the symbols |
| 238 | |
| 239 | @return an iterable of one DebugTarball pointing to the right debug symbols. |
| 240 | This is an iterable so that it's similar to GatherArtifactDownloads. |
| 241 | Also, it's possible that someday we might have more than one. |
| 242 | """ |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 243 | |
| 244 | # Wait up to 10 minutes for the debug symbols to be uploaded. |
| 245 | to_wait_list = [downloadable_artifact.DEBUG_SYMBOLS] |
| 246 | err_str = 'debug symbols' |
| 247 | WaitUntilAvailable(to_wait_list, archive_url, err_str, timeout=timeout, |
| 248 | delay=delay) |
| 249 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 250 | symbol_url = archive_url + '/' + downloadable_artifact.DEBUG_SYMBOLS |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 251 | return [downloadable_artifact.DebugTarball(symbol_url, temp_download_dir, |
| 252 | staging_dir)] |
| 253 | |
| 254 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 255 | def PrepareBuildDirectory(build_dir): |
| 256 | """Preliminary staging of installation directory for build. |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 257 | |
| 258 | Args: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 259 | build_dir: Directory to install build components into. |
| 260 | """ |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 261 | if not os.path.isdir(build_dir): |
| 262 | os.path.makedirs(build_dir) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 263 | |
| 264 | # Create blank chromiumos_test_image.bin. Otherwise the Dev Server will |
| 265 | # try to rebuild it unnecessarily. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 266 | test_image = os.path.join(build_dir, downloadable_artifact.TEST_IMAGE) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 267 | open(test_image, 'a').close() |
| 268 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 269 | |
| 270 | def SafeSandboxAccess(static_dir, path): |
| 271 | """Verify that the path is in static_dir. |
| 272 | |
| 273 | Args: |
| 274 | static_dir: Directory where builds are served from. |
| 275 | path: Path to verify. |
| 276 | |
| 277 | Returns: |
| 278 | True if path is in static_dir, False otherwise |
| 279 | """ |
| 280 | static_dir = os.path.realpath(static_dir) |
| 281 | path = os.path.realpath(path) |
| 282 | return (path.startswith(static_dir) and path != static_dir) |
| 283 | |
| 284 | |
| 285 | def AcquireLock(static_dir, tag): |
| 286 | """Acquires a lock for a given tag. |
| 287 | |
| 288 | Creates a directory for the specified tag, telling other |
| 289 | components the resource/task represented by the tag is unavailable. |
| 290 | |
| 291 | Args: |
| 292 | static_dir: Directory where builds are served from. |
| 293 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 294 | |
| 295 | Returns: |
| 296 | Path to the created directory or None if creation failed. |
| 297 | |
| 298 | Raises: |
| 299 | DevServerUtilError: If lock can't be acquired. |
| 300 | """ |
| 301 | build_dir = os.path.join(static_dir, tag) |
| 302 | if not SafeSandboxAccess(static_dir, build_dir): |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 303 | raise DevServerUtilError('Invalid tag "%s".' % tag) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 304 | |
| 305 | try: |
| 306 | os.makedirs(build_dir) |
| 307 | except OSError, e: |
| 308 | if e.errno == errno.EEXIST: |
| 309 | raise DevServerUtilError(str(e)) |
| 310 | else: |
| 311 | raise |
| 312 | |
| 313 | return build_dir |
| 314 | |
| 315 | |
| 316 | def ReleaseLock(static_dir, tag): |
| 317 | """Releases the lock for a given tag. Removes lock directory content. |
| 318 | |
| 319 | Args: |
| 320 | static_dir: Directory where builds are served from. |
| 321 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 322 | |
| 323 | Raises: |
| 324 | DevServerUtilError: If lock can't be released. |
| 325 | """ |
| 326 | build_dir = os.path.join(static_dir, tag) |
| 327 | if not SafeSandboxAccess(static_dir, build_dir): |
| 328 | raise DevServerUtilError('Invaid tag "%s".' % tag) |
| 329 | |
| 330 | shutil.rmtree(build_dir) |
| 331 | |
| 332 | |
| 333 | def FindMatchingBoards(static_dir, board): |
| 334 | """Returns a list of boards given a partial board name. |
| 335 | |
| 336 | Args: |
| 337 | static_dir: Directory where builds are served from. |
| 338 | board: Partial board name for this build; e.g. x86-generic. |
| 339 | |
| 340 | Returns: |
| 341 | Returns a list of boards given a partial board. |
| 342 | """ |
| 343 | return [brd for brd in os.listdir(static_dir) if board in brd] |
| 344 | |
| 345 | |
| 346 | def FindMatchingBuilds(static_dir, board, build): |
| 347 | """Returns a list of matching builds given a board and partial build. |
| 348 | |
| 349 | Args: |
| 350 | static_dir: Directory where builds are served from. |
| 351 | board: Partial board name for this build; e.g. x86-generic-release. |
| 352 | build: Partial build string to look for; e.g. R17-1234. |
| 353 | |
| 354 | Returns: |
| 355 | Returns a list of (board, build) tuples given a partial board and build. |
| 356 | """ |
| 357 | matches = [] |
| 358 | for brd in FindMatchingBoards(static_dir, board): |
| 359 | a = [(brd, bld) for bld in |
| 360 | os.listdir(os.path.join(static_dir, brd)) if build in bld] |
| 361 | matches.extend(a) |
| 362 | return matches |
| 363 | |
| 364 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 365 | def GetLatestBuildVersion(static_dir, target, milestone=None): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 366 | """Retrieves the latest build version for a given board. |
| 367 | |
| 368 | Args: |
| 369 | static_dir: Directory where builds are served from. |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 370 | target: The build target, typically a combination of the board and the |
| 371 | type of build e.g. x86-mario-release. |
| 372 | milestone: For latest build set to None, for builds only in a specific |
| 373 | milestone set to a str of format Rxx (e.g. R16). Default: None. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 374 | |
| 375 | Returns: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 376 | If latest found, a full build string is returned e.g. R17-1234.0.0-a1-b983. |
| 377 | If no latest is found for some reason or another a '' string is returned. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 378 | |
| 379 | Raises: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 380 | DevServerUtilError: If for some reason the latest build cannot be |
| 381 | deteremined, this could be due to the dir not existing or no builds |
| 382 | being present after filtering on milestone. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 383 | """ |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 384 | target_path = os.path.join(static_dir, target) |
| 385 | if not os.path.isdir(target_path): |
| 386 | raise DevServerUtilError('Cannot find path %s' % target_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 387 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 388 | builds = [distutils.version.LooseVersion(build) for build in |
| 389 | os.listdir(target_path)] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 390 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 391 | if milestone and builds: |
| 392 | # Check if milestone Rxx is in the string representation of the build. |
| 393 | builds = filter(lambda x: milestone.upper() in str(x), builds) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 394 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 395 | if not builds: |
| 396 | raise DevServerUtilError('Could not determine build for %s' % target) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 397 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 398 | return str(max(builds)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 399 | |
| 400 | |
| 401 | def CloneBuild(static_dir, board, build, tag, force=False): |
| 402 | """Clone an official build into the developer sandbox. |
| 403 | |
| 404 | Developer sandbox directory must already exist. |
| 405 | |
| 406 | Args: |
| 407 | static_dir: Directory where builds are served from. |
| 408 | board: Fully qualified board name; e.g. x86-generic-release. |
| 409 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 410 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 411 | force: Force re-creation of build_dir even if it already exists. |
| 412 | |
| 413 | Returns: |
| 414 | The path to the new build. |
| 415 | """ |
| 416 | # Create the developer build directory. |
| 417 | dev_static_dir = os.path.join(static_dir, DEV_BUILD_PREFIX) |
| 418 | dev_build_dir = os.path.join(dev_static_dir, tag) |
| 419 | official_build_dir = os.path.join(static_dir, board, build) |
| 420 | cherrypy.log('Cloning %s -> %s' % (official_build_dir, dev_build_dir), |
| 421 | 'DEVSERVER_UTIL') |
| 422 | dev_build_exists = False |
| 423 | try: |
| 424 | AcquireLock(dev_static_dir, tag) |
| 425 | except DevServerUtilError: |
| 426 | dev_build_exists = True |
| 427 | if force: |
| 428 | dev_build_exists = False |
| 429 | ReleaseLock(dev_static_dir, tag) |
| 430 | AcquireLock(dev_static_dir, tag) |
| 431 | |
| 432 | # Make a copy of the official build, only take necessary files. |
| 433 | if not dev_build_exists: |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 434 | copy_list = [downloadable_artifact.TEST_IMAGE, |
| 435 | downloadable_artifact.ROOT_UPDATE, |
| 436 | downloadable_artifact.STATEFUL_UPDATE] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 437 | for f in copy_list: |
| 438 | shutil.copy(os.path.join(official_build_dir, f), dev_build_dir) |
| 439 | |
| 440 | return dev_build_dir |
| 441 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 442 | |
| 443 | def GetControlFile(static_dir, build, control_path): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 444 | """Attempts to pull the requested control file from the Dev Server. |
| 445 | |
| 446 | Args: |
| 447 | static_dir: Directory where builds are served from. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 448 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 449 | control_path: Path to control file on Dev Server relative to Autotest root. |
| 450 | |
| 451 | Raises: |
| 452 | DevServerUtilError: If lock can't be acquired. |
| 453 | |
| 454 | Returns: |
| 455 | Content of the requested control file. |
| 456 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 457 | # Be forgiving if the user passes in the control_path with a leading / |
| 458 | control_path = control_path.lstrip('/') |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 459 | control_path = os.path.join(static_dir, build, 'autotest', |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 460 | control_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 461 | if not SafeSandboxAccess(static_dir, control_path): |
| 462 | raise DevServerUtilError('Invaid control file "%s".' % control_path) |
| 463 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 464 | if not os.path.exists(control_path): |
| 465 | # TODO(scottz): Come up with some sort of error mechanism. |
| 466 | # crosbug.com/25040 |
| 467 | return 'Unknown control path %s' % control_path |
| 468 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 469 | with open(control_path, 'r') as control_file: |
| 470 | return control_file.read() |
| 471 | |
| 472 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 473 | def GetControlFileList(static_dir, build): |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 474 | """List all control|control. files in the specified board/build path. |
| 475 | |
| 476 | Args: |
| 477 | static_dir: Directory where builds are served from. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 478 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 479 | |
| 480 | Raises: |
| 481 | DevServerUtilError: If path is outside of sandbox. |
| 482 | |
| 483 | Returns: |
| 484 | String of each file separated by a newline. |
| 485 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 486 | autotest_dir = os.path.join(static_dir, build, 'autotest/') |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 487 | if not SafeSandboxAccess(static_dir, autotest_dir): |
| 488 | raise DevServerUtilError('Autotest dir not in sandbox "%s".' % autotest_dir) |
| 489 | |
| 490 | control_files = set() |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 491 | if not os.path.exists(autotest_dir): |
| 492 | # TODO(scottz): Come up with some sort of error mechanism. |
| 493 | # crosbug.com/25040 |
| 494 | return 'Unknown build path %s' % autotest_dir |
| 495 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 496 | for entry in os.walk(autotest_dir): |
| 497 | dir_path, _, files = entry |
| 498 | for file_entry in files: |
| 499 | if file_entry.startswith('control.') or file_entry == 'control': |
| 500 | control_files.add(os.path.join(dir_path, |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 501 | file_entry).replace(autotest_dir, '')) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 502 | |
| 503 | return '\n'.join(control_files) |
| 504 | |
| 505 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 506 | def ListAutoupdateTargets(static_dir, board, build): |
| 507 | """Returns a list of autoupdate test targets for the given board, build. |
| 508 | |
| 509 | Args: |
| 510 | static_dir: Directory where builds are served from. |
| 511 | board: Fully qualified board name; e.g. x86-generic-release. |
| 512 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 513 | |
| 514 | Returns: |
| 515 | List of autoupdate test targets; e.g. ['0.14.747.0-r2bf8859c-b2927_nton'] |
| 516 | """ |
| 517 | return os.listdir(os.path.join(static_dir, board, build, AU_BASE)) |