Index: tools/cr/cr/base/client.py |
diff --git a/tools/cr/cr/base/client.py b/tools/cr/cr/base/client.py |
index ed18733626b91b60b8f95a17e94b976218d4f48e..cb2122220b3f500d6d01cfc01a0e90e2c7893c44 100644 |
--- a/tools/cr/cr/base/client.py |
+++ b/tools/cr/cr/base/client.py |
@@ -20,10 +20,14 @@ import cr.auto.client |
# The config version currently supported. |
VERSION = 0.5 |
-# The default directory name to store config inside |
-CLIENT_CONFIG_PATH = '.cr' |
+# The default directory name to store configs inside. |
+CONFIG_PATH = '.cr' |
# The filename of the config file inside a config directory. |
-CLIENT_CONFIG_FILE = 'config.py' |
+CONFIG_FILE = 'config.py' |
+# The directory inside the config directory which contains the client config. |
+CLIENT_CONFIG_DIR = 'client' |
+# The directory inside the config directory which contains build configs. |
+BUILD_CONFIG_DIR = 'builds' |
# The format string for the header of a config file. |
CONFIG_FILE_PREFIX = """ |
# This is an autogenerated file |
@@ -74,11 +78,60 @@ def DetectClient(): |
def _GetConfigDir(use_build_dir): |
+ base_path = os.path.join(cr.context.Get('CR_CLIENT_PATH'), CONFIG_PATH) |
+ if use_build_dir: |
+ path_suffix = os.path.join(BUILD_CONFIG_DIR, cr.context.Get('CR_OUT_FULL')) |
+ else: |
+ path_suffix = CLIENT_CONFIG_DIR |
+ return os.path.realpath(os.path.join(base_path, path_suffix)) |
+ |
+ |
+def _GetDeprecatedConfigDir(use_build_dir): |
if use_build_dir: |
path = cr.context.Get('CR_BUILD_DIR') |
else: |
path = cr.context.Get('CR_CLIENT_PATH') |
- return os.path.realpath(os.path.join(path, CLIENT_CONFIG_PATH)) |
+ return os.path.realpath(os.path.join(path, CONFIG_PATH)) |
+ |
+ |
+def _GetConfigFile(config_dir): |
+ return os.path.join(config_dir, CONFIG_FILE) |
+ |
+ |
+def _MigrateAndGetConfigDir(use_build_dir): |
+ new_config_dir = _GetConfigDir(use_build_dir) |
+ new_config_file = _GetConfigFile(new_config_dir) |
+ new_config_exists = os.path.exists(new_config_file) |
+ |
+ old_config_dir = _GetDeprecatedConfigDir(use_build_dir) |
+ old_config_file = _GetConfigFile(old_config_dir) |
+ old_config_exists = os.path.exists(old_config_file) |
+ |
+ if old_config_exists: |
+ if new_config_exists: |
+ print 'Warning: Old config file %s superseded by new config file %s' % ( |
+ old_config_file, new_config_file) |
+ else: |
+ print 'Migrating config file from %s to %s...' % ( |
+ old_config_file, new_config_file) |
+ if not cr.context.dry_run: |
+ # Make the new config directory (if necessary). |
+ try: |
+ os.makedirs(new_config_dir) |
+ except OSError: |
+ if not os.path.isdir(new_config_dir): |
+ raise |
+ # Move the config file. |
+ os.rename(old_config_file, new_config_file) |
+ # Delete the old config directory (only applies to the build config). |
+ if use_build_dir: |
+ try: |
+ os.removedirs(old_config_dir) |
+ except OSError: |
+ print 'Warning: Old config directory %s could not be removed' % ( |
+ old_config_dir) |
+ |
+ return new_config_dir |
def _WriteConfig(writer, data): |
@@ -158,11 +211,11 @@ def LoadConfig(): |
""" |
# Load the root config, will help set default build dir |
- client_config_dir = _GetConfigDir(use_build_dir=False) |
+ client_config_dir = _MigrateAndGetConfigDir(use_build_dir=False) |
cr.auto.client.__path__.append(client_config_dir) |
cr.loader.Scan() |
# Now load build dir config |
- build_config_dir = _GetConfigDir(use_build_dir=True) |
+ build_config_dir = _MigrateAndGetConfigDir(use_build_dir=True) |
cr.auto.build.__path__.append(build_config_dir) |
cr.loader.Scan() |
return hasattr(cr.auto.build, 'config') |
@@ -179,7 +232,7 @@ def WriteConfig(use_build_dir, data): |
data: The key value pairs to write. |
""" |
config_dir = _GetConfigDir(use_build_dir) |
- filename = os.path.join(config_dir, CLIENT_CONFIG_FILE) |
+ filename = _GetConfigFile(config_dir) |
if cr.context.dry_run: |
print 'makedirs', config_dir |
print 'Write config to', filename |
@@ -196,6 +249,8 @@ def WriteConfig(use_build_dir, data): |
def PrintInfo(): |
print 'Selected output directory is', cr.context.Find('CR_BUILD_DIR') |
+ print 'Build config file is', _GetConfigFile(_GetConfigDir( |
+ use_build_dir=True)) |
try: |
for name in cr.auto.build.config.OVERRIDES.exported.keys(): |
print ' ', name, '=', cr.context.Get(name) |