utils.py
110 lines
| 3.9 KiB
| text/x-python
|
PythonLexer
r5088 | # Copyright (C) 2010-2023 RhodeCode GmbH | |||
r1 | # | |||
# 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 <http://www.gnu.org/licenses/>. | ||||
# | ||||
# 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 os | ||||
import platform | ||||
r5430 | DEFAULT_USER = 'default' | |||
r1 | ||||
def configure_vcs(config): | ||||
""" | ||||
Patch VCS config with some RhodeCode specific stuff | ||||
""" | ||||
from rhodecode.lib.vcs import conf | ||||
r2833 | import rhodecode.lib.vcs.conf.settings | |||
r1 | conf.settings.BACKENDS = { | |||
'hg': 'rhodecode.lib.vcs.backends.hg.MercurialRepository', | ||||
'git': 'rhodecode.lib.vcs.backends.git.GitRepository', | ||||
'svn': 'rhodecode.lib.vcs.backends.svn.SubversionRepository', | ||||
} | ||||
r5496 | conf.settings.HOOKS_PROTOCOL = config['vcs.hooks.protocol.v2'] | |||
r2833 | conf.settings.HOOKS_HOST = config['vcs.hooks.host'] | |||
Martin Bornhold
|
r588 | conf.settings.DEFAULT_ENCODINGS = config['default_encoding'] | ||
conf.settings.ALIASES[:] = config['vcs.backends'] | ||||
conf.settings.SVN_COMPATIBLE_VERSION = config['vcs.svn.compatible_version'] | ||||
r1 | ||||
def initialize_database(config): | ||||
r261 | from rhodecode.lib.utils2 import engine_from_config, get_encryption_key | |||
r5325 | from rhodecode.model import init_model | |||
r1 | engine = engine_from_config(config, 'sqlalchemy.db1.') | |||
r261 | init_model(engine, encryption_key=get_encryption_key(config)) | |||
r1 | ||||
r116 | def initialize_test_environment(settings, test_env=None): | |||
if test_env is None: | ||||
test_env = not int(os.environ.get('RC_NO_TMP_PATH', 0)) | ||||
Martin Bornhold
|
r215 | from rhodecode.lib.utils import ( | ||
create_test_directory, create_test_database, create_test_repositories, | ||||
create_test_index) | ||||
r116 | from rhodecode.tests import TESTS_TMP_PATH | |||
r1563 | from rhodecode.lib.vcs.backends.hg import largefiles_store | |||
r1564 | from rhodecode.lib.vcs.backends.git import lfs_store | |||
r116 | # test repos | |||
if test_env: | ||||
Martin Bornhold
|
r215 | create_test_directory(TESTS_TMP_PATH) | ||
r1563 | # large object stores | |||
create_test_directory(largefiles_store(TESTS_TMP_PATH)) | ||||
r1564 | create_test_directory(lfs_store(TESTS_TMP_PATH)) | |||
r1563 | ||||
Martin Bornhold
|
r215 | create_test_database(TESTS_TMP_PATH, settings) | ||
create_test_repositories(TESTS_TMP_PATH, settings) | ||||
Martin Bornhold
|
r214 | create_test_index(TESTS_TMP_PATH, settings) | ||
r116 | ||||
r1 | def get_vcs_server_protocol(config): | |||
Martin Bornhold
|
r588 | return config['vcs.server.protocol'] | ||
r1 | ||||
def set_instance_id(config): | ||||
r3006 | """ | |||
Sets a dynamic generated config['instance_id'] if missing or '*' | ||||
E.g instance_id = *cluster-1 or instance_id = * | ||||
""" | ||||
r1 | ||||
config['instance_id'] = config.get('instance_id') or '' | ||||
r3006 | instance_id = config['instance_id'] | |||
if instance_id.startswith('*') or not instance_id: | ||||
prefix = instance_id.lstrip('*') | ||||
r1 | _platform_id = platform.uname()[1] or 'instance' | |||
r4851 | config['instance_id'] = '{prefix}uname:{platform}-pid:{pid}'.format( | |||
prefix=prefix, | ||||
platform=_platform_id, | ||||
pid=os.getpid()) | ||||
r4332 | ||||
def get_default_user_id(): | ||||
r5060 | from sqlalchemy import text | |||
from rhodecode.model import meta | ||||
engine = meta.get_engine() | ||||
with meta.SA_Session(engine) as session: | ||||
r5430 | result = session.execute(text( | |||
"SELECT user_id from users where username = :uname" | ||||
), {'uname': DEFAULT_USER}) | ||||
r5462 | user = result.first() | |||
if not user: | ||||
raise ValueError('Unable to retrieve default user data from DB') | ||||
user_id = user[0] | ||||
r5060 | ||||
r4332 | return user_id | |||