# HG changeset patch # User Marcin Kuzminski # Date 2011-02-12 20:21:23 # Node ID 405b80e4ccd5645dbbba657a5da7fdc06006575a # Parent 59b7131c15b1175c7446f43f59387a91ae5d4480 Major refactoring, removed when possible calls to app globals. Refactored models to fetch the paths needed for scans from database directly small fixes in base diff --git a/rhodecode/config/environment.py b/rhodecode/config/environment.py --- a/rhodecode/config/environment.py +++ b/rhodecode/config/environment.py @@ -13,7 +13,7 @@ import rhodecode.lib.helpers from rhodecode.config.routing import make_map from rhodecode.lib import celerypylons -from rhodecode.lib.auth import set_available_permissions, set_base_path +from rhodecode.lib.auth import set_available_permissions from rhodecode.lib.utils import repo2db_mapper, make_ui, set_rhodecode_config from rhodecode.model import init_model from rhodecode.model.scm import ScmModel @@ -72,13 +72,11 @@ def load_environment(global_conf, app_co sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.') init_model(sa_engine_db1) - #init baseui - config['pylons.app_globals'].baseui = make_ui('db') - g = config['pylons.app_globals'] - repo2db_mapper(ScmModel().repo_scan(g.paths[0][1], g.baseui)) + repos_path = make_ui('db').configitems('paths')[0][1] + repo2db_mapper(ScmModel().repo_scan(repos_path)) set_available_permissions(config) - set_base_path(config) + config['base_path'] = repos_path set_rhodecode_config(config) # CONFIGURATION OPTIONS HERE (note: all config options will override # any Pylons config options) diff --git a/rhodecode/controllers/admin/settings.py b/rhodecode/controllers/admin/settings.py --- a/rhodecode/controllers/admin/settings.py +++ b/rhodecode/controllers/admin/settings.py @@ -31,8 +31,7 @@ import formencode from sqlalchemy import func from formencode import htmlfill -from pylons import request, session, tmpl_context as c, url, app_globals as g, \ - config +from pylons import request, session, tmpl_context as c, url, config from pylons.controllers.util import abort, redirect from pylons.i18n.translation import _ @@ -49,7 +48,7 @@ from rhodecode.model.forms import UserFo from rhodecode.model.scm import ScmModel from rhodecode.model.settings import SettingsModel from rhodecode.model.user import UserModel - +from rhodecode.model.repo import RepoModel log = logging.getLogger(__name__) @@ -105,8 +104,7 @@ class SettingsController(BaseController) if setting_id == 'mapping': rm_obsolete = request.POST.get('destroy', False) log.debug('Rescanning directories with destroy=%s', rm_obsolete) - - initial = ScmModel().repo_scan(g.paths[0][1], g.baseui) + initial = ScmModel().repo_scan() for repo_name in initial.keys(): invalidate_cache('get_repo_cached_%s' % repo_name) diff --git a/rhodecode/lib/app_globals.py b/rhodecode/lib/app_globals.py --- a/rhodecode/lib/app_globals.py +++ b/rhodecode/lib/app_globals.py @@ -2,7 +2,6 @@ from beaker.cache import CacheManager from beaker.util import parse_cache_config_options -from vcs.utils.lazy import LazyProperty class Globals(object): """Globals acts as a container for objects available throughout the @@ -18,14 +17,3 @@ class Globals(object): """ self.cache = CacheManager(**parse_cache_config_options(config)) self.available_permissions = None # propagated after init_model - self.baseui = None # propagated after init_model - - @LazyProperty - def paths(self): - if self.baseui: - return self.baseui.configitems('paths') - - @LazyProperty - def base_path(self): - if self.baseui: - return self.paths[0][1] diff --git a/rhodecode/lib/auth.py b/rhodecode/lib/auth.py --- a/rhodecode/lib/auth.py +++ b/rhodecode/lib/auth.py @@ -213,10 +213,6 @@ def set_available_permissions(config): config['available_permissions'] = [x.permission_name for x in all_perms] -def set_base_path(config): - config['base_path'] = config['pylons.app_globals'].base_path - - def fill_perms(user): """Fills user permission attribute with permissions taken from database works for permissions given for repositories, and for permissions that diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -19,11 +19,12 @@ class BaseController(WSGIController): c.rhodecode_name = config.get('rhodecode_title') c.ga_code = config.get('rhodecode_ga_code') c.repo_name = get_repo_slug(request) - c.cached_repo_list = ScmModel().get_repos() c.backends = BACKENDS.keys() self.cut_off_limit = int(config.get('cut_off_limit')) + self.sa = meta.Session() scm_model = ScmModel(self.sa) + c.cached_repo_list = scm_model.get_repos() #c.unread_journal = scm_model.get_unread_journal() if c.repo_name: diff --git a/rhodecode/lib/indexers/daemon.py b/rhodecode/lib/indexers/daemon.py --- a/rhodecode/lib/indexers/daemon.py +++ b/rhodecode/lib/indexers/daemon.py @@ -81,7 +81,7 @@ class WhooshIndexingDaemon(object): if not repo_location: raise Exception('You have to provide repositories location') - self.repo_paths = ScmModel(sa).repo_scan(self.repo_location, None) + self.repo_paths = ScmModel(sa).repo_scan(self.repo_location) if repo_list: filtered_repo_paths = {} diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -30,38 +30,30 @@ import logging import traceback from datetime import datetime +from sqlalchemy.orm import joinedload + +from vcs.utils.lazy import LazyProperty +from vcs.backends import get_backend + from rhodecode.model import BaseModel from rhodecode.model.caching_query import FromCache from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \ - Statistics, UsersGroup, UsersGroupToPerm + Statistics, UsersGroup, UsersGroupToPerm, RhodeCodeUi from rhodecode.model.user import UserModel from rhodecode.model.users_group import UsersGroupMember, UsersGroupModel -from vcs.backends import get_backend log = logging.getLogger(__name__) class RepoModel(BaseModel): - def __init__(self, sa=None): - try: - from pylons import app_globals - self._base_path = app_globals.base_path - except: - self._base_path = None - - super(RepoModel, self).__init__(sa) + @LazyProperty + def repos_path(self): + """Get's the repositories root path from database + """ - @property - def base_path(self): - if self._base_path is None: - raise Exception('Base Path is empty, try set this after' - 'class initialization when not having ' - 'app_globals available') - return self._base_path - - super(RepoModel, self).__init__() - + q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one() + return q.ui_value def get(self, repo_id, cache=False): repo = self.sa.query(Repository)\ @@ -82,6 +74,25 @@ class RepoModel(BaseModel): "get_repo_%s" % repo_name)) return repo.scalar() + + def get_full(self, repo_name, cache=False, invalidate=False): + repo = self.sa.query(Repository)\ + .options(joinedload(Repository.fork))\ + .options(joinedload(Repository.user))\ + .options(joinedload(Repository.followers))\ + .options(joinedload(Repository.repo_to_perm))\ + .options(joinedload(Repository.users_group_to_perm))\ + .filter(Repository.repo_name == repo_name)\ + + if cache: + repo = repo.options(FromCache("sql_cache_long", + "get_repo_full_%s" % repo_name)) + if invalidate: + repo.invalidate() + + return repo.scalar() + + def get_users_js(self): users = self.sa.query(User).filter(User.active == True).all() @@ -290,8 +301,8 @@ class RepoModel(BaseModel): :param alias: """ from rhodecode.lib.utils import check_repo - repo_path = os.path.join(self.base_path, repo_name) - if check_repo(repo_name, self.base_path): + repo_path = os.path.join(self.repos_path, repo_name) + if check_repo(repo_name, self.repos_path): log.info('creating repo %s in %s', repo_name, repo_path) backend = get_backend(alias) backend(repo_path, create=True) @@ -304,8 +315,8 @@ class RepoModel(BaseModel): """ log.info('renaming repo from %s to %s', old, new) - old_path = os.path.join(self.base_path, old) - new_path = os.path.join(self.base_path, new) + old_path = os.path.join(self.repos_path, old) + new_path = os.path.join(self.repos_path, new) if os.path.isdir(new_path): raise Exception('Was trying to rename to already existing dir %s', new_path) @@ -319,13 +330,13 @@ class RepoModel(BaseModel): by reverting the renames on this repository :param repo: repo object """ - rm_path = os.path.join(self.base_path, repo.repo_name) + rm_path = os.path.join(self.repos_path, repo.repo_name) log.info("Removing %s", rm_path) #disable hg/git alias = repo.repo_type shutil.move(os.path.join(rm_path, '.%s' % alias), os.path.join(rm_path, 'rm__.%s' % alias)) #disable repo - shutil.move(rm_path, os.path.join(self.base_path, 'rm__%s__%s' \ + shutil.move(rm_path, os.path.join(self.repos_path, 'rm__%s__%s' \ % (datetime.today().isoformat(), repo.repo_name)))