annotate rhodecode/lib/middleware/simplegit.py @ 620:19a62a5490fe

added base simple git middleware, for future usage
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 19 Oct 2010 00:55:05 +0200
parents
children d5372213db98
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 #!/usr/bin/env python
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 # encoding: utf-8
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 # middleware to handle mercurial api calls
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 #
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 # This program is free software; you can redistribute it and/or
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 # modify it under the terms of the GNU General Public License
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 # as published by the Free Software Foundation; version 2
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 # of the License or (at your opinion) any later version of the license.
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 #
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 # GNU General Public License for more details.
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 #
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 # You should have received a copy of the GNU General Public License
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 # along with this program; if not, write to the Free Software
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 # MA 02110-1301, USA.
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 from dulwich.repo import Repo
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 from dulwich.server import DictBackend
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 from dulwich.web import HTTPGitApplication
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 from itertools import chain
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 from paste.auth.basic import AuthBasicAuthenticator
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 from paste.httpheaders import REMOTE_USER, AUTH_TYPE
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 from rhodecode.lib.auth import authfunc, HasPermissionAnyMiddleware, \
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 get_user_cached
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 from rhodecode.lib.utils import action_logger, is_git, invalidate_cache, \
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 check_repo_fast
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 import logging
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 import os
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 import traceback
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 """
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 Created on 2010-04-28
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 @author: marcink
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 SimpleHG middleware for handling mercurial protocol request (push/clone etc.)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 It's implemented with basic auth function
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 """
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 log = logging.getLogger(__name__)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 class SimpleGit(object):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 def __init__(self, application, config):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 self.application = application
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 self.config = config
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 #authenticate this mercurial request using
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 self.authenticate = AuthBasicAuthenticator('', authfunc)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 def __call__(self, environ, start_response):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 if not is_git(environ):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 return self.application(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 #===================================================================
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 # AUTHENTICATE THIS MERCURIAL REQUEST
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 #===================================================================
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 username = REMOTE_USER(environ)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 if not username:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 self.authenticate.realm = self.config['rhodecode_realm']
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 result = self.authenticate(environ)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 if isinstance(result, str):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 AUTH_TYPE.update(environ, 'basic')
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 REMOTE_USER.update(environ, result)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 else:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70 return result.wsgi_application(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72 try:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 self.repo_name = environ['PATH_INFO'].split('/')[1]
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 if self.repo_name.endswith('/'):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 self.repo_name = self.repo_name.rstrip('/')
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 except:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 log.error(traceback.format_exc())
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 return HTTPInternalServerError()(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 #===================================================================
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 # CHECK PERMISSIONS FOR THIS REQUEST
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 #===================================================================
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 action = self.__get_action(environ)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 if action:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 username = self.__get_environ_user(environ)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 try:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 user = self.__get_user(username)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 except:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 log.error(traceback.format_exc())
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 return HTTPInternalServerError()(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 #check permissions for this repository
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 if action == 'push':
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 if not HasPermissionAnyMiddleware('repository.write',
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 'repository.admin')\
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 (user, self.repo_name):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 return HTTPForbidden()(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 else:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 #any other action need at least read permission
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 if not HasPermissionAnyMiddleware('repository.read',
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 'repository.write',
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 'repository.admin')\
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 (user, self.repo_name):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 return HTTPForbidden()(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 #log action
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 if action in ('push', 'pull', 'clone'):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 proxy_key = 'HTTP_X_REAL_IP'
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 def_key = 'REMOTE_ADDR'
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 ipaddr = environ.get(proxy_key, environ.get(def_key, '0.0.0.0'))
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 self.__log_user_action(user, action, self.repo_name, ipaddr)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 #===================================================================
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 # GIT REQUEST HANDLING
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 #===================================================================
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 self.basepath = self.config['base_path']
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 self.repo_path = os.path.join(self.basepath, self.repo_name)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 #quick check if that dir exists...
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 if check_repo_fast(self.repo_name, self.basepath):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 return HTTPNotFound()(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 try:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 app = self.__make_app()
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 except Exception:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 log.error(traceback.format_exc())
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 return HTTPInternalServerError()(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 #invalidate cache on push
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 if action == 'push':
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 self.__invalidate_cache(self.repo_name)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 messages = []
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 messages.append('thank you for using rhodecode')
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 return app(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 #TODO: check other alternatives for msg wrapping
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 #return self.msg_wrapper(app, environ, start_response, messages)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 else:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 return app(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 def msg_wrapper(self, app, environ, start_response, messages=[]):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 """
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 Wrapper for custom messages that come out of mercurial respond messages
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 is a list of messages that the user will see at the end of response
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 from merurial protocol actions that involves remote answers
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 :param app:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 :param environ:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 :param start_response:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 """
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 def custom_messages(msg_list):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 for msg in msg_list:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 yield msg + '\n'
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 org_response = app(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 return chain(org_response, custom_messages(messages))
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 def __make_app(self):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 backend = DictBackend({'/' + self.repo_name: Repo(self.repo_path)})
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 gitserve = HTTPGitApplication(backend)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 return gitserve
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 def __get_environ_user(self, environ):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 return environ.get('REMOTE_USER')
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 def __get_user(self, username):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 return get_user_cached(username)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 def __get_action(self, environ):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 """
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 Maps git request commands into a pull or push command.
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 :param environ:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 """
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 service = environ['QUERY_STRING'].split('=')
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 if len(service) > 1:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 service_cmd = service[1]
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 mapping = {'git-receive-pack': 'pull',
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 'git-upload-pack': 'push',
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 }
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 return mapping.get(service_cmd, service_cmd)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 def __log_user_action(self, user, action, repo, ipaddr):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 action_logger(user, action, repo, ipaddr)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185 def __invalidate_cache(self, repo_name):
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186 """we know that some change was made to repositories and we should
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187 invalidate the cache to see the changes right away but only for
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 push requests"""
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189 invalidate_cache('cached_repo_list')
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190 invalidate_cache('full_changelog', repo_name)