Mercurial > kallithea
annotate rhodecode/lib/utils2.py @ 3540:6e8027c2f49c beta
Merge
author | Leonardo <leo@unity3d.com> |
---|---|
date | Mon, 11 Mar 2013 17:09:43 +0100 |
parents | ed48d17836a4 867c4d7fd2d8 |
children | 7967d00e4692 |
rev | line source |
---|---|
2109 | 1 # -*- coding: utf-8 -*- |
2 """ | |
3 rhodecode.lib.utils | |
4 ~~~~~~~~~~~~~~~~~~~ | |
5 | |
6 Some simple helper functions | |
7 | |
8 :created_on: Jan 5, 2011 | |
9 :author: marcink | |
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> | |
11 :license: GPLv3, see COPYING for more details. | |
12 """ | |
13 # This program is free software: you can redistribute it and/or modify | |
14 # it under the terms of the GNU General Public License as published by | |
15 # the Free Software Foundation, either version 3 of the License, or | |
16 # (at your option) any later version. | |
17 # | |
18 # This program is distributed in the hope that it will be useful, | |
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 # GNU General Public License for more details. | |
22 # | |
23 # You should have received a copy of the GNU General Public License | |
24 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 | |
26 import re | |
2699
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
27 import time |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
28 import datetime |
2969
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
29 import webob |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
30 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
31 from pylons.i18n.translation import _, ungettext |
2109 | 32 from rhodecode.lib.vcs.utils.lazy import LazyProperty |
33 | |
34 | |
35 def __get_lem(): | |
36 """ | |
37 Get language extension map based on what's inside pygments lexers | |
38 """ | |
39 from pygments import lexers | |
40 from string import lower | |
41 from collections import defaultdict | |
42 | |
43 d = defaultdict(lambda: []) | |
44 | |
45 def __clean(s): | |
46 s = s.lstrip('*') | |
47 s = s.lstrip('.') | |
48 | |
49 if s.find('[') != -1: | |
50 exts = [] | |
51 start, stop = s.find('['), s.find(']') | |
52 | |
53 for suffix in s[start + 1:stop]: | |
54 exts.append(s[:s.find('[')] + suffix) | |
55 return map(lower, exts) | |
56 else: | |
57 return map(lower, [s]) | |
58 | |
59 for lx, t in sorted(lexers.LEXERS.items()): | |
60 m = map(__clean, t[-2]) | |
61 if m: | |
62 m = reduce(lambda x, y: x + y, m) | |
63 for ext in m: | |
64 desc = lx.replace('Lexer', '') | |
65 d[ext].append(desc) | |
66 | |
67 return dict(d) | |
68 | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
69 |
2109 | 70 def str2bool(_str): |
71 """ | |
72 returs True/False value from given string, it tries to translate the | |
73 string into boolean | |
74 | |
75 :param _str: string value to translate into boolean | |
76 :rtype: boolean | |
77 :returns: boolean from given string | |
78 """ | |
79 if _str is None: | |
80 return False | |
81 if _str in (True, False): | |
82 return _str | |
83 _str = str(_str).strip().lower() | |
84 return _str in ('t', 'true', 'y', 'yes', 'on', '1') | |
85 | |
86 | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
87 def aslist(obj, sep=None, strip=True): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
88 """ |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
89 Returns given string separated by sep as list |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
90 |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
91 :param obj: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
92 :param sep: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
93 :param strip: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
94 """ |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
95 if isinstance(obj, (basestring)): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
96 lst = obj.split(sep) |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
97 if strip: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
98 lst = [v.strip() for v in lst] |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
99 return lst |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
100 elif isinstance(obj, (list, tuple)): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
101 return obj |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
102 elif obj is None: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
103 return [] |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
104 else: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
105 return [obj] |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
106 |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
107 |
2109 | 108 def convert_line_endings(line, mode): |
109 """ | |
110 Converts a given line "line end" accordingly to given mode | |
111 | |
112 Available modes are:: | |
113 0 - Unix | |
114 1 - Mac | |
115 2 - DOS | |
116 | |
117 :param line: given line to convert | |
118 :param mode: mode to convert to | |
119 :rtype: str | |
120 :return: converted line according to mode | |
121 """ | |
122 from string import replace | |
123 | |
124 if mode == 0: | |
125 line = replace(line, '\r\n', '\n') | |
126 line = replace(line, '\r', '\n') | |
127 elif mode == 1: | |
128 line = replace(line, '\r\n', '\r') | |
129 line = replace(line, '\n', '\r') | |
130 elif mode == 2: | |
131 line = re.sub("\r(?!\n)|(?<!\r)\n", "\r\n", line) | |
132 return line | |
133 | |
134 | |
135 def detect_mode(line, default): | |
136 """ | |
137 Detects line break for given line, if line break couldn't be found | |
138 given default value is returned | |
139 | |
140 :param line: str line | |
141 :param default: default | |
142 :rtype: int | |
143 :return: value of line end on of 0 - Unix, 1 - Mac, 2 - DOS | |
144 """ | |
145 if line.endswith('\r\n'): | |
146 return 2 | |
147 elif line.endswith('\n'): | |
148 return 0 | |
149 elif line.endswith('\r'): | |
150 return 1 | |
151 else: | |
152 return default | |
153 | |
154 | |
155 def generate_api_key(username, salt=None): | |
156 """ | |
157 Generates unique API key for given username, if salt is not given | |
158 it'll be generated from some random string | |
159 | |
160 :param username: username as string | |
161 :param salt: salt to hash generate KEY | |
162 :rtype: str | |
163 :returns: sha1 hash from username+salt | |
164 """ | |
165 from tempfile import _RandomNameSequence | |
166 import hashlib | |
167 | |
168 if salt is None: | |
169 salt = _RandomNameSequence().next() | |
170 | |
171 return hashlib.sha1(username + salt).hexdigest() | |
172 | |
173 | |
2845
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
174 def safe_int(val, default=None): |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
175 """ |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
176 Returns int() of val if val is not convertable to int use default |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
177 instead |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
178 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
179 :param val: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
180 :param default: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
181 """ |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
182 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
183 try: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
184 val = int(val) |
3218
c0b0351233ec
safe_int should also catch TypeError
Marcin Kuzminski <marcin@python-works.com>
parents:
3056
diff
changeset
|
185 except (ValueError, TypeError): |
2845
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
186 val = default |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
187 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
188 return val |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
189 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
190 |
2109 | 191 def safe_unicode(str_, from_encoding=None): |
192 """ | |
193 safe unicode function. Does few trick to turn str_ into unicode | |
194 | |
195 In case of UnicodeDecode error we try to return it with encoding detected | |
196 by chardet library if it fails fallback to unicode with errors replaced | |
197 | |
198 :param str_: string to decode | |
199 :rtype: unicode | |
200 :returns: unicode object | |
201 """ | |
202 if isinstance(str_, unicode): | |
203 return str_ | |
204 | |
205 if not from_encoding: | |
206 import rhodecode | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
207 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding', |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
208 'utf8'), sep=',') |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
209 from_encoding = DEFAULT_ENCODINGS |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
210 |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
211 if not isinstance(from_encoding, (list, tuple)): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
212 from_encoding = [from_encoding] |
2109 | 213 |
214 try: | |
215 return unicode(str_) | |
216 except UnicodeDecodeError: | |
217 pass | |
218 | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
219 for enc in from_encoding: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
220 try: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
221 return unicode(str_, enc) |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
222 except UnicodeDecodeError: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
223 pass |
2109 | 224 |
225 try: | |
226 import chardet | |
227 encoding = chardet.detect(str_)['encoding'] | |
228 if encoding is None: | |
229 raise Exception() | |
230 return str_.decode(encoding) | |
231 except (ImportError, UnicodeDecodeError, Exception): | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
232 return unicode(str_, from_encoding[0], 'replace') |
2109 | 233 |
234 | |
235 def safe_str(unicode_, to_encoding=None): | |
236 """ | |
237 safe str function. Does few trick to turn unicode_ into string | |
238 | |
239 In case of UnicodeEncodeError we try to return it with encoding detected | |
240 by chardet library if it fails fallback to string with errors replaced | |
241 | |
242 :param unicode_: unicode to encode | |
243 :rtype: str | |
244 :returns: str object | |
245 """ | |
246 | |
247 # if it's not basestr cast to str | |
248 if not isinstance(unicode_, basestring): | |
249 return str(unicode_) | |
250 | |
251 if isinstance(unicode_, str): | |
252 return unicode_ | |
253 | |
254 if not to_encoding: | |
255 import rhodecode | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
256 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding', |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
257 'utf8'), sep=',') |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
258 to_encoding = DEFAULT_ENCODINGS |
2109 | 259 |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
260 if not isinstance(to_encoding, (list, tuple)): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
261 to_encoding = [to_encoding] |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
262 |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
263 for enc in to_encoding: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
264 try: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
265 return unicode_.encode(enc) |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
266 except UnicodeEncodeError: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
267 pass |
2109 | 268 |
269 try: | |
270 import chardet | |
271 encoding = chardet.detect(unicode_)['encoding'] | |
272 if encoding is None: | |
273 raise UnicodeEncodeError() | |
274 | |
275 return unicode_.encode(encoding) | |
276 except (ImportError, UnicodeEncodeError): | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
277 return unicode_.encode(to_encoding[0], 'replace') |
2109 | 278 |
279 return safe_str | |
280 | |
281 | |
3018
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
282 def remove_suffix(s, suffix): |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
283 if s.endswith(suffix): |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
284 s = s[:-1 * len(suffix)] |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
285 return s |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
286 |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
287 |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
288 def remove_prefix(s, prefix): |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
289 if s.startswith(prefix): |
3056
6104dfd35b16
Implemented #379 defaults settings page for creation of repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3018
diff
changeset
|
290 s = s[len(prefix):] |
3018
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
291 return s |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
292 |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
293 |
2109 | 294 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs): |
295 """ | |
296 Custom engine_from_config functions that makes sure we use NullPool for | |
297 file based sqlite databases. This prevents errors on sqlite. This only | |
298 applies to sqlalchemy versions < 0.7.0 | |
299 | |
300 """ | |
301 import sqlalchemy | |
302 from sqlalchemy import engine_from_config as efc | |
303 import logging | |
304 | |
305 if int(sqlalchemy.__version__.split('.')[1]) < 7: | |
306 | |
307 # This solution should work for sqlalchemy < 0.7.0, and should use | |
308 # proxy=TimerProxy() for execution time profiling | |
309 | |
310 from sqlalchemy.pool import NullPool | |
311 url = configuration[prefix + 'url'] | |
312 | |
313 if url.startswith('sqlite'): | |
314 kwargs.update({'poolclass': NullPool}) | |
315 return efc(configuration, prefix, **kwargs) | |
316 else: | |
317 import time | |
318 from sqlalchemy import event | |
319 from sqlalchemy.engine import Engine | |
320 | |
321 log = logging.getLogger('sqlalchemy.engine') | |
322 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) | |
323 engine = efc(configuration, prefix, **kwargs) | |
324 | |
325 def color_sql(sql): | |
326 COLOR_SEQ = "\033[1;%dm" | |
327 COLOR_SQL = YELLOW | |
328 normal = '\x1b[0m' | |
329 return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal]) | |
330 | |
331 if configuration['debug']: | |
332 #attach events only for debug configuration | |
333 | |
334 def before_cursor_execute(conn, cursor, statement, | |
335 parameters, context, executemany): | |
336 context._query_start_time = time.time() | |
337 log.info(color_sql(">>>>> STARTING QUERY >>>>>")) | |
338 | |
339 def after_cursor_execute(conn, cursor, statement, | |
340 parameters, context, executemany): | |
341 total = time.time() - context._query_start_time | |
342 log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total)) | |
343 | |
344 event.listen(engine, "before_cursor_execute", | |
345 before_cursor_execute) | |
346 event.listen(engine, "after_cursor_execute", | |
347 after_cursor_execute) | |
348 | |
349 return engine | |
350 | |
351 | |
3536 | 352 def age(prevdate, show_short_version=False): |
2109 | 353 """ |
354 turns a datetime into an age string. | |
3536 | 355 If show_short_version is True, then it will generate a not so accurate but shorter string, |
356 example: 2days ago, instead of 2 days and 23 hours ago. | |
357 | |
2109 | 358 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
359 :param prevdate: datetime object |
3536 | 360 :param show_short_version: if it should aproximate the date and return a shorter string |
2109 | 361 :rtype: unicode |
362 :returns: unicode words describing age | |
363 """ | |
3261
54a439edaf99
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents:
3218
diff
changeset
|
364 now = datetime.datetime.now() |
54a439edaf99
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents:
3218
diff
changeset
|
365 now = now.replace(microsecond=0) |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
366 order = ['year', 'month', 'day', 'hour', 'minute', 'second'] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
367 deltas = {} |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
368 future = False |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
369 |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
370 if prevdate > now: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
371 now, prevdate = prevdate, now |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
372 future = True |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
373 |
3261
54a439edaf99
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents:
3218
diff
changeset
|
374 # Get date parts deltas |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
375 for part in order: |
3261
54a439edaf99
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents:
3218
diff
changeset
|
376 if future: |
3378
24ffe239e3d7
fix dateutil import
Marcin Kuzminski <marcin@python-works.com>
parents:
3261
diff
changeset
|
377 from dateutil import relativedelta |
24ffe239e3d7
fix dateutil import
Marcin Kuzminski <marcin@python-works.com>
parents:
3261
diff
changeset
|
378 d = relativedelta.relativedelta(now, prevdate) |
3261
54a439edaf99
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents:
3218
diff
changeset
|
379 deltas[part] = getattr(d, part + 's') |
54a439edaf99
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents:
3218
diff
changeset
|
380 else: |
54a439edaf99
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents:
3218
diff
changeset
|
381 deltas[part] = getattr(now, part) - getattr(prevdate, part) |
2109 | 382 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
383 # Fix negative offsets (there is 1 second between 10:59:59 and 11:00:00, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
384 # not 1 hour, -59 minutes and -59 seconds) |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
385 for num, length in [(5, 60), (4, 60), (3, 24)]: # seconds, minutes, hours |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
386 part = order[num] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
387 carry_part = order[num - 1] |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
388 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
389 if deltas[part] < 0: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
390 deltas[part] += length |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
391 deltas[carry_part] -= 1 |
2109 | 392 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
393 # Same thing for days except that the increment depends on the (variable) |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
394 # number of days in the month |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
395 month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
396 if deltas['day'] < 0: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
397 if prevdate.month == 2 and (prevdate.year % 4 == 0 and |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
398 (prevdate.year % 100 != 0 or prevdate.year % 400 == 0)): |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
399 deltas['day'] += 29 |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
400 else: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
401 deltas['day'] += month_lengths[prevdate.month - 1] |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
402 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
403 deltas['month'] -= 1 |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
404 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
405 if deltas['month'] < 0: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
406 deltas['month'] += 12 |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
407 deltas['year'] -= 1 |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
408 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
409 # Format the result |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
410 fmt_funcs = { |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
411 'year': lambda d: ungettext(u'%d year', '%d years', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
412 'month': lambda d: ungettext(u'%d month', '%d months', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
413 'day': lambda d: ungettext(u'%d day', '%d days', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
414 'hour': lambda d: ungettext(u'%d hour', '%d hours', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
415 'minute': lambda d: ungettext(u'%d minute', '%d minutes', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
416 'second': lambda d: ungettext(u'%d second', '%d seconds', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
417 } |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
418 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
419 for i, part in enumerate(order): |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
420 value = deltas[part] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
421 if value == 0: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
422 continue |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
423 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
424 if i < 5: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
425 sub_part = order[i + 1] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
426 sub_value = deltas[sub_part] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
427 else: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
428 sub_value = 0 |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
429 |
3536 | 430 if sub_value == 0 or show_short_version: |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
431 if future: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
432 return _(u'in %s') % fmt_funcs[part](value) |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
433 else: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
434 return _(u'%s ago') % fmt_funcs[part](value) |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
435 if future: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
436 return _(u'in %s and %s') % (fmt_funcs[part](value), |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
437 fmt_funcs[sub_part](sub_value)) |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
438 else: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
439 return _(u'%s and %s ago') % (fmt_funcs[part](value), |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
440 fmt_funcs[sub_part](sub_value)) |
2109 | 441 |
442 return _(u'just now') | |
443 | |
444 | |
445 def uri_filter(uri): | |
446 """ | |
447 Removes user:password from given url string | |
448 | |
449 :param uri: | |
450 :rtype: unicode | |
451 :returns: filtered list of strings | |
452 """ | |
453 if not uri: | |
454 return '' | |
455 | |
456 proto = '' | |
457 | |
458 for pat in ('https://', 'http://'): | |
459 if uri.startswith(pat): | |
460 uri = uri[len(pat):] | |
461 proto = pat | |
462 break | |
463 | |
464 # remove passwords and username | |
465 uri = uri[uri.find('@') + 1:] | |
466 | |
467 # get the port | |
468 cred_pos = uri.find(':') | |
469 if cred_pos == -1: | |
470 host, port = uri, None | |
471 else: | |
472 host, port = uri[:cred_pos], uri[cred_pos + 1:] | |
473 | |
474 return filter(None, [proto, host, port]) | |
475 | |
476 | |
477 def credentials_filter(uri): | |
478 """ | |
479 Returns a url with removed credentials | |
480 | |
481 :param uri: | |
482 """ | |
483 | |
484 uri = uri_filter(uri) | |
485 #check if we have port | |
486 if len(uri) > 2 and uri[2]: | |
487 uri[2] = ':' + uri[2] | |
488 | |
489 return ''.join(uri) | |
490 | |
491 | |
492 def get_changeset_safe(repo, rev): | |
493 """ | |
494 Safe version of get_changeset if this changeset doesn't exists for a | |
495 repo it returns a Dummy one instead | |
496 | |
497 :param repo: | |
498 :param rev: | |
499 """ | |
500 from rhodecode.lib.vcs.backends.base import BaseRepository | |
501 from rhodecode.lib.vcs.exceptions import RepositoryError | |
2684
2b6939a77052
Bumped mercurial version to 2.3
Marcin Kuzminski <marcin@python-works.com>
parents:
2674
diff
changeset
|
502 from rhodecode.lib.vcs.backends.base import EmptyChangeset |
2109 | 503 if not isinstance(repo, BaseRepository): |
504 raise Exception('You must pass an Repository ' | |
505 'object as first argument got %s', type(repo)) | |
506 | |
507 try: | |
508 cs = repo.get_changeset(rev) | |
509 except RepositoryError: | |
510 cs = EmptyChangeset(requested_revision=rev) | |
511 return cs | |
512 | |
513 | |
2699
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
514 def datetime_to_time(dt): |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
515 if dt: |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
516 return time.mktime(dt.timetuple()) |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
517 |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
518 |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
519 def time_to_datetime(tm): |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
520 if tm: |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
521 if isinstance(tm, basestring): |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
522 try: |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
523 tm = float(tm) |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
524 except ValueError: |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
525 return |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
526 return datetime.datetime.fromtimestamp(tm) |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
527 |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
528 MENTIONS_REGEX = r'(?:^@|\s@)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+)(?:\s{1})' |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
529 |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
530 |
2109 | 531 def extract_mentioned_users(s): |
532 """ | |
533 Returns unique usernames from given string s that have @mention | |
534 | |
535 :param s: string to get mentions | |
536 """ | |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
537 usrs = set() |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
538 for username in re.findall(MENTIONS_REGEX, s): |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
539 usrs.add(username) |
2109 | 540 |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
541 return sorted(list(usrs), key=lambda k: k.lower()) |
2674
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
542 |
2699
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
543 |
2674
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
544 class AttributeDict(dict): |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
545 def __getattr__(self, attr): |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
546 return self.get(attr, None) |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
547 __setattr__ = dict.__setitem__ |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
548 __delattr__ = dict.__delitem__ |
2869
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
549 |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
550 |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
551 def fix_PATH(os_=None): |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
552 """ |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
553 Get current active python path, and append it to PATH variable to fix issues |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
554 of subprocess calls and different python versions |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
555 """ |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
556 import sys |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
557 if os_ is None: |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
558 import os |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
559 else: |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
560 os = os_ |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
561 |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
562 cur_path = os.path.split(sys.executable)[0] |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
563 if not os.environ['PATH'].startswith(cur_path): |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
564 os.environ['PATH'] = '%s:%s' % (cur_path, os.environ['PATH']) |
2882
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
565 |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
566 |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
567 def obfuscate_url_pw(engine): |
3454
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
568 _url = engine or '' |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
569 from sqlalchemy.engine import url as sa_url |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
570 try: |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
571 _url = sa_url.make_url(engine) |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
572 if _url.password: |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
573 _url.password = 'XXXXX' |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
574 except: |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
575 pass |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
576 return str(_url) |
2969
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
577 |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
578 |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
579 def get_server_url(environ): |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
580 req = webob.Request(environ) |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
581 return req.host_url + req.script_name |