Mercurial > kallithea
annotate rhodecode/lib/middleware/simplehg.py @ 674:99875a8f2ad1 beta
#49 Enabled anonymous access push and pull commands
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 11 Nov 2010 01:21:33 +0100 |
parents | 070f32743632 |
children | 62c1fab8d29b |
rev | line source |
---|---|
178 | 1 #!/usr/bin/env python |
2 # encoding: utf-8 | |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
3 # middleware to handle mercurial api calls |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
385
eda5f01de3c4
fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents:
381
diff
changeset
|
5 # |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
6 # This program is free software; you can redistribute it and/or |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
7 # modify it under the terms of the GNU General Public License |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
8 # as published by the Free Software Foundation; version 2 |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
9 # of the License or (at your opinion) any later version of the license. |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
10 # |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
14 # GNU General Public License for more details. |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
15 # |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
16 # You should have received a copy of the GNU General Public License |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
17 # along with this program; if not, write to the Free Software |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
248
diff
changeset
|
19 # MA 02110-1301, USA. |
392
b27d32cb3157
Implemented hooks system,
Marcin Kuzminski <marcin@python-works.com>
parents:
385
diff
changeset
|
20 """ |
b27d32cb3157
Implemented hooks system,
Marcin Kuzminski <marcin@python-works.com>
parents:
385
diff
changeset
|
21 Created on 2010-04-28 |
b27d32cb3157
Implemented hooks system,
Marcin Kuzminski <marcin@python-works.com>
parents:
385
diff
changeset
|
22 |
b27d32cb3157
Implemented hooks system,
Marcin Kuzminski <marcin@python-works.com>
parents:
385
diff
changeset
|
23 @author: marcink |
b27d32cb3157
Implemented hooks system,
Marcin Kuzminski <marcin@python-works.com>
parents:
385
diff
changeset
|
24 SimpleHG middleware for handling mercurial protocol request (push/clone etc.) |
b27d32cb3157
Implemented hooks system,
Marcin Kuzminski <marcin@python-works.com>
parents:
385
diff
changeset
|
25 It's implemented with basic auth function |
b27d32cb3157
Implemented hooks system,
Marcin Kuzminski <marcin@python-works.com>
parents:
385
diff
changeset
|
26 """ |
317
c961b78ff0a0
rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents:
257
diff
changeset
|
27 from itertools import chain |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
28 from mercurial.error import RepoError |
111 | 29 from mercurial.hgweb import hgweb |
30 from mercurial.hgweb.request import wsgiapplication | |
177
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
31 from paste.auth.basic import AuthBasicAuthenticator |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
32 from paste.httpheaders import REMOTE_USER, AUTH_TYPE |
629
7e536d1af60d
Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents:
607
diff
changeset
|
33 from rhodecode.lib.auth import authfunc, HasPermissionAnyMiddleware |
547
1e757ac98988
renamed project to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
536
diff
changeset
|
34 from rhodecode.lib.utils import is_mercurial, make_ui, invalidate_cache, \ |
385
eda5f01de3c4
fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents:
381
diff
changeset
|
35 check_repo_fast, ui_sections |
629
7e536d1af60d
Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents:
607
diff
changeset
|
36 from rhodecode.model.user import UserModel |
334
6c23e72437e3
mercurial middleware now returns 500's instead of 404 on errors and 404 when repo not found, added tracebacks
Marcin Kuzminski <marcin@python-works.com>
parents:
331
diff
changeset
|
37 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError |
197
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
38 import logging |
177
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
39 import os |
317
c961b78ff0a0
rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents:
257
diff
changeset
|
40 import traceback |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
41 |
197
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
42 log = logging.getLogger(__name__) |
177
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
43 |
111 | 44 class SimpleHg(object): |
45 | |
46 def __init__(self, application, config): | |
47 self.application = application | |
48 self.config = config | |
674
99875a8f2ad1
#49 Enabled anonymous access push and pull commands
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
49 #authenticate this mercurial request using authfunc |
350
664a5b8c551a
Added application settings, are now customizable from database
Marcin Kuzminski <marcin@python-works.com>
parents:
343
diff
changeset
|
50 self.authenticate = AuthBasicAuthenticator('', authfunc) |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
51 self.ipaddr = '0.0.0.0' |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
52 self.repository = None |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
53 self.username = None |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
54 self.action = None |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
55 |
111 | 56 def __call__(self, environ, start_response): |
57 if not is_mercurial(environ): | |
58 return self.application(environ, start_response) | |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
59 |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
60 proxy_key = 'HTTP_X_REAL_IP' |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
61 def_key = 'REMOTE_ADDR' |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
62 self.ipaddr = environ.get(proxy_key, environ.get(def_key, '0.0.0.0')) |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
63 |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
64 #=================================================================== |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
65 # AUTHENTICATE THIS MERCURIAL REQUEST |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
66 #=================================================================== |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
67 username = REMOTE_USER(environ) |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
68 |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
69 if not username: |
548
b75b77ef649d
renamed hg_app to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
70 self.authenticate.realm = self.config['rhodecode_realm'] |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
71 result = self.authenticate(environ) |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
72 if isinstance(result, str): |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
73 AUTH_TYPE.update(environ, 'basic') |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
74 REMOTE_USER.update(environ, result) |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
75 else: |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
76 return result.wsgi_application(environ, start_response) |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
77 |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
78 #======================================================================= |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
79 # GET REPOSITORY |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
80 #======================================================================= |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
81 try: |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
82 repo_name = '/'.join(environ['PATH_INFO'].split('/')[1:]) |
445
946d8a680a1d
made fix for pushing/pulling into paths that end with /
Marcin Kuzminski <marcin@python-works.com>
parents:
392
diff
changeset
|
83 if repo_name.endswith('/'): |
946d8a680a1d
made fix for pushing/pulling into paths that end with /
Marcin Kuzminski <marcin@python-works.com>
parents:
392
diff
changeset
|
84 repo_name = repo_name.rstrip('/') |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
85 self.repository = repo_name |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
86 except: |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
87 log.error(traceback.format_exc()) |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
88 return HTTPInternalServerError()(environ, start_response) |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
89 |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
90 #=================================================================== |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
91 # CHECK PERMISSIONS FOR THIS REQUEST |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
92 #=================================================================== |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
93 self.action = self.__get_action(environ) |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
94 if self.action: |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
95 username = self.__get_environ_user(environ) |
124
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
96 try: |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
97 user = self.__get_user(username) |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
98 self.username = user.username |
334
6c23e72437e3
mercurial middleware now returns 500's instead of 404 on errors and 404 when repo not found, added tracebacks
Marcin Kuzminski <marcin@python-works.com>
parents:
331
diff
changeset
|
99 except: |
317
c961b78ff0a0
rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents:
257
diff
changeset
|
100 log.error(traceback.format_exc()) |
334
6c23e72437e3
mercurial middleware now returns 500's instead of 404 on errors and 404 when repo not found, added tracebacks
Marcin Kuzminski <marcin@python-works.com>
parents:
331
diff
changeset
|
101 return HTTPInternalServerError()(environ, start_response) |
607
ff449e9e6e38
fixed chrome repo switcher issue
Marcin Kuzminski <marcin@python-works.com>
parents:
606
diff
changeset
|
102 |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
103 #check permissions for this repository |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
104 if self.action == 'push': |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
105 if not HasPermissionAnyMiddleware('repository.write', |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
106 'repository.admin')\ |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
107 (user, repo_name): |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
108 return HTTPForbidden()(environ, start_response) |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
109 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
110 else: |
607
ff449e9e6e38
fixed chrome repo switcher issue
Marcin Kuzminski <marcin@python-works.com>
parents:
606
diff
changeset
|
111 #any other action need at least read permission |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
112 if not HasPermissionAnyMiddleware('repository.read', |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
113 'repository.write', |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
114 'repository.admin')\ |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
115 (user, repo_name): |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
116 return HTTPForbidden()(environ, start_response) |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
117 |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
118 self.extras = {'ip':self.ipaddr, |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
119 'username':self.username, |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
120 'action':self.action, |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
121 'repository':self.repository} |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
654
diff
changeset
|
122 |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
123 #=================================================================== |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
124 # MERCURIAL REQUEST HANDLING |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
125 #=================================================================== |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
126 environ['PATH_INFO'] = '/'#since we wrap into hgweb, reset the path |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
127 self.baseui = make_ui('db') |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
128 self.basepath = self.config['base_path'] |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
129 self.repo_path = os.path.join(self.basepath, repo_name) |
334
6c23e72437e3
mercurial middleware now returns 500's instead of 404 on errors and 404 when repo not found, added tracebacks
Marcin Kuzminski <marcin@python-works.com>
parents:
331
diff
changeset
|
130 |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
131 #quick check if that dir exists... |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
132 if check_repo_fast(repo_name, self.basepath): |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
133 return HTTPNotFound()(environ, start_response) |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
134 try: |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
135 app = wsgiapplication(self.__make_app) |
503
3d6d548ad3cc
Added user action mapper to map push to changeset.
Marcin Kuzminski <marcin@python-works.com>
parents:
445
diff
changeset
|
136 except RepoError, e: |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
137 if str(e).find('not found') != -1: |
334
6c23e72437e3
mercurial middleware now returns 500's instead of 404 on errors and 404 when repo not found, added tracebacks
Marcin Kuzminski <marcin@python-works.com>
parents:
331
diff
changeset
|
138 return HTTPNotFound()(environ, start_response) |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
139 except Exception: |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
140 log.error(traceback.format_exc()) |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
141 return HTTPInternalServerError()(environ, start_response) |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
142 |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
143 #invalidate cache on push |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
144 if self.action == 'push': |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
145 self.__invalidate_cache(repo_name) |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
146 messages = [] |
549
f99075170eb4
more renames for rhode code !!
Marcin Kuzminski <marcin@python-works.com>
parents:
548
diff
changeset
|
147 messages.append('thank you for using rhodecode') |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
148 |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
149 return self.msg_wrapper(app, environ, start_response, messages) |
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
150 else: |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
151 return app(environ, start_response) |
257
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
152 |
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
153 |
340
71f25781079d
small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents:
334
diff
changeset
|
154 def msg_wrapper(self, app, environ, start_response, messages=[]): |
257
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
155 """ |
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
156 Wrapper for custom messages that come out of mercurial respond messages |
317
c961b78ff0a0
rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents:
257
diff
changeset
|
157 is a list of messages that the user will see at the end of response |
c961b78ff0a0
rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents:
257
diff
changeset
|
158 from merurial protocol actions that involves remote answers |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
159 :param app: |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
160 :param environ: |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
161 :param start_response: |
257
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
162 """ |
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
163 def custom_messages(msg_list): |
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
164 for msg in msg_list: |
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
165 yield msg + '\n' |
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
166 org_response = app(environ, start_response) |
66f617f162f8
added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents:
252
diff
changeset
|
167 return chain(org_response, custom_messages(messages)) |
111 | 168 |
197
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
169 def __make_app(self): |
341
1ef52a70f3b7
Made config file free configuration based on database and capable of beeing manage via application settings + some code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
340
diff
changeset
|
170 hgserve = hgweb(str(self.repo_path), baseui=self.baseui) |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
171 return self.__load_web_settings(hgserve, self.extras) |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
172 |
197
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
173 def __get_environ_user(self, environ): |
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
174 return environ.get('REMOTE_USER') |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
175 |
343
6484963056cd
implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
341
diff
changeset
|
176 def __get_user(self, username): |
629
7e536d1af60d
Code refactoring,models renames
Marcin Kuzminski <marcin@python-works.com>
parents:
607
diff
changeset
|
177 return UserModel().get_by_username(username, cache=True) |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
178 |
197
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
179 def __get_action(self, environ): |
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
180 """ |
606
f31f1327c1e9
Fixed journal action loggin doubled messages.
Marcin Kuzminski <marcin@python-works.com>
parents:
605
diff
changeset
|
181 Maps mercurial request commands into a clone,pull or push command. |
f31f1327c1e9
Fixed journal action loggin doubled messages.
Marcin Kuzminski <marcin@python-works.com>
parents:
605
diff
changeset
|
182 This should always return a valid command string |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
183 :param environ: |
197
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
184 """ |
330
4c9a295d80a4
added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents:
317
diff
changeset
|
185 mapping = {'changegroup': 'pull', |
4c9a295d80a4
added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents:
317
diff
changeset
|
186 'changegroupsubset': 'pull', |
4c9a295d80a4
added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents:
317
diff
changeset
|
187 'stream_out': 'pull', |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
188 'listkeys': 'pull', |
330
4c9a295d80a4
added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents:
317
diff
changeset
|
189 'unbundle': 'push', |
4c9a295d80a4
added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents:
317
diff
changeset
|
190 'pushkey': 'push', } |
197
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
191 for qry in environ['QUERY_STRING'].split('&'): |
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
192 if qry.startswith('cmd'): |
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
193 cmd = qry.split('=')[-1] |
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
194 if mapping.has_key(cmd): |
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
195 return mapping[cmd] |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
196 else: |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
197 return cmd |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
198 |
197
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
199 def __invalidate_cache(self, repo_name): |
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
200 """we know that some change was made to repositories and we should |
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
201 invalidate the cache to see the changes right away but only for |
da59b7e07e3c
Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents:
194
diff
changeset
|
202 push requests""" |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
203 invalidate_cache('get_repo_cached_%s' % repo_name) |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
204 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
205 |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
206 def __load_web_settings(self, hgserve, extras={}): |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
207 #set the global ui for hgserve instance passed |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
208 hgserve.repo.ui = self.baseui |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
209 |
341
1ef52a70f3b7
Made config file free configuration based on database and capable of beeing manage via application settings + some code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
340
diff
changeset
|
210 hgrc = os.path.join(self.repo_path, '.hg', 'hgrc') |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
211 |
654
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
212 #inject some additional parameters that will be available in ui |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
213 #for hooks |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
214 for k, v in extras.items(): |
7f5976da192c
#48 rewrite action loggers into hooks with all changesets that are inside a push
Marcin Kuzminski <marcin@python-works.com>
parents:
629
diff
changeset
|
215 hgserve.repo.ui.setconfig('rhodecode_extras', k, v) |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
216 |
341
1ef52a70f3b7
Made config file free configuration based on database and capable of beeing manage via application settings + some code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
340
diff
changeset
|
217 repoui = make_ui('file', hgrc, False) |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
218 |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
219 if repoui: |
385
eda5f01de3c4
fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents:
381
diff
changeset
|
220 #overwrite our ui instance with the section from hgrc file |
eda5f01de3c4
fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents:
381
diff
changeset
|
221 for section in ui_sections: |
eda5f01de3c4
fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents:
381
diff
changeset
|
222 for k, v in repoui.configitems(section): |
eda5f01de3c4
fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents:
381
diff
changeset
|
223 hgserve.repo.ui.setconfig(section, k, v) |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
224 |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
225 return hgserve |
605
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
226 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
227 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
228 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
229 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
230 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
231 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
232 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
233 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
234 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
235 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
236 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
237 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
238 |
72bed56219d6
security bugfix simplehg wasn't checking for permissions on remote commands different than pull or push.
Marcin Kuzminski <marcin@python-works.com>
parents:
549
diff
changeset
|
239 |