Mercurial > public > src > rhodecode
annotate pylons_app/controllers/hg.py @ 21:fac1f62a1d71
Wrapped into mako templates,
changed templates info. Added session and cache middleware.
author | Marcin Kuzminski |
---|---|
date | Sun, 28 Feb 2010 02:24:44 +0100 |
parents | bbaab7501c1a |
children | 3142616771cd |
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 |
0 | 5 from pylons import c, g, session, h, request |
6 from mako.template import Template | |
7 from pprint import pprint | |
5 | 8 import os |
9 from mercurial import ui, hg | |
10 from mercurial.error import RepoError | |
6 | 11 from ConfigParser import ConfigParser |
0 | 12 |
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
|
13 log = logging.getLogger(__name__) |
0 | 14 |
15 class HgController(BaseController): | |
21 | 16 |
17 def __before__(self): | |
18 c.repos_prefix = 'etelko' | |
19 | |
8 | 20 |
21 def view(self, *args, **kwargs): | |
21 | 22 response = g.hgapp(request.environ, self.start_response) |
23 #for mercurial protocols we can't wrap into mako | |
24 if request.environ['HTTP_ACCEPT'].find("mercurial") >= 0: | |
25 return response | |
26 | |
27 #wrap the murcurial response in a mako template. | |
28 template = Template("".join(response), | |
29 lookup = request.environ['pylons.pylons']\ | |
30 .config['pylons.g'].mako_lookup) | |
31 | |
32 return template.render(g = g, c = c, session = session, h = h) | |
8 | 33 |
34 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
|
35 c.staticurl = g.statics |
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
36 |
8 | 37 #extra check it can be add since it's the command |
38 if new_repo == 'add': | |
20
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
39 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
|
40 c.new_repo = '' |
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
41 return render('add.html') |
8 | 42 |
43 new_repo = new_repo.replace(" ", "_") | |
44 new_repo = new_repo.replace("-", "_") | |
45 | |
46 try: | |
47 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
|
48 c.new_repo = new_repo |
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
49 c.msg = 'added repo' |
8 | 50 except Exception as e: |
20
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
51 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
|
52 c.msg = str(e) |
8 | 53 |
20
bbaab7501c1a
Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents:
12
diff
changeset
|
54 return render('add.html') |
0 | 55 |
5 | 56 def _check_repo(self, repo_name): |
12 | 57 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
58 config_path = os.path.join(p, 'hgwebdir.config') | |
6 | 59 |
60 cp = ConfigParser() | |
61 | |
62 cp.read(config_path) | |
63 repos_path = cp.get('paths', '/').replace("**", '') | |
64 | |
65 if not repos_path: | |
66 raise Exception('Could not read config !') | |
67 | |
5 | 68 self.repo_path = os.path.join(repos_path, repo_name) |
0 | 69 |
5 | 70 try: |
71 r = hg.repository(ui.ui(), self.repo_path) | |
72 hg.verify(r) | |
73 #here we hnow that repo exists it was verified | |
74 log.info('%s repo is already created', repo_name) | |
75 raise Exception('Repo exists') | |
76 except RepoError: | |
77 log.info('%s repo is free for creation', repo_name) | |
78 #it means that there is no valid repo there... | |
79 return True | |
80 | |
81 | |
82 def _create_repo(self, repo_name): | |
83 if repo_name in [None, '', 'add']: | |
84 raise Exception('undefined repo_name of repo') | |
85 | |
86 if self._check_repo(repo_name): | |
6 | 87 log.info('creating repo %s in %s', repo_name, self.repo_path) |
5 | 88 cmd = """mkdir %s && hg init %s""" \ |
89 % (self.repo_path, self.repo_path) | |
90 os.popen(cmd) | |
91 | |
8 | 92 #def _make_app(): |
93 # #for single a repo | |
94 # #return hgweb("/path/to/repo", "Name") | |
95 # repos = "hgwebdir.config" | |
96 # return hgwebdir(repos) | |
97 # | |
5 | 98 |
8 | 99 # def view(self, environ, start_response): |
100 # #the following is only needed when using hgwebdir | |
101 # app = _make_app() | |
102 # #return wsgi_app(environ, start_response) | |
103 # response = app(request.environ, self.start_response) | |
104 # | |
105 # if environ['PATH_INFO'].find("static") != -1: | |
106 # return response | |
107 # else: | |
108 # #wrap the murcurial response in a mako template. | |
109 # template = Template("".join(response), | |
110 # lookup = environ['pylons.pylons']\ | |
111 # .config['pylons.g'].mako_lookup) | |
112 # | |
113 # return template.render(g = g, c = c, session = session, h = h) |