0
|
1 #!/usr/bin/python
|
|
2 # -*- coding: utf-8 -*-
|
|
3 import logging
|
|
4 from pylons_app.lib.base import BaseController
|
|
5 from pylons import c, g, session, h, request
|
|
6 from mako.template import Template
|
|
7 from pprint import pprint
|
5
|
8 import os
|
0
|
9 #uncomment the following if you want to serve a single repo
|
|
10 #from mercurial.hgweb.hgweb_mod import hgweb
|
|
11 from mercurial.hgweb.hgwebdir_mod import hgwebdir
|
|
12 from mercurial.hgweb.request import wsgiapplication
|
|
13 log = logging.getLogger(__name__)
|
|
14
|
5
|
15 from mercurial import ui, hg
|
|
16 from mercurial.error import RepoError
|
6
|
17 from ConfigParser import ConfigParser
|
0
|
18 #http://bel-epa.com/hg/
|
|
19 #def make_web_app():
|
|
20 # repos = "hgwebdir.config"
|
|
21 # hgwebapp = hgwebdir(repos)
|
|
22 # return hgwebapp
|
|
23 #
|
|
24 #class HgController(BaseController):
|
|
25 #
|
|
26 # def index(self):
|
|
27 # hgapp = wsgiapplication(make_web_app)
|
|
28 # return hgapp(request.environ, self.start_response)
|
|
29 #
|
|
30 # def view(self, *args, **kwargs):
|
|
31 # return u'dupa'
|
|
32 # #pprint(request.environ)
|
|
33 # hgapp = wsgiapplication(make_web_app)
|
|
34 # return hgapp(request.environ, self.start_response)
|
|
35
|
|
36 def _make_app():
|
|
37 #for single a repo
|
|
38 #return hgweb("/path/to/repo", "Name")
|
|
39 repos = "hgwebdir.config"
|
|
40 return hgwebdir(repos)
|
|
41
|
|
42 def wsgi_app(environ, start_response):
|
|
43 start_response('200 OK', [('Content-type', 'text/html')])
|
|
44 return ['<html>\n<body>\nHello World!\n</body>\n</html>']
|
|
45
|
|
46 class HgController(BaseController):
|
|
47
|
5
|
48 def _check_repo(self, repo_name):
|
6
|
49
|
|
50 p = os.path.dirname(__file__)
|
|
51 config_path = os.path.join(p, '../..', 'hgwebdir.config')
|
|
52 print config_path
|
|
53
|
|
54 cp = ConfigParser()
|
|
55
|
|
56 cp.read(config_path)
|
|
57 repos_path = cp.get('paths', '/').replace("**", '')
|
|
58
|
|
59 if not repos_path:
|
|
60 raise Exception('Could not read config !')
|
|
61
|
5
|
62 self.repo_path = os.path.join(repos_path, repo_name)
|
0
|
63
|
5
|
64 try:
|
|
65 r = hg.repository(ui.ui(), self.repo_path)
|
|
66 hg.verify(r)
|
|
67 #here we hnow that repo exists it was verified
|
|
68 log.info('%s repo is already created', repo_name)
|
|
69 raise Exception('Repo exists')
|
|
70 except RepoError:
|
|
71 log.info('%s repo is free for creation', repo_name)
|
|
72 #it means that there is no valid repo there...
|
|
73 return True
|
|
74
|
|
75
|
|
76 def _create_repo(self, repo_name):
|
|
77 if repo_name in [None, '', 'add']:
|
|
78 raise Exception('undefined repo_name of repo')
|
|
79
|
|
80 if self._check_repo(repo_name):
|
6
|
81 log.info('creating repo %s in %s', repo_name, self.repo_path)
|
5
|
82 cmd = """mkdir %s && hg init %s""" \
|
|
83 % (self.repo_path, self.repo_path)
|
|
84 os.popen(cmd)
|
|
85
|
|
86
|
|
87 def add_repo(self, new_repo):
|
|
88 tmpl = '''
|
|
89 <html>
|
|
90 <body>
|
|
91 %(msg)s%(new_repo)s!<br \>
|
|
92 <a href="/">repos</a>
|
|
93 </body>
|
|
94 </html>
|
|
95 '''
|
|
96 #extra check it can be add since it's the command
|
|
97 if new_repo == 'add':
|
|
98 return [tmpl % ({'new_repo':'', 'msg':'you basstard ! this repo is a command'})]
|
|
99
|
|
100 new_repo = new_repo.replace(" ", "_")
|
|
101 new_repo = new_repo.replace("-", "_")
|
|
102
|
|
103 try:
|
|
104 self._create_repo(new_repo)
|
|
105 except Exception as e:
|
|
106 return [tmpl % ({'new_repo':' Exception when adding: ' + new_repo, 'msg':str(e)})]
|
|
107
|
|
108 return [tmpl % ({'new_repo':new_repo, 'msg':'added repo: '})]
|
0
|
109
|
|
110 def view(self, environ, start_response):
|
|
111 #the following is only needed when using hgwebdir
|
|
112 app = _make_app()
|
|
113 #return wsgi_app(environ, start_response)
|
|
114 response = app(request.environ, self.start_response)
|
|
115
|
|
116 if environ['PATH_INFO'].find("static") != -1:
|
|
117 return response
|
|
118 else:
|
|
119 #wrap the murcurial response in a mako template.
|
|
120 template = Template("".join(response),
|
|
121 lookup = environ['pylons.pylons']\
|
|
122 .config['pylons.g'].mako_lookup)
|
|
123
|
|
124 return template.render(g = g, c = c, session = session, h = h)
|
|
125
|