Home | History | Annotate | Download | only in catapult_build
      1 #!/usr/bin/env python
      2 # Copyright (c) 2014 The Chromium Authors. All rights reserved.
      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 argparse
      7 import sys
      8 import os
      9 import json
     10 
     11 """
     12 Prints the source path of the provided project name.
     13 
     14 This utility loads the specified x_project.py from one of our standard
     15 module folders, constructs its project module, and prints the source paths that
     16 it uses.
     17 
     18 This is used by the node_bootstrap.js to load the tracing code into node.
     19 """
     20 
     21 def _ToUpperCamelCase(name):
     22   in_parts = name.split('_')
     23   out_parts = []
     24   for part in in_parts:
     25     out_part = part[0].upper() + part[1:]
     26     out_parts.append(out_part)
     27   return ''.join(out_parts)
     28 
     29 def _RelPathToUnixPath(p):
     30   return p.replace(os.sep, '/')
     31 
     32 def Main(args):
     33   parser = argparse.ArgumentParser(
     34       usage='%(prog)s project_name',
     35       epilog='Prints the source paths for the provided catapult project\n')
     36   parser.add_argument('--source-paths', action='store_true')
     37   parser.add_argument('--headless-test-module-filenames', action='store_true')
     38   parser.add_argument('project_name', nargs=1)
     39   args = parser.parse_args(args)
     40 
     41   catapult_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
     42                                  '..'))
     43 
     44   project_name = args.project_name[0]
     45   project_path = os.path.join(catapult_path, project_name)
     46   sys.path.append(project_path)
     47 
     48   project_module_name = project_name + '_project'
     49   try:
     50     project_module = __import__(project_module_name, fromlist=[True])
     51   except:
     52     sys.stderr.write('Could not import %s from %s' % (project_module_name,
     53                                                       project_path))
     54     return 1
     55 
     56   project_module.UpdateSysPathIfNeeded()
     57 
     58   class_name = _ToUpperCamelCase(project_name) + 'Project'
     59 
     60   try:
     61     project_class = project_module.__dict__[class_name]
     62   except:
     63     sys.stderr.write('Could not find %s in %s' % (class_name,
     64                                                   project_module_name))
     65     return 1
     66 
     67   project = project_class()
     68 
     69   if args.source_paths:
     70     print json.dumps(project.source_paths)
     71 
     72   if args.headless_test_module_filenames:
     73     headless_test_module_filenames = ['/' + _RelPathToUnixPath(x)
     74                               for x in project.FindAllD8TestModuleRelPaths()]
     75     headless_test_module_filenames.sort()
     76     print json.dumps(headless_test_module_filenames)
     77 
     78 
     79 if __name__ == '__main__':
     80   sys.exit(Main(sys.argv[1:]))