Show More
@@ -1,193 +1,192 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | # database managment for hg app |
|
3 | # database managment for hg app | |
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
5 |
|
5 | # | ||
6 | # This program is free software; you can redistribute it and/or |
|
6 | # This program is free software; you can redistribute it and/or | |
7 | # modify it under the terms of the GNU General Public License |
|
7 | # modify it under the terms of the GNU General Public License | |
8 | # as published by the Free Software Foundation; version 2 |
|
8 | # as published by the Free Software Foundation; version 2 | |
9 | # of the License or (at your opinion) any later version of the license. |
|
9 | # of the License or (at your opinion) any later version of the license. | |
10 | # |
|
10 | # | |
11 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
15 | # |
|
15 | # | |
16 | # You should have received a copy of the GNU General Public License |
|
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with this program; if not, write to the Free Software |
|
17 | # along with this program; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
19 | # MA 02110-1301, USA. |
|
19 | # MA 02110-1301, USA. | |
20 |
|
20 | |||
21 | """ |
|
21 | """ | |
22 | Created on April 10, 2010 |
|
22 | Created on April 10, 2010 | |
23 | database managment and creation for hg app |
|
23 | database managment and creation for hg app | |
24 | @author: marcink |
|
24 | @author: marcink | |
25 | """ |
|
25 | """ | |
26 |
|
26 | |||
27 | from os.path import dirname as dn, join as jn |
|
27 | from os.path import dirname as dn, join as jn | |
28 | import os |
|
28 | import os | |
29 | import sys |
|
29 | import sys | |
30 | import uuid |
|
30 | import uuid | |
31 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) |
|
31 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) | |
32 | sys.path.append(ROOT) |
|
32 | sys.path.append(ROOT) | |
33 |
|
33 | |||
34 | from pylons_app.lib.auth import get_crypt_password |
|
34 | from pylons_app.lib.auth import get_crypt_password | |
|
35 | from pylons_app.lib.utils import ask_ok | |||
35 | from pylons_app.model import init_model |
|
36 | from pylons_app.model import init_model | |
36 | from pylons_app.model.db import User, Permission, HgAppUi, HgAppSettings |
|
37 | from pylons_app.model.db import User, Permission, HgAppUi, HgAppSettings | |
37 | from pylons_app.model import meta |
|
38 | from pylons_app.model import meta | |
38 | from sqlalchemy.engine import create_engine |
|
39 | from sqlalchemy.engine import create_engine | |
39 | import logging |
|
40 | import logging | |
40 |
|
41 | |||
41 |
log = logging.getLogger( |
|
42 | log = logging.getLogger(__name__) | |
42 | log.setLevel(logging.DEBUG) |
|
|||
43 | console_handler = logging.StreamHandler() |
|
|||
44 | console_handler.setFormatter(logging.Formatter("%(asctime)s.%(msecs)03d" |
|
|||
45 | " %(levelname)-5.5s [%(name)s] %(message)s")) |
|
|||
46 | log.addHandler(console_handler) |
|
|||
47 |
|
43 | |||
48 | class DbManage(object): |
|
44 | class DbManage(object): | |
49 | def __init__(self, log_sql): |
|
45 | def __init__(self, log_sql): | |
50 | self.dbname = 'hg_app.db' |
|
46 | self.dbname = 'hg_app.db' | |
51 | dburi = 'sqlite:////%s' % jn(ROOT, self.dbname) |
|
47 | dburi = 'sqlite:////%s' % jn(ROOT, self.dbname) | |
52 | engine = create_engine(dburi, echo=log_sql) |
|
48 | engine = create_engine(dburi, echo=log_sql) | |
53 | init_model(engine) |
|
49 | init_model(engine) | |
54 | self.sa = meta.Session |
|
50 | self.sa = meta.Session | |
55 | self.db_exists = False |
|
51 | self.db_exists = False | |
56 |
|
52 | |||
57 | def check_for_db(self, override): |
|
53 | def check_for_db(self, override): | |
58 | log.info('checking for exisiting db') |
|
54 | log.info('checking for exisiting db') | |
59 | if os.path.isfile(jn(ROOT, self.dbname)): |
|
55 | if os.path.isfile(jn(ROOT, self.dbname)): | |
60 | self.db_exists = True |
|
56 | self.db_exists = True | |
61 | log.info('database exisist') |
|
57 | log.info('database exisist') | |
62 | if not override: |
|
58 | if not override: | |
63 | raise Exception('database already exists') |
|
59 | raise Exception('database already exists') | |
64 |
|
60 | |||
65 | def create_tables(self, override=False): |
|
61 | def create_tables(self, override=False): | |
66 | """ |
|
62 | """ | |
67 | Create a auth database |
|
63 | Create a auth database | |
68 | """ |
|
64 | """ | |
69 | self.check_for_db(override) |
|
65 | self.check_for_db(override) | |
70 | if override: |
|
66 | if override: | |
71 | log.info("database exisist and it's going to be destroyed") |
|
67 | log.info("database exisist and it's going to be destroyed") | |
72 | if self.db_exists: |
|
68 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') | |
|
69 | if not destroy: | |||
|
70 | sys.exit() | |||
|
71 | if self.db_exists and destroy: | |||
73 | os.remove(jn(ROOT, self.dbname)) |
|
72 | os.remove(jn(ROOT, self.dbname)) | |
74 | meta.Base.metadata.create_all(checkfirst=override) |
|
73 | checkfirst = not override | |
|
74 | meta.Base.metadata.create_all(checkfirst=checkfirst) | |||
75 | log.info('Created tables for %s', self.dbname) |
|
75 | log.info('Created tables for %s', self.dbname) | |
76 |
|
76 | |||
77 | def admin_prompt(self): |
|
77 | def admin_prompt(self): | |
78 | import getpass |
|
78 | import getpass | |
79 | username = raw_input('Specify admin username:') |
|
79 | username = raw_input('Specify admin username:') | |
80 | password = getpass.getpass('Specify admin password:') |
|
80 | password = getpass.getpass('Specify admin password:') | |
81 | self.create_user(username, password, True) |
|
81 | self.create_user(username, password, True) | |
82 |
|
82 | |||
83 | def config_prompt(self): |
|
83 | def config_prompt(self): | |
84 | log.info('Setting up repositories config') |
|
84 | log.info('Setting up repositories config') | |
85 |
|
85 | |||
86 |
|
||||
87 | path = raw_input('Specify valid full path to your repositories' |
|
86 | path = raw_input('Specify valid full path to your repositories' | |
88 | ' you can change this later application settings:') |
|
87 | ' you can change this later in application settings:') | |
89 |
|
88 | |||
90 | if not os.path.isdir(path): |
|
89 | if not os.path.isdir(path): | |
91 | log.error('You entered wrong path') |
|
90 | log.error('You entered wrong path') | |
92 | sys.exit() |
|
91 | sys.exit() | |
93 |
|
92 | |||
94 | hooks = HgAppUi() |
|
93 | hooks = HgAppUi() | |
95 | hooks.ui_section = 'hooks' |
|
94 | hooks.ui_section = 'hooks' | |
96 | hooks.ui_key = 'changegroup' |
|
95 | hooks.ui_key = 'changegroup' | |
97 | hooks.ui_value = 'hg update >&2' |
|
96 | hooks.ui_value = 'hg update >&2' | |
98 |
|
97 | |||
99 | web1 = HgAppUi() |
|
98 | web1 = HgAppUi() | |
100 | web1.ui_section = 'web' |
|
99 | web1.ui_section = 'web' | |
101 | web1.ui_key = 'push_ssl' |
|
100 | web1.ui_key = 'push_ssl' | |
102 | web1.ui_value = 'false' |
|
101 | web1.ui_value = 'false' | |
103 |
|
102 | |||
104 | web2 = HgAppUi() |
|
103 | web2 = HgAppUi() | |
105 | web2.ui_section = 'web' |
|
104 | web2.ui_section = 'web' | |
106 | web2.ui_key = 'allow_archive' |
|
105 | web2.ui_key = 'allow_archive' | |
107 | web2.ui_value = 'gz zip bz2' |
|
106 | web2.ui_value = 'gz zip bz2' | |
108 |
|
107 | |||
109 | web3 = HgAppUi() |
|
108 | web3 = HgAppUi() | |
110 | web3.ui_section = 'web' |
|
109 | web3.ui_section = 'web' | |
111 | web3.ui_key = 'allow_push' |
|
110 | web3.ui_key = 'allow_push' | |
112 | web3.ui_value = '*' |
|
111 | web3.ui_value = '*' | |
113 |
|
112 | |||
114 | web4 = HgAppUi() |
|
113 | web4 = HgAppUi() | |
115 | web4.ui_section = 'web' |
|
114 | web4.ui_section = 'web' | |
116 | web4.ui_key = 'baseurl' |
|
115 | web4.ui_key = 'baseurl' | |
117 | web4.ui_value = '/' |
|
116 | web4.ui_value = '/' | |
118 |
|
117 | |||
119 | paths = HgAppUi() |
|
118 | paths = HgAppUi() | |
120 | paths.ui_section = 'paths' |
|
119 | paths.ui_section = 'paths' | |
121 | paths.ui_key = '/' |
|
120 | paths.ui_key = '/' | |
122 | paths.ui_value = os.path.join(path, '*') |
|
121 | paths.ui_value = os.path.join(path, '*') | |
123 |
|
122 | |||
124 |
|
123 | |||
125 | hgsettings = HgAppSettings() |
|
124 | hgsettings = HgAppSettings() | |
126 | hgsettings.app_auth_realm = 'hg-app authentication' |
|
125 | hgsettings.app_auth_realm = 'hg-app authentication' | |
127 | hgsettings.app_title = 'hg-app' |
|
126 | hgsettings.app_title = 'hg-app' | |
128 |
|
127 | |||
129 | try: |
|
128 | try: | |
130 | self.sa.add(hooks) |
|
129 | self.sa.add(hooks) | |
131 | self.sa.add(web1) |
|
130 | self.sa.add(web1) | |
132 | self.sa.add(web2) |
|
131 | self.sa.add(web2) | |
133 | self.sa.add(web3) |
|
132 | self.sa.add(web3) | |
134 | self.sa.add(web4) |
|
133 | self.sa.add(web4) | |
135 | self.sa.add(paths) |
|
134 | self.sa.add(paths) | |
136 | self.sa.add(hgsettings) |
|
135 | self.sa.add(hgsettings) | |
137 | self.sa.commit() |
|
136 | self.sa.commit() | |
138 | except: |
|
137 | except: | |
139 | self.sa.rollback() |
|
138 | self.sa.rollback() | |
140 | raise |
|
139 | raise | |
141 | log.info('created ui config') |
|
140 | log.info('created ui config') | |
142 |
|
141 | |||
143 | def create_user(self, username, password, admin=False): |
|
142 | def create_user(self, username, password, admin=False): | |
144 |
|
143 | |||
145 | log.info('creating default user') |
|
144 | log.info('creating default user') | |
146 | #create default user for handling default permissions. |
|
145 | #create default user for handling default permissions. | |
147 | def_user = User() |
|
146 | def_user = User() | |
148 | def_user.username = 'default' |
|
147 | def_user.username = 'default' | |
149 | def_user.password = get_crypt_password(str(uuid.uuid1())[:8]) |
|
148 | def_user.password = get_crypt_password(str(uuid.uuid1())[:8]) | |
150 | def_user.name = 'default' |
|
149 | def_user.name = 'default' | |
151 | def_user.lastname = 'default' |
|
150 | def_user.lastname = 'default' | |
152 | def_user.email = 'default@default.com' |
|
151 | def_user.email = 'default@default.com' | |
153 | def_user.admin = False |
|
152 | def_user.admin = False | |
154 | def_user.active = False |
|
153 | def_user.active = False | |
155 |
|
154 | |||
156 | log.info('creating administrator user %s', username) |
|
155 | log.info('creating administrator user %s', username) | |
157 | new_user = User() |
|
156 | new_user = User() | |
158 | new_user.username = username |
|
157 | new_user.username = username | |
159 | new_user.password = get_crypt_password(password) |
|
158 | new_user.password = get_crypt_password(password) | |
160 | new_user.name = 'Hg' |
|
159 | new_user.name = 'Hg' | |
161 | new_user.lastname = 'Admin' |
|
160 | new_user.lastname = 'Admin' | |
162 | new_user.email = 'admin@localhost' |
|
161 | new_user.email = 'admin@localhost' | |
163 | new_user.admin = admin |
|
162 | new_user.admin = admin | |
164 | new_user.active = True |
|
163 | new_user.active = True | |
165 |
|
164 | |||
166 | try: |
|
165 | try: | |
167 | self.sa.add(def_user) |
|
166 | self.sa.add(def_user) | |
168 | self.sa.add(new_user) |
|
167 | self.sa.add(new_user) | |
169 | self.sa.commit() |
|
168 | self.sa.commit() | |
170 | except: |
|
169 | except: | |
171 | self.sa.rollback() |
|
170 | self.sa.rollback() | |
172 | raise |
|
171 | raise | |
173 |
|
172 | |||
174 | def create_permissions(self): |
|
173 | def create_permissions(self): | |
175 | #module.(access|create|change|delete)_[name] |
|
174 | #module.(access|create|change|delete)_[name] | |
176 | #module.(read|write|owner) |
|
175 | #module.(read|write|owner) | |
177 | perms = [('repository.none', 'Repository no access'), |
|
176 | perms = [('repository.none', 'Repository no access'), | |
178 | ('repository.read', 'Repository read access'), |
|
177 | ('repository.read', 'Repository read access'), | |
179 | ('repository.write', 'Repository write access'), |
|
178 | ('repository.write', 'Repository write access'), | |
180 | ('repository.admin', 'Repository admin access'), |
|
179 | ('repository.admin', 'Repository admin access'), | |
181 | ('hg.admin', 'Hg Administrator'), |
|
180 | ('hg.admin', 'Hg Administrator'), | |
182 | ] |
|
181 | ] | |
183 |
|
182 | |||
184 | for p in perms: |
|
183 | for p in perms: | |
185 | new_perm = Permission() |
|
184 | new_perm = Permission() | |
186 | new_perm.permission_name = p[0] |
|
185 | new_perm.permission_name = p[0] | |
187 | new_perm.permission_longname = p[1] |
|
186 | new_perm.permission_longname = p[1] | |
188 | try: |
|
187 | try: | |
189 | self.sa.add(new_perm) |
|
188 | self.sa.add(new_perm) | |
190 | self.sa.commit() |
|
189 | self.sa.commit() | |
191 | except: |
|
190 | except: | |
192 | self.sa.rollback() |
|
191 | self.sa.rollback() | |
193 | raise |
|
192 | raise |
@@ -1,221 +1,228 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | # Utilities for hg app |
|
3 | # Utilities for hg app | |
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
5 | # This program is free software; you can redistribute it and/or |
|
5 | # This program is free software; you can redistribute it and/or | |
6 | # modify it under the terms of the GNU General Public License |
|
6 | # modify it under the terms of the GNU General Public License | |
7 | # as published by the Free Software Foundation; version 2 |
|
7 | # as published by the Free Software Foundation; version 2 | |
8 | # of the License or (at your opinion) any later version of the license. |
|
8 | # of the License or (at your opinion) any later version of the license. | |
9 | # |
|
9 | # | |
10 | # This program is distributed in the hope that it will be useful, |
|
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. |
|
13 | # GNU General Public License for more details. | |
14 | # |
|
14 | # | |
15 | # You should have received a copy of the GNU General Public License |
|
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program; if not, write to the Free Software |
|
16 | # along with this program; if not, write to the Free Software | |
17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
18 | # MA 02110-1301, USA. |
|
18 | # MA 02110-1301, USA. | |
19 | from beaker.cache import cache_region |
|
|||
20 |
|
19 | |||
21 | """ |
|
20 | """ | |
22 | Created on April 18, 2010 |
|
21 | Created on April 18, 2010 | |
23 | Utilities for hg app |
|
22 | Utilities for hg app | |
24 | @author: marcink |
|
23 | @author: marcink | |
25 | """ |
|
24 | """ | |
26 |
|
25 | from beaker.cache import cache_region | ||
27 | import os |
|
|||
28 | import logging |
|
|||
29 | from mercurial import ui, config, hg |
|
26 | from mercurial import ui, config, hg | |
30 | from mercurial.error import RepoError |
|
27 | from mercurial.error import RepoError | |
|
28 | from pylons_app.model import meta | |||
31 | from pylons_app.model.db import Repository, User, HgAppUi, HgAppSettings |
|
29 | from pylons_app.model.db import Repository, User, HgAppUi, HgAppSettings | |
32 | from pylons_app.model import meta |
|
30 | from vcs.backends.base import BaseChangeset | |
|
31 | from vcs.utils.lazy import LazyProperty | |||
|
32 | import logging | |||
|
33 | import os | |||
33 | log = logging.getLogger(__name__) |
|
34 | log = logging.getLogger(__name__) | |
34 |
|
35 | |||
35 |
|
36 | |||
36 | def get_repo_slug(request): |
|
37 | def get_repo_slug(request): | |
37 | return request.environ['pylons.routes_dict'].get('repo_name') |
|
38 | return request.environ['pylons.routes_dict'].get('repo_name') | |
38 |
|
39 | |||
39 | def is_mercurial(environ): |
|
40 | def is_mercurial(environ): | |
40 | """ |
|
41 | """ | |
41 | Returns True if request's target is mercurial server - header |
|
42 | Returns True if request's target is mercurial server - header | |
42 | ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``. |
|
43 | ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``. | |
43 | """ |
|
44 | """ | |
44 | http_accept = environ.get('HTTP_ACCEPT') |
|
45 | http_accept = environ.get('HTTP_ACCEPT') | |
45 | if http_accept and http_accept.startswith('application/mercurial'): |
|
46 | if http_accept and http_accept.startswith('application/mercurial'): | |
46 | return True |
|
47 | return True | |
47 | return False |
|
48 | return False | |
48 |
|
49 | |||
49 | def check_repo_dir(paths): |
|
50 | def check_repo_dir(paths): | |
50 | repos_path = paths[0][1].split('/') |
|
51 | repos_path = paths[0][1].split('/') | |
51 | if repos_path[-1] in ['*', '**']: |
|
52 | if repos_path[-1] in ['*', '**']: | |
52 | repos_path = repos_path[:-1] |
|
53 | repos_path = repos_path[:-1] | |
53 | if repos_path[0] != '/': |
|
54 | if repos_path[0] != '/': | |
54 | repos_path[0] = '/' |
|
55 | repos_path[0] = '/' | |
55 | if not os.path.isdir(os.path.join(*repos_path)): |
|
56 | if not os.path.isdir(os.path.join(*repos_path)): | |
56 | raise Exception('Not a valid repository in %s' % paths[0][1]) |
|
57 | raise Exception('Not a valid repository in %s' % paths[0][1]) | |
57 |
|
58 | |||
58 | def check_repo_fast(repo_name, base_path): |
|
59 | def check_repo_fast(repo_name, base_path): | |
59 | if os.path.isdir(os.path.join(base_path, repo_name)):return False |
|
60 | if os.path.isdir(os.path.join(base_path, repo_name)):return False | |
60 | return True |
|
61 | return True | |
61 |
|
62 | |||
62 | def check_repo(repo_name, base_path, verify=True): |
|
63 | def check_repo(repo_name, base_path, verify=True): | |
63 |
|
64 | |||
64 | repo_path = os.path.join(base_path, repo_name) |
|
65 | repo_path = os.path.join(base_path, repo_name) | |
65 |
|
66 | |||
66 | try: |
|
67 | try: | |
67 | if not check_repo_fast(repo_name, base_path): |
|
68 | if not check_repo_fast(repo_name, base_path): | |
68 | return False |
|
69 | return False | |
69 | r = hg.repository(ui.ui(), repo_path) |
|
70 | r = hg.repository(ui.ui(), repo_path) | |
70 | if verify: |
|
71 | if verify: | |
71 | hg.verify(r) |
|
72 | hg.verify(r) | |
72 | #here we hnow that repo exists it was verified |
|
73 | #here we hnow that repo exists it was verified | |
73 | log.info('%s repo is already created', repo_name) |
|
74 | log.info('%s repo is already created', repo_name) | |
74 | return False |
|
75 | return False | |
75 | except RepoError: |
|
76 | except RepoError: | |
76 | #it means that there is no valid repo there... |
|
77 | #it means that there is no valid repo there... | |
77 | log.info('%s repo is free for creation', repo_name) |
|
78 | log.info('%s repo is free for creation', repo_name) | |
78 | return True |
|
79 | return True | |
79 |
|
80 | |||
80 |
|
81 | def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): | ||
|
82 | while True: | |||
|
83 | ok = raw_input(prompt) | |||
|
84 | if ok in ('y', 'ye', 'yes'): return True | |||
|
85 | if ok in ('n', 'no', 'nop', 'nope'): return False | |||
|
86 | retries = retries - 1 | |||
|
87 | if retries < 0: raise IOError | |||
|
88 | print complaint | |||
|
89 | ||||
81 | @cache_region('super_short_term', 'cached_hg_ui') |
|
90 | @cache_region('super_short_term', 'cached_hg_ui') | |
82 | def get_hg_ui_cached(): |
|
91 | def get_hg_ui_cached(): | |
83 | try: |
|
92 | try: | |
84 | sa = meta.Session |
|
93 | sa = meta.Session | |
85 | ret = sa.query(HgAppUi).all() |
|
94 | ret = sa.query(HgAppUi).all() | |
86 | finally: |
|
95 | finally: | |
87 | meta.Session.remove() |
|
96 | meta.Session.remove() | |
88 | return ret |
|
97 | return ret | |
89 |
|
98 | |||
90 |
|
99 | |||
91 | def get_hg_settings(): |
|
100 | def get_hg_settings(): | |
92 | try: |
|
101 | try: | |
93 | sa = meta.Session |
|
102 | sa = meta.Session | |
94 | ret = sa.query(HgAppSettings).scalar() |
|
103 | ret = sa.query(HgAppSettings).scalar() | |
95 | finally: |
|
104 | finally: | |
96 | meta.Session.remove() |
|
105 | meta.Session.remove() | |
97 |
|
106 | |||
98 | if not ret: |
|
107 | if not ret: | |
99 | raise Exception('Could not get application settings !') |
|
108 | raise Exception('Could not get application settings !') | |
100 | return ret |
|
109 | return ret | |
101 |
|
110 | |||
102 | def make_ui(read_from='file', path=None, checkpaths=True): |
|
111 | def make_ui(read_from='file', path=None, checkpaths=True): | |
103 | """ |
|
112 | """ | |
104 | A function that will read python rc files or database |
|
113 | A function that will read python rc files or database | |
105 | and make an mercurial ui object from read options |
|
114 | and make an mercurial ui object from read options | |
106 |
|
115 | |||
107 | @param path: path to mercurial config file |
|
116 | @param path: path to mercurial config file | |
108 | @param checkpaths: check the path |
|
117 | @param checkpaths: check the path | |
109 | @param read_from: read from 'file' or 'db' |
|
118 | @param read_from: read from 'file' or 'db' | |
110 | """ |
|
119 | """ | |
111 | #propagated from mercurial documentation |
|
120 | #propagated from mercurial documentation | |
112 | sections = ['alias', 'auth', |
|
121 | sections = ['alias', 'auth', | |
113 | 'decode/encode', 'defaults', |
|
122 | 'decode/encode', 'defaults', | |
114 | 'diff', 'email', |
|
123 | 'diff', 'email', | |
115 | 'extensions', 'format', |
|
124 | 'extensions', 'format', | |
116 | 'merge-patterns', 'merge-tools', |
|
125 | 'merge-patterns', 'merge-tools', | |
117 | 'hooks', 'http_proxy', |
|
126 | 'hooks', 'http_proxy', | |
118 | 'smtp', 'patch', |
|
127 | 'smtp', 'patch', | |
119 | 'paths', 'profiling', |
|
128 | 'paths', 'profiling', | |
120 | 'server', 'trusted', |
|
129 | 'server', 'trusted', | |
121 | 'ui', 'web', ] |
|
130 | 'ui', 'web', ] | |
122 | baseui = ui.ui() |
|
131 | baseui = ui.ui() | |
123 |
|
132 | |||
124 |
|
133 | |||
125 | if read_from == 'file': |
|
134 | if read_from == 'file': | |
126 | if not os.path.isfile(path): |
|
135 | if not os.path.isfile(path): | |
127 | log.warning('Unable to read config file %s' % path) |
|
136 | log.warning('Unable to read config file %s' % path) | |
128 | return False |
|
137 | return False | |
129 |
|
138 | |||
130 | cfg = config.config() |
|
139 | cfg = config.config() | |
131 | cfg.read(path) |
|
140 | cfg.read(path) | |
132 | for section in sections: |
|
141 | for section in sections: | |
133 | for k, v in cfg.items(section): |
|
142 | for k, v in cfg.items(section): | |
134 | baseui.setconfig(section, k, v) |
|
143 | baseui.setconfig(section, k, v) | |
135 | if checkpaths:check_repo_dir(cfg.items('paths')) |
|
144 | if checkpaths:check_repo_dir(cfg.items('paths')) | |
136 |
|
145 | |||
137 |
|
146 | |||
138 | elif read_from == 'db': |
|
147 | elif read_from == 'db': | |
139 | hg_ui = get_hg_ui_cached() |
|
148 | hg_ui = get_hg_ui_cached() | |
140 | for ui_ in hg_ui: |
|
149 | for ui_ in hg_ui: | |
141 | baseui.setconfig(ui_.ui_section, ui_.ui_key, ui_.ui_value) |
|
150 | baseui.setconfig(ui_.ui_section, ui_.ui_key, ui_.ui_value) | |
142 |
|
151 | |||
143 |
|
152 | |||
144 | return baseui |
|
153 | return baseui | |
145 |
|
154 | |||
146 |
|
155 | |||
147 | def set_hg_app_config(config): |
|
156 | def set_hg_app_config(config): | |
148 | hgsettings = get_hg_settings() |
|
157 | hgsettings = get_hg_settings() | |
149 | config['hg_app_auth_realm'] = hgsettings.app_auth_realm |
|
158 | config['hg_app_auth_realm'] = hgsettings.app_auth_realm | |
150 | config['hg_app_name'] = hgsettings.app_title |
|
159 | config['hg_app_name'] = hgsettings.app_title | |
151 |
|
160 | |||
152 | def invalidate_cache(name, *args): |
|
161 | def invalidate_cache(name, *args): | |
153 | """Invalidates given name cache""" |
|
162 | """Invalidates given name cache""" | |
154 |
|
163 | |||
155 | from beaker.cache import region_invalidate |
|
164 | from beaker.cache import region_invalidate | |
156 | log.info('INVALIDATING CACHE FOR %s', name) |
|
165 | log.info('INVALIDATING CACHE FOR %s', name) | |
157 |
|
166 | |||
158 | """propagate our arguments to make sure invalidation works. First |
|
167 | """propagate our arguments to make sure invalidation works. First | |
159 | argument has to be the name of cached func name give to cache decorator |
|
168 | argument has to be the name of cached func name give to cache decorator | |
160 | without that the invalidation would not work""" |
|
169 | without that the invalidation would not work""" | |
161 | tmp = [name] |
|
170 | tmp = [name] | |
162 | tmp.extend(args) |
|
171 | tmp.extend(args) | |
163 | args = tuple(tmp) |
|
172 | args = tuple(tmp) | |
164 |
|
173 | |||
165 | if name == 'cached_repo_list': |
|
174 | if name == 'cached_repo_list': | |
166 | from pylons_app.model.hg_model import _get_repos_cached |
|
175 | from pylons_app.model.hg_model import _get_repos_cached | |
167 | region_invalidate(_get_repos_cached, None, *args) |
|
176 | region_invalidate(_get_repos_cached, None, *args) | |
168 |
|
177 | |||
169 | if name == 'full_changelog': |
|
178 | if name == 'full_changelog': | |
170 | from pylons_app.model.hg_model import _full_changelog_cached |
|
179 | from pylons_app.model.hg_model import _full_changelog_cached | |
171 | region_invalidate(_full_changelog_cached, None, *args) |
|
180 | region_invalidate(_full_changelog_cached, None, *args) | |
172 |
|
181 | |||
173 | from vcs.backends.base import BaseChangeset |
|
|||
174 | from vcs.utils.lazy import LazyProperty |
|
|||
175 | class EmptyChangeset(BaseChangeset): |
|
182 | class EmptyChangeset(BaseChangeset): | |
176 |
|
183 | |||
177 | revision = -1 |
|
184 | revision = -1 | |
178 | message = '' |
|
185 | message = '' | |
179 |
|
186 | |||
180 | @LazyProperty |
|
187 | @LazyProperty | |
181 | def raw_id(self): |
|
188 | def raw_id(self): | |
182 | """ |
|
189 | """ | |
183 | Returns raw string identifing this changeset, useful for web |
|
190 | Returns raw string identifing this changeset, useful for web | |
184 | representation. |
|
191 | representation. | |
185 | """ |
|
192 | """ | |
186 | return '0' * 12 |
|
193 | return '0' * 12 | |
187 |
|
194 | |||
188 |
|
195 | |||
189 | def repo2db_mapper(initial_repo_list, remove_obsolete=False): |
|
196 | def repo2db_mapper(initial_repo_list, remove_obsolete=False): | |
190 | """ |
|
197 | """ | |
191 | maps all found repositories into db |
|
198 | maps all found repositories into db | |
192 | """ |
|
199 | """ | |
193 | from pylons_app.model.repo_model import RepoModel |
|
200 | from pylons_app.model.repo_model import RepoModel | |
194 |
|
201 | |||
195 | sa = meta.Session |
|
202 | sa = meta.Session | |
196 | user = sa.query(User).filter(User.admin == True).first() |
|
203 | user = sa.query(User).filter(User.admin == True).first() | |
197 |
|
204 | |||
198 | rm = RepoModel() |
|
205 | rm = RepoModel() | |
199 |
|
206 | |||
200 | for name, repo in initial_repo_list.items(): |
|
207 | for name, repo in initial_repo_list.items(): | |
201 | if not sa.query(Repository).get(name): |
|
208 | if not sa.query(Repository).get(name): | |
202 | log.info('repository %s not found creating default', name) |
|
209 | log.info('repository %s not found creating default', name) | |
203 |
|
210 | |||
204 | form_data = { |
|
211 | form_data = { | |
205 | 'repo_name':name, |
|
212 | 'repo_name':name, | |
206 | 'description':repo.description if repo.description != 'unknown' else \ |
|
213 | 'description':repo.description if repo.description != 'unknown' else \ | |
207 | 'auto description for %s' % name, |
|
214 | 'auto description for %s' % name, | |
208 | 'private':False |
|
215 | 'private':False | |
209 | } |
|
216 | } | |
210 | rm.create(form_data, user, just_db=True) |
|
217 | rm.create(form_data, user, just_db=True) | |
211 |
|
218 | |||
212 |
|
219 | |||
213 | if remove_obsolete: |
|
220 | if remove_obsolete: | |
214 | #remove from database those repositories that are not in the filesystem |
|
221 | #remove from database those repositories that are not in the filesystem | |
215 | for repo in sa.query(Repository).all(): |
|
222 | for repo in sa.query(Repository).all(): | |
216 | if repo.repo_name not in initial_repo_list.keys(): |
|
223 | if repo.repo_name not in initial_repo_list.keys(): | |
217 | sa.delete(repo) |
|
224 | sa.delete(repo) | |
218 | sa.commit() |
|
225 | sa.commit() | |
219 |
|
226 | |||
220 |
|
227 | |||
221 | meta.Session.remove() |
|
228 | meta.Session.remove() |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now