Mercurial > kallithea
annotate pylons_app/lib/simplehg.py @ 171:52bbeb1e813f
Added universal cache invalidator for two cached functions.
added invalidation when repository was added or deleted, and another invalidation when there was a mercurial command involved.
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 21 May 2010 02:44:40 +0200 |
parents | f8ae5c1dfae2 |
children | 93bd77e1f3c1 |
rev | line source |
---|---|
111 | 1 import os |
2 from mercurial.hgweb import hgweb | |
3 from mercurial.hgweb.request import wsgiapplication | |
171
52bbeb1e813f
Added universal cache invalidator for two cached functions.
Marcin Kuzminski <marcin@python-works.com>
parents:
124
diff
changeset
|
4 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
|
5 from pylons.controllers.util import abort |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
6 from webob.exc import HTTPNotFound |
111 | 7 class SimpleHg(object): |
8 | |
9 def __init__(self, application, config): | |
10 self.application = application | |
11 self.config = config | |
12 | |
13 def __call__(self, environ, start_response): | |
14 if not is_mercurial(environ): | |
15 return self.application(environ, start_response) | |
16 else: | |
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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 |
f8ae5c1dfae2
Removed unneeded PATH_INFO manipulation, and added 404 codes on bad repositories paths
Marcin Kuzminski <marcin@python-works.com>
parents:
116
diff
changeset
|
22 #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
|
23 environ['PATH_INFO'] = '/' |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
24 self.baseui = make_ui() |
171
52bbeb1e813f
Added universal cache invalidator for two cached functions.
Marcin Kuzminski <marcin@python-works.com>
parents:
124
diff
changeset
|
25 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
|
26 .replace('*', '') |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 |
52bbeb1e813f
Added universal cache invalidator for two cached functions.
Marcin Kuzminski <marcin@python-works.com>
parents:
124
diff
changeset
|
33 """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
|
34 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
|
35 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
|
36 return app(environ, start_response) |
111 | 37 |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
38 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
|
39 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
|
40 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
|
41 |
111 | 42 |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
43 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
|
44 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
|
45 #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
|
46 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
|
47 |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
48 if repoui: |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
49 #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
|
50 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
|
51 |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
52 return hgserve |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
53 |
111 | 54 def is_mercurial(environ): |
55 """ | |
56 Returns True if request's target is mercurial server - header | |
57 ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``. | |
58 """ | |
59 http_accept = environ.get('HTTP_ACCEPT') | |
60 if http_accept and http_accept.startswith('application/mercurial'): | |
61 return True | |
62 return False | |
114
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
63 |
cc5cf1a93902
Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents:
111
diff
changeset
|
64 |