annotate pylons_app/controllers/admin.py @ 43:2e1247e62c5b

changed for pylons 0.1 / 1.0 added admin controller
author marcink
date Wed, 07 Apr 2010 15:28:50 +0200
parents
children d924b931b488
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
1 import logging
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
2
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
3 from pylons import request, response, session, tmpl_context as c, url, app_globals as g
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
4 from pylons.controllers.util import abort, redirect
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
5
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
6 from pylons_app.lib.base import BaseController, render
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
7 import os
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
8 from mercurial import ui, hg
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
9 from mercurial.error import RepoError
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
10 from ConfigParser import ConfigParser
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
11 log = logging.getLogger(__name__)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
12
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
13 class AdminController(BaseController):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
14
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
15
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
16 def __before__(self):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
17 c.staticurl = g.statics
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
18
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
19 def index(self):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
20 # Return a rendered template
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
21 return render('/admin.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
22
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
23
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
24 def manage_hgrc(self):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
25 pass
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
26
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
27 def hgrc(self, dirname):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
28 filename = os.path.join(dirname, '.hg', 'hgrc')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
29 return filename
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
30
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
31 def add_repo(self, new_repo):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
32
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
33
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
34 #extra check it can be add since it's the command
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
35 if new_repo == 'add':
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
36 c.msg = 'you basstard ! this repo is a command'
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
37 c.new_repo = ''
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
38 return render('add.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
39
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
40 new_repo = new_repo.replace(" ", "_")
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
41 new_repo = new_repo.replace("-", "_")
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
42
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
43 try:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
44 self._create_repo(new_repo)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
45 c.new_repo = new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
46 c.msg = 'added repo'
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
47 except Exception as e:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
48 c.new_repo = 'Exception when adding: %s' % new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
49 c.msg = str(e)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
50
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
51 return render('add.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
52
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
53 def _check_repo(self, repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
54 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
55 config_path = os.path.join(p, 'hgwebdir.config')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
56
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
57 cp = ConfigParser()
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
58
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
59 cp.read(config_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
60 repos_path = cp.get('paths', '/').replace("**", '')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
61
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
62 if not repos_path:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
63 raise Exception('Could not read config !')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
64
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
65 self.repo_path = os.path.join(repos_path, repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
66
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
67 try:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
68 r = hg.repository(ui.ui(), self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
69 hg.verify(r)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
70 #here we hnow that repo exists it was verified
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
71 log.info('%s repo is already created', repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
72 raise Exception('Repo exists')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
73 except RepoError:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
74 log.info('%s repo is free for creation', repo_name)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
75 #it means that there is no valid repo there...
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
76 return True
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
77
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
78
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
79 def _create_repo(self, repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
80 if repo_name in [None, '', 'add']:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
81 raise Exception('undefined repo_name of repo')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
82
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
83 if self._check_repo(repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
84 log.info('creating repo %s in %s', repo_name, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
85 cmd = """mkdir %s && hg init %s""" \
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
86 % (self.repo_path, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
87 os.popen(cmd)