Mercurial > public > src > rhodecode
annotate pylons_app/controllers/hg.py @ 41:71ffa932799d
Added app basic auth.
Changed few deprecations for new pylons.
added sqlite logging for user actions.
author | Marcin Kuzminski <marcin@python-blog.com> |
---|---|
date | Wed, 07 Apr 2010 00:51:55 +0200 |
parents | 707dfdb1c7a8 |
children | 2e1247e62c5b |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 import logging | |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
4 import os |
20
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
5 from pylons_app.lib.base import BaseController, render |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
6 from pylons import tmpl_context as c, app_globals as g, session, request, config |
22 | 7 from pylons_app.lib import helpers as h |
0 | 8 from mako.template import Template |
5 | 9 from mercurial import ui, hg |
10 from mercurial.error import RepoError | |
6 | 11 from ConfigParser import ConfigParser |
37
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
12 from pylons.controllers.util import abort |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
13 |
10
525ed90e4577
major app speedup moved the wsgi creation to app globals, in order to make it run only once.
Marcin Kuzminski
parents:
8
diff
changeset
|
14 log = logging.getLogger(__name__) |
0 | 15 |
16 class HgController(BaseController): | |
21 | 17 |
18 def __before__(self): | |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
19 c.repos_prefix = config['repos_name'] |
21 | 20 |
8 | 21 def view(self, *args, **kwargs): |
21 | 22 response = g.hgapp(request.environ, self.start_response) |
37
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
23 |
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
24 http_accept = request.environ.get('HTTP_ACCEPT', False) |
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
25 if not http_accept: |
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
26 return abort(status_code=400, detail='no http accept in header') |
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
27 |
31
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
28 #for mercurial protocols and raw files we can't wrap into mako |
37
707dfdb1c7a8
Bugfix when client is using old mercurial version and not setting http accept
marcink
parents:
32
diff
changeset
|
29 if http_accept.find("mercurial") != -1 or \ |
31
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
30 request.environ['PATH_INFO'].find('raw-file') != -1: |
21 | 31 return response |
32
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
32 try: |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
33 tmpl = u''.join(response) |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
34 template = Template(tmpl, lookup=request.environ['pylons.pylons']\ |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
35 .config['pylons.app_globals'].mako_lookup) |
32
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
36 |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
37 except (RuntimeError, UnicodeDecodeError): |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
38 log.info('disabling unicode due to encoding error') |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
39 response = g.hgapp(request.environ, self.start_response) |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
40 tmpl = ''.join(response) |
f93b523c0be3
dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents:
31
diff
changeset
|
41 template = Template(tmpl, lookup=request.environ['pylons.pylons']\ |
41
71ffa932799d
Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents:
37
diff
changeset
|
42 .config['pylons.app_globals'].mako_lookup, disable_unicode=True) |
31
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
43 |
21 | 44 |
31
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
45 return template.render(g=g, c=c, session=session, h=h) |
8 | 46 |
22 | 47 |
48 def manage_hgrc(self): | |
49 pass | |
50 | |
31
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
51 def hgrc(self, dirname): |
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
52 filename = os.path.join(dirname, '.hg', 'hgrc') |
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
53 return filename |
2963f2894a7a
Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents:
22
diff
changeset
|
54 |
8 | 55 def add_repo(self, new_repo): |
20
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
56 c.staticurl = g.statics |
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
57 |
8 | 58 #extra check it can be add since it's the command |
59 if new_repo == 'add': | |
20
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
60 c.msg = 'you basstard ! this repo is a command' |
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
61 c.new_repo = '' |
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
62 return render('add.html') |
8 | 63 |
64 new_repo = new_repo.replace(" ", "_") | |
65 new_repo = new_repo.replace("-", "_") | |
66 | |
67 try: | |
68 self._create_repo(new_repo) | |
20
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
69 c.new_repo = new_repo |
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
70 c.msg = 'added repo' |
8 | 71 except Exception as e: |
20
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
72 c.new_repo = 'Exception when adding: %s' % new_repo |
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
73 c.msg = str(e) |
8 | 74 |
20
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
75 return render('add.html') |
0 | 76 |
5 | 77 def _check_repo(self, repo_name): |
12 | 78 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
79 config_path = os.path.join(p, 'hgwebdir.config') | |
6 | 80 |
81 cp = ConfigParser() | |
82 | |
83 cp.read(config_path) | |
84 repos_path = cp.get('paths', '/').replace("**", '') | |
85 | |
86 if not repos_path: | |
87 raise Exception('Could not read config !') | |
88 | |
5 | 89 self.repo_path = os.path.join(repos_path, repo_name) |
0 | 90 |
5 | 91 try: |
92 r = hg.repository(ui.ui(), self.repo_path) | |
93 hg.verify(r) | |
94 #here we hnow that repo exists it was verified | |
95 log.info('%s repo is already created', repo_name) | |
96 raise Exception('Repo exists') | |
97 except RepoError: | |
98 log.info('%s repo is free for creation', repo_name) | |
99 #it means that there is no valid repo there... | |
100 return True | |
101 | |
102 | |
103 def _create_repo(self, repo_name): | |
104 if repo_name in [None, '', 'add']: | |
105 raise Exception('undefined repo_name of repo') | |
106 | |
107 if self._check_repo(repo_name): | |
6 | 108 log.info('creating repo %s in %s', repo_name, self.repo_path) |
5 | 109 cmd = """mkdir %s && hg init %s""" \ |
110 % (self.repo_path, self.repo_path) | |
111 os.popen(cmd) | |
112 | |
8 | 113 #def _make_app(): |
114 # #for single a repo | |
115 # #return hgweb("/path/to/repo", "Name") | |
116 # repos = "hgwebdir.config" | |
117 # return hgwebdir(repos) | |
118 # | |
5 | 119 |
8 | 120 # def view(self, environ, start_response): |
121 # #the following is only needed when using hgwebdir | |
122 # app = _make_app() | |
123 # #return wsgi_app(environ, start_response) | |
124 # response = app(request.environ, self.start_response) | |
125 # | |
126 # if environ['PATH_INFO'].find("static") != -1: | |
127 # return response | |
128 # else: | |
129 # #wrap the murcurial response in a mako template. | |
130 # template = Template("".join(response), | |
131 # lookup = environ['pylons.pylons']\ | |
132 # .config['pylons.g'].mako_lookup) | |
133 # | |
134 # return template.render(g = g, c = c, session = session, h = h) |