Changeset - 238486bb71ab
[Not reviewed]
beta
0 9 0
Marcin Kuzminski - 13 years ago 2013-03-24 03:07:04
marcin@python-works.com
Switched handling of RhodeCode extra params in consistent way
- use only os.environ for both hg and git
- hacking ui objects was bad
- os.environ allows easier to call rhodecode hooks externally
9 files changed with 74 insertions and 160 deletions:
0 comments (0 inline, 0 general)
rhodecode/lib/hooks.py
Show inline comments
 
@@ -23,23 +23,24 @@
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
import os
 
import sys
 
import time
 
import binascii
 
import traceback
 
from inspect import isfunction
 

	
 
from mercurial.scmutil import revrange
 
from mercurial.node import nullrev
 

	
 
from rhodecode.lib import helpers as h
 
from rhodecode.lib.utils import action_logger
 
from rhodecode.lib.vcs.backends.base import EmptyChangeset
 
from rhodecode.lib.compat import json
 
from rhodecode.lib.exceptions import HTTPLockedRC
 
from rhodecode.lib.utils2 import safe_str
 
from rhodecode.lib.utils2 import safe_str, _extract_extras
 
from rhodecode.model.db import Repository, User
 

	
 

	
 
def _get_scm_size(alias, root_path):
 

	
 
    if not alias.startswith('.'):
 
