| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Handles generating profiles and transferring them to/from mobile devices.""" | 5 """Handles generating profiles and transferring them to/from mobile devices.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 import optparse | 8 import optparse |
| 9 import os | 9 import os |
| 10 import shutil | 10 import shutil |
| 11 import stat |
| 11 import sys | 12 import sys |
| 12 import tempfile | 13 import tempfile |
| 13 | 14 |
| 14 from telemetry.core import browser_options | 15 from telemetry.core import browser_options |
| 15 from telemetry.core import discover | 16 from telemetry.core import discover |
| 16 from telemetry.core import util | 17 from telemetry.core import util |
| 17 from telemetry.page import page_runner | 18 from telemetry.page import page_runner |
| 18 from telemetry.page import profile_creator | 19 from telemetry.page import profile_creator |
| 19 from telemetry.page import test_expectations | 20 from telemetry.page import test_expectations |
| 20 | 21 |
| 21 | 22 |
| 22 def _DiscoverProfileCreatorClasses(): | 23 def _DiscoverProfileCreatorClasses(): |
| 23 profile_creators_dir = os.path.abspath(os.path.join(util.GetBaseDir(), | 24 profile_creators_dir = os.path.abspath(os.path.join(util.GetBaseDir(), |
| 24 os.pardir, 'perf', 'profile_creators')) | 25 os.pardir, 'perf', 'profile_creators')) |
| 25 base_dir = os.path.abspath(os.path.join(profile_creators_dir, os.pardir)) | 26 base_dir = os.path.abspath(os.path.join(profile_creators_dir, os.pardir)) |
| 26 | 27 |
| 27 profile_creators_unfiltered = discover.DiscoverClasses( | 28 profile_creators_unfiltered = discover.DiscoverClasses( |
| 28 profile_creators_dir, base_dir, profile_creator.ProfileCreator) | 29 profile_creators_dir, base_dir, profile_creator.ProfileCreator) |
| 29 | 30 |
| 30 # Remove '_creator' suffix from keys. | 31 # Remove '_creator' suffix from keys. |
| 31 profile_creators = {} | 32 profile_creators = {} |
| 32 for test_name, test_class in profile_creators_unfiltered.iteritems(): | 33 for test_name, test_class in profile_creators_unfiltered.iteritems(): |
| 33 assert test_name.endswith('_creator') | 34 assert test_name.endswith('_creator') |
| 34 test_name = test_name[:-len('_creator')] | 35 test_name = test_name[:-len('_creator')] |
| 35 profile_creators[test_name] = test_class | 36 profile_creators[test_name] = test_class |
| 36 return profile_creators | 37 return profile_creators |
| 37 | 38 |
| 38 | 39 |
| 40 def _IsPseudoFile(directory, paths): |
| 41 """Filter function for shutil.copytree() to reject socket files and symlinks |
| 42 since those can't be copied around on bots.""" |
| 43 def IsSocket(full_path): |
| 44 """Check if a file at a given path is a socket.""" |
| 45 try: |
| 46 if stat.S_ISSOCK(os.stat(full_path).st_mode): |
| 47 return True |
| 48 except OSError: |
| 49 # Thrown if we encounter a broken symlink. |
| 50 pass |
| 51 return False |
| 52 |
| 53 ignore_list = [] |
| 54 for path in paths: |
| 55 full_path = os.path.join(directory, path) |
| 56 |
| 57 if os.path.isdir(full_path): |
| 58 continue |
| 59 if not IsSocket(full_path) and not os.path.islink(full_path): |
| 60 continue |
| 61 |
| 62 logging.warning('Ignoring pseudo file: %s' % full_path) |
| 63 ignore_list.append(path) |
| 64 |
| 65 return ignore_list |
| 66 |
| 39 def GenerateProfiles(profile_creator_class, profile_creator_name, options): | 67 def GenerateProfiles(profile_creator_class, profile_creator_name, options): |
| 40 """Generate a profile""" | 68 """Generate a profile""" |
| 41 expectations = test_expectations.TestExpectations() | 69 expectations = test_expectations.TestExpectations() |
| 42 test = profile_creator_class() | 70 test = profile_creator_class() |
| 43 | 71 |
| 44 temp_output_directory = tempfile.mkdtemp() | 72 temp_output_directory = tempfile.mkdtemp() |
| 45 options.output_profile_path = temp_output_directory | 73 options.output_profile_path = temp_output_directory |
| 46 | 74 |
| 47 results = page_runner.Run(test, test.page_set, expectations, options) | 75 results = page_runner.Run(test, test.page_set, expectations, options) |
| 48 | 76 |
| 49 if results.failures: | 77 if results.failures: |
| 50 logging.warning('Some pages failed.') | 78 logging.warning('Some pages failed.') |
| 51 logging.warning('Failed pages:\n%s', | 79 logging.warning('Failed pages:\n%s', |
| 52 '\n'.join(zip(*results.failures)[0])) | 80 '\n'.join(zip(*results.failures)[0])) |
| 53 return 1 | 81 return 1 |
| 54 | 82 |
| 55 # Everything is a-ok, move results to final destination. | 83 # Everything is a-ok, move results to final destination. |
| 56 generated_profiles_dir = os.path.abspath(options.output_dir) | 84 generated_profiles_dir = os.path.abspath(options.output_dir) |
| 57 if not os.path.exists(generated_profiles_dir): | 85 if not os.path.exists(generated_profiles_dir): |
| 58 os.makedirs(generated_profiles_dir) | 86 os.makedirs(generated_profiles_dir) |
| 59 out_path = os.path.join(generated_profiles_dir, profile_creator_name) | 87 out_path = os.path.join(generated_profiles_dir, profile_creator_name) |
| 60 if os.path.exists(out_path): | 88 if os.path.exists(out_path): |
| 61 shutil.rmtree(out_path) | 89 shutil.rmtree(out_path) |
| 62 | 90 |
| 63 # A profile may contain pseudo files like sockets which can't be copied | 91 shutil.copytree(temp_output_directory, out_path, ignore=_IsPseudoFile) |
| 64 # around by bots. | |
| 65 def IsPseudoFile(directory, paths): | |
| 66 ignore_list = [] | |
| 67 for path in paths: | |
| 68 full_path = os.path.join(directory, path) | |
| 69 if (not os.path.isfile(full_path) and | |
| 70 not os.path.isdir(full_path) and | |
| 71 not os.path.islink(full_path)): | |
| 72 logging.warning('Ignoring pseudo file: %s' % full_path) | |
| 73 ignore_list.append(path) | |
| 74 return ignore_list | |
| 75 shutil.copytree(temp_output_directory, out_path, ignore=IsPseudoFile) | |
| 76 shutil.rmtree(temp_output_directory) | 92 shutil.rmtree(temp_output_directory) |
| 77 sys.stderr.write("SUCCESS: Generated profile copied to: '%s'.\n" % out_path) | 93 sys.stderr.write("SUCCESS: Generated profile copied to: '%s'.\n" % out_path) |
| 78 | 94 |
| 79 return 0 | 95 return 0 |
| 80 | 96 |
| 81 | 97 |
| 82 def AddCommandLineArgs(parser): | 98 def AddCommandLineArgs(parser): |
| 83 page_runner.AddCommandLineArgs(parser) | 99 page_runner.AddCommandLineArgs(parser) |
| 84 | 100 |
| 85 profile_creators = _DiscoverProfileCreatorClasses().keys() | 101 profile_creators = _DiscoverProfileCreatorClasses().keys() |
| (...skipping 28 matching lines...) Expand all Loading... |
| 114 if not args.output_dir: | 130 if not args.output_dir: |
| 115 parser.error("Must specify --output-dir option.") | 131 parser.error("Must specify --output-dir option.") |
| 116 | 132 |
| 117 if args.browser_options.dont_override_profile: | 133 if args.browser_options.dont_override_profile: |
| 118 parser.error("Can't use existing profile when generating profile.") | 134 parser.error("Can't use existing profile when generating profile.") |
| 119 | 135 |
| 120 | 136 |
| 121 def Main(): | 137 def Main(): |
| 122 options = browser_options.BrowserFinderOptions() | 138 options = browser_options.BrowserFinderOptions() |
| 123 parser = options.CreateParser( | 139 parser = options.CreateParser( |
| 124 "%%prog <--profile-type-to-generate=...> <--browser=...>" | 140 "%%prog <--profile-type-to-generate=...> <--browser=...> <--output-dir>") |
| 125 " <--output-directory>") | |
| 126 AddCommandLineArgs(parser) | 141 AddCommandLineArgs(parser) |
| 127 _, _ = parser.parse_args() | 142 _, _ = parser.parse_args() |
| 128 ProcessCommandLineArgs(parser, options) | 143 ProcessCommandLineArgs(parser, options) |
| 129 | 144 |
| 130 # Generate profile. | 145 # Generate profile. |
| 131 profile_creators = _DiscoverProfileCreatorClasses() | 146 profile_creators = _DiscoverProfileCreatorClasses() |
| 132 profile_creator_class = profile_creators[options.profile_type_to_generate] | 147 profile_creator_class = profile_creators[options.profile_type_to_generate] |
| 133 return GenerateProfiles(profile_creator_class, | 148 return GenerateProfiles(profile_creator_class, |
| 134 options.profile_type_to_generate, options) | 149 options.profile_type_to_generate, options) |
| OLD | NEW |