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