I am writing a fabfile, and I am having trouble with a replacement. I have already checked using regex101, and it shows that my regex works (both with and without the 'extended' switch). However, when I run the fabfile, there is no replacement. The regex is:
ALLOWED_HOSTS\ ?=\ ?\[([^]]+)\]
It is supposed to match any ALLOWED_HOSTS in a Django settings.py-file, from empty definitions to multiliners. The minimum text to match is
ALLOWED_HOSTS = []
A multiline text to match could look like this:
ALLOWED_HOSTS = [
'django-stage.somedomain.com',
'django-deployment.somedomain.com',
'localhost',
]
In other words, the regex should match a string containing 'ALLOWED_HOSTS', followed by 0 or 1 spaces, followed by an equal sign '=', followed by 0 or 1 spaces, followed by a forward bracket '[', followed by any text (including line breaks), followed by a backward bracket ']'. However, when the fabfile is run, the lines above are not changed. The full function in the fabfile:
def _update_settings(source_folder, site_name):
settings_path = source_folder + '/appname/settings.py'
sed(settings_path, "DEBUG = True", "DEBUG = False")
sed(settings_path,
'ALLOWED_HOSTS\ ?=\ ?\[([^]]+)\]',
f'ALLOWED_HOSTS = ["{site_name}"]'
)
When run, the output is:
[django-stage.somedomain.com] run: sed -i.bak -r -e 's/ALLOWED_HOSTS\ ?=\ ?[([^]]+)]/ALLOWED_HOSTS = ["django-stage.somedomain.com"]/g' "$(echo /srv/django-stage.somedomain.com/source/appname/settings.py)"
Any help much appreciated.