summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Tanner <trtanner@btinternet.com>2016-03-19 13:09:56 +0000
committerThomas Tanner <trtanner@btinternet.com>2016-03-19 13:09:56 +0000
commit045548c469469285d8cd59f7c140c94ba7b1780b (patch)
treeda4d4b1e2319412edc1a5c1f5be89d77e36a05ad
parent24855d3acfb438bf22d308a634b9e8b75c867697 (diff)
downloadscons-045548c469469285d8cd59f7c140c94ba7b1780b.tar.gz
Add some error recovery, cleanup scons-configure-cache
-rw-r--r--src/engine/SCons/CacheDir.py28
-rw-r--r--src/script/scons-configure-cache.py52
2 files changed, 56 insertions, 24 deletions
diff --git a/src/engine/SCons/CacheDir.py b/src/engine/SCons/CacheDir.py
index a9419862..aaf3c124 100644
--- a/src/engine/SCons/CacheDir.py
+++ b/src/engine/SCons/CacheDir.py
@@ -169,19 +169,35 @@ class CacheDir(object):
global warned
if self.path not in warned:
msg = "Please upgrade your cache by running " +\
- " scons-upgrade-cache.py " + self.path
+ " scons-configure-cache.py " + self.path
SCons.Warnings.warn(SCons.Warnings.CacheVersionWarning, msg)
warned[self.path] = True
else:
if not os.path.isdir(path):
- os.makedirs(path)
+ try:
+ os.makedirs(path)
+ except OSError:
+ # If someone else is trying to create the directory at
+ # the same time as me, bad things will happen
+ msg = "Failed to create cache directory " + path
+ raise SCons.Errors.EnvironmentError(msg)
+
self.config['prefix_len'] = 2
if not os.path.exists(config_file):
- with open(config_file, 'w') as config:
- self.config = json.dump(self.config, config)
+ try:
+ with open(config_file, 'w') as config:
+ self.config = json.dump(self.config, config)
+ except:
+ msg = "Failed to write cache configuration for " + path
+ raise SCons.Errors.EnvironmentError(msg)
else:
- with open(config_file) as config:
- self.config = json.load(config)
+ try:
+ with open(config_file) as config:
+ self.config = json.load(config)
+ except ValueError:
+ msg = "Failed to read cache configuration for " + path
+ raise SCons.Errors.EnvironmentError(msg)
+
def CacheDebug(self, fmt, target, cachefile):
if cache_debug != self.current_cache_debug:
diff --git a/src/script/scons-configure-cache.py b/src/script/scons-configure-cache.py
index ec3a9407..c1b7d591 100644
--- a/src/script/scons-configure-cache.py
+++ b/src/script/scons-configure-cache.py
@@ -60,6 +60,29 @@ def rearrange_cache_entries(current_prefix_len, new_prefix_len):
for dir in old_dirs:
os.rmdir(dir)
+# This dictionary should have one entry per entry in the cache config
+# Each entry should have the following:
+# implicit - (optional) This is to allow adding a new config entry and also
+# changing the behaviour of the system at the same time. This
+# indicates the value the config entry would have had if it had been
+# specified.
+# default - The value the config entry should have if it wasn't previously
+# specified
+# command-line - parameters to pass to ArgumentParser.add_argument
+# converter - (optional) Function to call if it's necessary to do some work
+# if this configuration entry changes
+config_entries = {
+ 'prefix_len' : {
+ 'implicit' : 1,
+ 'default' : 2 ,
+ 'command-line' : {
+ 'help' : 'Length of cache file name used as subdirectory prefix',
+ 'metavar' : '<number>',
+ 'type' : int
+ },
+ 'converter' : rearrange_cache_entries
+ }
+}
parser = argparse.ArgumentParser(
description = 'Modify the configuration of an scons cache directory',
epilog = '''
@@ -70,10 +93,9 @@ parser = argparse.ArgumentParser(
)
parser.add_argument('cache-dir', help='Path to scons cache directory')
-parser.add_argument('--prefix-len',
- help='Length of cache file name used as subdirectory prefix',
- metavar = '<number>',
- type=int)
+for param in config_entries:
+ parser.add_argument('--' + param.replace('_', '-'),
+ **config_entries[param]['command-line'])
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
# Get the command line as a dict without any of the unspecified entries.
@@ -84,15 +106,6 @@ args = dict(filter(lambda x: x[1], vars(parser.parse_args()).items()))
os.chdir(args['cache-dir'])
del args['cache-dir']
-# If a value isn't currently configured, this contains the way it behaves
-# currently (implied), and the way it should behave afer running this script
-# (default)
-# FIXME: I should use this to construct the parameter list and supply an
-# upgrade function
-implicit = {
- 'prefix_len' : { 'implied' : 1, 'default' : 2 }
-}
-
if not os.path.exists('config'):
# Validate the only files in the directory are directories 0-9, a-f
expected = [ '{:X}'.format(x) for x in range(0, 16) ]
@@ -104,18 +117,21 @@ else:
config = json.load(conf)
# Find any keys that aren't currently set but should be
-for key in implicit:
+for key in config_entries:
if key not in config:
- config[key] = implicit[key]['implied']
+ if 'implicit' in config_entries[key]:
+ config[key] = config_entries[key]['implicit']
+ else:
+ config[key] = config_entries[key]['default']
if key not in args:
- args[key] = implicit[key]['default']
+ args[key] = config_entries[key]['default']
#Now we go through each entry in args to see if it changes an existing config
#setting.
for key in args:
if args[key] != config[key]:
- if key == 'prefix_len':
- rearrange_cache_entries(config[key], args[key])
+ if 'converter' in config_entries[key]:
+ config_entries[key]['converter'](config[key], args[key])
config[key] = args[key]
# and write the updated config file