| OLD | NEW |
| (Empty) | |
| 1 # Copyright (c) 2013 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 """ Changes builder names in the expected factory configurations. """ |
| 6 |
| 7 |
| 8 import os |
| 9 import json |
| 10 import re |
| 11 import subprocess |
| 12 import sys |
| 13 |
| 14 |
| 15 EXPECTED_DIR = os.path.join(os.path.dirname(__file__), 'expected') |
| 16 if os.name == 'nt': |
| 17 SVN = 'svn.bat' |
| 18 else: |
| 19 SVN = 'svn' |
| 20 |
| 21 |
| 22 def CheckName(builder_name): |
| 23 if not re.match('^[\w\-_\.]+$', builder_name): |
| 24 raise ValueError('"%s" is not a valid builder name.' % builder_name) |
| 25 |
| 26 |
| 27 def ExistingBuilders(): |
| 28 builders = os.listdir(EXPECTED_DIR) |
| 29 builders.sort() |
| 30 for builder_file in builders: |
| 31 if not builder_file.startswith('.'): |
| 32 yield builder_file |
| 33 |
| 34 |
| 35 def CreateMappingTemplate(mapping_file): |
| 36 mapping = {} |
| 37 for builder_file in ExistingBuilders(): |
| 38 mapping[builder_file] = '' |
| 39 with open(mapping_file, 'w') as f: |
| 40 json.dump(mapping, f, indent=4, sort_keys=True) |
| 41 print 'Created mapping template file in %s' % mapping_file |
| 42 |
| 43 |
| 44 def Rename(old_name, new_name): |
| 45 print 'Changing "%s" to "%s".' % (old_name, new_name) |
| 46 old_file_path = os.path.join(EXPECTED_DIR, old_name) |
| 47 new_file_path = os.path.join(EXPECTED_DIR, new_name) |
| 48 |
| 49 # Verify the new file names. |
| 50 CheckName(old_name) |
| 51 CheckName(new_name) |
| 52 if not os.path.isfile(old_file_path): |
| 53 raise Exception('Config file for "%s" does not exist!' % old_file_path) |
| 54 if os.path.isfile(new_file_path): |
| 55 raise Exception('Config file for "%s" already exists!' % new_file_path) |
| 56 |
| 57 # Read the old builder configuration. |
| 58 with open(old_file_path) as old_file: |
| 59 old_file_contents = old_file.read() |
| 60 |
| 61 # Use "svn mv" to create the new file so that the diff only shows the changes. |
| 62 subprocess.call([SVN, 'mv', old_file_path, new_file_path]) |
| 63 |
| 64 # Replace any instances of old_name with new_name in file_contents. |
| 65 new_file_contents = old_file_contents.replace(old_name, new_name) |
| 66 |
| 67 # Write the new builder configuration. |
| 68 with open(new_file_path, 'w') as new_file: |
| 69 new_file.write(new_file_contents) |
| 70 |
| 71 |
| 72 def main(argv): |
| 73 if len(argv) > 1: |
| 74 if argv[1] == '--create-mapping-template': |
| 75 if len(argv) == 3: |
| 76 mapping_file = argv[2] |
| 77 elif len(argv) == 2: |
| 78 mapping_file = 'mapping.json' |
| 79 else: |
| 80 raise Exception('') |
| 81 CreateMappingTemplate(mapping_file) |
| 82 return |
| 83 else: |
| 84 with open(argv[1]) as f: |
| 85 mapping = json.load(f) |
| 86 for builder_file in ExistingBuilders(): |
| 87 if not builder_file in mapping: |
| 88 raise Exception('Your mapping file contains no mapping for "%s"' % \ |
| 89 builder_file) |
| 90 else: |
| 91 print ('No mapping file provided. Next time you can provide a ' |
| 92 'JSON-formatted file which maps old names to new names.') |
| 93 mapping = {} |
| 94 for builder_file in ExistingBuilders(): |
| 95 new_name = raw_input('New name for %s: ' % builder_file) |
| 96 mapping[builder_file] = new_name |
| 97 break |
| 98 |
| 99 for old_name, new_name in mapping.iteritems(): |
| 100 Rename(old_name, new_name) |
| 101 |
| 102 |
| 103 if '__main__' == __name__: |
| 104 sys.exit(main(sys.argv)) |
| OLD | NEW |