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