Changeset - 2811259dc12d
[Not reviewed]
default
0 3 0
Marcin Kuzminski - 16 years ago 2010-04-29 01:26:20
marcin@python-works.com
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
3 files changed with 30 insertions and 36 deletions:
0 comments (0 inline, 0 general)
pylons_app/controllers/admin.py
Show inline comments
 
@@ -2,22 +2,20 @@ import logging
 

	
 
from pylons import request, response, session, tmpl_context as c, url, app_globals as g
 
from pylons.controllers.util import abort, redirect
 

	
 
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
 
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):
 

	
 
    def __before__(self):
 
        
 
@@ -87,41 +85,16 @@ class AdminController(BaseController):
 
        except Exception as e:
 
            c.new_repo = 'Exception when adding: %s' % new_repo
 
            c.msg = str(e)
 

	
 
        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)
 
            os.popen(cmd)
pylons_app/controllers/error.py
Show inline comments
 
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):
 
    """
 
    Generates error documents as and when they are required.
 

	
 
@@ -22,25 +22,27 @@ class ErrorController(BaseController):
 
    """
 
#
 
    def __before__(self):
 
        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')
 
        log.debug(resp.status)
 

	
 
        e = request.environ
 
        c.serv_p = r'%(protocol)s://%(host)s/' % {
 
                                                'protocol': e.get('wsgi.url_scheme'),
 
                                                'host':e.get('HTTP_HOST'),
 
                                                }
 
                
 
                        
 
        if resp.status_int == 404:
 
            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)
 

	
 
        #redirect to when error with given seconds
pylons_app/lib/utils.py
Show inline comments
 
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('/')   
 
    repo_name = uri_lst[1]
 
    return repo_name
 
@@ -24,20 +27,36 @@ def check_repo_dir(paths):
 
        repos_path = repos_path[:-1]
 
    if repos_path[0] != '/':
 
        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
 
    
 
    @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 = [
 
                'alias',
 
                'auth',
 
                'decode/encode',
0 comments (0 inline, 0 general)