# HG changeset patch # User Martin Bornhold # Date 2016-07-25 14:30:31 # Node ID 61adadbc135c6abf4d136ce6ce758dc4159c6e9d # Parent f9cad2b316824f924a7e6a9335bcd1b3680b278f svn: Add subscriber to generate the mod_dav_svn config on RepoGroupEvents #4082 diff --git a/rhodecode/svn_support/__init__.py b/rhodecode/svn_support/__init__.py new file mode 100644 --- /dev/null +++ b/rhodecode/svn_support/__init__.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2016-2016 RhodeCode GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License, version 3 +# (only), as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# This program is dual-licensed. If you wish to learn more about the +# RhodeCode Enterprise Edition, including its added features, Support services, +# and proprietary license terms, please see https://rhodecode.com/licenses/ + +import logging +import os + +from rhodecode import events +from rhodecode.lib.utils2 import str2bool +from rhodecode.svn_support.subscribers import generate_mod_dav_svn_config +from rhodecode.svn_support import keys + + +log = logging.getLogger(__name__) + + +def includeme(config): + settings = config.registry.settings + _sanitize_settings_and_apply_defaults(settings) + + if settings[keys.generate_config]: + log.error('Add subscriber') + config.add_subscriber( + generate_mod_dav_svn_config, events.RepoGroupEvent) + + +def _sanitize_settings_and_apply_defaults(settings): + # Convert bool settings from string to bool. + settings[keys.generate_config] = str2bool( + settings.get(keys.generate_config, 'false')) + settings[keys.list_parent_path] = str2bool( + settings.get(keys.list_parent_path, 'true')) + + # Set defaults if key not present. + settings.setdefault(keys.config_file_path, None) + settings.setdefault(keys.location_root, '/') + settings.setdefault(keys.parent_path_root, None) + + # Append path separator to paths. + settings[keys.location_root] = _append_slash( + settings[keys.location_root]) + settings[keys.parent_path_root] = _append_slash( + settings[keys.parent_path_root]) + + # Validate settings. + if settings[keys.generate_config]: + assert settings[keys.config_file_path] is not None + + +def _append_slash(path): + if isinstance(path, basestring) and not path.endswith(os.path.sep): + path += os.path.sep + return path diff --git a/rhodecode/svn_support/keys.py b/rhodecode/svn_support/keys.py new file mode 100644 --- /dev/null +++ b/rhodecode/svn_support/keys.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2016-2016 RhodeCode GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License, version 3 +# (only), as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# This program is dual-licensed. If you wish to learn more about the +# RhodeCode Enterprise Edition, including its added features, Support services, +# and proprietary license terms, please see https://rhodecode.com/licenses/ + + +# Settings keys used in the ini file. +config_file_path = 'svn.proxy.config_file_path' +generate_config = 'svn.proxy.generate_config' +list_parent_path = 'svn.proxy.list_parent_path' +location_root = 'svn.proxy.location_root' +parent_path_root = 'svn.proxy.parent_path_root' diff --git a/rhodecode/svn_support/subscribers.py b/rhodecode/svn_support/subscribers.py new file mode 100644 --- /dev/null +++ b/rhodecode/svn_support/subscribers.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2016-2016 RhodeCode GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License, version 3 +# (only), as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# This program is dual-licensed. If you wish to learn more about the +# RhodeCode Enterprise Edition, including its added features, Support services, +# and proprietary license terms, please see https://rhodecode.com/licenses/ + + +import logging + +from pyramid.renderers import render + +from rhodecode.model.db import RepoGroup +from rhodecode.svn_support import keys + + +log = logging.getLogger(__name__) + + +def generate_mod_dav_svn_config(event): + _generate(event.request) + + +def _generate(request): + settings = request.registry.settings + filepath = settings[keys.config_file_path] + repository_root = settings[keys.parent_path_root] + list_parent_path = settings[keys.list_parent_path] + location_root = settings[keys.location_root] + + # Render the configuration to string. + template = 'rhodecode:svn_support/templates/mod-dav-svn.conf.mako' + context = { + 'location_root': location_root, + 'repository_root': repository_root, + 'repo_groups': RepoGroup.get_all_repo_groups(), + 'svn_list_parent_path': list_parent_path, + } + mod_dav_svn_config = render(template, context) + + # Write configuration to file. + with open(filepath, 'w') as file_: + file_.write(mod_dav_svn_config)