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