##// END OF EJS Templates
Bugfix when client is using old mercurial version and not setting http accept
marcink -
r37:707dfdb1 default
parent child Browse files
Show More
@@ -1,129 +1,135 b''
1 1 #!/usr/bin/python
2 2 # -*- coding: utf-8 -*-
3 3 import logging
4 4 from pylons_app.lib.base import BaseController, render
5 5 from pylons import c, g, session, request
6 6 from pylons_app.lib import helpers as h
7 7 from mako.template import Template
8 8 from pprint import pprint
9 9 import os
10 10 from mercurial import ui, hg
11 11 from mercurial.error import RepoError
12 12 from ConfigParser import ConfigParser
13 13 import encodings
14 from pylons.controllers.util import abort
14 15 log = logging.getLogger(__name__)
15 16
16 17 class HgController(BaseController):
17 18
18 19 def __before__(self):
19 20 c.repos_prefix = 'etelko'
20 21
21 22 def view(self, *args, **kwargs):
22 23 response = g.hgapp(request.environ, self.start_response)
24
25 http_accept = request.environ.get('HTTP_ACCEPT', False)
26 if not http_accept:
27 return abort(status_code=400, detail='no http accept in header')
28
23 29 #for mercurial protocols and raw files we can't wrap into mako
24 if request.environ['HTTP_ACCEPT'].find("mercurial") != -1 or \
30 if http_accept.find("mercurial") != -1 or \
25 31 request.environ['PATH_INFO'].find('raw-file') != -1:
26 32 return response
27 33 try:
28 34 tmpl = u''.join(response)
29 35 template = Template(tmpl, lookup=request.environ['pylons.pylons']\
30 36 .config['pylons.g'].mako_lookup)
31 37
32 38 except (RuntimeError, UnicodeDecodeError):
33 39 log.info('disabling unicode due to encoding error')
34 40 response = g.hgapp(request.environ, self.start_response)
35 41 tmpl = ''.join(response)
36 42 template = Template(tmpl, lookup=request.environ['pylons.pylons']\
37 43 .config['pylons.g'].mako_lookup, disable_unicode=True)
38 44
39 45
40 46 return template.render(g=g, c=c, session=session, h=h)
41 47
42 48
43 49 def manage_hgrc(self):
44 50 pass
45 51
46 52 def hgrc(self, dirname):
47 53 filename = os.path.join(dirname, '.hg', 'hgrc')
48 54 return filename
49 55
50 56 def add_repo(self, new_repo):
51 57 c.staticurl = g.statics
52 58
53 59 #extra check it can be add since it's the command
54 60 if new_repo == 'add':
55 61 c.msg = 'you basstard ! this repo is a command'
56 62 c.new_repo = ''
57 63 return render('add.html')
58 64
59 65 new_repo = new_repo.replace(" ", "_")
60 66 new_repo = new_repo.replace("-", "_")
61 67
62 68 try:
63 69 self._create_repo(new_repo)
64 70 c.new_repo = new_repo
65 71 c.msg = 'added repo'
66 72 except Exception as e:
67 73 c.new_repo = 'Exception when adding: %s' % new_repo
68 74 c.msg = str(e)
69 75
70 76 return render('add.html')
71 77
72 78 def _check_repo(self, repo_name):
73 79 p = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
74 80 config_path = os.path.join(p, 'hgwebdir.config')
75 81
76 82 cp = ConfigParser()
77 83
78 84 cp.read(config_path)
79 85 repos_path = cp.get('paths', '/').replace("**", '')
80 86
81 87 if not repos_path:
82 88 raise Exception('Could not read config !')
83 89
84 90 self.repo_path = os.path.join(repos_path, repo_name)
85 91
86 92 try:
87 93 r = hg.repository(ui.ui(), self.repo_path)
88 94 hg.verify(r)
89 95 #here we hnow that repo exists it was verified
90 96 log.info('%s repo is already created', repo_name)
91 97 raise Exception('Repo exists')
92 98 except RepoError:
93 99 log.info('%s repo is free for creation', repo_name)
94 100 #it means that there is no valid repo there...
95 101 return True
96 102
97 103
98 104 def _create_repo(self, repo_name):
99 105 if repo_name in [None, '', 'add']:
100 106 raise Exception('undefined repo_name of repo')
101 107
102 108 if self._check_repo(repo_name):
103 109 log.info('creating repo %s in %s', repo_name, self.repo_path)
104 110 cmd = """mkdir %s && hg init %s""" \
105 111 % (self.repo_path, self.repo_path)
106 112 os.popen(cmd)
107 113
108 114 #def _make_app():
109 115 # #for single a repo
110 116 # #return hgweb("/path/to/repo", "Name")
111 117 # repos = "hgwebdir.config"
112 118 # return hgwebdir(repos)
113 119 #
114 120
115 121 # def view(self, environ, start_response):
116 122 # #the following is only needed when using hgwebdir
117 123 # app = _make_app()
118 124 # #return wsgi_app(environ, start_response)
119 125 # response = app(request.environ, self.start_response)
120 126 #
121 127 # if environ['PATH_INFO'].find("static") != -1:
122 128 # return response
123 129 # else:
124 130 # #wrap the murcurial response in a mako template.
125 131 # template = Template("".join(response),
126 132 # lookup = environ['pylons.pylons']\
127 133 # .config['pylons.g'].mako_lookup)
128 134 #
129 135 # return template.render(g = g, c = c, session = session, h = h)
General Comments 0
You need to be logged in to leave comments. Login now