def apply_settings(app_name, version, settings=None): """Apply settings to a project. It searches the settings in: .deploy/settings/<app name>/ and applies them to the project: .deploy/current/<app name>/ Params app_name: Application. version: Application version. settings (optional): Settings dir to apply, if None it looks for a dir with the same name as <app_name>. Raises ValueError if the settings or the unpacked project are not found. Returns the tuple (settings_path, project_path) """ if settings is None: settings = app_name settings_path = os.path.join(get_settings_path(), settings) if not os.path.isdir(settings_path): msg = "No settings found." raise ValueError(msg) project_path = os.path.join(get_current_path(), app_name) if not os.path.isdir(project_path): msg = "No unpacked project found." raise ValueError(msg) config_name = "app.yaml" for filename in glob.iglob("{0}/*".format(settings_path)): if config_name in filename: with open(filename) as src: dst_path = os.path.join(project_path, config_name) with open(dst_path, "w") as dst: for line in src: if re.match(r"^application:", line): line = "application: {0}\n".format(app_name) elif re.match(r"^version:", line): line = "version: {0}\n".format(version) dst.write(line) else: shutil.copy(filename, project_path) previous_version = config("current_version", section=app_name) if previous_version != version: config("previous_version", section=app_name, value=previous_version) config("current_version", section=app_name, value=version) return settings_path, project_path
def fetch(app_name): """Fetch settings for a given app. Fetch the settings and stores them in .deploy/settings/<app name>/ Params app_name: Application name. Raises NoSettingsUrl if the `settings_url` param is not set in the config file. UnsupportedUrl if the url can't be handled. """ url = config("settings_url", section=app_name) if not url: msg = "You need to set `settings_url` in the config" raise NoSettingsUrl(msg) settings_path = os.path.join(get_settings_path(), app_name) if os.path.isdir(url): src = url if os.path.exists(settings_path): shutil.rmtree(settings_path) shutil.copytree(src, settings_path) else: msg = "I don't know how to fetch this url {0!r}".format(url) raise UnsupportedUrl(msg) return url, settings_path
def handle(args): section, name = args.name.split(".", 1) value = args.value delete = args.delete option = config(name=name, section=section, value=value, delete=delete) if value is None: print option
def transform_settings(app_name, filename, key, transformer): """Get the settings specified in the config file under `security` section. It looks for the comma separated list of setting names specified in the config file under the `security` section and the `secure_settings` option. Params app_name: Application name. The settings of this app are the ones parsed. filename: Name of the file holding the settings. Raises ValueError if no settings are found for that app name. Returns A list of tuples where the first element of the tuple is the setting name and second element the setting value. """ settings_path = os.path.join(get_current_path(), app_name, filename) if not os.path.isfile(settings_path): msg = "No settings found for {0!r} app".format(app_name) raise ValueError(msg) secure_settings_string = config("secure_settings", section="security") if secure_settings_string is not None: secure_settings = set(s.strip() for s in secure_settings_string.split(",")) with open(settings_path) as f: cipher_tokens = [] callback = parse_settings(cipher_tokens, secure_settings, key, transformer) tokenize.tokenize(f.readline, callback) cipher_settings = tokenize.untokenize(cipher_tokens) with open(settings_path, "w") as f: f.write(cipher_settings)
def test_get(self): "Test that we get the right value from the config file" self.assertEqual("", config("settings_url", "production"))
def test_delete_returns_previous(self): "Test that the old value is returned when setting is deleted" previous = config("settings_url", "production", delete=True) self.assertEqual("", previous)
def test_delete(self): "Test that we can delete values" config("settings_url", "production", delete=True) self.assertIsNone(config("settings_url", "production"))
def test_set_returns_previous(self): "Test that the old value is returned when new one is set" value = "url" previous = config("settings_url", "production", value) self.assertEqual("", previous)
def test_set(self): "Test that we can set new values" value = "url" config("settings_url", "production", value) self.assertEqual(value, config("settings_url", "production"))
def test_get_nonexistent(self): "Test that we get None if the setting doesn't exist" self.assertIsNone(config("nothing", "production"))