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
|
|
17
|
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):
|
|
49 repos_path = '/home/marcink/python_workspace'
|
|
50 self.repo_path = os.path.join(repos_path, repo_name)
|
0
|
51
|
5
|
52 try:
|
|
53 r = hg.repository(ui.ui(), self.repo_path)
|
|
54 hg.verify(r)
|
|
55 #here we hnow that repo exists it was verified
|
|
56 log.info('%s repo is already created', repo_name)
|
|
57 raise Exception('Repo exists')
|
|
58 except RepoError:
|
|
59 log.info('%s repo is free for creation', repo_name)
|
|
60 #it means that there is no valid repo there...
|
|
61 return True
|
|
62
|
|
63
|
|
64 def _create_repo(self, repo_name):
|
|
65 if repo_name in [None, '', 'add']:
|
|
66 raise Exception('undefined repo_name of repo')
|
|
67
|
|
68 if self._check_repo(repo_name):
|
|
69 log.info('creating repo %s', repo_name)
|
|
70 cmd = """mkdir %s && hg init %s""" \
|
|
71 % (self.repo_path, self.repo_path)
|
|
72 os.popen(cmd)
|
|
73
|
|
74
|
|
75 def add_repo(self, new_repo):
|
|
76 tmpl = '''
|
|
77 <html>
|
|
78 <body>
|
|
79 %(msg)s%(new_repo)s!<br \>
|
|
80 <a href="/">repos</a>
|
|
81 </body>
|
|
82 </html>
|
|
83 '''
|
|
84 #extra check it can be add since it's the command
|
|
85 if new_repo == 'add':
|
|
86 return [tmpl % ({'new_repo':'', 'msg':'you basstard ! this repo is a command'})]
|
|
87
|
|
88 new_repo = new_repo.replace(" ", "_")
|
|
89 new_repo = new_repo.replace("-", "_")
|
|
90
|
|
91 try:
|
|
92 self._create_repo(new_repo)
|
|
93 except Exception as e:
|
|
94 return [tmpl % ({'new_repo':' Exception when adding: ' + new_repo, 'msg':str(e)})]
|
|
95
|
|
96 return [tmpl % ({'new_repo':new_repo, 'msg':'added repo: '})]
|
0
|
97
|
|
98 def view(self, environ, start_response):
|
|
99 #the following is only needed when using hgwebdir
|
|
100 app = _make_app()
|
|
101 #return wsgi_app(environ, start_response)
|
|
102 response = app(request.environ, self.start_response)
|
|
103
|
|
104 if environ['PATH_INFO'].find("static") != -1:
|
|
105 return response
|
|
106 else:
|
|
107 #wrap the murcurial response in a mako template.
|
|
108 template = Template("".join(response),
|
|
109 lookup = environ['pylons.pylons']\
|
|
110 .config['pylons.g'].mako_lookup)
|
|
111
|
|
112 return template.render(g = g, c = c, session = session, h = h)
|
|
113
|