Mercurial > kallithea
comparison rhodecode/lib/utils.py @ 2165:dc2584ba5fbc
merged beta into default branch
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 28 Mar 2012 19:54:16 +0200 |
parents | 6c6718c06ea2 d25bd432bc3e |
children | a437a986d399 |
comparison
equal
deleted
inserted
replaced
2097:8fd6650bb436 | 2165:dc2584ba5fbc |
---|---|
49 | 49 |
50 from rhodecode.lib.caching_query import FromCache | 50 from rhodecode.lib.caching_query import FromCache |
51 | 51 |
52 from rhodecode.model import meta | 52 from rhodecode.model import meta |
53 from rhodecode.model.db import Repository, User, RhodeCodeUi, \ | 53 from rhodecode.model.db import Repository, User, RhodeCodeUi, \ |
54 UserLog, RepoGroup, RhodeCodeSetting, UserRepoGroupToPerm | 54 UserLog, RepoGroup, RhodeCodeSetting, UserRepoGroupToPerm,\ |
55 CacheInvalidation | |
55 from rhodecode.model.meta import Session | 56 from rhodecode.model.meta import Session |
56 from rhodecode.model.repos_group import ReposGroupModel | 57 from rhodecode.model.repos_group import ReposGroupModel |
58 from rhodecode.lib.utils2 import safe_str, safe_unicode | |
59 from rhodecode.lib.vcs.utils.fakemod import create_module | |
57 | 60 |
58 log = logging.getLogger(__name__) | 61 log = logging.getLogger(__name__) |
59 | 62 |
60 REMOVED_REPO_PAT = re.compile(r'rm__\d{8}_\d{6}_\d{6}__.*') | 63 REMOVED_REPO_PAT = re.compile(r'rm__\d{8}_\d{6}_\d{6}__.*') |
61 | 64 |
62 | 65 |
63 def recursive_replace(str_, replace=' '): | 66 def recursive_replace(str_, replace=' '): |
64 """Recursive replace of given sign to just one instance | 67 """ |
68 Recursive replace of given sign to just one instance | |
65 | 69 |
66 :param str_: given string | 70 :param str_: given string |
67 :param replace: char to find and replace multiple instances | 71 :param replace: char to find and replace multiple instances |
68 | 72 |
69 Examples:: | 73 Examples:: |
77 str_ = str_.replace(replace * 2, replace) | 81 str_ = str_.replace(replace * 2, replace) |
78 return recursive_replace(str_, replace) | 82 return recursive_replace(str_, replace) |
79 | 83 |
80 | 84 |
81 def repo_name_slug(value): | 85 def repo_name_slug(value): |
82 """Return slug of name of repository | 86 """ |
87 Return slug of name of repository | |
83 This function is called on each creation/modification | 88 This function is called on each creation/modification |
84 of repository to prevent bad names in repo | 89 of repository to prevent bad names in repo |
85 """ | 90 """ |
86 | 91 |
87 slug = remove_formatting(value) | 92 slug = remove_formatting(value) |
152 | 157 |
153 user_log.action_date = datetime.datetime.now() | 158 user_log.action_date = datetime.datetime.now() |
154 user_log.user_ip = ipaddr | 159 user_log.user_ip = ipaddr |
155 sa.add(user_log) | 160 sa.add(user_log) |
156 | 161 |
157 log.info('Adding user %s, action %s on %s' % (user_obj, action, repo)) | 162 log.info( |
163 'Adding user %s, action %s on %s' % (user_obj, action, | |
164 safe_unicode(repo)) | |
165 ) | |
158 if commit: | 166 if commit: |
159 sa.commit() | 167 sa.commit() |
160 except: | 168 except: |
161 log.error(traceback.format_exc()) | 169 log.error(traceback.format_exc()) |
162 raise | 170 raise |
196 | 204 |
197 | 205 |
198 def is_valid_repo(repo_name, base_path): | 206 def is_valid_repo(repo_name, base_path): |
199 """ | 207 """ |
200 Returns True if given path is a valid repository False otherwise | 208 Returns True if given path is a valid repository False otherwise |
209 | |
201 :param repo_name: | 210 :param repo_name: |
202 :param base_path: | 211 :param base_path: |
203 | 212 |
204 :return True: if given path is a valid repository | 213 :return True: if given path is a valid repository |
205 """ | 214 """ |
206 full_path = os.path.join(base_path, repo_name) | 215 full_path = os.path.join(safe_str(base_path), safe_str(repo_name)) |
207 | 216 |
208 try: | 217 try: |
209 get_scm(full_path) | 218 get_scm(full_path) |
210 return True | 219 return True |
211 except VCSError: | 220 except VCSError: |
217 Returns True if given path is a repos group False otherwise | 226 Returns True if given path is a repos group False otherwise |
218 | 227 |
219 :param repo_name: | 228 :param repo_name: |
220 :param base_path: | 229 :param base_path: |
221 """ | 230 """ |
222 full_path = os.path.join(base_path, repos_group_name) | 231 full_path = os.path.join(safe_str(base_path), safe_str(repos_group_name)) |
223 | 232 |
224 # check if it's not a repo | 233 # check if it's not a repo |
225 if is_valid_repo(repos_group_name, base_path): | 234 if is_valid_repo(repos_group_name, base_path): |
226 return False | 235 return False |
227 | 236 |
256 'server', 'trusted', | 265 'server', 'trusted', |
257 'ui', 'web', ] | 266 'ui', 'web', ] |
258 | 267 |
259 | 268 |
260 def make_ui(read_from='file', path=None, checkpaths=True): | 269 def make_ui(read_from='file', path=None, checkpaths=True): |
261 """A function that will read python rc files or database | 270 """ |
271 A function that will read python rc files or database | |
262 and make an mercurial ui object from read options | 272 and make an mercurial ui object from read options |
263 | 273 |
264 :param path: path to mercurial config file | 274 :param path: path to mercurial config file |
265 :param checkpaths: check the path | 275 :param checkpaths: check the path |
266 :param read_from: read from 'file' or 'db' | 276 :param read_from: read from 'file' or 'db' |
369 | 379 |
370 def get_file_size(self, path): | 380 def get_file_size(self, path): |
371 return 0 | 381 return 0 |
372 | 382 |
373 | 383 |
374 def map_groups(groups): | 384 def map_groups(path): |
375 """ | 385 """ |
376 Checks for groups existence, and creates groups structures. | 386 Given a full path to a repository, create all nested groups that this |
377 It returns last group in structure | 387 repo is inside. This function creates parent-child relationships between |
378 | 388 groups and creates default perms for all new groups. |
379 :param groups: list of groups structure | 389 |
390 :param paths: full path to repository | |
380 """ | 391 """ |
381 sa = meta.Session | 392 sa = meta.Session |
382 | 393 groups = path.split(Repository.url_sep()) |
383 parent = None | 394 parent = None |
384 group = None | 395 group = None |
385 | 396 |
386 # last element is repo in nested groups structure | 397 # last element is repo in nested groups structure |
387 groups = groups[:-1] | 398 groups = groups[:-1] |
389 for lvl, group_name in enumerate(groups): | 400 for lvl, group_name in enumerate(groups): |
390 group_name = '/'.join(groups[:lvl] + [group_name]) | 401 group_name = '/'.join(groups[:lvl] + [group_name]) |
391 group = RepoGroup.get_by_group_name(group_name) | 402 group = RepoGroup.get_by_group_name(group_name) |
392 desc = '%s group' % group_name | 403 desc = '%s group' % group_name |
393 | 404 |
394 # # WTF that doesn't work !? | |
395 # if group is None: | |
396 # group = rgm.create(group_name, desc, parent, just_db=True) | |
397 # sa.commit() | |
398 | |
399 # skip folders that are now removed repos | 405 # skip folders that are now removed repos |
400 if REMOVED_REPO_PAT.match(group_name): | 406 if REMOVED_REPO_PAT.match(group_name): |
401 break | 407 break |
402 | 408 |
403 if group is None: | 409 if group is None: |
404 log.debug('creating group level: %s group_name: %s' % (lvl, group_name)) | 410 log.debug('creating group level: %s group_name: %s' % (lvl, |
411 group_name)) | |
405 group = RepoGroup(group_name, parent) | 412 group = RepoGroup(group_name, parent) |
406 group.group_description = desc | 413 group.group_description = desc |
407 sa.add(group) | 414 sa.add(group) |
408 rgm._create_default_perms(group) | 415 rgm._create_default_perms(group) |
409 sa.commit() | 416 sa.flush() |
410 parent = group | 417 parent = group |
411 return group | 418 return group |
412 | 419 |
413 | 420 |
414 def repo2db_mapper(initial_repo_list, remove_obsolete=False): | 421 def repo2db_mapper(initial_repo_list, remove_obsolete=False): |
427 if user is None: | 434 if user is None: |
428 raise Exception('Missing administrative account !') | 435 raise Exception('Missing administrative account !') |
429 added = [] | 436 added = [] |
430 | 437 |
431 for name, repo in initial_repo_list.items(): | 438 for name, repo in initial_repo_list.items(): |
432 group = map_groups(name.split(Repository.url_sep())) | 439 group = map_groups(name) |
433 if not rm.get_by_repo_name(name, cache=False): | 440 if not rm.get_by_repo_name(name, cache=False): |
434 log.info('repository %s not found creating default' % name) | 441 log.info('repository %s not found creating default' % name) |
435 added.append(name) | 442 added.append(name) |
436 form_data = { | 443 form_data = { |
437 'repo_name': name, | 444 'repo_name': name, |
444 } | 451 } |
445 rm.create(form_data, user, just_db=True) | 452 rm.create(form_data, user, just_db=True) |
446 sa.commit() | 453 sa.commit() |
447 removed = [] | 454 removed = [] |
448 if remove_obsolete: | 455 if remove_obsolete: |
449 #remove from database those repositories that are not in the filesystem | 456 # remove from database those repositories that are not in the filesystem |
450 for repo in sa.query(Repository).all(): | 457 for repo in sa.query(Repository).all(): |
451 if repo.repo_name not in initial_repo_list.keys(): | 458 if repo.repo_name not in initial_repo_list.keys(): |
459 log.debug("Removing non existing repository found in db %s" % | |
460 repo.repo_name) | |
452 removed.append(repo.repo_name) | 461 removed.append(repo.repo_name) |
453 sa.delete(repo) | 462 sa.delete(repo) |
454 sa.commit() | 463 sa.commit() |
455 | 464 |
465 # clear cache keys | |
466 log.debug("Clearing cache keys now...") | |
467 CacheInvalidation.clear_cache() | |
468 sa.commit() | |
456 return added, removed | 469 return added, removed |
457 | 470 |
458 | 471 |
459 # set cache regions for beaker so celery can utilise it | 472 # set cache regions for beaker so celery can utilise it |
460 def add_cache(settings): | 473 def add_cache(settings): |
482 region_settings['type'] = cache_settings.get('type', | 495 region_settings['type'] = cache_settings.get('type', |
483 'memory') | 496 'memory') |
484 beaker.cache.cache_regions[region] = region_settings | 497 beaker.cache.cache_regions[region] = region_settings |
485 | 498 |
486 | 499 |
500 def load_rcextensions(root_path): | |
501 import rhodecode | |
502 from rhodecode.config import conf | |
503 | |
504 path = os.path.join(root_path, 'rcextensions', '__init__.py') | |
505 if os.path.isfile(path): | |
506 rcext = create_module('rc', path) | |
507 EXT = rhodecode.EXTENSIONS = rcext | |
508 log.debug('Found rcextensions now loading %s...' % rcext) | |
509 | |
510 # Additional mappings that are not present in the pygments lexers | |
511 conf.LANGUAGES_EXTENSIONS_MAP.update(getattr(EXT, 'EXTRA_MAPPINGS', {})) | |
512 | |
513 #OVERRIDE OUR EXTENSIONS FROM RC-EXTENSIONS (if present) | |
514 | |
515 if getattr(EXT, 'INDEX_EXTENSIONS', []) != []: | |
516 log.debug('settings custom INDEX_EXTENSIONS') | |
517 conf.INDEX_EXTENSIONS = getattr(EXT, 'INDEX_EXTENSIONS', []) | |
518 | |
519 #ADDITIONAL MAPPINGS | |
520 log.debug('adding extra into INDEX_EXTENSIONS') | |
521 conf.INDEX_EXTENSIONS.extend(getattr(EXT, 'EXTRA_INDEX_EXTENSIONS', [])) | |
522 | |
523 | |
487 #============================================================================== | 524 #============================================================================== |
488 # TEST FUNCTIONS AND CREATORS | 525 # TEST FUNCTIONS AND CREATORS |
489 #============================================================================== | 526 #============================================================================== |
490 def create_test_index(repo_location, config, full_index): | 527 def create_test_index(repo_location, config, full_index): |
491 """ | 528 """ |
622 """ | 659 """ |
623 Loads the pylons configuration. | 660 Loads the pylons configuration. |
624 """ | 661 """ |
625 from pylons import config as pylonsconfig | 662 from pylons import config as pylonsconfig |
626 | 663 |
627 path_to_ini_file = os.path.realpath(conf) | 664 self.path_to_ini_file = os.path.realpath(conf) |
628 conf = paste.deploy.appconfig('config:' + path_to_ini_file) | 665 conf = paste.deploy.appconfig('config:' + self.path_to_ini_file) |
629 pylonsconfig.init_app(conf.global_conf, conf.local_conf) | 666 pylonsconfig.init_app(conf.global_conf, conf.local_conf) |