You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.2 KiB

6 years ago
  1. #! /usr/bin/env python
  2. #
  3. # SCons - a Software Constructor
  4. #
  5. # Copyright (c) 2001 - 2017 The SCons Foundation
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining
  8. # a copy of this software and associated documentation files (the
  9. # "Software"), to deal in the Software without restriction, including
  10. # without limitation the rights to use, copy, modify, merge, publish,
  11. # distribute, sublicense, and/or sell copies of the Software, and to
  12. # permit persons to whom the Software is furnished to do so, subject to
  13. # the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included
  16. # in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  19. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  20. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. from __future__ import print_function
  26. __revision__ = "src/script/scons-configure-cache.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  27. __version__ = "3.0.0"
  28. __build__ = "rel_3.0.0:4395:8972f6a2f699"
  29. __buildsys__ = "ubuntu-16"
  30. __date__ = "2017/09/18 12:59:24"
  31. __developer__ = "bdbaddog"
  32. import argparse
  33. import glob
  34. import json
  35. import os
  36. def rearrange_cache_entries(current_prefix_len, new_prefix_len):
  37. print('Changing prefix length from', current_prefix_len, 'to', new_prefix_len)
  38. dirs = set()
  39. old_dirs = set()
  40. for file in glob.iglob(os.path.join('*', '*')):
  41. name = os.path.basename(file)
  42. dir = name[:current_prefix_len].upper()
  43. if dir not in old_dirs:
  44. print('Migrating', dir)
  45. old_dirs.add(dir)
  46. dir = name[:new_prefix_len].upper()
  47. if dir not in dirs:
  48. os.mkdir(dir)
  49. dirs.add(dir)
  50. os.rename(file, os.path.join(dir, name))
  51. # Now delete the original directories
  52. for dir in old_dirs:
  53. os.rmdir(dir)
  54. # This dictionary should have one entry per entry in the cache config
  55. # Each entry should have the following:
  56. # implicit - (optional) This is to allow adding a new config entry and also
  57. # changing the behaviour of the system at the same time. This
  58. # indicates the value the config entry would have had if it had been
  59. # specified.
  60. # default - The value the config entry should have if it wasn't previously
  61. # specified
  62. # command-line - parameters to pass to ArgumentParser.add_argument
  63. # converter - (optional) Function to call if it's necessary to do some work
  64. # if this configuration entry changes
  65. config_entries = {
  66. 'prefix_len' : {
  67. 'implicit' : 1,
  68. 'default' : 2 ,
  69. 'command-line' : {
  70. 'help' : 'Length of cache file name used as subdirectory prefix',
  71. 'metavar' : '<number>',
  72. 'type' : int
  73. },
  74. 'converter' : rearrange_cache_entries
  75. }
  76. }
  77. parser = argparse.ArgumentParser(
  78. description = 'Modify the configuration of an scons cache directory',
  79. epilog = '''
  80. Unless you specify an option, it will not be changed (if it is
  81. already set in the cache config), or changed to an appropriate
  82. default (it it is not set).
  83. '''
  84. )
  85. parser.add_argument('cache-dir', help='Path to scons cache directory')
  86. for param in config_entries:
  87. parser.add_argument('--' + param.replace('_', '-'),
  88. **config_entries[param]['command-line'])
  89. parser.add_argument('--version', action='version', version='%(prog)s 1.0')
  90. # Get the command line as a dict without any of the unspecified entries.
  91. args = dict([x for x in vars(parser.parse_args()).items() if x[1]])
  92. # It seems somewhat strange to me, but positional arguments don't get the -
  93. # in the name changed to _, whereas optional arguments do...
  94. os.chdir(args['cache-dir'])
  95. del args['cache-dir']
  96. if not os.path.exists('config'):
  97. # Validate the only files in the directory are directories 0-9, a-f
  98. expected = [ '{:X}'.format(x) for x in range(0, 16) ]
  99. if not set(os.listdir('.')).issubset(expected):
  100. raise RuntimeError("This doesn't look like a version 1 cache directory")
  101. config = dict()
  102. else:
  103. with open('config') as conf:
  104. config = json.load(conf)
  105. # Find any keys that aren't currently set but should be
  106. for key in config_entries:
  107. if key not in config:
  108. if 'implicit' in config_entries[key]:
  109. config[key] = config_entries[key]['implicit']
  110. else:
  111. config[key] = config_entries[key]['default']
  112. if key not in args:
  113. args[key] = config_entries[key]['default']
  114. #Now we go through each entry in args to see if it changes an existing config
  115. #setting.
  116. for key in args:
  117. if args[key] != config[key]:
  118. if 'converter' in config_entries[key]:
  119. config_entries[key]['converter'](config[key], args[key])
  120. config[key] = args[key]
  121. # and write the updated config file
  122. with open('config', 'w') as conf:
  123. json.dump(config, conf)