diff --git a/pylons_app/controllers/admin.py b/pylons_app/controllers/admin.py --- a/pylons_app/controllers/admin.py +++ b/pylons_app/controllers/admin.py @@ -5,9 +5,6 @@ from pylons.controllers.util import abor 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 @@ import formencode.htmlfill as htmlfill 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 @@ class AdminController(BaseController): 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) diff --git a/pylons_app/controllers/error.py b/pylons_app/controllers/error.py --- a/pylons_app/controllers/error.py +++ b/pylons_app/controllers/error.py @@ -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 @@ class ErrorController(BaseController): 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 @@ class ErrorController(BaseController): '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) diff --git a/pylons_app/lib/utils.py b/pylons_app/lib/utils.py --- a/pylons_app/lib/utils.py +++ b/pylons_app/lib/utils.py @@ -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 @@ def check_repo_dir(paths): 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 @@ def make_ui(path='hgwebdir.config', chec @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 = [