annotate pylons_app/controllers/hg.py @ 22:3142616771cd

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