@@ -88,70 +89,36 @@ def repo_size(ui, repo, hooktype=None, *
 
    sys.stdout.write(msg)
 

	
 

	
 
def pre_push(ui, repo, **kwargs):
 
    # pre push function, currently used to ban pushing when
 
    # repository is locked
 
    try:
 
        rc_extras = json.loads(os.environ.get('RC_SCM_DATA', "{}"))
 
    except:
 
        rc_extras = {}
 
    extras = dict(repo.ui.configitems('rhodecode_extras'))
 
    ex = _extract_extras()
 

	
 
    if 'username' in extras:
 
        username = extras['username']
 
        repository = extras['repository']
 
        scm = extras['scm']
 
        locked_by = extras['locked_by']
 
    elif 'username' in rc_extras:
 
        username = rc_extras['username']
 
        repository = rc_extras['repository']
 
        scm = rc_extras['scm']
 
        locked_by = rc_extras['locked_by']
 
    else:
 
        raise Exception('Missing data in repo.ui and os.environ')
 

	
 
    usr = User.get_by_username(username)
 
    if locked_by[0] and usr.user_id != int(locked_by[0]):
 
        locked_by = User.get(locked_by[0]).username
 
    usr = User.get_by_username(ex.username)
 
    if ex.locked_by[0] and usr.user_id != int(ex.locked_by[0]):
 
        locked_by = User.get(ex.locked_by[0]).username
 
        # this exception is interpreted in git/hg middlewares and based
 
        # on that proper return code is server to client
 
        _http_ret = HTTPLockedRC(repository, locked_by)
 
        _http_ret = HTTPLockedRC(ex.repository, locked_by)
 
        if str(_http_ret.code).startswith('2'):
 
            #2xx Codes don't raise exceptions
 
            sys.stdout.write(_http_ret.title)
 
        else:
 
            raise _http_ret
 

	
 

	
 
def pre_pull(ui, repo, **kwargs):
 
    # pre push function, currently used to ban pushing when
 
    # repository is locked
 
    try:
 
        rc_extras = json.loads(os.environ.get('RC_SCM_DATA', "{}"))
 
    except:
 
        rc_extras = {}
 
    extras = dict(repo.ui.configitems('rhodecode_extras'))
 
    if 'username' in extras:
 
        username = extras['username']
 
        repository = extras['repository']
 
        scm = extras['scm']
 
        locked_by = extras['locked_by']
 
    elif 'username' in rc_extras:
 
        username = rc_extras['username']
 
        repository = rc_extras['repository']
 
        scm = rc_extras['scm']
 
        locked_by = rc_extras['locked_by']
 
    else:
 
        raise Exception('Missing data in repo.ui and os.environ')
 

	
 
    if locked_by[0]:
 
        locked_by = User.get(locked_by[0]).username
 
    ex = _extract_extras()
 
    if ex.locked_by[0]:
 
        locked_by = User.get(ex.locked_by[0]).username
 
        # this exception is interpreted in git/hg middlewares and based
 
        # on that proper return code is server to client
 
        _http_ret = HTTPLockedRC(repository, locked_by)
 
        _http_ret = HTTPLockedRC(ex.repository, locked_by)
 
        if str(_http_ret.code).startswith('2'):
 
            #2xx Codes don't raise exceptions
 
            sys.stdout.write(_http_ret.title)
 
        else:
 
            raise _http_ret
 

	
 
@@ -160,53 +127,33 @@ def log_pull_action(ui, repo, **kwargs):
 
    """
 
    Logs user last pull action
 

	
 
    :param ui:
 
    :param repo:
 
    """
 
    try:
 
        rc_extras = json.loads(os.environ.get('RC_SCM_DATA', "{}"))
 
    except:
 
        rc_extras = {}
 
    extras = dict(repo.ui.configitems('rhodecode_extras'))
 
    if 'username' in extras:
 
        username = extras['username']
 
        repository = extras['repository']
 
        scm = extras['scm']
 
        make_lock = extras['make_lock']
 
        locked_by = extras['locked_by']
 
        ip = extras['ip']
 
    elif 'username' in rc_extras:
 
        username = rc_extras['username']
 
        repository = rc_extras['repository']
 
        scm = rc_extras['scm']
 
        make_lock = rc_extras['make_lock']
 
        locked_by = rc_extras['locked_by']
 
        ip = rc_extras['ip']
 
    else:
 
        raise Exception('Missing data in repo.ui and os.environ')
 
    user = User.get_by_username(username)
 
    ex = _extract_extras()
 

	
 
    user = User.get_by_username(ex.username)
 
    action = 'pull'
 
    action_logger(user, action, repository, ip, commit=True)
 
    action_logger(user, action, ex.repository, ex.ip, commit=True)
 
    # extension hook call
 
    from rhodecode import EXTENSIONS
 
    callback = getattr(EXTENSIONS, 'PULL_HOOK', None)
 

	
 
    if isfunction(callback):
 
        kw = {}
 
        kw.update(extras)
 
        kw.update(ex)
 
        callback(**kw)
 

	
 
    if make_lock is True:
 
        Repository.lock(Repository.get_by_repo_name(repository), user.user_id)
 
    if ex.make_lock is True:
 
        Repository.lock(Repository.get_by_repo_name(ex.repository), user.user_id)
 
        #msg = 'Made lock on repo `%s`' % repository
 
        #sys.stdout.write(msg)
 

	
 
    if locked_by[0]:
 
        locked_by = User.get(locked_by[0]).username
 
        _http_ret = HTTPLockedRC(repository, locked_by)
 
    if ex.locked_by[0]:
 
        locked_by = User.get(ex.locked_by[0]).username
 
        _http_ret = HTTPLockedRC(ex.repository, locked_by)
 
        if str(_http_ret.code).startswith('2'):
 
            #2xx Codes don't raise exceptions
 
            sys.stdout.write(_http_ret.title)
 
    return 0
 

	
 

	
 
@@ -215,38 +162,17 @@ def log_push_action(ui, repo, **kwargs):
 
    Maps user last push action to new changeset id, from mercurial
 

	
 
    :param ui:
 
    :param repo: repo object containing the `ui` object
 
    """
 

	
 
    try:
 
        rc_extras = json.loads(os.environ.get('RC_SCM_DATA', "{}"))
 
    except:
 
        rc_extras = {}
 
    ex = _extract_extras()
 

	
 
    extras = dict(repo.ui.configitems('rhodecode_extras'))
 
    if 'username' in extras:
 
        username = extras['username']
 
        repository = extras['repository']
 
        scm = extras['scm']
 
        make_lock = extras['make_lock']
 
        locked_by = extras['locked_by']
 
        action = extras['action']
 
    elif 'username' in rc_extras:
 
        username = rc_extras['username']
 
        repository = rc_extras['repository']
 
        scm = rc_extras['scm']
 
        make_lock = rc_extras['make_lock']
 
        locked_by = rc_extras['locked_by']
 
        action = extras['action']
 
    else:
 
        raise Exception('Missing data in repo.ui and os.environ')
 
    action = ex.action + ':%s'
 

	
 
    action = action + ':%s'
 

	
 
    if scm == 'hg':
 
    if ex.scm == 'hg':
 
        node = kwargs['node']
 

	
 
        def get_revs(repo, rev_opt):
 
            if rev_opt:
 
                revs = revrange(repo, rev_opt)
 

	
 
@@ -256,37 +182,37 @@ def log_push_action(ui, repo, **kwargs):
 
            else:
 
                return (len(repo) - 1, 0)
 

	
 
        stop, start = get_revs(repo, [node + ':'])
 
        h = binascii.hexlify
 
        revs = [h(repo[r].node()) for r in xrange(start, stop + 1)]
 
    elif scm == 'git':
 
    elif ex.scm == 'git':
 
        revs = kwargs.get('_git_revs', [])
 
        if '_git_revs' in kwargs:
 
            kwargs.pop('_git_revs')
 

	
 
    action = action % ','.join(revs)
 

	
 
    action_logger(username, action, repository, extras['ip'], commit=True)
 
    action_logger(ex.username, action, ex.repository, ex.ip, commit=True)
 

	
 
    # extension hook call
 
    from rhodecode import EXTENSIONS
 
    callback = getattr(EXTENSIONS, 'PUSH_HOOK', None)
 
    if isfunction(callback):
 
        kw = {'pushed_revs': revs}
 
        kw.update(extras)
 
        kw.update(ex)
 
        callback(**kw)
 

	
 
    if make_lock is False:
 
        Repository.unlock(Repository.get_by_repo_name(repository))
 
        msg = 'Released lock on repo `%s`\n' % repository
 
    if ex.make_lock is False:
 
        Repository.unlock(Repository.get_by_repo_name(ex.repository))
 
        msg = 'Released lock on repo `%s`\n' % ex.repository
 
        sys.stdout.write(msg)
 

	
 
    if locked_by[0]:
 
        locked_by = User.get(locked_by[0]).username
 
        _http_ret = HTTPLockedRC(repository, locked_by)
 
    if ex.locked_by[0]:
 
        locked_by = User.get(ex.locked_by[0]).username
 
        _http_ret = HTTPLockedRC(ex.repository, locked_by)
 
        if str(_http_ret.code).startswith('2'):
 
            #2xx Codes don't raise exceptions
 
            sys.stdout.write(_http_ret.title)
 

	
 
    return 0
 

	
rhodecode/lib/middleware/pygrack.py
Show inline comments
 
@@ -118,14 +118,12 @@ class GitRepository(object):
 
                                      request.content_length)
 
        else:
 
            inputstream = environ['wsgi.input']
 

	
 
        try:
 
            gitenv = os.environ
 
            from rhodecode.lib.compat import json
 
            gitenv['RHODECODE_EXTRAS'] = json.dumps(self.extras)
 
            # forget all configs
 
            gitenv['GIT_CONFIG_NOGLOBAL'] = '1'
 
            opts = dict(
 
                env=gitenv,
 
                cwd=os.getcwd()
 
            )
rhodecode/lib/middleware/simplegit.py
Show inline comments
 
@@ -76,13 +76,14 @@ dulserver.DEFAULT_HANDLERS = {
 
#from dulwich.web import make_wsgi_chain
 

	
 
from paste.httpheaders import REMOTE_USER, AUTH_TYPE
 
from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError, \
 
    HTTPBadRequest, HTTPNotAcceptable
 

	
 
from rhodecode.lib.utils2 import safe_str, fix_PATH, get_server_url
 
from rhodecode.lib.utils2 import safe_str, fix_PATH, get_server_url,\
 
    _set_extras
 
from rhodecode.lib.base import BaseVCSController
 
from rhodecode.lib.auth import get_container_username
 
from rhodecode.lib.utils import is_valid_repo, make_ui
 
from rhodecode.lib.compat import json
 
from rhodecode.model.db import User, RhodeCodeUi
 

	
 
@@ -330,13 +331,7 @@ class SimpleGit(BaseVCSController):
 
        Injects some extra params into baseui instance
 

	
 
        :param baseui: baseui instance
 
        :param extras: dict with extra params to put into baseui
 
        """
 

	
 
        # make our hgweb quiet so it doesn't print output
 
        baseui.setconfig('ui', 'quiet', 'true')
 

	
 
        #inject some additional parameters that will be available in ui
 
        #for hooks
 
        for k, v in extras.items():
 
            baseui.setconfig('rhodecode_extras', k, v)
 
        _set_extras(extras)
rhodecode/lib/middleware/simplehg.py
Show inline comments
 
@@ -32,13 +32,14 @@ from mercurial.error import RepoError
 
from mercurial.hgweb import hgweb_mod
 

	
 
from paste.httpheaders import REMOTE_USER, AUTH_TYPE
 
from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError, \
 
    HTTPBadRequest, HTTPNotAcceptable
 

	
 
from rhodecode.lib.utils2 import safe_str, fix_PATH, get_server_url
 
from rhodecode.lib.utils2 import safe_str, fix_PATH, get_server_url,\
 
    _set_extras
 
from rhodecode.lib.base import BaseVCSController
 
from rhodecode.lib.auth import get_container_username
 
from rhodecode.lib.utils import make_ui, is_valid_repo, ui_sections
 
from rhodecode.lib.compat import json
 
from rhodecode.model.db import User
 
from rhodecode.lib.exceptions import HTTPLockedRC
 
@@ -273,18 +274,14 @@ class SimpleHg(BaseVCSController):
 

	
 
        hgrc = os.path.join(repo_path, '.hg', 'hgrc')
 

	
 
        # make our hgweb quiet so it doesn't print output
 
        baseui.setconfig('ui', 'quiet', 'true')
 

	
 
        #inject some additional parameters that will be available in ui
 
        #for hooks
 
        for k, v in extras.items():
 
            baseui.setconfig('rhodecode_extras', k, v)
 

	
 
        repoui = make_ui('file', hgrc, False)
 

	
 
        if repoui:
 
            #overwrite our ui instance with the section from hgrc file
 
            for section in ui_sections:
 
                for k, v in repoui.configitems(section):
 
                    baseui.setconfig(section, k, v)
 
        _set_extras(extras)
rhodecode/lib/utils2.py
Show inline comments
 
@@ -20,19 +20,23 @@
 
# 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 General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
import os
 
import re
 
import sys
 
import time
 
import datetime
 
import traceback
 
import webob
 

	
 
from pylons.i18n.translation import _, ungettext
 
from rhodecode.lib.vcs.utils.lazy import LazyProperty
 
from rhodecode.lib.compat import json
 

	
 

	
 
def __get_lem():
 
    """
 
    Get language extension map based on what's inside pygments lexers
 
    """
 
@@ -549,13 +553,12 @@ class AttributeDict(dict):
 

	
 
def fix_PATH(os_=None):
 
    """
 
    Get current active python path, and append it to PATH variable to fix issues
 
    of subprocess calls and different python versions
 
    """
 
    import sys
 
    if os_ is None:
 
        import os
 
    else:
 
        os = os_
 

	
 
    cur_path = os.path.split(sys.executable)[0]
 
@@ -575,6 +578,32 @@ def obfuscate_url_pw(engine):
 
    return str(_url)
 

	
 

	
 
def get_server_url(environ):
 
    req = webob.Request(environ)
 
    return req.host_url + req.script_name
 

	
 

	
 
def _extract_extras():
 
    """
 
    Extracts the rc extras data from os.environ, and wraps it into named
 
    AttributeDict object
 
    """
 
    try:
 
        rc_extras = json.loads(os.environ['RC_SCM_DATA'])
 
    except:
 
        print os.environ
 
        print >> sys.stderr, traceback.format_exc()
 
        rc_extras = {}
 

	
 
    try:
 
        for k in ['username', 'repository', 'locked_by', 'scm', 'make_lock',
 
                  'action', 'ip']:
 
            rc_extras[k]
 
    except KeyError, e:
 
        raise Exception('Missing key %s in os.environ %s' % (e, rc_extras))
 

	
 
    return AttributeDict(rc_extras)
 

	
 

	
 
def _set_extras(extras):
 
    os.environ['RC_SCM_DATA'] = json.dumps(extras)
rhodecode/lib/vcs/backends/base.py
Show inline comments
 
@@ -308,33 +308,12 @@ class BaseRepository(object):
 
    def workdir(self):
 
        """
 
        Returns ``Workdir`` instance for this repository.
 
        """
 
        raise NotImplementedError
 

	
 
    def inject_ui(self, **extras):
 
        """
 
        Injects extra parameters into UI object of this repo
 
        """
 
        required_extras = [
 
            'ip',
 
            'username',
 
            'action',
 
            'repository',
 
            'scm',
 
            'config',
 
            'server_url',
 
            'make_lock',
 
            'locked_by',
 
        ]
 
        for req in required_extras:
 
            if req not in extras:
 
                raise AttributeError('Missing attribute %s in extras' % (req))
 
        for k, v in extras.items():
 
            self._repo.ui.setconfig('rhodecode_extras', k, v)
 

	
 

	
 
class BaseChangeset(object):
 
    """
 
    Each backend should implement it's changeset representation.
 

	
 
    **Attributes**
rhodecode/lib/vcs/backends/git/repository.py
Show inline comments
 
@@ -63,20 +63,13 @@ class GitRepository(BaseRepository):
 
                     or abspath(self.path, '.git', 'config'),
 
            abspath(get_user_home(), '.gitconfig'),
 
        ]
 

	
 
    @ThreadLocalLazyProperty
 
    def _repo(self):
 
        repo = Repo(self.path)
 
        # patch the instance of GitRepo with an "FAKE" ui object to add
 
        # compatibility layer with Mercurial
 
        if not hasattr(repo, 'ui'):
 
            from mercurial.ui import ui
 
            baseui = ui()
 
            setattr(repo, 'ui', baseui)
 
        return repo
 
        return Repo(self.path)
 

	
 
    @property
 
    def head(self):
 
        try:
 
            return self._repo.head()
 
        except KeyError:
rhodecode/model/db.py
Show inline comments
 
@@ -44,13 +44,13 @@ from rhodecode.lib.vcs import get_backen
 
from rhodecode.lib.vcs.utils.helpers import get_scm
 
from rhodecode.lib.vcs.exceptions import VCSError
 
from rhodecode.lib.vcs.utils.lazy import LazyProperty
 
from rhodecode.lib.vcs.backends.base import EmptyChangeset
 

	
 
from rhodecode.lib.utils2 import str2bool, safe_str, get_changeset_safe, \
 
    safe_unicode, remove_suffix, remove_prefix, time_to_datetime
 
    safe_unicode, remove_suffix, remove_prefix, time_to_datetime, _set_extras
 
from rhodecode.lib.compat import json
 
from rhodecode.lib.caching_query import FromCache
 

	
 
from rhodecode.model.meta import Base, Session
 

	
 
URL_SEP = '/'
 
@@ -934,16 +934,12 @@ class Repository(Base, BaseModel):
 
        Creates an db based ui object for this repository
 
        """
 
        from rhodecode.lib.utils import make_ui
 
        return make_ui('db', clear_session=False)
 

	
 
    @classmethod
 
    def inject_ui(cls, repo, extras={}):
 
        repo.inject_ui(extras)
 

	
 
    @classmethod
 
    def is_valid(cls, repo_name):
 
        """
 
        returns True if given repo name is a valid filesystem repository
 

	
 
        :param cls:
 
        :param repo_name:
rhodecode/model/scm.py
Show inline comments
 
@@ -41,13 +41,14 @@ from rhodecode.lib.vcs.exceptions import
 
from rhodecode.lib.vcs.utils.lazy import LazyProperty
 
from rhodecode.lib.vcs.nodes import FileNode
 
from rhodecode.lib.vcs.backends.base import EmptyChangeset
 

	
 
from rhodecode import BACKENDS
 
from rhodecode.lib import helpers as h
 
from rhodecode.lib.utils2 import safe_str, safe_unicode, get_server_url
 
from rhodecode.lib.utils2 import safe_str, safe_unicode, get_server_url,\
 
    _set_extras
 
from rhodecode.lib.auth import HasRepoPermissionAny, HasReposGroupPermissionAny
 
from rhodecode.lib.utils import get_filesystem_repos, make_ui, \
 
    action_logger, REMOVED_REPO_PAT
 
from rhodecode.model import BaseModel
 
from rhodecode.model.db import Repository, RhodeCodeUi, CacheInvalidation, \
 
    UserFollowing, UserLog, User, RepoGroup, PullRequest
 
@@ -434,13 +435,13 @@ class ScmModel(BaseModel):
 
            'config': CONFIG['__file__'],
 
            'server_url': get_server_url(environ),
 
            'make_lock': None,
 
            'locked_by': [None, None]
 
        }
 
        _scm_repo = repo._repo
 
        repo.inject_ui(**extras)
 
        _set_extras(extras)
 
        if repo.alias == 'hg':
 
            log_push_action(_scm_repo.ui, _scm_repo, node=revisions[0])
 
        elif repo.alias == 'git':
 
            log_push_action(_scm_repo.ui, _scm_repo, _git_revs=revisions)
 

	
 
    def _get_IMC_module(self, scm_type):
0 comments (0 inline, 0 general)