| OLD | NEW | 
|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 stat | 
| 12 import sys | 12 import sys | 
| 13 import tempfile | 13 import tempfile | 
| 14 | 14 | 
| 15 from profile_creators import profile_creator | 15 from profile_creators import profile_extender | 
| 16 from telemetry.core import browser_options | 16 from telemetry.core import browser_options | 
| 17 from telemetry.core import discover | 17 from telemetry.core import discover | 
| 18 from telemetry.core import util | 18 from telemetry.core import util | 
| 19 from telemetry.user_story import user_story_runner | 19 from telemetry.user_story import user_story_runner | 
| 20 | 20 | 
| 21 | 21 | 
| 22 def _DiscoverProfileCreatorClasses(): | 22 def _DiscoverProfileExtenderClasses(): | 
| 23   profile_creators_dir = os.path.abspath(os.path.join(util.GetBaseDir(), | 23   profile_extenders_dir = os.path.abspath(os.path.join(util.GetBaseDir(), | 
| 24       os.pardir, 'perf', 'profile_creators')) | 24       os.pardir, 'perf', 'profile_creators')) | 
| 25   base_dir = os.path.abspath(os.path.join(profile_creators_dir, os.pardir)) | 25   base_dir = os.path.abspath(os.path.join(profile_extenders_dir, os.pardir)) | 
| 26 | 26 | 
| 27   profile_creators_unfiltered = discover.DiscoverClasses( | 27   profile_extenders_unfiltered = discover.DiscoverClasses( | 
| 28       profile_creators_dir, base_dir, profile_creator.ProfileCreator) | 28       profile_extenders_dir, base_dir, profile_extender.ProfileExtender) | 
| 29 | 29 | 
| 30   # Remove '_creator' suffix from keys. | 30   # Remove 'extender' suffix from keys. | 
| 31   profile_creators = {} | 31   profile_extenders = {} | 
| 32   for test_name, test_class in profile_creators_unfiltered.iteritems(): | 32   for test_name, test_class in profile_extenders_unfiltered.iteritems(): | 
| 33     assert test_name.endswith('_creator') | 33     assert test_name.endswith('_extender') | 
| 34     test_name = test_name[:-len('_creator')] | 34     test_name = test_name[:-len('_extender')] | 
| 35     profile_creators[test_name] = test_class | 35     profile_extenders[test_name] = test_class | 
| 36   return profile_creators | 36   return profile_extenders | 
| 37 | 37 | 
| 38 | 38 | 
| 39 def _IsPseudoFile(directory, paths): | 39 def _IsPseudoFile(directory, paths): | 
| 40   """Filter function for shutil.copytree() to reject socket files and symlinks | 40   """Filter function for shutil.copytree() to reject socket files and symlinks | 
| 41   since those can't be copied around on bots.""" | 41   since those can't be copied around on bots.""" | 
| 42   def IsSocket(full_path): | 42   def IsSocket(full_path): | 
| 43     """Check if a file at a given path is a socket.""" | 43     """Check if a file at a given path is a socket.""" | 
| 44     try: | 44     try: | 
| 45       if stat.S_ISSOCK(os.stat(full_path).st_mode): | 45       if stat.S_ISSOCK(os.stat(full_path).st_mode): | 
| 46         return True | 46         return True | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 57       continue | 57       continue | 
| 58     if not IsSocket(full_path) and not os.path.islink(full_path): | 58     if not IsSocket(full_path) and not os.path.islink(full_path): | 
| 59       continue | 59       continue | 
| 60 | 60 | 
| 61     logging.warning('Ignoring pseudo file: %s' % full_path) | 61     logging.warning('Ignoring pseudo file: %s' % full_path) | 
| 62     ignore_list.append(path) | 62     ignore_list.append(path) | 
| 63 | 63 | 
| 64   return ignore_list | 64   return ignore_list | 
| 65 | 65 | 
| 66 | 66 | 
| 67 def GenerateProfiles(profile_creator_class, profile_creator_name, options): | 67 def GenerateProfiles(profile_extender_class, profile_creator_name, options): | 
| 68   """Generate a profile""" | 68   """Generate a profile""" | 
| 69 | 69 | 
| 70   temp_output_directory = tempfile.mkdtemp() | 70   temp_output_directory = tempfile.mkdtemp() | 
| 71   options.output_profile_path = temp_output_directory | 71   options.output_profile_path = temp_output_directory | 
| 72 | 72 | 
| 73   profile_creator_instance = profile_creator_class() | 73   profile_creator_instance = profile_extender_class() | 
| 74   try: | 74   try: | 
| 75     profile_creator_instance.Run(options) | 75     profile_creator_instance.Run(options) | 
| 76   except Exception as e: | 76   except Exception as e: | 
| 77     logging.exception('Profile creation failed.') | 77     logging.exception('Profile creation failed.') | 
| 78     shutil.rmtree(temp_output_directory) | 78     shutil.rmtree(temp_output_directory) | 
| 79     raise e | 79     raise e | 
| 80 | 80 | 
| 81   # Everything is a-ok, move results to final destination. | 81   # Everything is a-ok, move results to final destination. | 
| 82   generated_profiles_dir = os.path.abspath(options.output_dir) | 82   generated_profiles_dir = os.path.abspath(options.output_dir) | 
| 83   if not os.path.exists(generated_profiles_dir): | 83   if not os.path.exists(generated_profiles_dir): | 
| 84     os.makedirs(generated_profiles_dir) | 84     os.makedirs(generated_profiles_dir) | 
| 85   out_path = os.path.join(generated_profiles_dir, profile_creator_name) | 85   out_path = os.path.join(generated_profiles_dir, profile_creator_name) | 
| 86   if os.path.exists(out_path): | 86   if os.path.exists(out_path): | 
| 87     shutil.rmtree(out_path) | 87     shutil.rmtree(out_path) | 
| 88 | 88 | 
| 89   shutil.copytree(temp_output_directory, out_path, ignore=_IsPseudoFile) | 89   shutil.copytree(temp_output_directory, out_path, ignore=_IsPseudoFile) | 
| 90   shutil.rmtree(temp_output_directory) | 90   shutil.rmtree(temp_output_directory) | 
| 91   sys.stderr.write("SUCCESS: Generated profile copied to: '%s'.\n" % out_path) | 91   sys.stderr.write("SUCCESS: Generated profile copied to: '%s'.\n" % out_path) | 
| 92 | 92 | 
| 93   return 0 | 93   return 0 | 
| 94 | 94 | 
| 95 | 95 | 
| 96 def AddCommandLineArgs(parser): | 96 def AddCommandLineArgs(parser): | 
| 97   user_story_runner.AddCommandLineArgs(parser) | 97   user_story_runner.AddCommandLineArgs(parser) | 
| 98 | 98 | 
| 99   profile_creators = _DiscoverProfileCreatorClasses().keys() | 99   profile_extenders = _DiscoverProfileExtenderClasses().keys() | 
| 100   legal_profile_creators = '|'.join(profile_creators) | 100   legal_profile_creators = '|'.join(profile_extenders) | 
| 101   group = optparse.OptionGroup(parser, 'Profile generation options') | 101   group = optparse.OptionGroup(parser, 'Profile generation options') | 
| 102   group.add_option('--profile-type-to-generate', | 102   group.add_option('--profile-type-to-generate', | 
| 103       dest='profile_type_to_generate', | 103       dest='profile_type_to_generate', | 
| 104       default=None, | 104       default=None, | 
| 105       help='Type of profile to generate. ' | 105       help='Type of profile to generate. ' | 
| 106            'Supported values: %s' % legal_profile_creators) | 106            'Supported values: %s' % legal_profile_creators) | 
| 107   parser.add_option_group(group) | 107   parser.add_option_group(group) | 
| 108 | 108 | 
| 109 | 109 | 
| 110 def ProcessCommandLineArgs(parser, args): | 110 def ProcessCommandLineArgs(parser, args): | 
| 111   user_story_runner.ProcessCommandLineArgs(parser, args) | 111   user_story_runner.ProcessCommandLineArgs(parser, args) | 
| 112 | 112 | 
| 113   if not args.profile_type_to_generate: | 113   if not args.profile_type_to_generate: | 
| 114     parser.error("Must specify --profile-type-to-generate option.") | 114     parser.error("Must specify --profile-type-to-generate option.") | 
| 115 | 115 | 
| 116   profile_creators = _DiscoverProfileCreatorClasses().keys() | 116   profile_extenders = _DiscoverProfileExtenderClasses().keys() | 
| 117   if args.profile_type_to_generate not in profile_creators: | 117   if args.profile_type_to_generate not in profile_extenders: | 
| 118     legal_profile_creators = '|'.join(profile_creators) | 118     legal_profile_creators = '|'.join(profile_extenders) | 
| 119     parser.error("Invalid profile type, legal values are: %s." % | 119     parser.error("Invalid profile type, legal values are: %s." % | 
| 120         legal_profile_creators) | 120         legal_profile_creators) | 
| 121 | 121 | 
| 122   if not args.browser_type: | 122   if not args.browser_type: | 
| 123     parser.error("Must specify --browser option.") | 123     parser.error("Must specify --browser option.") | 
| 124 | 124 | 
| 125   if not args.output_dir: | 125   if not args.output_dir: | 
| 126     parser.error("Must specify --output-dir option.") | 126     parser.error("Must specify --output-dir option.") | 
| 127 | 127 | 
| 128   if args.browser_options.dont_override_profile: | 128   if args.browser_options.dont_override_profile: | 
| 129     parser.error("Can't use existing profile when generating profile.") | 129     parser.error("Can't use existing profile when generating profile.") | 
| 130 | 130 | 
| 131 | 131 | 
| 132 def Main(): | 132 def Main(): | 
| 133   options = browser_options.BrowserFinderOptions() | 133   options = browser_options.BrowserFinderOptions() | 
| 134   parser = options.CreateParser( | 134   parser = options.CreateParser( | 
| 135       "%%prog <--profile-type-to-generate=...> <--browser=...> <--output-dir>") | 135       "%%prog <--profile-type-to-generate=...> <--browser=...> <--output-dir>") | 
| 136   AddCommandLineArgs(parser) | 136   AddCommandLineArgs(parser) | 
| 137   _, _ = parser.parse_args() | 137   _, _ = parser.parse_args() | 
| 138   ProcessCommandLineArgs(parser, options) | 138   ProcessCommandLineArgs(parser, options) | 
| 139 | 139 | 
| 140   # Generate profile. | 140   # Generate profile. | 
| 141   profile_creators = _DiscoverProfileCreatorClasses() | 141   profile_extenders = _DiscoverProfileExtenderClasses() | 
| 142   profile_creator_class = profile_creators[options.profile_type_to_generate] | 142   profile_extender_class = profile_extenders[options.profile_type_to_generate] | 
| 143   return GenerateProfiles(profile_creator_class, | 143   return GenerateProfiles(profile_extender_class, | 
| 144       options.profile_type_to_generate, options) | 144       options.profile_type_to_generate, options) | 
| OLD | NEW | 
|---|