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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
1 #!/usr/bin/python
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
2 # -*- coding: utf-8 -*-
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
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
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
5 from pylons import c, g, session, h, request
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
6 from mako.template import Template
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
7 from pprint import pprint
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
8 import os
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
9 from mercurial import ui, hg
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
10 from mercurial.error import RepoError
6
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
11 from ConfigParser import ConfigParser
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
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
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
14
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
15 class HgController(BaseController):
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
16
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
17 def __before__(self):
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
18 c.repos_prefix = 'etelko'
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
19
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
20
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
21 def view(self, *args, **kwargs):
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
22 response = g.hgapp(request.environ, self.start_response)
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
23 #for mercurial protocols we can't wrap into mako
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
24 if request.environ['HTTP_ACCEPT'].find("mercurial") >= 0:
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
25 return response
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
26
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
27 #wrap the murcurial response in a mako template.
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
28 template = Template("".join(response),
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
29 lookup = request.environ['pylons.pylons']\
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
30 .config['pylons.g'].mako_lookup)
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
31
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
32 return template.render(g = g, c = c, session = session, h = h)
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
33
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
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
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
37 #extra check it can be add since it's the command
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
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
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
42
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
43 new_repo = new_repo.replace(" ", "_")
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
44 new_repo = new_repo.replace("-", "_")
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
45
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
46 try:
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
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
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
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
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
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
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
55
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
56 def _check_repo(self, repo_name):
12
5f30a6d558dc Added pylons manage script
Marcin Kuzminski
parents: 10
diff changeset
57 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
5f30a6d558dc Added pylons manage script
Marcin Kuzminski
parents: 10
diff changeset
58 config_path = os.path.join(p, 'hgwebdir.config')
6
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
59
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
60 cp = ConfigParser()
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
61
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
62 cp.read(config_path)
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
63 repos_path = cp.get('paths', '/').replace("**", '')
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
64
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
65 if not repos_path:
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
66 raise Exception('Could not read config !')
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
67
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
68 self.repo_path = os.path.join(repos_path, repo_name)
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
69
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
70 try:
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
71 r = hg.repository(ui.ui(), self.repo_path)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
72 hg.verify(r)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
73 #here we hnow that repo exists it was verified
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
74 log.info('%s repo is already created', repo_name)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
75 raise Exception('Repo exists')
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
76 except RepoError:
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
77 log.info('%s repo is free for creation', repo_name)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
78 #it means that there is no valid repo there...
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
79 return True
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
80
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
81
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
82 def _create_repo(self, repo_name):
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
83 if repo_name in [None, '', 'add']:
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
84 raise Exception('undefined repo_name of repo')
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
85
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
86 if self._check_repo(repo_name):
6
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
87 log.info('creating repo %s in %s', repo_name, self.repo_path)
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
88 cmd = """mkdir %s && hg init %s""" \
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
89 % (self.repo_path, self.repo_path)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
90 os.popen(cmd)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
91
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
92 #def _make_app():
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
93 # #for single a repo
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
94 # #return hgweb("/path/to/repo", "Name")
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
95 # repos = "hgwebdir.config"
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
96 # return hgwebdir(repos)
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
97 #
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
98
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
99 # def view(self, environ, start_response):
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
100 # #the following is only needed when using hgwebdir
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
101 # app = _make_app()
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
102 # #return wsgi_app(environ, start_response)
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
103 # response = app(request.environ, self.start_response)
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
104 #
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
105 # if environ['PATH_INFO'].find("static") != -1:
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
106 # return response
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
107 # else:
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
108 # #wrap the murcurial response in a mako template.
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
109 # template = Template("".join(response),
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
110 # lookup = environ['pylons.pylons']\
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
111 # .config['pylons.g'].mako_lookup)
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
112 #
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
113 # return template.render(g = g, c = c, session = session, h = h)