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