Mercurial > public > src > rhodecode
comparison pylons_app/lib/utils.py @ 308:248642ed1912
new repo2db mapper, now uses repoModel to make scanns
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 25 Jun 2010 02:06:45 +0200 |
parents | cd2ee462fc2c |
children | a074dec6ee40 |
comparison
equal
deleted
inserted
replaced
306:c60eeeb8ac4d | 308:248642ed1912 |
---|---|
1 #!/usr/bin/env python | |
2 # encoding: utf-8 | |
3 # Utilities for hg app | |
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
5 | |
6 # This program is free software; you can redistribute it and/or | |
7 # modify it under the terms of the GNU General Public License | |
8 # as published by the Free Software Foundation; version 2 | |
9 # of the License or (at your opinion) any later version of the license. | |
10 # | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU General Public License for more details. | |
15 # | |
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 | |
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
19 # MA 02110-1301, USA. | |
20 | |
21 """ | |
22 Created on April 18, 2010 | |
23 Utilities for hg app | |
24 @author: marcink | |
25 """ | |
26 | |
27 import os | |
28 import logging | |
29 from mercurial import ui, config, hg | |
30 from mercurial.error import RepoError | |
31 from pylons_app.model.db import Repository, User | |
32 log = logging.getLogger(__name__) | |
33 | |
34 | |
35 def get_repo_slug(request): | |
36 return request.environ['pylons.routes_dict'].get('repo_name') | |
37 | |
38 def is_mercurial(environ): | |
39 """ | |
40 Returns True if request's target is mercurial server - header | |
41 ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``. | |
42 """ | |
43 http_accept = environ.get('HTTP_ACCEPT') | |
44 if http_accept and http_accept.startswith('application/mercurial'): | |
45 return True | |
46 return False | |
47 | |
48 def check_repo_dir(paths): | |
49 repos_path = paths[0][1].split('/') | |
50 if repos_path[-1] in ['*', '**']: | |
51 repos_path = repos_path[:-1] | |
52 if repos_path[0] != '/': | |
53 repos_path[0] = '/' | |
54 if not os.path.isdir(os.path.join(*repos_path)): | |
55 raise Exception('Not a valid repository in %s' % paths[0][1]) | |
56 | |
57 def check_repo(repo_name, base_path): | |
58 | |
59 repo_path = os.path.join(base_path, repo_name) | |
60 | |
61 try: | |
62 r = hg.repository(ui.ui(), repo_path) | |
63 hg.verify(r) | |
64 #here we hnow that repo exists it was verified | |
65 log.info('%s repo is already created', repo_name) | |
66 return False | |
67 #raise Exception('Repo exists') | |
68 except RepoError: | |
69 log.info('%s repo is free for creation', repo_name) | |
70 #it means that there is no valid repo there... | |
71 return True | |
72 | |
73 def make_ui(path=None, checkpaths=True): | |
74 """ | |
75 A funcion that will read python rc files and make an ui from read options | |
76 | |
77 @param path: path to mercurial config file | |
78 """ | |
79 if not path: | |
80 log.error('repos config path is empty !') | |
81 | |
82 if not os.path.isfile(path): | |
83 log.warning('Unable to read config file %s' % path) | |
84 return False | |
85 #propagated from mercurial documentation | |
86 sections = [ | |
87 'alias', | |
88 'auth', | |
89 'decode/encode', | |
90 'defaults', | |
91 'diff', | |
92 'email', | |
93 'extensions', | |
94 'format', | |
95 'merge-patterns', | |
96 'merge-tools', | |
97 'hooks', | |
98 'http_proxy', | |
99 'smtp', | |
100 'patch', | |
101 'paths', | |
102 'profiling', | |
103 'server', | |
104 'trusted', | |
105 'ui', | |
106 'web', | |
107 ] | |
108 | |
109 baseui = ui.ui() | |
110 cfg = config.config() | |
111 cfg.read(path) | |
112 if checkpaths:check_repo_dir(cfg.items('paths')) | |
113 | |
114 for section in sections: | |
115 for k, v in cfg.items(section): | |
116 baseui.setconfig(section, k, v) | |
117 | |
118 return baseui | |
119 | |
120 def invalidate_cache(name, *args): | |
121 """Invalidates given name cache""" | |
122 | |
123 from beaker.cache import region_invalidate | |
124 log.info('INVALIDATING CACHE FOR %s', name) | |
125 | |
126 """propagate our arguments to make sure invalidation works. First | |
127 argument has to be the name of cached func name give to cache decorator | |
128 without that the invalidation would not work""" | |
129 tmp = [name] | |
130 tmp.extend(args) | |
131 args = tuple(tmp) | |
132 | |
133 if name == 'cached_repo_list': | |
134 from pylons_app.model.hg_model import _get_repos_cached | |
135 region_invalidate(_get_repos_cached, None, *args) | |
136 | |
137 if name == 'full_changelog': | |
138 from pylons_app.model.hg_model import _full_changelog_cached | |
139 region_invalidate(_full_changelog_cached, None, *args) | |
140 | |
141 from vcs.backends.base import BaseChangeset | |
142 from vcs.utils.lazy import LazyProperty | |
143 class EmptyChangeset(BaseChangeset): | |
144 | |
145 revision = -1 | |
146 message = '' | |
147 | |
148 @LazyProperty | |
149 def raw_id(self): | |
150 """ | |
151 Returns raw string identifing this changeset, useful for web | |
152 representation. | |
153 """ | |
154 return '0' * 12 | |
155 | |
156 | |
157 def repo2db_mapper(initial_repo_list): | |
158 """ | |
159 maps all found repositories into db | |
160 """ | |
161 from pylons_app.model.meta import Session | |
162 sa = Session() | |
163 user = sa.query(User).filter(User.admin == True).first() | |
164 for name, repo in initial_repo_list.items(): | |
165 if not sa.query(Repository).get(name): | |
166 log.info('%s not found creating default', name) | |
167 try: | |
168 | |
169 new_repo = Repository() | |
170 new_repo.repo_name = name | |
171 desc = repo.description if repo.description != 'unknown' else \ | |
172 'auto description for %s' % name | |
173 new_repo.description = desc | |
174 new_repo.user_id = user.user_id | |
175 new_repo.private = False | |
176 sa.add(new_repo) | |
177 sa.commit() | |
178 except: | |
179 sa.rollback() | |
180 raise | |
181 |