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