Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
|
tonyg
2014/01/29 05:10:08
2014
dtu
2014/02/13 21:49:35
Done. Fun fact, Ctrl+A in Vim increments the first
| |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 import collections | |
| 7 import fnmatch | |
| 8 import imp | |
| 9 import logging | |
| 10 import modulefinder | |
| 11 import optparse | |
| 12 import os | |
| 13 import sys | |
| 14 import zipfile | |
| 15 | |
| 16 sys.path.append(os.path.join( | |
| 17 os.path.dirname(os.path.realpath(__file__)), os.pardir, 'telemetry')) | |
| 18 from telemetry import test | |
| 19 from telemetry.core import discover | |
| 20 from telemetry.core import util | |
| 21 from telemetry.page import cloud_storage | |
| 22 | |
| 23 import path_set | |
| 24 import telemetry_bootstrap | |
| 25 | |
| 26 | |
| 27 DEPS_FILE = 'bootstrap_deps' | |
| 28 | |
| 29 | |
| 30 def _InDirectory(subdirectory, directory): | |
| 31 subdirectory = os.path.realpath(subdirectory) | |
| 32 directory = os.path.realpath(directory) | |
| 33 common_prefix = os.path.commonprefix([subdirectory, directory]) | |
|
tonyg
2014/01/29 05:10:08
This has bitten me before. It does string comparis
dtu
2014/02/13 21:49:35
Yup! It's okay in this particular use.
| |
| 34 return common_prefix == directory | |
| 35 | |
| 36 | |
| 37 def FindBootstrapDependencies(base_dir): | |
| 38 deps_file = os.path.join(base_dir, DEPS_FILE) | |
| 39 if not os.path.exists(deps_file): | |
| 40 return [] | |
| 41 deps_paths = telemetry_bootstrap.ListAllDepsPaths(deps_file) | |
| 42 return set( | |
| 43 os.path.realpath(os.path.join(util.GetChromiumSrcDir(), os.pardir, path)) | |
| 44 for path in deps_paths) | |
| 45 | |
| 46 | |
| 47 def FindPythonDependencies(module_path): | |
| 48 logging.info('Finding Python dependencies of %s' % module_path) | |
| 49 | |
| 50 # Load the module to inherit its sys.path modifications. | |
| 51 imp.load_source( | |
| 52 os.path.splitext(os.path.basename(module_path))[0], module_path) | |
| 53 | |
| 54 # Analyze the module for its imports. | |
| 55 finder = modulefinder.ModuleFinder() | |
| 56 finder.run_script(module_path) | |
| 57 | |
| 58 # Filter for only imports in Chromium. | |
| 59 for module in finder.modules.itervalues(): | |
| 60 # If it's an __init__.py, module.__path__ gives the package's folder. | |
| 61 module_path = module.__path__[0] if module.__path__ else module.__file__ | |
| 62 if not module_path: | |
| 63 continue | |
| 64 | |
| 65 module_path = os.path.realpath(module_path) | |
| 66 if not _InDirectory(module_path, util.GetChromiumSrcDir()): | |
| 67 continue | |
| 68 | |
| 69 yield module_path | |
| 70 | |
| 71 | |
| 72 def FindPageSetDependencies(base_dir): | |
| 73 logging.info('Finding page sets in %s' % base_dir) | |
| 74 | |
| 75 # Add base_dir to path so our imports relative to base_dir will work. | |
| 76 sys.path.append(base_dir) | |
| 77 tests = discover.DiscoverClasses(base_dir, base_dir, test.Test, | |
| 78 index_by_class_name=True) | |
| 79 | |
| 80 for test_class in tests.itervalues(): | |
| 81 test_obj = test_class() | |
| 82 | |
| 83 # Ensure the test's default options are set if needed. | |
| 84 parser = optparse.OptionParser() | |
| 85 test_obj.AddTestCommandLineOptions(parser) | |
| 86 options = optparse.Values() | |
| 87 for k, v in parser.get_default_values().__dict__.iteritems(): | |
| 88 options.ensure_value(k, v) | |
| 89 | |
| 90 # Page set paths are relative to their runner script, not relative to us. | |
| 91 util.GetBaseDir = lambda: base_dir | |
| 92 # Loading the page set will automatically download its Cloud Storage deps. | |
| 93 page_set = test_obj.CreatePageSet(options) | |
| 94 | |
| 95 # Add all of its serving_dirs as dependencies. | |
| 96 for serving_dir in page_set.serving_dirs: | |
| 97 yield serving_dir | |
| 98 for page in page_set: | |
| 99 if page.is_file: | |
| 100 yield page.serving_dir | |
| 101 | |
| 102 | |
| 103 def FindCloudStorageFiles(files): | |
| 104 for path in files: | |
| 105 data_path, extension = os.path.splitext(path) | |
| 106 if extension != '.sha1': | |
| 107 continue | |
| 108 | |
| 109 if (cloud_storage.GetIfChanged(cloud_storage.PUBLIC_BUCKET, data_path) or | |
| 110 cloud_storage.GetIfChanged(cloud_storage.INTERNAL_BUCKET, data_path)): | |
| 111 yield data_path | |
| 112 | |
| 113 | |
| 114 def FindExcludedFiles(files, options): | |
| 115 def CheckConditions(path, conditions): | |
| 116 for condition in conditions: | |
| 117 if condition(path): | |
| 118 return True | |
| 119 return False | |
| 120 | |
| 121 # Define some filters for files. | |
| 122 def IsHidden(path): | |
| 123 for pathname_component in path.split(os.sep): | |
| 124 if pathname_component.startswith('.'): | |
| 125 return True | |
| 126 return False | |
| 127 def IsPyc(path): | |
| 128 return os.path.splitext(path)[1] == '.pyc' | |
| 129 def IsInCloudStorage(path): | |
| 130 return os.path.exists(path + '.sha1') | |
| 131 def MatchesExcludeOptions(path): | |
| 132 for pattern in options.exclude: | |
| 133 if (fnmatch.fnmatch(path, pattern) or | |
| 134 fnmatch.fnmatch(os.path.basename(path), pattern)): | |
| 135 return True | |
| 136 return False | |
| 137 def MatchesConditions(path, conditions): | |
| 138 for condition in conditions: | |
| 139 if condition(path): | |
| 140 return True | |
| 141 return False | |
| 142 | |
| 143 # Collect filters we're going to use to exclude files. | |
| 144 exclude_conditions = [MatchesExcludeOptions, IsHidden, IsPyc] | |
| 145 if not options.include_cloud_storage_data: | |
| 146 exclude_conditions.append(IsInCloudStorage) | |
| 147 | |
| 148 # Check all the files against the filters. | |
| 149 for path in files: | |
| 150 if MatchesConditions(path, exclude_conditions): | |
| 151 yield path | |
| 152 | |
| 153 | |
| 154 def FindDependencies(paths, options): | |
| 155 # Verify arguments. | |
| 156 for path in paths: | |
| 157 if not os.path.exists(path): | |
| 158 raise ValueError('Path does not exist: %s' % path) | |
| 159 | |
| 160 dependencies = path_set.PathSet() | |
| 161 dependencies |= FindPythonDependencies(os.path.realpath(__file__)) | |
| 162 | |
| 163 # Add dependencies. | |
| 164 for path in paths: | |
| 165 base_dir = os.path.dirname(os.path.realpath(path)) | |
| 166 | |
| 167 dependencies.add(base_dir) | |
| 168 dependencies |= FindBootstrapDependencies(base_dir) | |
| 169 dependencies |= FindPythonDependencies(path) | |
| 170 if options.include_page_set_data: | |
| 171 dependencies |= FindPageSetDependencies(base_dir) | |
| 172 | |
| 173 # Find the Cloud Storage files in the existing paths. | |
| 174 if options.include_cloud_storage_data: | |
| 175 dependencies |= FindCloudStorageFiles(set(dependencies)) | |
| 176 | |
| 177 # Remove excluded files. | |
| 178 dependencies -= FindExcludedFiles(set(dependencies), options) | |
| 179 | |
| 180 return dependencies | |
| 181 | |
| 182 | |
| 183 def ZipDependencies(paths, dependencies, options): | |
| 184 base_dir = os.path.dirname(os.path.realpath(util.GetChromiumSrcDir())) | |
| 185 | |
| 186 with zipfile.ZipFile(options.zip, 'w', zipfile.ZIP_DEFLATED) as zip_file: | |
| 187 # Add dependencies to archive. | |
| 188 for path in dependencies: | |
| 189 path_in_archive = os.path.join( | |
| 190 'telemetry', os.path.relpath(path, base_dir)) | |
| 191 zip_file.write(path, path_in_archive) | |
| 192 | |
| 193 # Add symlinks to executable paths, for ease of use. | |
| 194 for path in paths: | |
| 195 relative_path = os.path.relpath(path, base_dir) | |
| 196 link_info = zipfile.ZipInfo( | |
| 197 os.path.join('telemetry', os.path.basename(path))) | |
| 198 link_info.create_system = 3 # Unix attributes. | |
| 199 # 012 is symlink, 0777 is the permission bits rwxrwxrwx. | |
| 200 link_info.external_attr = 0120777 << 16 # Octal. | |
| 201 zip_file.writestr(link_info, relative_path) | |
| 202 | |
| 203 # Add gsutil to the archive, if it's available. The gsutil in | |
| 204 # depot_tools is modified to allow authentication using prodaccess. | |
| 205 gsutil_path = cloud_storage.FindGsutil() | |
| 206 if cloud_storage.SupportsProdaccess(gsutil_path): | |
| 207 gsutil_dependencies = path_set.PathSet() | |
| 208 gsutil_dependencies.add(os.path.dirname(gsutil_path)) | |
| 209 gsutil_dependencies -= FindExcludedFiles( | |
| 210 set(gsutil_dependencies), options) | |
| 211 | |
| 212 gsutil_base_dir = os.path.join(os.path.dirname(gsutil_path), os.pardir) | |
| 213 for path in gsutil_dependencies: | |
| 214 path_in_archive = os.path.join( | |
| 215 'telemetry', os.path.relpath(util.GetTelemetryDir(), base_dir), | |
| 216 'third_party', os.path.relpath(path, gsutil_base_dir)) | |
| 217 zip_file.write(path, path_in_archive) | |
| 218 | |
| 219 | |
| 220 def ParseCommandLine(): | |
| 221 parser = optparse.OptionParser() | |
| 222 parser.add_option( | |
| 223 '-v', '--verbose', action='count', dest='verbosity', | |
| 224 help='Increase verbosity level (repeat as needed).') | |
| 225 | |
| 226 parser.add_option( | |
| 227 '-p', '--include-page-set-data', action='store_true', default=False, | |
|
tonyg
2014/01/29 05:10:08
Shouldn't this default to true?
dtu
2014/02/13 21:49:35
No, for the bots we're actually going to do False,
| |
| 228 help='Scan tests for page set data and include them.') | |
| 229 parser.add_option( | |
| 230 '-c', '--include-cloud-storage-data', action='store_true', default=False, | |
|
tonyg
2014/01/29 05:10:08
This seems dangerous and I can't think why we'd ev
dtu
2014/02/13 21:49:35
Guess not. Done.
| |
| 231 help='Scan paths for data in Cloud Storage. Download and include them.') | |
| 232 | |
| 233 parser.add_option( | |
| 234 '-e', '--exclude', action='append', default=[], | |
| 235 help='Exclude paths matching EXCLUDE. Can be used multiple times.') | |
| 236 parser.add_option( | |
| 237 '-E', '--exclude-file', action='append', | |
| 238 help='Exclude all files in EXCLUDE_FILE. EXCLUDE_FILE must be a ' | |
| 239 'newline-separated list of paths. Can be used multiple times.') | |
| 240 | |
| 241 parser.add_option( | |
| 242 '-z', '--zip', | |
| 243 help='Store files in a zip archive at ZIP.') | |
| 244 | |
| 245 options, args = parser.parse_args() | |
| 246 | |
| 247 if options.verbosity >= 2: | |
| 248 logging.getLogger().setLevel(logging.DEBUG) | |
| 249 elif options.verbosity: | |
| 250 logging.getLogger().setLevel(logging.INFO) | |
| 251 else: | |
| 252 logging.getLogger().setLevel(logging.WARNING) | |
| 253 | |
| 254 return options, args | |
| 255 | |
| 256 | |
| 257 def Main(): | |
|
tonyg
2014/01/29 05:10:08
Can this thing generate bootstrap_deps now? Since
dtu
2014/02/13 21:49:35
Unfortunately, this script can't find some depende
| |
| 258 options, paths = ParseCommandLine() | |
| 259 | |
| 260 dependencies = FindDependencies(paths, options) | |
| 261 | |
| 262 if options.zip: | |
| 263 ZipDependencies(paths, dependencies, options) | |
| 264 print 'Zip archive written to %s.' % options.zip | |
| 265 else: | |
| 266 print '\n'.join(sorted(dependencies)) | |
| 267 | |
| 268 | |
| 269 if __name__ == '__main__': | |
| 270 Main() | |
| OLD | NEW |