Mercurial > public > src > rhodecode
annotate pylons_app/lib/simplehg.py @ 185:3380ca40cdba
added version generation to pylons_app and showed it into template. Propagated baseController with some data for acces into each controller. Fixed simplehg middleware to get proper name of application
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sat, 22 May 2010 00:51:49 +0200 |
parents | 24dbf4bc57aa |
children | 3d1dd13887f9 |
rev | line source |
---|---|
178 | 1 #!/usr/bin/env python |
2 # encoding: utf-8 | |
3 # | |
4 # Copyright (c) 2010 marcink. All rights reserved. | |
5 # | |
6 """ | |
7 Created on 2010-04-28 | |
8 | |
9 @author: marcink | |
10 SimpleHG middleware for handling mercurial protocol request (push/clone etc.) | |
11 It's implemented with basic auth function | |
12 """ | |
13 | |
111 | 14 from mercurial.hgweb import hgweb |
15 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
|
16 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
|
17 from paste.httpheaders import REMOTE_USER, AUTH_TYPE |
178 | 18 from pylons_app.lib.utils import is_mercurial |
177
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
19 from pylons_app.lib.auth import authfunc |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
20 from pylons_app.lib.utils import make_ui, invalidate_cache |
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
|
21 from webob.exc import HTTPNotFound |
177
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
22 import os |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
23 |
111 | 24 class SimpleHg(object): |
25 | |
26 def __init__(self, application, config): | |
27 self.application = application | |
28 self.config = config | |
177
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
29 #authenticate this mercurial request using |
185
3380ca40cdba
added version generation to pylons_app and showed it into template. Propagated baseController with some data for acces into each controller. Fixed simplehg middleware to get proper name of application
Marcin Kuzminski <marcin@python-works.com>
parents:
178
diff
changeset
|
30 realm = '%s %s' % (config['hg_app_name'], 'mercurial repository') |
177
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
31 self.authenticate = AuthBasicAuthenticator(realm, authfunc) |
111 | 32 |
33 def __call__(self, environ, start_response): | |
34 if not is_mercurial(environ): | |
35 return self.application(environ, start_response) | |
36 else: | |
177
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
37 #=================================================================== |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
38 # AUTHENTICATE THIS MERCURIAL REQUEST |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
39 #=================================================================== |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
40 username = REMOTE_USER(environ) |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
41 if not username: |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
42 result = self.authenticate(environ) |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
43 if isinstance(result, str): |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
44 AUTH_TYPE.update(environ, 'basic') |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
45 REMOTE_USER.update(environ, result) |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
46 else: |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
47 return result.wsgi_application(environ, start_response) |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
48 |
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
|
49 try: |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
50 repo_name = environ['PATH_INFO'].split('/')[1] |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
51 except: |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
52 return HTTPNotFound()(environ, start_response) |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
53 |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
54 #since we wrap into hgweb, just reset the path |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
55 environ['PATH_INFO'] = '/' |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
56 self.baseui = make_ui() |
171
52bbeb1e813f
Added universal cache invalidator for two cached functions.
Marcin Kuzminski <marcin@python-works.com>
parents:
124
diff
changeset
|
57 self.basepath = self.baseui.configitems('paths')[0][1]\ |
52bbeb1e813f
Added universal cache invalidator for two cached functions.
Marcin Kuzminski <marcin@python-works.com>
parents:
124
diff
changeset
|
58 .replace('*', '') |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
59 self.repo_path = os.path.join(self.basepath, repo_name) |
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
|
60 try: |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
61 app = wsgiapplication(self._make_app) |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
62 except Exception as e: |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
63 return HTTPNotFound()(environ, start_response) |
171
52bbeb1e813f
Added universal cache invalidator for two cached functions.
Marcin Kuzminski <marcin@python-works.com>
parents:
124
diff
changeset
|
64 |
52bbeb1e813f
Added universal cache invalidator for two cached functions.
Marcin Kuzminski <marcin@python-works.com>
parents:
124
diff
changeset
|
65 """we know that some change was made to repositories and we should |
52bbeb1e813f
Added universal cache invalidator for two cached functions.
Marcin Kuzminski <marcin@python-works.com>
parents:
124
diff
changeset
|
66 invalidate the cache to see the changes right away""" |
52bbeb1e813f
Added universal cache invalidator for two cached functions.
Marcin Kuzminski <marcin@python-works.com>
parents:
124
diff
changeset
|
67 invalidate_cache('full_changelog', repo_name) |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
68 return app(environ, start_response) |
111 | 69 |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
70 def _make_app(self): |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
71 hgserve = hgweb(self.repo_path) |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
72 return self.load_web_settings(hgserve) |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
73 |
111 | 74 |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
75 def load_web_settings(self, hgserve): |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
76 repoui = make_ui(os.path.join(self.repo_path, '.hg', 'hgrc'), False) |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
77 #set the global ui for hgserve |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
78 hgserve.repo.ui = self.baseui |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
79 |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
80 if repoui: |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
81 #set the repository based config |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
82 hgserve.repo.ui = repoui |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
83 |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
84 return hgserve |
177
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
85 |
93bd77e1f3c1
Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents:
171
diff
changeset
|
86 |