Show More
@@ -1,102 +1,101 | |||
|
1 | 1 | import logging |
|
2 | 2 | import os |
|
3 | 3 | |
|
4 | 4 | from pylons import request, response, session, tmpl_context as c, url, app_globals as g |
|
5 | 5 | from pylons.controllers.util import abort, redirect |
|
6 | from beaker.cache import region_invalidate | |
|
7 | 6 | from pylons_app.lib.base import BaseController, render |
|
8 | 7 | from pylons_app.lib import auth |
|
9 | 8 | from pylons_app.model.forms import LoginForm |
|
10 | 9 | import formencode |
|
11 | 10 | import formencode.htmlfill as htmlfill |
|
12 | 11 | from pylons_app.model import meta |
|
13 | 12 | from pylons_app.model.db import Users, UserLogs |
|
14 | 13 | from webhelpers.paginate import Page |
|
15 | from pylons_app.lib.utils import check_repo | |
|
14 | from pylons_app.lib.utils import check_repo, invalidate_cache | |
|
15 | ||
|
16 | 16 | log = logging.getLogger(__name__) |
|
17 | 17 | |
|
18 | 18 | class AdminController(BaseController): |
|
19 | 19 | |
|
20 | 20 | def __before__(self): |
|
21 | 21 | c.admin_user = session.get('admin_user', False) |
|
22 | 22 | c.admin_username = session.get('admin_username') |
|
23 | 23 | |
|
24 | 24 | def index(self): |
|
25 | 25 | # Return a rendered template |
|
26 | 26 | if request.POST: |
|
27 | 27 | #import Login Form validator class |
|
28 | 28 | login_form = LoginForm() |
|
29 | 29 | |
|
30 | 30 | try: |
|
31 | 31 | c.form_result = login_form.to_python(dict(request.params)) |
|
32 | 32 | if auth.admin_auth(c.form_result['username'], c.form_result['password']): |
|
33 | 33 | session['admin_user'] = True |
|
34 | 34 | session['admin_username'] = c.form_result['username'] |
|
35 | 35 | session.save() |
|
36 | 36 | return redirect(url('admin_home')) |
|
37 | 37 | else: |
|
38 | 38 | raise formencode.Invalid('Login Error', None, None, |
|
39 | 39 | error_dict={'username':'invalid login', |
|
40 | 40 | 'password':'invalid password'}) |
|
41 | 41 | |
|
42 | 42 | except formencode.Invalid, error: |
|
43 | 43 | c.form_result = error.value |
|
44 | 44 | c.form_errors = error.error_dict or {} |
|
45 | 45 | html = render('admin/admin.html') |
|
46 | 46 | |
|
47 | 47 | return htmlfill.render( |
|
48 | 48 | html, |
|
49 | 49 | defaults=c.form_result, |
|
50 | 50 | encoding="UTF-8" |
|
51 | 51 | ) |
|
52 | 52 | if c.admin_user: |
|
53 | 53 | sa = meta.Session |
|
54 | 54 | |
|
55 | 55 | users_log = sa.query(UserLogs)\ |
|
56 | 56 | .order_by(UserLogs.action_date.desc()) |
|
57 | 57 | p = int(request.params.get('page', 1)) |
|
58 | 58 | c.users_log = Page(users_log, page=p, items_per_page=10) |
|
59 | 59 | c.log_data = render('admin/admin_log.html') |
|
60 | 60 | if request.params.get('partial'): |
|
61 | 61 | return c.log_data |
|
62 | 62 | return render('admin/admin.html') |
|
63 | 63 | |
|
64 | 64 | def hgrc(self, dirname): |
|
65 | 65 | filename = os.path.join(dirname, '.hg', 'hgrc') |
|
66 | 66 | return filename |
|
67 | 67 | |
|
68 | 68 | def add_repo(self, new_repo): |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | #extra check it can be add since it's the command |
|
72 | 72 | if new_repo == '_admin': |
|
73 | 73 | c.msg = 'DENIED' |
|
74 | 74 | c.new_repo = '' |
|
75 | 75 | return render('admin/add.html') |
|
76 | 76 | |
|
77 | 77 | new_repo = new_repo.replace(" ", "_") |
|
78 | 78 | new_repo = new_repo.replace("-", "_") |
|
79 | 79 | |
|
80 | 80 | try: |
|
81 | 81 | self._create_repo(new_repo) |
|
82 | 82 | c.new_repo = new_repo |
|
83 | 83 | c.msg = 'added repo' |
|
84 | from pylons_app.lib.base import _get_repos | |
|
85 | 84 | #clear our cached list for refresh with new repo |
|
86 |
|
|
|
85 | invalidate_cache('repo_list_2') | |
|
87 | 86 | except Exception as e: |
|
88 | 87 | c.new_repo = 'Exception when adding: %s' % new_repo |
|
89 | 88 | c.msg = str(e) |
|
90 | 89 | |
|
91 | 90 | return render('admin/add.html') |
|
92 | 91 | |
|
93 | 92 | |
|
94 | 93 | def _create_repo(self, repo_name): |
|
95 | 94 | if repo_name in [None, '', 'add']: |
|
96 | 95 | raise Exception('undefined repo_name of repo') |
|
97 | 96 | repo_path = os.path.join(g.base_path, repo_name) |
|
98 | 97 | if check_repo(repo_name, g.base_path): |
|
99 | 98 | log.info('creating repo %s in %s', repo_name, repo_path) |
|
100 | 99 | from vcs.backends.hg import MercurialRepository |
|
101 | 100 | MercurialRepository(repo_path, create=True) |
|
102 | 101 |
@@ -1,76 +1,81 | |||
|
1 | 1 | import logging |
|
2 | 2 | import os |
|
3 | 3 | from pylons import request, response, session, tmpl_context as c, url, app_globals as g |
|
4 | 4 | from pylons.controllers.util import abort, redirect |
|
5 | 5 | from pylons_app.lib import auth |
|
6 | 6 | from pylons_app.lib.base import BaseController, render |
|
7 | 7 | from pylons_app.model import meta |
|
8 | 8 | from pylons_app.model.db import Users, UserLogs |
|
9 | 9 | from pylons_app.lib.auth import authenticate |
|
10 | 10 | from pylons_app.model.hg_model import HgModel |
|
11 | 11 | from operator import itemgetter |
|
12 | 12 | import shutil |
|
13 | from pylons_app.lib.utils import invalidate_cache | |
|
13 | 14 | log = logging.getLogger(__name__) |
|
14 | 15 | |
|
15 | 16 | class ReposController(BaseController): |
|
16 | 17 | """REST Controller styled on the Atom Publishing Protocol""" |
|
17 | 18 | # To properly map this controller, ensure your config/routing.py |
|
18 | 19 | # file has a resource setup: |
|
19 | 20 | # map.resource('repo', 'repos') |
|
20 | 21 | |
|
21 | 22 | @authenticate |
|
22 | 23 | def __before__(self): |
|
23 | 24 | |
|
24 | 25 | c.admin_user = session.get('admin_user') |
|
25 | 26 | c.admin_username = session.get('admin_username') |
|
26 | 27 | self.sa = meta.Session |
|
27 | 28 | |
|
28 | 29 | def index(self, format='html'): |
|
29 | 30 | """GET /repos: All items in the collection""" |
|
30 | 31 | # url('repos') |
|
31 | 32 | hg_model = HgModel() |
|
32 | 33 | c.repos_list = list(hg_model.get_repos()) |
|
33 | 34 | c.repos_list.sort(key=itemgetter('name')) |
|
34 | 35 | return render('admin/repos/repos.html') |
|
35 | 36 | |
|
36 | 37 | def create(self): |
|
37 | 38 | """POST /repos: Create a new item""" |
|
38 | 39 | # url('repos') |
|
39 | 40 | |
|
40 | 41 | def new(self, format='html'): |
|
41 | 42 | """GET /repos/new: Form to create a new item""" |
|
42 | 43 | # url('new_repo') |
|
43 | 44 | |
|
44 | 45 | def update(self, id): |
|
45 | 46 | """PUT /repos/id: Update an existing item""" |
|
46 | 47 | # Forms posted to this method should contain a hidden field: |
|
47 | 48 | # <input type="hidden" name="_method" value="PUT" /> |
|
48 | 49 | # Or using helpers: |
|
49 | 50 | # h.form(url('repo', id=ID), |
|
50 | 51 | # method='put') |
|
51 | 52 | # url('repo', id=ID) |
|
52 | 53 | |
|
53 | 54 | def delete(self, id): |
|
54 | 55 | """DELETE /repos/id: Delete an existing item""" |
|
55 | 56 | # Forms posted to this method should contain a hidden field: |
|
56 | 57 | # <input type="hidden" name="_method" value="DELETE" /> |
|
57 | 58 | # Or using helpers: |
|
58 | 59 | # h.form(url('repo', id=ID), |
|
59 | 60 | # method='delete') |
|
60 | 61 | # url('repo', id=ID) |
|
61 | 62 | from datetime import datetime |
|
62 | 63 | path = g.paths[0][1].replace('*', '') |
|
63 | 64 | rm_path = os.path.join(path, id) |
|
64 | 65 | log.info("Removing %s", rm_path) |
|
65 | 66 | shutil.move(os.path.join(rm_path, '.hg'), os.path.join(rm_path, 'rm__.hg')) |
|
66 | 67 | shutil.move(rm_path, os.path.join(path, 'rm__%s-%s' % (datetime.today(), id))) |
|
68 | ||
|
69 | #clear our cached list for refresh with new repo | |
|
70 | invalidate_cache('repo_list_2') | |
|
71 | ||
|
67 | 72 | return redirect(url('repos')) |
|
68 | 73 | |
|
69 | 74 | |
|
70 | 75 | def show(self, id, format='html'): |
|
71 | 76 | """GET /repos/id: Show a specific item""" |
|
72 | 77 | # url('repo', id=ID) |
|
73 | 78 | return render('/repos_show.html') |
|
74 | 79 | def edit(self, id, format='html'): |
|
75 | 80 | """GET /repos/id/edit: Form to edit an existing item""" |
|
76 | 81 | # url('edit_repo', id=ID) |
@@ -1,106 +1,114 | |||
|
1 | 1 | import os |
|
2 | 2 | import logging |
|
3 | 3 | from mercurial import ui, config, hg |
|
4 | 4 | from mercurial.error import RepoError |
|
5 | 5 | log = logging.getLogger(__name__) |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | def get_repo_slug(request): |
|
9 | 9 | path_info = request.environ.get('PATH_INFO') |
|
10 | 10 | uri_lst = path_info.split('/') |
|
11 | 11 | repo_name = uri_lst[1] |
|
12 | 12 | return repo_name |
|
13 | 13 | |
|
14 | 14 | def is_mercurial(environ): |
|
15 | 15 | """ |
|
16 | 16 | Returns True if request's target is mercurial server - header |
|
17 | 17 | ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``. |
|
18 | 18 | """ |
|
19 | 19 | http_accept = environ.get('HTTP_ACCEPT') |
|
20 | 20 | if http_accept and http_accept.startswith('application/mercurial'): |
|
21 | 21 | return True |
|
22 | 22 | return False |
|
23 | 23 | |
|
24 | 24 | def check_repo_dir(paths): |
|
25 | 25 | repos_path = paths[0][1].split('/') |
|
26 | 26 | if repos_path[-1] in ['*', '**']: |
|
27 | 27 | repos_path = repos_path[:-1] |
|
28 | 28 | if repos_path[0] != '/': |
|
29 | 29 | repos_path[0] = '/' |
|
30 | 30 | if not os.path.isdir(os.path.join(*repos_path)): |
|
31 | 31 | raise Exception('Not a valid repository in %s' % paths[0][1]) |
|
32 | 32 | |
|
33 | 33 | def check_repo(repo_name, base_path): |
|
34 | 34 | |
|
35 | 35 | repo_path = os.path.join(base_path, repo_name) |
|
36 | 36 | |
|
37 | 37 | try: |
|
38 | 38 | r = hg.repository(ui.ui(), repo_path) |
|
39 | 39 | hg.verify(r) |
|
40 | 40 | #here we hnow that repo exists it was verified |
|
41 | 41 | log.info('%s repo is already created', repo_name) |
|
42 | 42 | return False |
|
43 | 43 | #raise Exception('Repo exists') |
|
44 | 44 | except RepoError: |
|
45 | 45 | log.info('%s repo is free for creation', repo_name) |
|
46 | 46 | #it means that there is no valid repo there... |
|
47 | 47 | return True |
|
48 | 48 | |
|
49 | 49 | def make_ui(path='hgwebdir.config', checkpaths=True): |
|
50 | 50 | """ |
|
51 | 51 | A funcion that will read python rc files and make an ui from read options |
|
52 | 52 | |
|
53 | 53 | @param path: path to mercurial config file |
|
54 | 54 | """ |
|
55 | 55 | if not os.path.isfile(path): |
|
56 | 56 | log.error('Unable to read config file %s' % path) |
|
57 | 57 | return False |
|
58 | 58 | #propagated from mercurial documentation |
|
59 | 59 | sections = [ |
|
60 | 60 | 'alias', |
|
61 | 61 | 'auth', |
|
62 | 62 | 'decode/encode', |
|
63 | 63 | 'defaults', |
|
64 | 64 | 'diff', |
|
65 | 65 | 'email', |
|
66 | 66 | 'extensions', |
|
67 | 67 | 'format', |
|
68 | 68 | 'merge-patterns', |
|
69 | 69 | 'merge-tools', |
|
70 | 70 | 'hooks', |
|
71 | 71 | 'http_proxy', |
|
72 | 72 | 'smtp', |
|
73 | 73 | 'patch', |
|
74 | 74 | 'paths', |
|
75 | 75 | 'profiling', |
|
76 | 76 | 'server', |
|
77 | 77 | 'trusted', |
|
78 | 78 | 'ui', |
|
79 | 79 | 'web', |
|
80 | 80 | ] |
|
81 | 81 | |
|
82 | 82 | baseui = ui.ui() |
|
83 | 83 | cfg = config.config() |
|
84 | 84 | cfg.read(path) |
|
85 | 85 | if checkpaths:check_repo_dir(cfg.items('paths')) |
|
86 | 86 | |
|
87 | 87 | for section in sections: |
|
88 | 88 | for k, v in cfg.items(section): |
|
89 | 89 | baseui.setconfig(section, k, v) |
|
90 | 90 | |
|
91 | 91 | return baseui |
|
92 | 92 | |
|
93 | def invalidate_cache(name): | |
|
94 | from beaker.cache import region_invalidate | |
|
95 | if name == 'repo_list_2': | |
|
96 | log.info('INVALIDATING CACHE FOR %s', name) | |
|
97 | from pylons_app.lib.base import _get_repos | |
|
98 | #clear our cached list for refresh with new repo | |
|
99 | region_invalidate(_get_repos, None, 'repo_list_2') | |
|
100 | ||
|
93 | 101 | from vcs.backends.base import BaseChangeset |
|
94 | 102 | from vcs.utils.lazy import LazyProperty |
|
95 | 103 | class EmptyChangeset(BaseChangeset): |
|
96 | 104 | |
|
97 | 105 | revision = -1 |
|
98 | 106 | |
|
99 | 107 | @LazyProperty |
|
100 | 108 | def raw_id(self): |
|
101 | 109 | """ |
|
102 | 110 | Returns raw string identifing this changeset, useful for web |
|
103 | 111 | representation. |
|
104 | 112 | """ |
|
105 | 113 | return '0' * 12 |
|
106 | 114 |
General Comments 0
You need to be logged in to leave comments.
Login now