I've written a Python script (below) which exports a group of configuration items using drush
. It may be useful in your case (it has been in my case). Usage:
export_config_group.py -s something -m foobar
This executes drush config-list
, gets all the items whose name contains the term something
, then saves them to modules/custom/foobar/config/install
.
The script also adjusts the yml as follows:
- removes the
default_config_hash
entry when it exists;
- removes the
uuid
entry when it exists.
The script depends on ruamel.yaml to load and dump the configuration. Make sure you pip install
it in advance.
import os
import argparse
import subprocess
import ruamel.yaml
MODULES_ROOT = "/var/www/html/dm/web/modules/custom"
def main():
search_term, module, keep_uuid = parse_arguments()
module_config_path = os.path.join(MODULES_ROOT, module, 'config/install')
items = run_process(['drush', 'config-list']).splitlines()
for item in items:
if search_term in item:
print "Config item:", item
yml = run_process(['drush', 'config-get', item])
new_yml = adjust_yml(yml, keep_uuid)
full_path = os.path.join(module_config_path, item + '.yml')
with open(full_path, 'w') as f:
f.write(new_yml)
def parse_arguments():
ap = argparse.ArgumentParser(description="Export config group.")
ap.add_argument("-s", "--search", required=True, help="Search term")
ap.add_argument("-m", "--module", required=True, help="Destination module")
ap.add_argument("-u", "--uuid", help="Keep UUID",
action='store_true', default=False)
args = ap.parse_args()
return args.search, args.module, args.uuid
def run_process(params):
process = subprocess.Popen(params, stdout=subprocess.PIPE)
stdout, _ = process.communicate()
return stdout
def adjust_yml(yml, keep_uuid):
loader = ruamel.yaml.RoundTripLoader
config = ruamel.yaml.load(yml, loader, preserve_quotes=True)
remove_core_config_hash(config)
if not keep_uuid:
remove_uuid(config)
dumper = Dumper = ruamel.yaml.RoundTripDumper
return ruamel.yaml.dump(config, Dumper=dumper, indent=2, block_seq_indent=2)
def remove_core_config_hash(config):
if '_core' in config:
if 'default_config_hash' in config['_core']:
config['_core'].pop('default_config_hash')
# Also remove '_core' node if empty
if not config['_core']:
config.pop('_core')
def remove_uuid(config):
if 'uuid' in config:
config.pop('uuid')
if __name__ == "__main__":
main()