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