##// END OF EJS Templates
spelling
super-admin -
r5133:0ea0ce62 default
parent child Browse files
Show More
@@ -1,807 +1,807 b''
1 1 # Copyright (C) 2010-2023 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 """
20 20 Utilities library for RhodeCode
21 21 """
22 22
23 23 import datetime
24 24 import decorator
25 25 import logging
26 26 import os
27 27 import re
28 28 import sys
29 29 import shutil
30 30 import socket
31 31 import tempfile
32 32 import traceback
33 33 import tarfile
34 34 import warnings
35 35 from os.path import join as jn
36 36
37 37 import paste
38 38 import pkg_resources
39 39 from webhelpers2.text import collapse, strip_tags, convert_accented_entities, convert_misc_entities
40 40
41 41 from mako import exceptions
42 42
43 43 from rhodecode.lib.hash_utils import sha256_safe, md5, sha1
44 44 from rhodecode.lib.str_utils import safe_bytes, safe_str
45 45 from rhodecode.lib.vcs.backends.base import Config
46 46 from rhodecode.lib.vcs.exceptions import VCSError
47 47 from rhodecode.lib.vcs.utils.helpers import get_scm, get_scm_backend
48 48 from rhodecode.lib.ext_json import sjson as json
49 49 from rhodecode.model import meta
50 50 from rhodecode.model.db import (
51 51 Repository, User, RhodeCodeUi, UserLog, RepoGroup, UserGroup)
52 52 from rhodecode.model.meta import Session
53 53
54 54
55 55 log = logging.getLogger(__name__)
56 56
57 57 REMOVED_REPO_PAT = re.compile(r'rm__\d{8}_\d{6}_\d{6}__.*')
58 58
59 59 # String which contains characters that are not allowed in slug names for
60 60 # repositories or repository groups. It is properly escaped to use it in
61 61 # regular expressions.
62 62 SLUG_BAD_CHARS = re.escape(r'`?=[]\;\'"<>,/~!@#$%^&*()+{}|:')
63 63
64 64 # Regex that matches forbidden characters in repo/group slugs.
65 65 SLUG_BAD_CHAR_RE = re.compile(r'[{}\x00-\x08\x0b-\x0c\x0e-\x1f]'.format(SLUG_BAD_CHARS))
66 66
67 67 # Regex that matches allowed characters in repo/group slugs.
68 68 SLUG_GOOD_CHAR_RE = re.compile(r'[^{}]'.format(SLUG_BAD_CHARS))
69 69
70 70 # Regex that matches whole repo/group slugs.
71 71 SLUG_RE = re.compile(r'[^{}]+'.format(SLUG_BAD_CHARS))
72 72
73 73 _license_cache = None
74 74
75 75
76 76 def repo_name_slug(value):
77 77 """
78 78 Return slug of name of repository
79 79 This function is called on each creation/modification
80 80 of repository to prevent bad names in repo
81 81 """
82 82
83 83 replacement_char = '-'
84 84
85 85 slug = strip_tags(value)
86 86 slug = convert_accented_entities(slug)
87 87 slug = convert_misc_entities(slug)
88 88
89 89 slug = SLUG_BAD_CHAR_RE.sub('', slug)
90 90 slug = re.sub(r'[\s]+', '-', slug)
91 91 slug = collapse(slug, replacement_char)
92 92
93 93 return slug
94 94
95 95
96 96 #==============================================================================
97 97 # PERM DECORATOR HELPERS FOR EXTRACTING NAMES FOR PERM CHECKS
98 98 #==============================================================================
99 99 def get_repo_slug(request):
100 100 _repo = ''
101 101
102 102 if hasattr(request, 'db_repo_name'):
103 103 # if our requests has set db reference use it for name, this
104 104 # translates the example.com/_<id> into proper repo names
105 105 _repo = request.db_repo_name
106 106 elif getattr(request, 'matchdict', None):
107 107 # pyramid
108 108 _repo = request.matchdict.get('repo_name')
109 109
110 110 if _repo:
111 111 _repo = _repo.rstrip('/')
112 112 return _repo
113 113
114 114
115 115 def get_repo_group_slug(request):
116 116 _group = ''
117 117 if hasattr(request, 'db_repo_group'):
118 118 # if our requests has set db reference use it for name, this
119 119 # translates the example.com/_<id> into proper repo group names
120 120 _group = request.db_repo_group.group_name
121 121 elif getattr(request, 'matchdict', None):
122 122 # pyramid
123 123 _group = request.matchdict.get('repo_group_name')
124 124
125 125 if _group:
126 126 _group = _group.rstrip('/')
127 127 return _group
128 128
129 129
130 130 def get_user_group_slug(request):
131 131 _user_group = ''
132 132
133 133 if hasattr(request, 'db_user_group'):
134 134 _user_group = request.db_user_group.users_group_name
135 135 elif getattr(request, 'matchdict', None):
136 136 # pyramid
137 137 _user_group = request.matchdict.get('user_group_id')
138 138 _user_group_name = request.matchdict.get('user_group_name')
139 139 try:
140 140 if _user_group:
141 141 _user_group = UserGroup.get(_user_group)
142 142 elif _user_group_name:
143 143 _user_group = UserGroup.get_by_group_name(_user_group_name)
144 144
145 145 if _user_group:
146 146 _user_group = _user_group.users_group_name
147 147 except Exception:
148 148 log.exception('Failed to get user group by id and name')
149 149 # catch all failures here
150 150 return None
151 151
152 152 return _user_group
153 153
154 154
155 155 def get_filesystem_repos(path, recursive=False, skip_removed_repos=True):
156 156 """
157 157 Scans given path for repos and return (name,(type,path)) tuple
158 158
159 159 :param path: path to scan for repositories
160 160 :param recursive: recursive search and return names with subdirs in front
161 161 """
162 162
163 163 # remove ending slash for better results
164 164 path = path.rstrip(os.sep)
165 165 log.debug('now scanning in %s location recursive:%s...', path, recursive)
166 166
167 167 def _get_repos(p):
168 168 dirpaths = get_dirpaths(p)
169 169 if not _is_dir_writable(p):
170 170 log.warning('repo path without write access: %s', p)
171 171
172 172 for dirpath in dirpaths:
173 173 if os.path.isfile(os.path.join(p, dirpath)):
174 174 continue
175 175 cur_path = os.path.join(p, dirpath)
176 176
177 177 # skip removed repos
178 178 if skip_removed_repos and REMOVED_REPO_PAT.match(dirpath):
179 179 continue
180 180
181 181 #skip .<somethin> dirs
182 182 if dirpath.startswith('.'):
183 183 continue
184 184
185 185 try:
186 186 scm_info = get_scm(cur_path)
187 187 yield scm_info[1].split(path, 1)[-1].lstrip(os.sep), scm_info
188 188 except VCSError:
189 189 if not recursive:
190 190 continue
191 191 #check if this dir containts other repos for recursive scan
192 192 rec_path = os.path.join(p, dirpath)
193 193 if os.path.isdir(rec_path):
194 194 yield from _get_repos(rec_path)
195 195
196 196 return _get_repos(path)
197 197
198 198
199 199 def get_dirpaths(p: str) -> list:
200 200 try:
201 201 # OS-independable way of checking if we have at least read-only
202 202 # access or not.
203 203 dirpaths = os.listdir(p)
204 204 except OSError:
205 205 log.warning('ignoring repo path without read access: %s', p)
206 206 return []
207 207
208 208 # os.listpath has a tweak: If a unicode is passed into it, then it tries to
209 209 # decode paths and suddenly returns unicode objects itself. The items it
210 210 # cannot decode are returned as strings and cause issues.
211 211 #
212 212 # Those paths are ignored here until a solid solution for path handling has
213 213 # been built.
214 214 expected_type = type(p)
215 215
216 216 def _has_correct_type(item):
217 217 if type(item) is not expected_type:
218 218 log.error(
219 219 "Ignoring path %s since it cannot be decoded into str.",
220 220 # Using "repr" to make sure that we see the byte value in case
221 221 # of support.
222 222 repr(item))
223 223 return False
224 224 return True
225 225
226 226 dirpaths = [item for item in dirpaths if _has_correct_type(item)]
227 227
228 228 return dirpaths
229 229
230 230
231 231 def _is_dir_writable(path):
232 232 """
233 233 Probe if `path` is writable.
234 234
235 235 Due to trouble on Cygwin / Windows, this is actually probing if it is
236 236 possible to create a file inside of `path`, stat does not produce reliable
237 237 results in this case.
238 238 """
239 239 try:
240 240 with tempfile.TemporaryFile(dir=path):
241 241 pass
242 242 except OSError:
243 243 return False
244 244 return True
245 245
246 246
247 247 def is_valid_repo(repo_name, base_path, expect_scm=None, explicit_scm=None, config=None):
248 248 """
249 249 Returns True if given path is a valid repository False otherwise.
250 250 If expect_scm param is given also, compare if given scm is the same
251 251 as expected from scm parameter. If explicit_scm is given don't try to
252 252 detect the scm, just use the given one to check if repo is valid
253 253
254 254 :param repo_name:
255 255 :param base_path:
256 256 :param expect_scm:
257 257 :param explicit_scm:
258 258 :param config:
259 259
260 260 :return True: if given path is a valid repository
261 261 """
262 262 full_path = os.path.join(safe_str(base_path), safe_str(repo_name))
263 263 log.debug('Checking if `%s` is a valid path for repository. '
264 264 'Explicit type: %s', repo_name, explicit_scm)
265 265
266 266 try:
267 267 if explicit_scm:
268 268 detected_scms = [get_scm_backend(explicit_scm)(
269 269 full_path, config=config).alias]
270 270 else:
271 271 detected_scms = get_scm(full_path)
272 272
273 273 if expect_scm:
274 274 return detected_scms[0] == expect_scm
275 275 log.debug('path: %s is an vcs object:%s', full_path, detected_scms)
276 276 return True
277 277 except VCSError:
278 278 log.debug('path: %s is not a valid repo !', full_path)
279 279 return False
280 280
281 281
282 282 def is_valid_repo_group(repo_group_name, base_path, skip_path_check=False):
283 283 """
284 Returns True if given path is a repository group, False otherwise
284 Returns True if a given path is a repository group, False otherwise
285 285
286 :param repo_name:
286 :param repo_group_name:
287 287 :param base_path:
288 288 """
289 289 full_path = os.path.join(safe_str(base_path), safe_str(repo_group_name))
290 290 log.debug('Checking if `%s` is a valid path for repository group',
291 291 repo_group_name)
292 292
293 293 # check if it's not a repo
294 294 if is_valid_repo(repo_group_name, base_path):
295 295 log.debug('Repo called %s exist, it is not a valid repo group', repo_group_name)
296 296 return False
297 297
298 298 try:
299 299 # we need to check bare git repos at higher level
300 300 # since we might match branches/hooks/info/objects or possible
301 301 # other things inside bare git repo
302 302 maybe_repo = os.path.dirname(full_path)
303 303 if maybe_repo == base_path:
304 # skip root level repo check, we know root location CANNOT BE a repo group
304 # skip root level repo check; we know root location CANNOT BE a repo group
305 305 return False
306 306
307 307 scm_ = get_scm(maybe_repo)
308 308 log.debug('path: %s is a vcs object:%s, not valid repo group', full_path, scm_)
309 309 return False
310 310 except VCSError:
311 311 pass
312 312
313 313 # check if it's a valid path
314 314 if skip_path_check or os.path.isdir(full_path):
315 315 log.debug('path: %s is a valid repo group !', full_path)
316 316 return True
317 317
318 318 log.debug('path: %s is not a valid repo group !', full_path)
319 319 return False
320 320
321 321
322 322 def ask_ok(prompt, retries=4, complaint='[y]es or [n]o please!'):
323 323 while True:
324 324 ok = eval(input(prompt))
325 325 if ok.lower() in ('y', 'ye', 'yes'):
326 326 return True
327 327 if ok.lower() in ('n', 'no', 'nop', 'nope'):
328 328 return False
329 329 retries = retries - 1
330 330 if retries < 0:
331 331 raise OSError
332 332 print(complaint)
333 333
334 334 # propagated from mercurial documentation
335 335 ui_sections = [
336 336 'alias', 'auth',
337 337 'decode/encode', 'defaults',
338 338 'diff', 'email',
339 339 'extensions', 'format',
340 340 'merge-patterns', 'merge-tools',
341 341 'hooks', 'http_proxy',
342 342 'smtp', 'patch',
343 343 'paths', 'profiling',
344 344 'server', 'trusted',
345 345 'ui', 'web', ]
346 346
347 347
348 348 def config_data_from_db(clear_session=True, repo=None):
349 349 """
350 350 Read the configuration data from the database and return configuration
351 351 tuples.
352 352 """
353 353 from rhodecode.model.settings import VcsSettingsModel
354 354
355 355 config = []
356 356
357 357 sa = meta.Session()
358 358 settings_model = VcsSettingsModel(repo=repo, sa=sa)
359 359
360 360 ui_settings = settings_model.get_ui_settings()
361 361
362 362 ui_data = []
363 363 for setting in ui_settings:
364 364 if setting.active:
365 365 ui_data.append((setting.section, setting.key, setting.value))
366 366 config.append((
367 367 safe_str(setting.section), safe_str(setting.key),
368 368 safe_str(setting.value)))
369 369 if setting.key == 'push_ssl':
370 370 # force set push_ssl requirement to False, rhodecode
371 371 # handles that
372 372 config.append((
373 373 safe_str(setting.section), safe_str(setting.key), False))
374 374 log.debug(
375 375 'settings ui from db@repo[%s]: %s',
376 376 repo,
377 377 ','.join(['[{}] {}={}'.format(*s) for s in ui_data]))
378 378 if clear_session:
379 379 meta.Session.remove()
380 380
381 381 # TODO: mikhail: probably it makes no sense to re-read hooks information.
382 382 # It's already there and activated/deactivated
383 383 skip_entries = []
384 384 enabled_hook_classes = get_enabled_hook_classes(ui_settings)
385 385 if 'pull' not in enabled_hook_classes:
386 386 skip_entries.append(('hooks', RhodeCodeUi.HOOK_PRE_PULL))
387 387 if 'push' not in enabled_hook_classes:
388 388 skip_entries.append(('hooks', RhodeCodeUi.HOOK_PRE_PUSH))
389 389 skip_entries.append(('hooks', RhodeCodeUi.HOOK_PRETX_PUSH))
390 390 skip_entries.append(('hooks', RhodeCodeUi.HOOK_PUSH_KEY))
391 391
392 392 config = [entry for entry in config if entry[:2] not in skip_entries]
393 393
394 394 return config
395 395
396 396
397 397 def make_db_config(clear_session=True, repo=None):
398 398 """
399 399 Create a :class:`Config` instance based on the values in the database.
400 400 """
401 401 config = Config()
402 402 config_data = config_data_from_db(clear_session=clear_session, repo=repo)
403 403 for section, option, value in config_data:
404 404 config.set(section, option, value)
405 405 return config
406 406
407 407
408 408 def get_enabled_hook_classes(ui_settings):
409 409 """
410 410 Return the enabled hook classes.
411 411
412 412 :param ui_settings: List of ui_settings as returned
413 413 by :meth:`VcsSettingsModel.get_ui_settings`
414 414
415 415 :return: a list with the enabled hook classes. The order is not guaranteed.
416 416 :rtype: list
417 417 """
418 418 enabled_hooks = []
419 419 active_hook_keys = [
420 420 key for section, key, value, active in ui_settings
421 421 if section == 'hooks' and active]
422 422
423 423 hook_names = {
424 424 RhodeCodeUi.HOOK_PUSH: 'push',
425 425 RhodeCodeUi.HOOK_PULL: 'pull',
426 426 RhodeCodeUi.HOOK_REPO_SIZE: 'repo_size'
427 427 }
428 428
429 429 for key in active_hook_keys:
430 430 hook = hook_names.get(key)
431 431 if hook:
432 432 enabled_hooks.append(hook)
433 433
434 434 return enabled_hooks
435 435
436 436
437 437 def set_rhodecode_config(config):
438 438 """
439 439 Updates pyramid config with new settings from database
440 440
441 441 :param config:
442 442 """
443 443 from rhodecode.model.settings import SettingsModel
444 444 app_settings = SettingsModel().get_all_settings()
445 445
446 446 for k, v in list(app_settings.items()):
447 447 config[k] = v
448 448
449 449
450 450 def get_rhodecode_realm():
451 451 """
452 452 Return the rhodecode realm from database.
453 453 """
454 454 from rhodecode.model.settings import SettingsModel
455 455 realm = SettingsModel().get_setting_by_name('realm')
456 456 return safe_str(realm.app_settings_value)
457 457
458 458
459 459 def get_rhodecode_base_path():
460 460 """
461 461 Returns the base path. The base path is the filesystem path which points
462 462 to the repository store.
463 463 """
464 464
465 465 import rhodecode
466 466 return rhodecode.CONFIG['default_base_path']
467 467
468 468
469 469 def map_groups(path):
470 470 """
471 471 Given a full path to a repository, create all nested groups that this
472 472 repo is inside. This function creates parent-child relationships between
473 473 groups and creates default perms for all new groups.
474 474
475 475 :param paths: full path to repository
476 476 """
477 477 from rhodecode.model.repo_group import RepoGroupModel
478 478 sa = meta.Session()
479 479 groups = path.split(Repository.NAME_SEP)
480 480 parent = None
481 481 group = None
482 482
483 483 # last element is repo in nested groups structure
484 484 groups = groups[:-1]
485 485 rgm = RepoGroupModel(sa)
486 486 owner = User.get_first_super_admin()
487 487 for lvl, group_name in enumerate(groups):
488 488 group_name = '/'.join(groups[:lvl] + [group_name])
489 489 group = RepoGroup.get_by_group_name(group_name)
490 490 desc = '%s group' % group_name
491 491
492 492 # skip folders that are now removed repos
493 493 if REMOVED_REPO_PAT.match(group_name):
494 494 break
495 495
496 496 if group is None:
497 497 log.debug('creating group level: %s group_name: %s',
498 498 lvl, group_name)
499 499 group = RepoGroup(group_name, parent)
500 500 group.group_description = desc
501 501 group.user = owner
502 502 sa.add(group)
503 503 perm_obj = rgm._create_default_perms(group)
504 504 sa.add(perm_obj)
505 505 sa.flush()
506 506
507 507 parent = group
508 508 return group
509 509
510 510
511 511 def repo2db_mapper(initial_repo_list, remove_obsolete=False):
512 512 """
513 513 maps all repos given in initial_repo_list, non existing repositories
514 514 are created, if remove_obsolete is True it also checks for db entries
515 515 that are not in initial_repo_list and removes them.
516 516
517 517 :param initial_repo_list: list of repositories found by scanning methods
518 518 :param remove_obsolete: check for obsolete entries in database
519 519 """
520 520 from rhodecode.model.repo import RepoModel
521 521 from rhodecode.model.repo_group import RepoGroupModel
522 522 from rhodecode.model.settings import SettingsModel
523 523
524 524 sa = meta.Session()
525 525 repo_model = RepoModel()
526 526 user = User.get_first_super_admin()
527 527 added = []
528 528
529 529 # creation defaults
530 530 defs = SettingsModel().get_default_repo_settings(strip_prefix=True)
531 531 enable_statistics = defs.get('repo_enable_statistics')
532 532 enable_locking = defs.get('repo_enable_locking')
533 533 enable_downloads = defs.get('repo_enable_downloads')
534 534 private = defs.get('repo_private')
535 535
536 536 for name, repo in list(initial_repo_list.items()):
537 537 group = map_groups(name)
538 538 str_name = safe_str(name)
539 539 db_repo = repo_model.get_by_repo_name(str_name)
540 540 # found repo that is on filesystem not in RhodeCode database
541 541 if not db_repo:
542 542 log.info('repository %s not found, creating now', name)
543 543 added.append(name)
544 544 desc = (repo.description
545 545 if repo.description != 'unknown'
546 546 else '%s repository' % name)
547 547
548 548 db_repo = repo_model._create_repo(
549 549 repo_name=name,
550 550 repo_type=repo.alias,
551 551 description=desc,
552 552 repo_group=getattr(group, 'group_id', None),
553 553 owner=user,
554 554 enable_locking=enable_locking,
555 555 enable_downloads=enable_downloads,
556 556 enable_statistics=enable_statistics,
557 557 private=private,
558 558 state=Repository.STATE_CREATED
559 559 )
560 560 sa.commit()
561 561 # we added that repo just now, and make sure we updated server info
562 562 if db_repo.repo_type == 'git':
563 563 git_repo = db_repo.scm_instance()
564 564 # update repository server-info
565 565 log.debug('Running update server info')
566 566 git_repo._update_server_info()
567 567
568 568 db_repo.update_commit_cache()
569 569
570 570 config = db_repo._config
571 571 config.set('extensions', 'largefiles', '')
572 572 repo = db_repo.scm_instance(config=config)
573 573 repo.install_hooks()
574 574
575 575 removed = []
576 576 if remove_obsolete:
577 577 # remove from database those repositories that are not in the filesystem
578 578 for repo in sa.query(Repository).all():
579 579 if repo.repo_name not in list(initial_repo_list.keys()):
580 580 log.debug("Removing non-existing repository found in db `%s`",
581 581 repo.repo_name)
582 582 try:
583 583 RepoModel(sa).delete(repo, forks='detach', fs_remove=False)
584 584 sa.commit()
585 585 removed.append(repo.repo_name)
586 586 except Exception:
587 587 # don't hold further removals on error
588 588 log.error(traceback.format_exc())
589 589 sa.rollback()
590 590
591 591 def splitter(full_repo_name):
592 592 _parts = full_repo_name.rsplit(RepoGroup.url_sep(), 1)
593 593 gr_name = None
594 594 if len(_parts) == 2:
595 595 gr_name = _parts[0]
596 596 return gr_name
597 597
598 598 initial_repo_group_list = [splitter(x) for x in
599 599 list(initial_repo_list.keys()) if splitter(x)]
600 600
601 601 # remove from database those repository groups that are not in the
602 602 # filesystem due to parent child relationships we need to delete them
603 603 # in a specific order of most nested first
604 604 all_groups = [x.group_name for x in sa.query(RepoGroup).all()]
605 605 def nested_sort(gr):
606 606 return len(gr.split('/'))
607 607 for group_name in sorted(all_groups, key=nested_sort, reverse=True):
608 608 if group_name not in initial_repo_group_list:
609 609 repo_group = RepoGroup.get_by_group_name(group_name)
610 610 if (repo_group.children.all() or
611 611 not RepoGroupModel().check_exist_filesystem(
612 612 group_name=group_name, exc_on_failure=False)):
613 613 continue
614 614
615 615 log.info(
616 616 'Removing non-existing repository group found in db `%s`',
617 617 group_name)
618 618 try:
619 619 RepoGroupModel(sa).delete(group_name, fs_remove=False)
620 620 sa.commit()
621 621 removed.append(group_name)
622 622 except Exception:
623 623 # don't hold further removals on error
624 624 log.exception(
625 625 'Unable to remove repository group `%s`',
626 626 group_name)
627 627 sa.rollback()
628 628 raise
629 629
630 630 return added, removed
631 631
632 632
633 633 def load_rcextensions(root_path):
634 634 import rhodecode
635 635 from rhodecode.config import conf
636 636
637 637 path = os.path.join(root_path)
638 638 sys.path.append(path)
639 639
640 640 try:
641 641 rcextensions = __import__('rcextensions')
642 642 except ImportError:
643 643 if os.path.isdir(os.path.join(path, 'rcextensions')):
644 644 log.warning('Unable to load rcextensions from %s', path)
645 645 rcextensions = None
646 646
647 647 if rcextensions:
648 648 log.info('Loaded rcextensions from %s...', rcextensions)
649 649 rhodecode.EXTENSIONS = rcextensions
650 650
651 651 # Additional mappings that are not present in the pygments lexers
652 652 conf.LANGUAGES_EXTENSIONS_MAP.update(
653 653 getattr(rhodecode.EXTENSIONS, 'EXTRA_MAPPINGS', {}))
654 654
655 655
656 656 def get_custom_lexer(extension):
657 657 """
658 658 returns a custom lexer if it is defined in rcextensions module, or None
659 659 if there's no custom lexer defined
660 660 """
661 661 import rhodecode
662 662 from pygments import lexers
663 663
664 664 # custom override made by RhodeCode
665 665 if extension in ['mako']:
666 666 return lexers.get_lexer_by_name('html+mako')
667 667
668 668 # check if we didn't define this extension as other lexer
669 669 extensions = rhodecode.EXTENSIONS and getattr(rhodecode.EXTENSIONS, 'EXTRA_LEXERS', None)
670 670 if extensions and extension in rhodecode.EXTENSIONS.EXTRA_LEXERS:
671 671 _lexer_name = rhodecode.EXTENSIONS.EXTRA_LEXERS[extension]
672 672 return lexers.get_lexer_by_name(_lexer_name)
673 673
674 674
675 675 #==============================================================================
676 676 # TEST FUNCTIONS AND CREATORS
677 677 #==============================================================================
678 678 def create_test_index(repo_location, config):
679 679 """
680 680 Makes default test index.
681 681 """
682 682 try:
683 683 import rc_testdata
684 684 except ImportError:
685 685 raise ImportError('Failed to import rc_testdata, '
686 686 'please make sure this package is installed from requirements_test.txt')
687 687 rc_testdata.extract_search_index(
688 688 'vcs_search_index', os.path.dirname(config['search.location']))
689 689
690 690
691 691 def create_test_directory(test_path):
692 692 """
693 693 Create test directory if it doesn't exist.
694 694 """
695 695 if not os.path.isdir(test_path):
696 696 log.debug('Creating testdir %s', test_path)
697 697 os.makedirs(test_path)
698 698
699 699
700 700 def create_test_database(test_path, config):
701 701 """
702 702 Makes a fresh database.
703 703 """
704 704 from rhodecode.lib.db_manage import DbManage
705 705 from rhodecode.lib.utils2 import get_encryption_key
706 706
707 707 # PART ONE create db
708 708 dbconf = config['sqlalchemy.db1.url']
709 709 enc_key = get_encryption_key(config)
710 710
711 711 log.debug('making test db %s', dbconf)
712 712
713 713 dbmanage = DbManage(log_sql=False, dbconf=dbconf, root=config['here'],
714 714 tests=True, cli_args={'force_ask': True}, enc_key=enc_key)
715 715 dbmanage.create_tables(override=True)
716 716 dbmanage.set_db_version()
717 717 # for tests dynamically set new root paths based on generated content
718 718 dbmanage.create_settings(dbmanage.config_prompt(test_path))
719 719 dbmanage.create_default_user()
720 720 dbmanage.create_test_admin_and_users()
721 721 dbmanage.create_permissions()
722 722 dbmanage.populate_default_permissions()
723 723 Session().commit()
724 724
725 725
726 726 def create_test_repositories(test_path, config):
727 727 """
728 728 Creates test repositories in the temporary directory. Repositories are
729 729 extracted from archives within the rc_testdata package.
730 730 """
731 731 import rc_testdata
732 732 from rhodecode.tests import HG_REPO, GIT_REPO, SVN_REPO
733 733
734 734 log.debug('making test vcs repositories')
735 735
736 736 idx_path = config['search.location']
737 737 data_path = config['cache_dir']
738 738
739 739 # clean index and data
740 740 if idx_path and os.path.exists(idx_path):
741 741 log.debug('remove %s', idx_path)
742 742 shutil.rmtree(idx_path)
743 743
744 744 if data_path and os.path.exists(data_path):
745 745 log.debug('remove %s', data_path)
746 746 shutil.rmtree(data_path)
747 747
748 748 rc_testdata.extract_hg_dump('vcs_test_hg', jn(test_path, HG_REPO))
749 749 rc_testdata.extract_git_dump('vcs_test_git', jn(test_path, GIT_REPO))
750 750
751 751 # Note: Subversion is in the process of being integrated with the system,
752 752 # until we have a properly packed version of the test svn repository, this
753 753 # tries to copy over the repo from a package "rc_testdata"
754 754 svn_repo_path = rc_testdata.get_svn_repo_archive()
755 755 with tarfile.open(svn_repo_path) as tar:
756 756 tar.extractall(jn(test_path, SVN_REPO))
757 757
758 758
759 759 def password_changed(auth_user, session):
760 760 # Never report password change in case of default user or anonymous user.
761 761 if auth_user.username == User.DEFAULT_USER or auth_user.user_id is None:
762 762 return False
763 763
764 764 password_hash = md5(safe_bytes(auth_user.password)) if auth_user.password else None
765 765 rhodecode_user = session.get('rhodecode_user', {})
766 766 session_password_hash = rhodecode_user.get('password', '')
767 767 return password_hash != session_password_hash
768 768
769 769
770 770 def read_opensource_licenses():
771 771 global _license_cache
772 772
773 773 if not _license_cache:
774 774 licenses = pkg_resources.resource_string(
775 775 'rhodecode', 'config/licenses.json')
776 776 _license_cache = json.loads(licenses)
777 777
778 778 return _license_cache
779 779
780 780
781 781 def generate_platform_uuid():
782 782 """
783 783 Generates platform UUID based on it's name
784 784 """
785 785 import platform
786 786
787 787 try:
788 788 uuid_list = [platform.platform()]
789 789 return sha256_safe(':'.join(uuid_list))
790 790 except Exception as e:
791 791 log.error('Failed to generate host uuid: %s', e)
792 792 return 'UNDEFINED'
793 793
794 794
795 795 def send_test_email(recipients, email_body='TEST EMAIL'):
796 796 """
797 797 Simple code for generating test emails.
798 798 Usage::
799 799
800 800 from rhodecode.lib import utils
801 801 utils.send_test_email()
802 802 """
803 803 from rhodecode.lib.celerylib import tasks, run_task
804 804
805 805 email_body = email_body_plaintext = email_body
806 806 subject = f'SUBJECT FROM: {socket.gethostname()}'
807 807 tasks.send_email(recipients, subject, email_body_plaintext, email_body)
General Comments 0
You need to be logged in to leave comments. Login now