Mercurial > kallithea
comparison rhodecode/lib/middleware/simplegit.py @ 1275:2723276285ae beta
pep8ify middlewares
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sat, 23 Apr 2011 15:17:42 +0200 |
parents | a671db5bdd58 |
children | d6524f3025e8 |
comparison
equal
deleted
inserted
replaced
1274:7a0004efde12 | 1275:2723276285ae |
---|---|
28 import logging | 28 import logging |
29 import traceback | 29 import traceback |
30 | 30 |
31 from dulwich import server as dulserver | 31 from dulwich import server as dulserver |
32 | 32 |
33 | |
33 class SimpleGitUploadPackHandler(dulserver.UploadPackHandler): | 34 class SimpleGitUploadPackHandler(dulserver.UploadPackHandler): |
34 | 35 |
35 def handle(self): | 36 def handle(self): |
36 write = lambda x: self.proto.write_sideband(1, x) | 37 write = lambda x: self.proto.write_sideband(1, x) |
37 | 38 |
38 graph_walker = dulserver.ProtocolGraphWalker(self, self.repo.object_store, | 39 graph_walker = dulserver.ProtocolGraphWalker(self, |
39 self.repo.get_peeled) | 40 self.repo.object_store, |
41 self.repo.get_peeled) | |
40 objects_iter = self.repo.fetch_objects( | 42 objects_iter = self.repo.fetch_objects( |
41 graph_walker.determine_wants, graph_walker, self.progress, | 43 graph_walker.determine_wants, graph_walker, self.progress, |
42 get_tagged=self.get_tagged) | 44 get_tagged=self.get_tagged) |
43 | 45 |
44 # Do they want any objects? | 46 # Do they want any objects? |
45 if len(objects_iter) == 0: | 47 if len(objects_iter) == 0: |
46 return | 48 return |
47 | 49 |
48 self.progress("counting objects: %d, done.\n" % len(objects_iter)) | 50 self.progress("counting objects: %d, done.\n" % len(objects_iter)) |
49 dulserver.write_pack_data(dulserver.ProtocolFile(None, write), objects_iter, | 51 dulserver.write_pack_data(dulserver.ProtocolFile(None, write), |
50 len(objects_iter)) | 52 objects_iter, len(objects_iter)) |
51 messages = [] | 53 messages = [] |
52 messages.append('thank you for using rhodecode') | 54 messages.append('thank you for using rhodecode') |
53 | 55 |
54 for msg in messages: | 56 for msg in messages: |
55 self.progress(msg + "\n") | 57 self.progress(msg + "\n") |
72 from rhodecode.model.user import UserModel | 74 from rhodecode.model.user import UserModel |
73 | 75 |
74 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError | 76 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError |
75 | 77 |
76 log = logging.getLogger(__name__) | 78 log = logging.getLogger(__name__) |
79 | |
77 | 80 |
78 def is_git(environ): | 81 def is_git(environ): |
79 """Returns True if request's target is git server. | 82 """Returns True if request's target is git server. |
80 ``HTTP_USER_AGENT`` would then have git client version given. | 83 ``HTTP_USER_AGENT`` would then have git client version given. |
81 | 84 |
83 """ | 86 """ |
84 http_user_agent = environ.get('HTTP_USER_AGENT') | 87 http_user_agent = environ.get('HTTP_USER_AGENT') |
85 if http_user_agent and http_user_agent.startswith('git'): | 88 if http_user_agent and http_user_agent.startswith('git'): |
86 return True | 89 return True |
87 return False | 90 return False |
91 | |
88 | 92 |
89 class SimpleGit(object): | 93 class SimpleGit(object): |
90 | 94 |
91 def __init__(self, application, config): | 95 def __init__(self, application, config): |
92 self.application = application | 96 self.application = application |
124 # CHECK ANONYMOUS PERMISSION | 128 # CHECK ANONYMOUS PERMISSION |
125 #====================================================================== | 129 #====================================================================== |
126 if self.action in ['pull', 'push'] or self.action: | 130 if self.action in ['pull', 'push'] or self.action: |
127 anonymous_user = self.__get_user('default') | 131 anonymous_user = self.__get_user('default') |
128 self.username = anonymous_user.username | 132 self.username = anonymous_user.username |
129 anonymous_perm = self.__check_permission(self.action, anonymous_user , | 133 anonymous_perm = self.__check_permission(self.action, |
130 self.repo_name) | 134 anonymous_user, |
135 self.repo_name) | |
131 | 136 |
132 if anonymous_perm is not True or anonymous_user.active is False: | 137 if anonymous_perm is not True or anonymous_user.active is False: |
133 if anonymous_perm is not True: | 138 if anonymous_perm is not True: |
134 log.debug('Not enough credentials to access this repository' | 139 log.debug('Not enough credentials to access this ' |
135 'as anonymous user') | 140 'repository as anonymous user') |
136 if anonymous_user.active is False: | 141 if anonymous_user.active is False: |
137 log.debug('Anonymous access is disabled, running ' | 142 log.debug('Anonymous access is disabled, running ' |
138 'authentication') | 143 'authentication') |
139 #============================================================== | 144 #============================================================== |
140 # DEFAULT PERM FAILED OR ANONYMOUS ACCESS IS DISABLED SO WE | 145 # DEFAULT PERM FAILED OR ANONYMOUS ACCESS IS DISABLED SO WE |
141 # NEED TO AUTHENTICATE AND ASK FOR AUTH USER PERMISSIONS | 146 # NEED TO AUTHENTICATE AND ASK FOR AUTH USER PERMISSIONS |
142 #============================================================== | 147 #============================================================== |
143 | 148 |
144 if not REMOTE_USER(environ): | 149 if not REMOTE_USER(environ): |
145 self.authenticate.realm = str(self.config['rhodecode_realm']) | 150 self.authenticate.realm = str( |
151 self.config['rhodecode_realm']) | |
146 result = self.authenticate(environ) | 152 result = self.authenticate(environ) |
147 if isinstance(result, str): | 153 if isinstance(result, str): |
148 AUTH_TYPE.update(environ, 'basic') | 154 AUTH_TYPE.update(environ, 'basic') |
149 REMOTE_USER.update(environ, result) | 155 REMOTE_USER.update(environ, result) |
150 else: | 156 else: |
151 return result.wsgi_application(environ, start_response) | 157 return result.wsgi_application(environ, start_response) |
152 | |
153 | 158 |
154 #============================================================== | 159 #============================================================== |
155 # CHECK PERMISSIONS FOR THIS REQUEST USING GIVEN USERNAME FROM | 160 # CHECK PERMISSIONS FOR THIS REQUEST USING GIVEN USERNAME FROM |
156 # BASIC AUTH | 161 # BASIC AUTH |
157 #============================================================== | 162 #============================================================== |
161 try: | 166 try: |
162 user = self.__get_user(username) | 167 user = self.__get_user(username) |
163 self.username = user.username | 168 self.username = user.username |
164 except: | 169 except: |
165 log.error(traceback.format_exc()) | 170 log.error(traceback.format_exc()) |
166 return HTTPInternalServerError()(environ, start_response) | 171 return HTTPInternalServerError()(environ, |
172 start_response) | |
167 | 173 |
168 #check permissions for this repository | 174 #check permissions for this repository |
169 perm = self.__check_permission(self.action, user, self.repo_name) | 175 perm = self.__check_permission(self.action, user, |
176 self.repo_name) | |
170 if perm is not True: | 177 if perm is not True: |
171 print 'not allowed' | 178 print 'not allowed' |
172 return HTTPForbidden()(environ, start_response) | 179 return HTTPForbidden()(environ, start_response) |
173 | 180 |
174 self.extras = {'ip':self.ipaddr, | 181 self.extras = {'ip': self.ipaddr, |
175 'username':self.username, | 182 'username': self.username, |
176 'action':self.action, | 183 'action': self.action, |
177 'repository':self.repo_name} | 184 'repository': self.repo_name} |
178 | 185 |
179 #=================================================================== | 186 #=================================================================== |
180 # GIT REQUEST HANDLING | 187 # GIT REQUEST HANDLING |
181 #=================================================================== | 188 #=================================================================== |
182 self.basepath = self.config['base_path'] | 189 self.basepath = self.config['base_path'] |
197 messages.append('thank you for using rhodecode') | 204 messages.append('thank you for using rhodecode') |
198 return app(environ, start_response) | 205 return app(environ, start_response) |
199 else: | 206 else: |
200 return app(environ, start_response) | 207 return app(environ, start_response) |
201 | 208 |
202 | |
203 def __make_app(self): | 209 def __make_app(self): |
204 backend = dulserver.DictBackend({'/' + self.repo_name: Repo(self.repo_path)}) | 210 _d = {'/' + self.repo_name: Repo(self.repo_path)} |
211 backend = dulserver.DictBackend(_d) | |
205 gitserve = HTTPGitApplication(backend) | 212 gitserve = HTTPGitApplication(backend) |
206 | 213 |
207 return gitserve | 214 return gitserve |
208 | 215 |
209 def __check_permission(self, action, user, repo_name): | 216 def __check_permission(self, action, user, repo_name): |
214 :param user: user instance | 221 :param user: user instance |
215 :param repo_name: repository name | 222 :param repo_name: repository name |
216 """ | 223 """ |
217 if action == 'push': | 224 if action == 'push': |
218 if not HasPermissionAnyMiddleware('repository.write', | 225 if not HasPermissionAnyMiddleware('repository.write', |
219 'repository.admin')\ | 226 'repository.admin')(user, |
220 (user, repo_name): | 227 repo_name): |
221 return False | 228 return False |
222 | 229 |
223 else: | 230 else: |
224 #any other action need at least read permission | 231 #any other action need at least read permission |
225 if not HasPermissionAnyMiddleware('repository.read', | 232 if not HasPermissionAnyMiddleware('repository.read', |
226 'repository.write', | 233 'repository.write', |
227 'repository.admin')\ | 234 'repository.admin')(user, |
228 (user, repo_name): | 235 repo_name): |
229 return False | 236 return False |
230 | 237 |
231 return True | 238 return True |
232 | |
233 | 239 |
234 def __get_repository(self, environ): | 240 def __get_repository(self, environ): |
235 """Get's repository name out of PATH_INFO header | 241 """Get's repository name out of PATH_INFO header |
236 | 242 |
237 :param environ: environ where PATH_INFO is stored | 243 :param environ: environ where PATH_INFO is stored |
244 log.error(traceback.format_exc()) | 250 log.error(traceback.format_exc()) |
245 raise | 251 raise |
246 repo_name = repo_name.split('/')[0] | 252 repo_name = repo_name.split('/')[0] |
247 return repo_name | 253 return repo_name |
248 | 254 |
249 | |
250 def __get_user(self, username): | 255 def __get_user(self, username): |
251 return UserModel().get_by_username(username, cache=True) | 256 return UserModel().get_by_username(username, cache=True) |
252 | 257 |
253 def __get_action(self, environ): | 258 def __get_action(self, environ): |
254 """Maps git request commands into a pull or push command. | 259 """Maps git request commands into a pull or push command. |
260 service_cmd = service[1] | 265 service_cmd = service[1] |
261 mapping = {'git-receive-pack': 'push', | 266 mapping = {'git-receive-pack': 'push', |
262 'git-upload-pack': 'pull', | 267 'git-upload-pack': 'pull', |
263 } | 268 } |
264 | 269 |
265 return mapping.get(service_cmd, service_cmd if service_cmd else 'other') | 270 return mapping.get(service_cmd, |
271 service_cmd if service_cmd else 'other') | |
266 else: | 272 else: |
267 return 'other' | 273 return 'other' |
268 | 274 |
269 def __invalidate_cache(self, repo_name): | 275 def __invalidate_cache(self, repo_name): |
270 """we know that some change was made to repositories and we should | 276 """we know that some change was made to repositories and we should |