Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import datetime | |
| 6 import os | |
| 7 | |
| 8 # infra subdirectory containing tools. | |
| 9 TOOL_DIR = os.path.dirname(os.path.dirname(__file__)) | |
| 10 TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates') | |
| 11 | |
| 12 COPYRIGHT_NOTICE = """\ | |
| 13 # Copyright %s The Chromium Authors. All rights reserved. | |
| 14 # Use of this source code is governed by a BSD-style license that can be | |
| 15 # found in the LICENSE file. | |
| 16 """ % datetime.datetime.now().strftime('%Y') | |
| 17 | |
| 18 | |
| 19 def add_argparse_options(parser): # pragma: no cover | |
|
Sergey Berezin
2015/06/11 17:33:16
why no cover? I'd add at least a smoke test, just
| |
| 20 parser.add_argument( | |
| 21 'name', metavar='name', type=str, nargs=1, | |
| 22 help='The name of the new tool.') | |
| 23 | |
| 24 parser.add_argument('--base-dir', default=TOOL_DIR, | |
| 25 help='Directory where to create the tool. Default: ' | |
| 26 '%(default)s') | |
| 27 | |
| 28 | |
| 29 def generate_python_file(dirpath, filename, template, | |
| 30 template_dir=TEMPLATE_DIR, **kwargs): | |
| 31 """Generate a python file based on a template. | |
| 32 | |
| 33 This function does nothing if the target file already exists. | |
| 34 | |
| 35 Args: | |
| 36 dirpath (str): directory where to create the file. | |
| 37 filename (str): base name of file to generate. | |
| 38 template (str): name of the template file (without extension) | |
| 39 | |
| 40 Keywords Args: | |
| 41 template_dir (str): path to the directory where templates are stored. | |
| 42 kwargs (dict): passed to the template. | |
| 43 | |
| 44 Return: | |
| 45 filename (str): path to the file that has been generated. | |
| 46 """ | |
| 47 file_path = os.path.join(dirpath, filename + '.py') | |
| 48 if os.path.isfile(file_path): | |
| 49 print 'Skipping existing file %s' % file_path | |
| 50 return file_path | |
| 51 | |
| 52 if template: | |
| 53 with open(os.path.join(template_dir, template + '.template'), 'r') as f: | |
| 54 MAIN_CONTENT = f.read().format(**kwargs) | |
| 55 else: | |
| 56 MAIN_CONTENT = '' | |
| 57 | |
| 58 with open(file_path, 'w') as f: | |
| 59 f.write(COPYRIGHT_NOTICE) | |
| 60 f.write(MAIN_CONTENT) | |
| 61 return file_path | |
| 62 | |
| 63 | |
| 64 def generate_tool_files(toolname, base_dir): | |
| 65 """Generate a stub tool from template files. | |
| 66 | |
| 67 Args: | |
| 68 toolname (str): name of the tool. This is also the name of the directory | |
| 69 generated. | |
| 70 base_dir (str): path to the directory where to create the files. | |
| 71 | |
| 72 Return: | |
| 73 tool_path (str or None): directory created or None is nothing has been done. | |
| 74 """ | |
| 75 | |
| 76 if not os.path.isdir(base_dir): | |
| 77 print 'Destination directory does not exist' | |
| 78 return 1 | |
| 79 | |
| 80 tool_dir = os.path.join(base_dir, toolname) | |
| 81 if os.path.exists(tool_dir): | |
| 82 print 'Tool seems to already exists: %s\nAborting.' % tool_dir | |
| 83 return 1 | |
| 84 | |
| 85 print 'Generating %s...' % tool_dir | |
| 86 os.mkdir(tool_dir) | |
| 87 generate_python_file(tool_dir, '__init__', None) | |
| 88 generate_python_file(tool_dir, '__main__', 'main', | |
| 89 toolname=toolname, | |
| 90 Toolname=toolname.capitalize()) | |
| 91 generate_python_file(tool_dir, toolname, 'tool', | |
| 92 Toolname=toolname.capitalize()) | |
| 93 | |
| 94 test_dir = os.path.join(tool_dir, 'test') | |
| 95 os.mkdir(test_dir) | |
| 96 generate_python_file(test_dir, '__init__', None) | |
| 97 generate_python_file(test_dir, toolname + '_test', 'test', | |
| 98 toolname=toolname, | |
| 99 tested_file=toolname) | |
| 100 print 'Done.' | |
| OLD | NEW |