annotate pylons_app/controllers/hg.py @ 20:bbaab7501c1a

Added custom templates, did over check of code to make it work. Added templating for add repository, and styling. App globals now handles our custom static files. (logo etc can be changed)
author Marcin Kuzminski
date Sun, 28 Feb 2010 01:52:38 +0100
parents 5f30a6d558dc
children fac1f62a1d71
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):
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
16 def index(self):
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
17 return g.hgapp(request.environ, self.start_response)
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
18
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
19 def view(self, *args, **kwargs):
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
20 return g.hgapp(request.environ, self.start_response)
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
21
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
22 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
23 c.staticurl = g.statics
bbaab7501c1a Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents: 12
diff changeset
24
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
25 #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
26 if new_repo == 'add':
20
bbaab7501c1a Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents: 12
diff changeset
27 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
28 c.new_repo = ''
bbaab7501c1a Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents: 12
diff changeset
29 return render('add.html')
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
30
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
31 new_repo = new_repo.replace(" ", "_")
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
32 new_repo = new_repo.replace("-", "_")
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 try:
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
35 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
36 c.new_repo = new_repo
bbaab7501c1a Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents: 12
diff changeset
37 c.msg = 'added repo'
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
38 except Exception as e:
20
bbaab7501c1a Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents: 12
diff changeset
39 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
40 c.msg = str(e)
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
41
20
bbaab7501c1a Added custom templates, did over check of code to make it work.
Marcin Kuzminski
parents: 12
diff changeset
42 return render('add.html')
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
43
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
44 def _check_repo(self, repo_name):
12
5f30a6d558dc Added pylons manage script
Marcin Kuzminski
parents: 10
diff changeset
45 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
5f30a6d558dc Added pylons manage script
Marcin Kuzminski
parents: 10
diff changeset
46 config_path = os.path.join(p, 'hgwebdir.config')
6
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
47
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
48 cp = ConfigParser()
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
49
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
50 cp.read(config_path)
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
51 repos_path = cp.get('paths', '/').replace("**", '')
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
52
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
53 if not repos_path:
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
54 raise Exception('Could not read config !')
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
55
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
56 self.repo_path = os.path.join(repos_path, repo_name)
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
57
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
58 try:
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
59 r = hg.repository(ui.ui(), self.repo_path)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
60 hg.verify(r)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
61 #here we hnow that repo exists it was verified
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
62 log.info('%s repo is already created', repo_name)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
63 raise Exception('Repo exists')
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
64 except RepoError:
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
65 log.info('%s repo is free for creation', repo_name)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
66 #it means that there is no valid repo there...
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
67 return True
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
68
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
69
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
70 def _create_repo(self, repo_name):
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
71 if repo_name in [None, '', 'add']:
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
72 raise Exception('undefined repo_name of repo')
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
73
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
74 if self._check_repo(repo_name):
6
2620dac853ad added autoconfig loading
Marcin Kuzminski
parents: 5
diff changeset
75 log.info('creating repo %s in %s', repo_name, self.repo_path)
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
76 cmd = """mkdir %s && hg init %s""" \
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
77 % (self.repo_path, self.repo_path)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
78 os.popen(cmd)
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
79
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
80 #def _make_app():
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
81 # #for single a repo
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
82 # #return hgweb("/path/to/repo", "Name")
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
83 # repos = "hgwebdir.config"
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
84 # return hgwebdir(repos)
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
85 #
5
ad0dd3904225 added repo creation
Marcin Kuzminski
parents: 0
diff changeset
86
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
87 # def view(self, environ, start_response):
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
88 # #the following is only needed when using hgwebdir
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
89 # app = _make_app()
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
90 # #return wsgi_app(environ, start_response)
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
91 # response = app(request.environ, self.start_response)
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
92 #
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
93 # if environ['PATH_INFO'].find("static") != -1:
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
94 # return response
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
95 # else:
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
96 # #wrap the murcurial response in a mako template.
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
97 # template = Template("".join(response),
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
98 # lookup = environ['pylons.pylons']\
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
99 # .config['pylons.g'].mako_lookup)
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
100 #
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
101 # return template.render(g = g, c = c, session = session, h = h)