annotate pylons_app/lib/middleware/simplehg.py @ 364:1ef52a70f3b7

Made config file free configuration based on database and capable of beeing manage via application settings + some code cleanups
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 14 Jul 2010 02:28:32 +0200
parents 71f25781079d
children 6484963056cd
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>
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 248
diff changeset
5
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.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 248
diff changeset
20
178
24dbf4bc57aa simplehg update
Marcin Kuzminski <marcin@python-works.com>
parents: 177
diff changeset
21 """
24dbf4bc57aa simplehg update
Marcin Kuzminski <marcin@python-works.com>
parents: 177
diff changeset
22 Created on 2010-04-28
24dbf4bc57aa simplehg update
Marcin Kuzminski <marcin@python-works.com>
parents: 177
diff changeset
23
24dbf4bc57aa simplehg update
Marcin Kuzminski <marcin@python-works.com>
parents: 177
diff changeset
24 @author: marcink
24dbf4bc57aa simplehg update
Marcin Kuzminski <marcin@python-works.com>
parents: 177
diff changeset
25 SimpleHG middleware for handling mercurial protocol request (push/clone etc.)
24dbf4bc57aa simplehg update
Marcin Kuzminski <marcin@python-works.com>
parents: 177
diff changeset
26 It's implemented with basic auth function
24dbf4bc57aa simplehg update
Marcin Kuzminski <marcin@python-works.com>
parents: 177
diff changeset
27 """
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
28 from datetime import datetime
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
29 from itertools import chain
111
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 from mercurial.hgweb import hgweb
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 from mercurial.hgweb.request import wsgiapplication
362
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
32 from mercurial.error import RepoError
177
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
33 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
34 from paste.httpheaders import REMOTE_USER, AUTH_TYPE
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
35 from pylons_app.lib.auth import authfunc, HasPermissionAnyMiddleware
355
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: 351
diff changeset
36 from pylons_app.lib.utils import is_mercurial, make_ui, invalidate_cache, \
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: 351
diff changeset
37 check_repo_fast
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
38 from pylons_app.model import meta
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 218
diff changeset
39 from pylons_app.model.db import UserLog, User
362
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
40 import pylons_app.lib.helpers as h
355
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: 351
diff changeset
41 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
42 import logging
177
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
43 import os
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
44 import traceback
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
45 log = logging.getLogger(__name__)
177
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
46
111
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 class SimpleHg(object):
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 def __init__(self, application, config):
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 self.application = application
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 self.config = config
177
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
52 #authenticate this mercurial request using
364
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: 362
diff changeset
53 realm = self.config['hg_app_auth_realm']
177
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
54 self.authenticate = AuthBasicAuthenticator(realm, authfunc)
111
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 def __call__(self, environ, start_response):
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 if not is_mercurial(environ):
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 return self.application(environ, start_response)
70b1e5d1e20d simplehg, cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 else:
177
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
60 #===================================================================
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
61 # AUTHENTICATE THIS MERCURIAL REQUEST
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
62 #===================================================================
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
63 username = REMOTE_USER(environ)
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
64 if not username:
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
65 result = self.authenticate(environ)
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
66 if isinstance(result, str):
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
67 AUTH_TYPE.update(environ, 'basic')
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
68 REMOTE_USER.update(environ, result)
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
69 else:
93bd77e1f3c1 Changed auth basic handler only for mercurial request.
Marcin Kuzminski <marcin@python-works.com>
parents: 171
diff changeset
70 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
71
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
72 try:
248
fb7f066126cc Added support for repository located in subdirectories.
Marcin Kuzminski <marcin@python-works.com>
parents: 241
diff changeset
73 repo_name = '/'.join(environ['PATH_INFO'].split('/')[1:])
355
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: 351
diff changeset
74 except:
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
75 log.error(traceback.format_exc())
355
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: 351
diff changeset
76 return HTTPInternalServerError()(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
77
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
78 #===================================================================
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
79 # CHECK PERMISSIONS FOR THIS REQUEST
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
80 #===================================================================
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
81 action = self.__get_action(environ)
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
82 if action:
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
83 username = self.__get_environ_user(environ)
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
84 try:
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
85 sa = meta.Session
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
86 user = sa.query(User)\
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
87 .filter(User.username == username).one()
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
88 except:
355
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: 351
diff changeset
89 log.error(traceback.format_exc())
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: 351
diff changeset
90 return HTTPInternalServerError()(environ, start_response)
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
91 #check permissions for this repository
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
92 if action == 'pull':
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
93 if not HasPermissionAnyMiddleware('repository.read',
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
94 'repository.write',
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
95 'repository.admin')\
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
96 (user, repo_name):
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
97 return HTTPForbidden()(environ, start_response)
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
98 if action == 'push':
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
99 if not HasPermissionAnyMiddleware('repository.write',
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
100 'repository.admin')\
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
101 (user, repo_name):
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
102 return HTTPForbidden()(environ, start_response)
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
103
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
104 #log action
351
a9a6c74ad2a6 added ip loggin into mercurial middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 350
diff changeset
105 proxy_key = 'HTTP_X_REAL_IP'
a9a6c74ad2a6 added ip loggin into mercurial middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 350
diff changeset
106 def_key = 'REMOTE_ADDR'
a9a6c74ad2a6 added ip loggin into mercurial middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 350
diff changeset
107 ipaddr = environ.get(proxy_key, environ.get(def_key, '0.0.0.0'))
a9a6c74ad2a6 added ip loggin into mercurial middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 350
diff changeset
108 self.__log_user_action(user, action, repo_name, ipaddr)
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
109
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
110 #===================================================================
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
111 # MERCURIAL REQUEST HANDLING
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
112 #===================================================================
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
113 environ['PATH_INFO'] = '/'#since we wrap into hgweb, reset the path
364
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: 362
diff changeset
114 self.baseui = make_ui('db')
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
115 self.basepath = self.config['base_path']
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 111
diff changeset
116 self.repo_path = os.path.join(self.basepath, repo_name)
355
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: 351
diff changeset
117
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: 351
diff changeset
118 #quick check if that dir exists...
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: 351
diff changeset
119 if check_repo_fast(repo_name, self.basepath):
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: 351
diff changeset
120 return HTTPNotFound()(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
121 try:
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
122 app = wsgiapplication(self.__make_app)
355
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: 351
diff changeset
123 except RepoError as e:
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: 351
diff changeset
124 if str(e).find('not found') != -1:
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: 351
diff changeset
125 return HTTPNotFound()(environ, start_response)
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
126 except Exception:
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
127 log.error(traceback.format_exc())
355
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: 351
diff changeset
128 return HTTPInternalServerError()(environ, start_response)
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
129
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
130 #invalidate cache on push
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
131 if action == 'push':
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
132 self.__invalidate_cache(repo_name)
362
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
133 messages = []
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
134 messages.append('thank you for using hg-app')
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
135
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
136 return self.msg_wrapper(app, environ, start_response, messages)
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
137 else:
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
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
362
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
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
330
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):
364
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: 362
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')
362
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
162
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
163 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: 355
diff changeset
164 size = int(content_size)
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
165 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: 355
diff changeset
166 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: 355
diff changeset
167 for f in files:
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
168 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: 355
diff changeset
169 return size
71f25781079d small fixes for hg middleware, messages are now propagated only on push
Marcin Kuzminski <marcin@python-works.com>
parents: 355
diff changeset
170 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
171
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
172 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
173 """
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
174 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
175 @param environ:
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
176 """
350
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 330
diff changeset
177 mapping = {'changegroup': 'pull',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 330
diff changeset
178 'changegroupsubset': 'pull',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 330
diff changeset
179 'stream_out': 'pull',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 330
diff changeset
180 'listkeys': 'pull',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 330
diff changeset
181 'unbundle': 'push',
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 330
diff changeset
182 'pushkey': 'push', }
4c9a295d80a4 added new command mappings for mercurial 1.6
Marcin Kuzminski <marcin@python-works.com>
parents: 330
diff changeset
183
197
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
184 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
185 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
186 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
187 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
188 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
189
351
a9a6c74ad2a6 added ip loggin into mercurial middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 350
diff changeset
190 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
191 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
192 try:
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 218
diff changeset
193 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
194 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
195 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
196 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
197 user_log.action_date = datetime.now()
351
a9a6c74ad2a6 added ip loggin into mercurial middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 350
diff changeset
198 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
199 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
200 sa.commit()
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
201 log.info('Adding user %s, action %s on %s',
330
c961b78ff0a0 rewritten simplehg middleware. Now permissions are checked for each repository/request/user
Marcin Kuzminski <marcin@python-works.com>
parents: 257
diff changeset
202 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
203 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
204 sa.rollback()
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
205 log.error('could not log user action:%s', str(e))
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
206
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
207 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
208 """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
209 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
210 push requests"""
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
211 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
212 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
213
da59b7e07e3c Changed import to base. Removed action logging from auth to simplehg.
Marcin Kuzminski <marcin@python-works.com>
parents: 194
diff changeset
214
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
215 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
216 #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
217 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
218
364
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: 362
diff changeset
219 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: 362
diff changeset
220 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: 362
diff changeset
221
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 111
diff changeset
222 if repoui:
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 111
diff changeset
223 #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
224 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
225
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 111
diff changeset
226 return hgserve