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: |
| 127 | to_delay = delay + random.uniform(.5 * delay, 1.5 * delay) |
| 128 | # Run "gsutil cat" to retrieve the list |
| 129 | uploaded_list = gsutil_util.GSUtilRun(cmd, msg).splitlines() |
| 130 | # Check if all target artifacts are available |
| 131 | if IsAvailable(to_wait_list, uploaded_list): |
| 132 | return uploaded_list |
| 133 | cherrypy.log('Retrying in %f seconds...%s' % (to_delay, err_str)) |
| 134 | time.sleep(to_delay) |
| 135 | |
| 136 | raise DevServerUtilError('Missing %s for %s.' % (err_str, archive_url)) |
| 137 | |
| 138 | |
| 139 | def GatherArtifactDownloads(main_staging_dir, archive_url, build, build_dir, |
| 140 | timeout=600, delay=10): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 141 | """Generates artifacts that we mean to download and install for autotest. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 142 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 143 | 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] | 144 | artifacts are instances of downloadable_artifact.DownloadableArtifact. |
| 145 | |
| 146 | Note, these artifacts can be downloaded asynchronously iff |
| 147 | !artifact.Synchronous(). |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 148 | """ |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame^] | 149 | |
| 150 | # Wait up to 10 minutes for the full payload to be uploaded because we |
| 151 | # do not know the exact name of the full payload. |
| 152 | |
| 153 | # We also wait for 'autotest.tar' because we do not know what type of |
| 154 | # autotest tarballs (tar or tar.bz2) is available |
| 155 | # (crosbug.com/32312). This dependency can be removed once all |
| 156 | # branches move to the new 'tar' format. |
| 157 | to_wait_list = ['_full_', 'autotest.tar'] |
| 158 | err_str = 'full payload or autotest tarball' |
| 159 | uploaded_list = WaitUntilAvailable(to_wait_list, archive_url, err_str, |
| 160 | timeout=600) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 161 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 162 | # First we gather the urls/paths for the update payloads. |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame^] | 163 | full_url, nton_url, mton_url = ParsePayloadList(archive_url, uploaded_list) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 164 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 165 | full_payload = os.path.join(build_dir, downloadable_artifact.ROOT_UPDATE) |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 166 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 167 | artifacts = [] |
| 168 | artifacts.append(downloadable_artifact.DownloadableArtifact(full_url, |
| 169 | main_staging_dir, full_payload, synchronous=True)) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 170 | |
| 171 | if nton_url: |
| 172 | nton_payload = os.path.join(build_dir, AU_BASE, build + NTON_DIR_SUFFIX, |
| 173 | downloadable_artifact.ROOT_UPDATE) |
| 174 | artifacts.append(downloadable_artifact.AUTestPayload(nton_url, |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 175 | main_staging_dir, nton_payload)) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 176 | |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 177 | if mton_url: |
| 178 | mton_payload = os.path.join(build_dir, AU_BASE, build + MTON_DIR_SUFFIX, |
| 179 | downloadable_artifact.ROOT_UPDATE) |
| 180 | artifacts.append(downloadable_artifact.AUTestPayload( |
| 181 | mton_url, main_staging_dir, mton_payload)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 182 | |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 183 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame^] | 184 | # Gather information about autotest tarballs. Use autotest.tar if available. |
| 185 | if downloadable_artifact.AUTOTEST_PACKAGE in uploaded_list: |
| 186 | autotest_url = '%s/%s' % (archive_url, |
| 187 | downloadable_artifact.AUTOTEST_PACKAGE) |
| 188 | else: |
| 189 | # Use autotest.tar.bz for backward compatibility. This can be |
| 190 | # removed once all branches start using "autotest.tar" |
| 191 | autotest_url = '%s/%s' % (archive_url, |
| 192 | downloadable_artifact.AUTOTEST_ZIPPED_PACKAGE) |
| 193 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 194 | # Next we gather the miscellaneous payloads. |
| 195 | stateful_url = archive_url + '/' + downloadable_artifact.STATEFUL_UPDATE |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 196 | test_suites_url = (archive_url + '/' + |
| 197 | downloadable_artifact.TEST_SUITES_PACKAGE) |
| 198 | |
| 199 | stateful_payload = os.path.join(build_dir, |
| 200 | downloadable_artifact.STATEFUL_UPDATE) |
| 201 | |
| 202 | artifacts.append(downloadable_artifact.DownloadableArtifact( |
| 203 | stateful_url, main_staging_dir, stateful_payload, synchronous=True)) |
| 204 | artifacts.append(downloadable_artifact.AutotestTarball( |
| 205 | autotest_url, main_staging_dir, build_dir)) |
| 206 | artifacts.append(downloadable_artifact.Tarball( |
| 207 | test_suites_url, main_staging_dir, build_dir, synchronous=True)) |
| 208 | return artifacts |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 209 | |
| 210 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 211 | def GatherSymbolArtifactDownloads(temp_download_dir, archive_url, staging_dir, |
| 212 | timeout=600, delay=10): |
| 213 | """Generates debug symbol artifacts that we mean to download and stage. |
| 214 | |
| 215 | This method generates the list of artifacts we will need to |
| 216 | symbolicate crash dumps that occur during autotest runs. These |
| 217 | artifacts are instances of downloadable_artifact.DownloadableArtifact. |
| 218 | |
| 219 | This will poll google storage until the debug symbol artifact becomes |
| 220 | available, or until the 10 minute timeout is up. |
| 221 | |
| 222 | @param temp_download_dir: the tempdir into which we're downloading artifacts |
| 223 | prior to staging them. |
| 224 | @param archive_url: the google storage url of the bucket where the debug |
| 225 | symbols for the desired build are stored. |
| 226 | @param staging_dir: the dir into which to stage the symbols |
| 227 | |
| 228 | @return an iterable of one DebugTarball pointing to the right debug symbols. |
| 229 | This is an iterable so that it's similar to GatherArtifactDownloads. |
| 230 | Also, it's possible that someday we might have more than one. |
| 231 | """ |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame^] | 232 | |
| 233 | # Wait up to 10 minutes for the debug symbols to be uploaded. |
| 234 | to_wait_list = [downloadable_artifact.DEBUG_SYMBOLS] |
| 235 | err_str = 'debug symbols' |
| 236 | WaitUntilAvailable(to_wait_list, archive_url, err_str, timeout=timeout, |
| 237 | delay=delay) |
| 238 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 239 | symbol_url = archive_url + '/' + downloadable_artifact.DEBUG_SYMBOLS |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 240 | return [downloadable_artifact.DebugTarball(symbol_url, temp_download_dir, |
| 241 | staging_dir)] |
| 242 | |
| 243 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 244 | def PrepareBuildDirectory(build_dir): |
| 245 | """Preliminary staging of installation directory for build. |
Scott Zawalski | 51ccf9e | 2012-03-28 08:16:01 -0700 | [diff] [blame] | 246 | |
| 247 | Args: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 248 | build_dir: Directory to install build components into. |
| 249 | """ |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 250 | if not os.path.isdir(build_dir): |
| 251 | os.path.makedirs(build_dir) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 252 | |
| 253 | # Create blank chromiumos_test_image.bin. Otherwise the Dev Server will |
| 254 | # try to rebuild it unnecessarily. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 255 | test_image = os.path.join(build_dir, downloadable_artifact.TEST_IMAGE) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 256 | open(test_image, 'a').close() |
| 257 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 258 | |
| 259 | def SafeSandboxAccess(static_dir, path): |
| 260 | """Verify that the path is in static_dir. |
| 261 | |
| 262 | Args: |
| 263 | static_dir: Directory where builds are served from. |
| 264 | path: Path to verify. |
| 265 | |
| 266 | Returns: |
| 267 | True if path is in static_dir, False otherwise |
| 268 | """ |
| 269 | static_dir = os.path.realpath(static_dir) |
| 270 | path = os.path.realpath(path) |
| 271 | return (path.startswith(static_dir) and path != static_dir) |
| 272 | |
| 273 | |
| 274 | def AcquireLock(static_dir, tag): |
| 275 | """Acquires a lock for a given tag. |
| 276 | |
| 277 | Creates a directory for the specified tag, telling other |
| 278 | components the resource/task represented by the tag is unavailable. |
| 279 | |
| 280 | Args: |
| 281 | static_dir: Directory where builds are served from. |
| 282 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 283 | |
| 284 | Returns: |
| 285 | Path to the created directory or None if creation failed. |
| 286 | |
| 287 | Raises: |
| 288 | DevServerUtilError: If lock can't be acquired. |
| 289 | """ |
| 290 | build_dir = os.path.join(static_dir, tag) |
| 291 | if not SafeSandboxAccess(static_dir, build_dir): |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 292 | raise DevServerUtilError('Invalid tag "%s".' % tag) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 293 | |
| 294 | try: |
| 295 | os.makedirs(build_dir) |
| 296 | except OSError, e: |
| 297 | if e.errno == errno.EEXIST: |
| 298 | raise DevServerUtilError(str(e)) |
| 299 | else: |
| 300 | raise |
| 301 | |
| 302 | return build_dir |
| 303 | |
| 304 | |
| 305 | def ReleaseLock(static_dir, tag): |
| 306 | """Releases the lock for a given tag. Removes lock directory content. |
| 307 | |
| 308 | Args: |
| 309 | static_dir: Directory where builds are served from. |
| 310 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 311 | |
| 312 | Raises: |
| 313 | DevServerUtilError: If lock can't be released. |
| 314 | """ |
| 315 | build_dir = os.path.join(static_dir, tag) |
| 316 | if not SafeSandboxAccess(static_dir, build_dir): |
| 317 | raise DevServerUtilError('Invaid tag "%s".' % tag) |
| 318 | |
| 319 | shutil.rmtree(build_dir) |
| 320 | |
| 321 | |
| 322 | def FindMatchingBoards(static_dir, board): |
| 323 | """Returns a list of boards given a partial board name. |
| 324 | |
| 325 | Args: |
| 326 | static_dir: Directory where builds are served from. |
| 327 | board: Partial board name for this build; e.g. x86-generic. |
| 328 | |
| 329 | Returns: |
| 330 | Returns a list of boards given a partial board. |
| 331 | """ |
| 332 | return [brd for brd in os.listdir(static_dir) if board in brd] |
| 333 | |
| 334 | |
| 335 | def FindMatchingBuilds(static_dir, board, build): |
| 336 | """Returns a list of matching builds given a board and partial build. |
| 337 | |
| 338 | Args: |
| 339 | static_dir: Directory where builds are served from. |
| 340 | board: Partial board name for this build; e.g. x86-generic-release. |
| 341 | build: Partial build string to look for; e.g. R17-1234. |
| 342 | |
| 343 | Returns: |
| 344 | Returns a list of (board, build) tuples given a partial board and build. |
| 345 | """ |
| 346 | matches = [] |
| 347 | for brd in FindMatchingBoards(static_dir, board): |
| 348 | a = [(brd, bld) for bld in |
| 349 | os.listdir(os.path.join(static_dir, brd)) if build in bld] |
| 350 | matches.extend(a) |
| 351 | return matches |
| 352 | |
| 353 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 354 | def GetLatestBuildVersion(static_dir, target, milestone=None): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 355 | """Retrieves the latest build version for a given board. |
| 356 | |
| 357 | Args: |
| 358 | static_dir: Directory where builds are served from. |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 359 | target: The build target, typically a combination of the board and the |
| 360 | type of build e.g. x86-mario-release. |
| 361 | milestone: For latest build set to None, for builds only in a specific |
| 362 | 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] | 363 | |
| 364 | Returns: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 365 | If latest found, a full build string is returned e.g. R17-1234.0.0-a1-b983. |
| 366 | 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] | 367 | |
| 368 | Raises: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 369 | DevServerUtilError: If for some reason the latest build cannot be |
| 370 | deteremined, this could be due to the dir not existing or no builds |
| 371 | being present after filtering on milestone. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 372 | """ |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 373 | target_path = os.path.join(static_dir, target) |
| 374 | if not os.path.isdir(target_path): |
| 375 | raise DevServerUtilError('Cannot find path %s' % target_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 376 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 377 | builds = [distutils.version.LooseVersion(build) for build in |
| 378 | os.listdir(target_path)] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 379 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 380 | if milestone and builds: |
| 381 | # Check if milestone Rxx is in the string representation of the build. |
| 382 | builds = filter(lambda x: milestone.upper() in str(x), builds) |
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 | if not builds: |
| 385 | raise DevServerUtilError('Could not determine build for %s' % target) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 386 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 387 | return str(max(builds)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 388 | |
| 389 | |
| 390 | def CloneBuild(static_dir, board, build, tag, force=False): |
| 391 | """Clone an official build into the developer sandbox. |
| 392 | |
| 393 | Developer sandbox directory must already exist. |
| 394 | |
| 395 | Args: |
| 396 | static_dir: Directory where builds are served from. |
| 397 | board: Fully qualified board name; e.g. x86-generic-release. |
| 398 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 399 | tag: Unique resource/task identifier. Use '/' for nested tags. |
| 400 | force: Force re-creation of build_dir even if it already exists. |
| 401 | |
| 402 | Returns: |
| 403 | The path to the new build. |
| 404 | """ |
| 405 | # Create the developer build directory. |
| 406 | dev_static_dir = os.path.join(static_dir, DEV_BUILD_PREFIX) |
| 407 | dev_build_dir = os.path.join(dev_static_dir, tag) |
| 408 | official_build_dir = os.path.join(static_dir, board, build) |
| 409 | cherrypy.log('Cloning %s -> %s' % (official_build_dir, dev_build_dir), |
| 410 | 'DEVSERVER_UTIL') |
| 411 | dev_build_exists = False |
| 412 | try: |
| 413 | AcquireLock(dev_static_dir, tag) |
| 414 | except DevServerUtilError: |
| 415 | dev_build_exists = True |
| 416 | if force: |
| 417 | dev_build_exists = False |
| 418 | ReleaseLock(dev_static_dir, tag) |
| 419 | AcquireLock(dev_static_dir, tag) |
| 420 | |
| 421 | # Make a copy of the official build, only take necessary files. |
| 422 | if not dev_build_exists: |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 423 | copy_list = [downloadable_artifact.TEST_IMAGE, |
| 424 | downloadable_artifact.ROOT_UPDATE, |
| 425 | downloadable_artifact.STATEFUL_UPDATE] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 426 | for f in copy_list: |
| 427 | shutil.copy(os.path.join(official_build_dir, f), dev_build_dir) |
| 428 | |
| 429 | return dev_build_dir |
| 430 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 431 | |
| 432 | def GetControlFile(static_dir, build, control_path): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 433 | """Attempts to pull the requested control file from the Dev Server. |
| 434 | |
| 435 | Args: |
| 436 | static_dir: Directory where builds are served from. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 437 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 438 | control_path: Path to control file on Dev Server relative to Autotest root. |
| 439 | |
| 440 | Raises: |
| 441 | DevServerUtilError: If lock can't be acquired. |
| 442 | |
| 443 | Returns: |
| 444 | Content of the requested control file. |
| 445 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 446 | # Be forgiving if the user passes in the control_path with a leading / |
| 447 | control_path = control_path.lstrip('/') |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 448 | control_path = os.path.join(static_dir, build, 'autotest', |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 449 | control_path) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 450 | if not SafeSandboxAccess(static_dir, control_path): |
| 451 | raise DevServerUtilError('Invaid control file "%s".' % control_path) |
| 452 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 453 | if not os.path.exists(control_path): |
| 454 | # TODO(scottz): Come up with some sort of error mechanism. |
| 455 | # crosbug.com/25040 |
| 456 | return 'Unknown control path %s' % control_path |
| 457 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 458 | with open(control_path, 'r') as control_file: |
| 459 | return control_file.read() |
| 460 | |
| 461 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 462 | def GetControlFileList(static_dir, build): |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 463 | """List all control|control. files in the specified board/build path. |
| 464 | |
| 465 | Args: |
| 466 | static_dir: Directory where builds are served from. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 467 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 468 | |
| 469 | Raises: |
| 470 | DevServerUtilError: If path is outside of sandbox. |
| 471 | |
| 472 | Returns: |
| 473 | String of each file separated by a newline. |
| 474 | """ |
Scott Zawalski | 1572d15 | 2012-01-16 14:36:02 -0500 | [diff] [blame] | 475 | autotest_dir = os.path.join(static_dir, build, 'autotest/') |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 476 | if not SafeSandboxAccess(static_dir, autotest_dir): |
| 477 | raise DevServerUtilError('Autotest dir not in sandbox "%s".' % autotest_dir) |
| 478 | |
| 479 | control_files = set() |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 480 | if not os.path.exists(autotest_dir): |
| 481 | # TODO(scottz): Come up with some sort of error mechanism. |
| 482 | # crosbug.com/25040 |
| 483 | return 'Unknown build path %s' % autotest_dir |
| 484 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 485 | for entry in os.walk(autotest_dir): |
| 486 | dir_path, _, files = entry |
| 487 | for file_entry in files: |
| 488 | if file_entry.startswith('control.') or file_entry == 'control': |
| 489 | control_files.add(os.path.join(dir_path, |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 490 | file_entry).replace(autotest_dir, '')) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 491 | |
| 492 | return '\n'.join(control_files) |
| 493 | |
| 494 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 495 | def ListAutoupdateTargets(static_dir, board, build): |
| 496 | """Returns a list of autoupdate test targets for the given board, build. |
| 497 | |
| 498 | Args: |
| 499 | static_dir: Directory where builds are served from. |
| 500 | board: Fully qualified board name; e.g. x86-generic-release. |
| 501 | build: Fully qualified build string; e.g. R17-1234.0.0-a1-b983. |
| 502 | |
| 503 | Returns: |
| 504 | List of autoupdate test targets; e.g. ['0.14.747.0-r2bf8859c-b2927_nton'] |
| 505 | """ |
| 506 | return os.listdir(os.path.join(static_dir, board, build, AU_BASE)) |