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