Mercurial > kallithea
changeset 125:2811259dc12d
Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 29 Apr 2010 01:26:20 +0200 |
parents | f8ae5c1dfae2 |
children | cfddee9d3693 |
files | pylons_app/controllers/admin.py pylons_app/controllers/error.py pylons_app/lib/utils.py |
diffstat | 3 files changed, 34 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/pylons_app/controllers/admin.py Thu Apr 29 00:29:49 2010 +0200 +++ b/pylons_app/controllers/admin.py Thu Apr 29 01:26:20 2010 +0200 @@ -5,9 +5,6 @@ from pylons_app.lib.base import BaseController, render import os -from mercurial import ui, hg -from mercurial.error import RepoError -from ConfigParser import ConfigParser from pylons_app.lib import auth from pylons_app.model.forms import LoginForm import formencode @@ -15,6 +12,7 @@ from pylons_app.model import meta from pylons_app.model.db import Users, UserLogs from webhelpers.paginate import Page +from pylons_app.lib.utils import check_repo log = logging.getLogger(__name__) class AdminController(BaseController): @@ -90,37 +88,12 @@ return render('add.html') - def _check_repo(self, repo_name): - p = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) - config_path = os.path.join(p, 'hgwebdir.config') - - cp = ConfigParser() - - cp.read(config_path) - repos_path = cp.get('paths', '/').replace("**", '') - - if not repos_path: - raise Exception('Could not read config !') - - self.repo_path = os.path.join(repos_path, repo_name) - - try: - r = hg.repository(ui.ui(), self.repo_path) - hg.verify(r) - #here we hnow that repo exists it was verified - log.info('%s repo is already created', repo_name) - raise Exception('Repo exists') - except RepoError: - log.info('%s repo is free for creation', repo_name) - #it means that there is no valid repo there... - return True - def _create_repo(self, repo_name): if repo_name in [None, '', 'add']: raise Exception('undefined repo_name of repo') - if self._check_repo(repo_name): + if check_repo(repo_name, g.base_path): log.info('creating repo %s in %s', repo_name, self.repo_path) cmd = """mkdir %s && hg init %s""" \ % (self.repo_path, self.repo_path)
--- a/pylons_app/controllers/error.py Thu Apr 29 00:29:49 2010 +0200 +++ b/pylons_app/controllers/error.py Thu Apr 29 01:26:20 2010 +0200 @@ -1,13 +1,13 @@ import logging -from paste.urlparser import PkgResourcesParser +import cgi +import os import paste.fileapp from pylons import tmpl_context as c, app_globals as g, request, config from pylons.controllers.util import forward from pylons.i18n.translation import _ from pylons_app.lib.base import BaseController, render -from pylons.middleware import error_document_template, media_path -import cgi -import os +from pylons.middleware import media_path +from pylons_app.lib.utils import check_repo log = logging.getLogger(__name__) class ErrorController(BaseController): @@ -25,7 +25,7 @@ c.repos_prefix = config['repos_name'] c.repo_name = request.environ['pylons.original_request']\ - .environ.get('PATH_INFO').split('/')[-1] + .environ.get('PATH_INFO').split('/')[1] def document(self): resp = request.environ.get('pylons.original_response') @@ -36,9 +36,11 @@ 'protocol': e.get('wsgi.url_scheme'), 'host':e.get('HTTP_HOST'), } - + + if resp.status_int == 404: - return render('/errors/error_404.html') + if check_repo(c.repo_name, g.base_path): + return render('/errors/error_404.html') c.error_message = cgi.escape(request.GET.get('code', str(resp.status))) c.error_explanation = self.get_error_explanation(resp.status_int)
--- a/pylons_app/lib/utils.py Thu Apr 29 00:29:49 2010 +0200 +++ b/pylons_app/lib/utils.py Thu Apr 29 01:26:20 2010 +0200 @@ -1,7 +1,10 @@ -from mercurial import ui, config import os import logging - +from mercurial import ui, config, hg +from mercurial.error import RepoError +log = logging.getLogger(__name__) + + def get_repo_slug(request): path_info = request.environ.get('PATH_INFO') uri_lst = path_info.split('/') @@ -26,7 +29,23 @@ repos_path[0] = '/' if not os.path.isdir(os.path.join(*repos_path)): raise Exception('Not a valid repository in %s' % paths[0][1]) - + +def check_repo(repo_name, base_path): + + repo_path = os.path.join(base_path, repo_name) + + try: + r = hg.repository(ui.ui(), repo_path) + hg.verify(r) + #here we hnow that repo exists it was verified + log.info('%s repo is already created', repo_name) + return False + #raise Exception('Repo exists') + except RepoError: + log.info('%s repo is free for creation', repo_name) + #it means that there is no valid repo there... + return True + def make_ui(path='hgwebdir.config', checkpaths=True): """ A funcion that will read python rc files and make an ui from read options @@ -34,7 +53,7 @@ @param path: path to mercurial config file """ if not os.path.isfile(path): - logging.error('Unable to read config file %s' % path) + log.error('Unable to read config file %s' % path) return False #propagated from mercurial documentation sections = [