Chromium Code Reviews| Index: tools/telemetry/telemetry/page/profile_generator.py |
| diff --git a/tools/telemetry/telemetry/page/profile_generator.py b/tools/telemetry/telemetry/page/profile_generator.py |
| index 498a6c48f787e1e2dcedcb0e77a997caf99c05a1..08469486e64fbcd1fff5dc797c0e2b8962024fb8 100644 |
| --- a/tools/telemetry/telemetry/page/profile_generator.py |
| +++ b/tools/telemetry/telemetry/page/profile_generator.py |
| @@ -4,48 +4,91 @@ |
| """Handles generating profiles and transferring them to/from mobile devices.""" |
| +import logging |
| +import os |
| +import shutil |
| +import sys |
| +import tempfile |
| + |
| from telemetry.core import browser_options |
| +from telemetry.core import discover |
| +from telemetry.core import util |
| from telemetry.page import page_runner |
| +from telemetry.page import profile_creator |
| +from telemetry.page import test_expectations |
| + |
| + |
| +def _DiscoverProfileCreatorClasses(): |
| + parent_dir = os.path.abspath(os.path.dirname(__file__)) |
| + profile_creators_dir = os.path.abspath(os.path.join(parent_dir, |
| + os.pardir, os.pardir, os.pardir, 'perf', 'profile_creators')) |
|
tonyg
2013/08/28 00:09:58
Is this another good case for util.GetBaseDir()?
|
| + base_dir = os.path.abspath(os.path.join(profile_creators_dir, os.pardir)) |
| + |
| + profile_creators_unfiltered = discover.DiscoverClasses( |
| + profile_creators_dir, base_dir, profile_creator.ProfileCreator) |
| -def GenerateProfiles(): |
| + # Remove '_creator' suffix from keys. |
| + profile_creators = {} |
| + for test_name, test_class in profile_creators_unfiltered.iteritems(): |
| + assert test_name.endswith('_creator') |
| + test_name = test_name[:-len('_creator')] |
| + profile_creators[test_name] = test_class |
| + return profile_creators |
| + |
| +def GenerateProfiles(profile_creator_class, profile_creator_name, options): |
| """Generate a profile""" |
| - raise Exception("create command unimplemented.") |
| + expectations = test_expectations.TestExpectations() |
| + test = profile_creator_class() |
| -def UploadProfiles(): |
| - """Upload stored generated profiles to a mobile device for use by telemetry |
| - tests. |
| - """ |
| - raise Exception("upload command unimplemented.") |
| + temp_output_directory = tempfile.mkdtemp() |
| + options.output_profile_path = temp_output_directory |
| -def DownloadProfiles(): |
| - """Download generated profiles from a mobile device for future use.""" |
| - raise Exception("download command unimplemented.") |
| + results = page_runner.Run(test, test.page_set, expectations, options) |
| -def Main(): |
| - COMMANDS = [ |
| - ('create', GenerateProfiles), |
| - ('upload', UploadProfiles), |
| - ('download', DownloadProfiles) |
| - ] |
| + if results.errors or results.failures or results.skipped: |
| + logging.warning('Some pages failed or were skipped') |
| + if results.errors or results.failures: |
| + logging.warning('Failed pages:\n%s', |
| + '\n'.join(zip(*results.errors + results.failures)[0])) |
| + if results.skipped: |
| + logging.warning('Skipped pages:\n%s', '\n'.join(zip(*results.skipped)[0])) |
| + return 1 |
| - LEGAL_COMMANDS = '|'.join([x[0] for x in COMMANDS]) |
| + # Everything is a-ok, move results to final destination. |
| + generated_profiles_dir = os.path.abspath(os.path.join(util.GetBaseDir(), |
| + os.pardir, os.pardir, 'out', 'Release', 'generated_profiles')) |
| + if not os.path.exists(generated_profiles_dir): |
| + os.makedirs(generated_profiles_dir) |
| + out_path = os.path.join(generated_profiles_dir, profile_creator_name) |
| + shutil.move(temp_output_directory, out_path) |
| + sys.stderr.write("Generated profile copied to: '%s'." % out_path) |
|
tonyg
2013/08/28 00:09:58
Since this is going to stderr, let's make it clear
|
| + |
| + return 0 |
| + |
| +def Main(): |
| + profile_creators = _DiscoverProfileCreatorClasses() |
| options = browser_options.BrowserOptions() |
| - parser = options.CreateParser("%%prog <%s> <--browser=...>" % LEGAL_COMMANDS) |
| + parser = options.CreateParser("%%prog <profile_type> <--browser=...>") |
| page_runner.AddCommandLineOptions(parser) |
| - |
| _, args = parser.parse_args() |
| - if len(args) < 1: |
| - raise Exception("Must specify one of <%s>" % LEGAL_COMMANDS) |
| + # Sanity check arguments. |
| + legal_profile_creators = '|'.join(profile_creators.keys()) |
| + if len(args) != 1: |
| + raise Exception("No profile type argument specified legal values are: %s" % |
| + legal_profile_creators) |
| + |
| + if args[0] not in profile_creators.keys(): |
| + raise Exception("Invalid profile type, legal values are: %s" % |
| + legal_profile_creators) |
| if not options.browser_type: |
| raise Exception("Must specify --browser option.") |
| - commands_dict = dict(COMMANDS) |
| - if args[0] not in commands_dict.keys(): |
| - raise Exception("Unsupported command '%s', Valid options are " |
| - "%s" % (args[0], LEGAL_COMMANDS)) |
| - commands_dict[args[0]]() |
| + if options.dont_override_profile: |
| + raise Exception("Can't use existing profile when generating profile.") |
| - return 0 |
| + # Generate profile. |
| + profile_creator_class = profile_creators[args[0]] |
| + return GenerateProfiles(profile_creator_class, args[0], options) |