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