annotate pylons_app/lib/middleware/simplehg.py @ 386:a9a607a58b1c

moved out ui_sections out of make ui function
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 05 Aug 2010 01:22:33 +0200
parents eda5f01de3c4
children b27d32cb3157
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
178
24dbf4bc57aa simplehg update
Marcin Kuzminski <marcin@python-works.com>
parents: 177
diff changeset
1 #!/usr/bin/env python
24dbf4bc57aa simplehg update
Marcin Kuzminski <marcin@python-works.com>
parents: 177
diff changeset
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.
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
20 from datetime import datetime
317
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
21 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
22 from mercurial.error import RepoError
111
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 from mercurial.hgweb import hgweb
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 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
25 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
26 from paste.httpheaders import REMOTE_USER, AUTH_TYPE
343
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
27 from pylons_app.lib.auth import authfunc, HasPermissionAnyMiddleware, \
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
28 get_user_cached
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
29 from pylons_app.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
30 check_repo_fast, ui_sections
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
31 from pylons_app.model import meta
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 218
diff changeset
32 from pylons_app.model.db import UserLog, User
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
33 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
34 import logging
177
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
35 import os
343
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
36 import pylons_app.lib.helpers as h
317
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
37 import traceback
385
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
38
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
39 """
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
40 Created on 2010-04-28
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
41
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
42 @author: marcink
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
43 SimpleHG middleware for handling mercurial protocol request (push/clone etc.)
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
44 It's implemented with basic auth function
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
45 """
343
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
46
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
47 log = logging.getLogger(__name__)
177
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
48
111
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 class SimpleHg(object):
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 def __init__(self, application, config):
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 self.application = application
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 self.config = config
177
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
54 #authenticate this mercurial request using
350
664a5b8c551a Added application settings, are now customizable from database
Marcin Kuzminski <marcin@python-works.com>
parents: 343
diff changeset
55 self.authenticate = AuthBasicAuthenticator('', authfunc)
111
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 def __call__(self, environ, start_response):
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 if not is_mercurial(environ):
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 return self.application(environ, start_response)
343
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
60
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
61 #===================================================================
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
62 # AUTHENTICATE THIS MERCURIAL REQUEST
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
63 #===================================================================
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
64 username = REMOTE_USER(environ)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
65 if not username:
381
55377fdc1fc6 cleared global application settings.
Marcin Kuzminski <marcin@python-works.com>
parents: 350
diff changeset
66 self.authenticate.realm = self.config['hg_app_realm']
343
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
67 result = self.authenticate(environ)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
68 if isinstance(result, str):
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
69 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
70 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
71 else:
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
72 return result.wsgi_application(environ, start_response)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
73
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
74 try:
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
75 repo_name = '/'.join(environ['PATH_INFO'].split('/')[1:])
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
76 except:
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
77 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
78 return HTTPInternalServerError()(environ, start_response)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
79
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
80 #===================================================================
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
81 # 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
82 #===================================================================
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
83 action = self.__get_action(environ)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
84 if action:
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
85 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
86 try:
343
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
87 user = self.__get_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
88 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
89 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
90 return HTTPInternalServerError()(environ, start_response)
343
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 repository
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
92 if action == 'pull':
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
93 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
94 'repository.write',
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
95 'repository.admin')\
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
96 (user, repo_name):
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
97 return HTTPForbidden()(environ, start_response)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
98 if action == 'push':
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
99 if not HasPermissionAnyMiddleware('repository.write',
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
100 'repository.admin')\
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
101 (user, repo_name):
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
102 return HTTPForbidden()(environ, start_response)
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
103
343
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
104 #log action
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
105 proxy_key = 'HTTP_X_REAL_IP'
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
106 def_key = 'REMOTE_ADDR'
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
107 ipaddr = environ.get(proxy_key, environ.get(def_key, '0.0.0.0'))
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
108 self.__log_user_action(user, action, repo_name, ipaddr)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
109
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
110 #===================================================================
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
111 # MERCURIAL REQUEST HANDLING
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
112 #===================================================================
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
113 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
114 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
115 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
116 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
117
343
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
118 #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
119 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
120 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
121 try:
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
122 app = wsgiapplication(self.__make_app)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
123 except RepoError as e:
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
124 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
125 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
126 except Exception:
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
127 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
128 return HTTPInternalServerError()(environ, start_response)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
129
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
130 #invalidate cache on push
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
131 if action == 'push':
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
132 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
133 messages = []
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
134 messages.append('thank you for using hg-app')
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
135
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
136 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
137 else:
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
138 return app(environ, start_response)
257
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
139
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
140
340
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
141 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
142 """
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
143 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
144 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
145 from merurial protocol actions that involves remote answers
257
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
146 @param app:
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
147 @param environ:
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
148 @param start_response:
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
149 """
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
150 def custom_messages(msg_list):
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
151 for msg in msg_list:
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
152 yield msg + '\n'
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
153 org_response = app(environ, start_response)
66f617f162f8 added custom messages for remote responses.
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
154 return chain(org_response, custom_messages(messages))
111
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
156 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
157 hgserve = hgweb(str(self.repo_path), baseui=self.baseui)
218
58b46f9194c3 version bump. Made changesets work as should, but vcs had to be fixed for that.
Marcin Kuzminski <marcin@python-works.com>
parents: 204
diff changeset
158 return self.__load_web_settings(hgserve)
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
159
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
160 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
161 return environ.get('REMOTE_USER')
340
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
162
343
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
163 def __get_user(self, username):
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
164 return get_user_cached(username)
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
165
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
166
6484963056cd implemented cache for repeated queries in simplehg mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 341
diff changeset
167
340
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
168 def __get_size(self, repo_path, content_size):
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
169 size = int(content_size)
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
170 for path, dirs, files in os.walk(repo_path):
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
171 if path.find('.hg') == -1:
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
172 for f in files:
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
173 size += os.path.getsize(os.path.join(path, f))
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
174 return size
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 334
diff changeset
175 return h.format_byte_size(size)
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 111
diff changeset
176
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
177 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
178 """
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
179 Maps mercurial request commands into a pull or push command.
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
180 @param environ:
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
181 """
330
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 317
diff changeset
182 mapping = {'changegroup': 'pull',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 317
diff changeset
183 'changegroupsubset': 'pull',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 317
diff changeset
184 'stream_out': 'pull',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 317
diff changeset
185 'listkeys': 'pull',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 317
diff changeset
186 'unbundle': 'push',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 317
diff changeset
187 'pushkey': 'push', }
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 317
diff changeset
188
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
189 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
190 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
191 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
192 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
193 return mapping[cmd]
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
194
331
a9a6c74ad2a6 added ip loggin into mercurial middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 330
diff changeset
195 def __log_user_action(self, user, action, repo, ipaddr):
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
196 sa = meta.Session
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
197 try:
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 218
diff changeset
198 user_log = UserLog()
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
199 user_log.user_id = user.user_id
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
200 user_log.action = action
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
201 user_log.repository = repo.replace('/', '')
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
202 user_log.action_date = datetime.now()
331
a9a6c74ad2a6 added ip loggin into mercurial middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 330
diff changeset
203 user_log.user_ip = ipaddr
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
204 sa.add(user_log)
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
205 sa.commit()
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
206 log.info('Adding user %s, action %s on %s',
317
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
207 user.username, action, repo)
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
208 except Exception as e:
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
209 sa.rollback()
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
210 log.error('could not log user action:%s', str(e))
350
664a5b8c551a Added application settings, are now customizable from database
Marcin Kuzminski <marcin@python-works.com>
parents: 343
diff changeset
211 finally:
664a5b8c551a Added application settings, are now customizable from database
Marcin Kuzminski <marcin@python-works.com>
parents: 343
diff changeset
212 meta.Session.remove()
664a5b8c551a Added application settings, are now customizable from database
Marcin Kuzminski <marcin@python-works.com>
parents: 343
diff changeset
213
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
214 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
215 """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
216 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
217 push requests"""
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
218 invalidate_cache('cached_repo_list')
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
219 invalidate_cache('full_changelog', repo_name)
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
220
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
221
218
58b46f9194c3 version bump. Made changesets work as should, but vcs had to be fixed for that.
Marcin Kuzminski <marcin@python-works.com>
parents: 204
diff changeset
222 def __load_web_settings(self, hgserve):
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 111
diff changeset
223 #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
224 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
225
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
226 hgrc = os.path.join(self.repo_path, '.hg', 'hgrc')
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
227 repoui = make_ui('file', hgrc, False)
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
228
385
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
229
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 111
diff changeset
230 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
231 #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
232 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
233 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
234 hgserve.repo.ui.setconfig(section, k, v)
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 111
diff changeset
235
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 111
diff changeset
236 return hgserve
385
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
237
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
238
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
239
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
240
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
241
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
242
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
243
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
244
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
245
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
246
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
247
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
248
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
249
eda5f01de3c4 fixes #20 hg middleware breaks ui() instance when repository has hgrc file.
Marcin Kuzminski <marcin@python-works.com>
parents: 381
diff changeset
250