shadow: Use only a single regular expression to generate and match repo/group slugs.

This commit is contained in:
Martin Bornhold 2016-09-22 15:33:11 +02:00
parent 86b7b9e996
commit 9dca5e1637
2 changed files with 18 additions and 32 deletions

View file

@ -112,14 +112,13 @@ class SimpleVCS(object):
vcs_repo_name and is_shadow_repo on the current instance.
"""
# TODO: martinb: Move to class or module scope.
# TODO: martinb: Check if we have to use re.UNICODE.
# TODO: martinb: Check which chars are allowed for repo/group names.
# These chars are excluded: '`?=[]\;\'"<>,/~!@#$%^&*()+{}|: '
# Code from: rhodecode/lib/utils.py:repo_name_slug()
from rhodecode.lib.utils import SLUG_RE
pr_regex = re.compile(
'(?P<base_name>(?:[\w-]+)(?:/[\w-]+)*)/' # repo groups
'(?P<repo_name>[\w-]+)' # target repo name
'/pull-request/(?P<pr_id>\d+)/repository') # pr suffix
'(?P<groups>(?:{slug_pat})(?:/{slug_pat})*)' # repo groups
'/(?P<target>{slug_pat})' # target repo
'/pull-request/(?P<pr_id>\d+)' # pull request
'/repository$' # shadow repo
.format(slug_pat=SLUG_RE.pattern))
# Get url repo name from environment.
self.url_repo_name = self._get_repository_name(environ)

View file

@ -58,42 +58,29 @@ log = logging.getLogger(__name__)
REMOVED_REPO_PAT = re.compile(r'rm__\d{8}_\d{6}_\d{6}__.*')
# String of characters which are not allowed in repo/group slugs.
SLUG_BAD_CHARS = re.escape('`?=[]\;\'"<>,/~!@#$%^&*()+{}|:')
# Regex that matches forbidden characters in repo/group slugs.
SLUG_BAD_CHAR_RE = re.compile('[{}]'.format(SLUG_BAD_CHARS))
# Regex that matches allowed characters in repo/group slugs.
SLUG_GOOD_CHAR_RE = re.compile('[^{}]'.format(SLUG_BAD_CHARS))
# Regex that matches whole repo/group slugs.
SLUG_RE = re.compile('[^{}]+'.format(SLUG_BAD_CHARS))
_license_cache = None
def recursive_replace(str_, replace=' '):
"""
Recursive replace of given sign to just one instance
:param str_: given string
:param replace: char to find and replace multiple instances
Examples::
>>> recursive_replace("Mighty---Mighty-Bo--sstones",'-')
'Mighty-Mighty-Bo-sstones'
"""
if str_.find(replace * 2) == -1:
return str_
else:
str_ = str_.replace(replace * 2, replace)
return recursive_replace(str_, replace)
def repo_name_slug(value):
"""
Return slug of name of repository
This function is called on each creation/modification
of repository to prevent bad names in repo
"""
replacement_char = '-'
slug = remove_formatting(value)
slug = strip_tags(slug)
for c in """`?=[]\;'"<>,/~!@#$%^&*()+{}|: """:
slug = slug.replace(c, '-')
slug = recursive_replace(slug, '-')
slug = collapse(slug, '-')
slug = SLUG_BAD_CHAR_RE.sub(replacement_char, slug)
slug = collapse(slug, replacement_char)
return slug