OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium 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 import download |
| 6 import logging |
| 7 import os |
| 8 from sdk_update_common import Error |
| 9 import sdk_update_common |
| 10 import sys |
| 11 import urlparse |
| 12 import urllib2 |
| 13 |
| 14 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 15 PARENT_DIR = os.path.dirname(SCRIPT_DIR) |
| 16 sys.path.append(PARENT_DIR) |
| 17 try: |
| 18 import cygtar |
| 19 except ImportError: |
| 20 # Try to find this in the Chromium repo. |
| 21 CHROME_SRC_DIR = os.path.abspath( |
| 22 os.path.join(PARENT_DIR, '..', '..', '..', '..')) |
| 23 sys.path.append(os.path.join(CHROME_SRC_DIR, 'native_client', 'build')) |
| 24 import cygtar |
| 25 |
| 26 __all__ = ['RECOMMENDED', 'SDK_TOOLS', 'UpdateDelegate', 'RealUpdateDelegate', |
| 27 'Update', 'UpdateBundleIfNeeded'] |
| 28 |
| 29 |
| 30 RECOMMENDED = 'recommended' |
| 31 SDK_TOOLS = 'sdk_tools' |
| 32 HTTP_CONTENT_LENGTH = 'Content-Length' # HTTP Header field for content length |
| 33 |
| 34 |
| 35 class UpdateDelegate(object): |
| 36 def BundleDirectoryExists(self, bundle_name): |
| 37 raise NotImplementedError() |
| 38 |
| 39 def DownloadToFile(self, url, dest_filename): |
| 40 raise NotImplementedError() |
| 41 |
| 42 def ExtractArchive(self, archive, extract_dir, repath_dir): |
| 43 raise NotImplementedError() |
| 44 |
| 45 |
| 46 class RealUpdateDelegate(UpdateDelegate): |
| 47 def __init__(self, user_data_dir, install_dir): |
| 48 UpdateDelegate.__init__(self) |
| 49 self.user_data_dir = user_data_dir |
| 50 self.install_dir = install_dir |
| 51 |
| 52 def BundleDirectoryExists(self, bundle_name): |
| 53 bundle_path = os.path.join(self.install_dir, bundle_name) |
| 54 return os.path.isdir(bundle_path) |
| 55 |
| 56 def DownloadToFile(self, url, dest_filename): |
| 57 sdk_update_common.MakeDirs(self.user_data_dir) |
| 58 dest_path = os.path.join(self.user_data_dir, dest_filename) |
| 59 out_stream = None |
| 60 url_stream = None |
| 61 try: |
| 62 out_stream = open(dest_path, 'wb') |
| 63 url_stream = download.UrlOpen(url) |
| 64 content_length = int(url_stream.info()[HTTP_CONTENT_LENGTH]) |
| 65 progress = download.MakeProgressFunction(content_length) |
| 66 sha1, size = download.DownloadAndComputeHash(url_stream, out_stream, |
| 67 progress) |
| 68 return sha1, size |
| 69 except urllib2.URLError as e: |
| 70 raise Error('Unable to read from URL "%s".\n %s' % (url, e)) |
| 71 except IOError as e: |
| 72 raise Error('Unable to write to file "%s".\n %s' % (dest_filename, e)) |
| 73 finally: |
| 74 if url_stream: |
| 75 url_stream.close() |
| 76 if out_stream: |
| 77 out_stream.close() |
| 78 |
| 79 def ExtractArchive(self, archive, extract_dir, repath_dir): |
| 80 tar_file = None |
| 81 archive_path = os.path.join(self.user_data_dir, archive) |
| 82 extract_path = os.path.join(self.install_dir, extract_dir) |
| 83 repath_path = os.path.join(self.install_dir, repath_dir) |
| 84 |
| 85 # Extract to extract_dir, usually "<bundle name>_update". |
| 86 # This way if the extraction fails, we haven't blown away the old bundle |
| 87 # (if it exists). |
| 88 sdk_update_common.RemoveDir(extract_path) |
| 89 sdk_update_common.MakeDirs(extract_path) |
| 90 curpath = os.getcwd() |
| 91 tar_file = None |
| 92 |
| 93 try: |
| 94 try: |
| 95 tar_file = cygtar.CygTar(archive_path, 'r', verbose=True) |
| 96 except Exception as e: |
| 97 raise Error('Can\'t open archive "%s".\n %s' % (archive_path, e)) |
| 98 |
| 99 try: |
| 100 os.chdir(extract_path) |
| 101 except Exception as e: |
| 102 raise Error('Unable to chdir into "%s".\n %s' % (extract_path, e)) |
| 103 |
| 104 tar_file.Extract() |
| 105 |
| 106 # Change the directory back so we can rename. |
| 107 os.chdir(curpath) |
| 108 |
| 109 # Rename from the update directory to its real name. |
| 110 sdk_update_common.RenameDir(extract_path, repath_path) |
| 111 finally: |
| 112 # Change the directory back so we can remove the update directory. |
| 113 os.chdir(curpath) |
| 114 |
| 115 # Clean up the ..._update directory on failure. |
| 116 if os.path.exists(extract_path): |
| 117 try: |
| 118 sdk_update_common.RemoveDir(extract_path) |
| 119 except Exception as e: |
| 120 logging.error('Failed to remove directory \"%s\". %s' % ( |
| 121 extract_path, e)) |
| 122 |
| 123 if tar_file: |
| 124 tar_file.Close() |
| 125 |
| 126 # Remove the archive. |
| 127 os.remove(archive_path) |
| 128 |
| 129 |
| 130 def Update(delegate, remote_manifest, local_manifest, bundle_names, force): |
| 131 valid_bundles = set([bundle.name for bundle in remote_manifest.GetBundles()]) |
| 132 requested_bundles = _GetRequestedBundlesFromArgs(remote_manifest, |
| 133 bundle_names) |
| 134 invalid_bundles = requested_bundles - valid_bundles |
| 135 if invalid_bundles: |
| 136 logging.warn('Ignoring unknown bundle(s): %s' % ( |
| 137 ', '.join(invalid_bundles))) |
| 138 requested_bundles -= invalid_bundles |
| 139 |
| 140 if SDK_TOOLS in requested_bundles: |
| 141 logging.warn('Updating sdk_tools happens automatically. ' |
| 142 'Ignoring manual update request.') |
| 143 requested_bundles.discard(SDK_TOOLS) |
| 144 |
| 145 if requested_bundles: |
| 146 for bundle_name in requested_bundles: |
| 147 logging.info('Trying to update %s' % (bundle_name,)) |
| 148 UpdateBundleIfNeeded(delegate, remote_manifest, local_manifest, |
| 149 bundle_name, force) |
| 150 else: |
| 151 logging.warn('No bundles to update.') |
| 152 |
| 153 |
| 154 def UpdateBundleIfNeeded(delegate, remote_manifest, local_manifest, |
| 155 bundle_name, force): |
| 156 bundle = remote_manifest.GetBundle(bundle_name) |
| 157 if _BundleNeedsUpdate(delegate, local_manifest, bundle): |
| 158 _UpdateBundle(delegate, bundle, local_manifest, force) |
| 159 else: |
| 160 print '%s is already up-to-date.' % (bundle.name,) |
| 161 |
| 162 |
| 163 def _GetRequestedBundlesFromArgs(remote_manifest, requested_bundles): |
| 164 requested_bundles = set(requested_bundles) |
| 165 if RECOMMENDED in requested_bundles: |
| 166 requested_bundles.discard(RECOMMENDED) |
| 167 requested_bundles |= set(_GetRecommendedBundles(remote_manifest)) |
| 168 |
| 169 return requested_bundles |
| 170 |
| 171 |
| 172 def _GetRecommendedBundles(remote_manifest): |
| 173 return [bundle for bundle in remote_manifest.GetBundles() if |
| 174 bundle.recommended] |
| 175 |
| 176 |
| 177 def _BundleNeedsUpdate(delegate, local_manifest, bundle): |
| 178 # Always update the bundle if the directory doesn't exist; |
| 179 # the user may have deleted it. |
| 180 if not delegate.BundleDirectoryExists(bundle.name): |
| 181 return True |
| 182 |
| 183 return local_manifest.BundleNeedsUpdate(bundle) |
| 184 |
| 185 |
| 186 def _UpdateBundle(delegate, bundle, local_manifest, force): |
| 187 archive = bundle.GetHostOSArchive() |
| 188 if not archive: |
| 189 logging.warn('Bundle %s does not exist for this platform.' % (bundle.name,)) |
| 190 return |
| 191 |
| 192 logging.info('Downloading bundle %s' % (bundle.name,)) |
| 193 dest_filename = _GetFilenameFromURL(archive.url) |
| 194 sha1, size = delegate.DownloadToFile(archive.url, dest_filename) |
| 195 _ValidateArchive(archive, sha1, size) |
| 196 |
| 197 logging.info('Updating bundle %s to version %s, revision %s' % ( |
| 198 bundle.name, bundle.version, bundle.revision)) |
| 199 extract_dir = bundle.name + '_update' |
| 200 repath_dir = bundle.get('repath', bundle.name) |
| 201 delegate.ExtractArchive(dest_filename, extract_dir, repath_dir) |
| 202 |
| 203 logging.info('Updating local manifest to include bundle %s' % (bundle.name)) |
| 204 local_manifest.MergeBundle(bundle) |
| 205 |
| 206 |
| 207 def _GetFilenameFromURL(url): |
| 208 _, _, path, _, _, _ = urlparse.urlparse(url) |
| 209 return path.split('/')[-1] |
| 210 |
| 211 |
| 212 def _ValidateArchive(archive, actual_sha1, actual_size): |
| 213 if actual_sha1 != archive.GetChecksum(): |
| 214 raise Error('SHA1 checksum mismatch on "%s". Expected %s but got %s' % ( |
| 215 archive.name, archive.GetChecksum(), actual_sha1)) |
| 216 if actual_size != archive.size: |
| 217 raise Error('Size mismatch on "%s". Expected %s but got %s bytes' % ( |
| 218 archive.name, archive.size, actual_size)) |
OLD | NEW |