Mercurial > kallithea
annotate rhodecode/lib/middleware/simplegit.py @ 898:9c4851dce8e6 beta
fixed error propagation when using git/mercurial requests
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 30 Dec 2010 18:43:04 +0100 |
parents | 01be209b9828 |
children | 07a6e8c65526 |
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 |
625
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
3 # middleware to handle git api calls |
620
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. |
635
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
20 """ |
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
21 Created on 2010-04-28 |
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
22 |
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
23 @author: marcink |
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
24 SimpleGit middleware for handling git protocol request (push/clone etc.) |
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
25 It's implemented with basic auth function |
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
26 """ |
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
27 |
625
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
28 from dulwich import server as dulserver |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
29 |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
30 class SimpleGitUploadPackHandler(dulserver.UploadPackHandler): |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
31 |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
32 def handle(self): |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
33 write = lambda x: self.proto.write_sideband(1, x) |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
34 |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
35 graph_walker = dulserver.ProtocolGraphWalker(self, self.repo.object_store, |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
36 self.repo.get_peeled) |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
37 objects_iter = self.repo.fetch_objects( |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
38 graph_walker.determine_wants, graph_walker, self.progress, |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
39 get_tagged=self.get_tagged) |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
40 |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
41 # Do they want any objects? |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
42 if len(objects_iter) == 0: |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
43 return |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
44 |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
45 self.progress("counting objects: %d, done.\n" % len(objects_iter)) |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
46 dulserver.write_pack_data(dulserver.ProtocolFile(None, write), objects_iter, |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
47 len(objects_iter)) |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
48 messages = [] |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
49 messages.append('thank you for using rhodecode') |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
50 |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
51 for msg in messages: |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
52 self.progress(msg + "\n") |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
53 # we are done |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
54 self.proto.write("0000") |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
55 |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
56 dulserver.DEFAULT_HANDLERS = { |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
57 'git-upload-pack': SimpleGitUploadPackHandler, |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
58 'git-receive-pack': dulserver.ReceivePackHandler, |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
59 } |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
60 |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 from dulwich.repo import Repo |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 from dulwich.web import HTTPGitApplication |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 from paste.auth.basic import AuthBasicAuthenticator |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 from paste.httpheaders import REMOTE_USER, AUTH_TYPE |
635
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
65 from rhodecode.lib.auth import authfunc, HasPermissionAnyMiddleware |
756
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
66 from rhodecode.lib.utils import invalidate_cache, check_repo_fast |
635
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
67 from rhodecode.model.user import UserModel |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 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
|
69 import logging |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 import os |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 import traceback |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 log = logging.getLogger(__name__) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 |
756
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
75 def is_git(environ): |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
76 """ |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
77 Returns True if request's target is git server. ``HTTP_USER_AGENT`` would |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
78 then have git client version given. |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
79 |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
80 :param environ: |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
81 """ |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
82 http_user_agent = environ.get('HTTP_USER_AGENT') |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
83 if http_user_agent and http_user_agent.startswith('git'): |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
84 return True |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
85 return False |
01be209b9828
project refactoring, cleaned up lib.utils from rarly used functions, and place them
Marcin Kuzminski <marcin@python-works.com>
parents:
665
diff
changeset
|
86 |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 class SimpleGit(object): |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
88 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 def __init__(self, application, config): |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 self.application = application |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 self.config = config |
625
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
92 #authenticate this git request using |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 self.authenticate = AuthBasicAuthenticator('', authfunc) |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
94 self.ipaddr = '0.0.0.0' |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
95 self.repository = None |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
96 self.username = None |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
97 self.action = None |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
98 |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 def __call__(self, environ, start_response): |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 if not is_git(environ): |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 return self.application(environ, start_response) |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
102 |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
103 proxy_key = 'HTTP_X_REAL_IP' |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
104 def_key = 'REMOTE_ADDR' |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
105 self.ipaddr = environ.get(proxy_key, environ.get(def_key, '0.0.0.0')) |
898
9c4851dce8e6
fixed error propagation when using git/mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
756
diff
changeset
|
106 # skip passing error to error controller |
9c4851dce8e6
fixed error propagation when using git/mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents:
756
diff
changeset
|
107 environ['pylons.status_code_redirect'] = True |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 #=================================================================== |
625
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
109 # AUTHENTICATE THIS GIT REQUEST |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 #=================================================================== |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 username = REMOTE_USER(environ) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 if not username: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 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
|
114 result = self.authenticate(environ) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 if isinstance(result, str): |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 AUTH_TYPE.update(environ, 'basic') |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 REMOTE_USER.update(environ, result) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 else: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 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
|
120 |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
121 #======================================================================= |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
122 # GET REPOSITORY |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
123 #======================================================================= |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 try: |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
125 repo_name = '/'.join(environ['PATH_INFO'].split('/')[1:]) |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
126 if repo_name.endswith('/'): |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
127 repo_name = repo_name.rstrip('/') |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
128 self.repository = repo_name |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 except: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 log.error(traceback.format_exc()) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
131 return HTTPInternalServerError()(environ, start_response) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
132 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
133 #=================================================================== |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
134 # CHECK PERMISSIONS FOR THIS REQUEST |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
135 #=================================================================== |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
136 self.action = self.__get_action(environ) |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
137 if self.action: |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
138 username = self.__get_environ_user(environ) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
139 try: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
140 user = self.__get_user(username) |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
141 self.username = user.username |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
142 except: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
143 log.error(traceback.format_exc()) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
144 return HTTPInternalServerError()(environ, start_response) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
145 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
146 #check permissions for this repository |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
147 if self.action == 'push': |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
148 if not HasPermissionAnyMiddleware('repository.write', |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
149 'repository.admin')\ |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
150 (user, repo_name): |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
151 return HTTPForbidden()(environ, start_response) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
152 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
153 else: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
154 #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
|
155 if not HasPermissionAnyMiddleware('repository.read', |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
156 'repository.write', |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
157 'repository.admin')\ |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
158 (user, repo_name): |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
159 return HTTPForbidden()(environ, start_response) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
160 |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
161 self.extras = {'ip':self.ipaddr, |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
162 'username':self.username, |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
163 'action':self.action, |
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
164 'repository':self.repository} |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
165 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
166 #=================================================================== |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
167 # GIT REQUEST HANDLING |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
168 #=================================================================== |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
169 self.basepath = self.config['base_path'] |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
170 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
|
171 #quick check if that dir exists... |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
172 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
|
173 return HTTPNotFound()(environ, start_response) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
174 try: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
175 app = self.__make_app() |
643
9dc1d92d82ed
updated setup for all newest versions
Marcin Kuzminski <marcin@python-works.com>
parents:
635
diff
changeset
|
176 except: |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
177 log.error(traceback.format_exc()) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
178 return HTTPInternalServerError()(environ, start_response) |
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 #invalidate cache on push |
655
aefc371a2531
propagate changes for #48 into simplegit.
Marcin Kuzminski <marcin@python-works.com>
parents:
643
diff
changeset
|
181 if self.action == 'push': |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
182 self.__invalidate_cache(self.repo_name) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
183 messages = [] |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
184 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
|
185 return app(environ, start_response) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
186 else: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
187 return app(environ, start_response) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
188 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
189 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
190 def __make_app(self): |
625
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
191 backend = dulserver.DictBackend({'/' + self.repo_name: Repo(self.repo_path)}) |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
192 gitserve = HTTPGitApplication(backend) |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
193 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
194 return gitserve |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
195 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
196 def __get_environ_user(self, environ): |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
197 return environ.get('REMOTE_USER') |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
198 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
199 def __get_user(self, username): |
635
fd63782c4426
Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents:
625
diff
changeset
|
200 return UserModel().get_by_username(username, cache=True) |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
201 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
202 def __get_action(self, environ): |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
203 """ |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
204 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
|
205 :param environ: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
206 """ |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
207 service = environ['QUERY_STRING'].split('=') |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
208 if len(service) > 1: |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
209 service_cmd = service[1] |
625
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
210 mapping = {'git-receive-pack': 'push', |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
211 'git-upload-pack': 'pull', |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
212 } |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
213 |
625
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
214 return mapping.get(service_cmd, service_cmd if service_cmd else 'other') |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
215 else: |
d5372213db98
some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents:
620
diff
changeset
|
216 return 'other' |
620
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
217 |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
218 def __invalidate_cache(self, repo_name): |
19a62a5490fe
added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
219 """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
|
220 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
|
221 push requests""" |
665
070f32743632
Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents:
655
diff
changeset
|
222 invalidate_cache('get_repo_cached_%s' % repo_name) |