From a8cf0196f743650876f4ada4db00f91147535614 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Fri, 11 Jun 2021 10:23:42 -0400 Subject: [PATCH] subversion - fix stack trace when getting repository information (#74405) * subversion - set LC_ALL for accurate command output parsing When LC_ALL is not set, the output language of commands will differ based on locale. There is a lot of history of trying to fix this. See the following pull requests: https://github.com/ansible/ansible-modules-core/pull/4358 https://github.com/ansible/ansible-modules-core/pull/4358 This patch attempts to fix this my setting LC_ALL to a UTF-8 locale. Setting LC_ALL to C reintroduces this bug https://github.com/ansible/ansible-modules-core/issues/4178. I'm sure there are some problems I am not seeing with setting this to en_US.UTF-8, but that is the only way I could find to fix this bug without reintriducing the bug mentioned above. * Rather than setting locale, just check for matches before trying to get groups This is a pragmatic solution to avoid the stack trace since setting the locale correctly to ensure message parsing is accurate is problematic. * Improve regexps for finding revision and URL --- .../36498-subversion-fix-info-parsing.yml | 2 ++ lib/ansible/modules/subversion.py | 33 ++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/36498-subversion-fix-info-parsing.yml diff --git a/changelogs/fragments/36498-subversion-fix-info-parsing.yml b/changelogs/fragments/36498-subversion-fix-info-parsing.yml new file mode 100644 index 00000000000..d14605b6c0f --- /dev/null +++ b/changelogs/fragments/36498-subversion-fix-info-parsing.yml @@ -0,0 +1,2 @@ +bugfixes: + - subversion - fix stack trace when getting information about the repository (https://github.com/ansible/ansible/issues/36498) diff --git a/lib/ansible/modules/subversion.py b/lib/ansible/modules/subversion.py index 348d8d9bc01..3b2701ce0b2 100644 --- a/lib/ansible/modules/subversion.py +++ b/lib/ansible/modules/subversion.py @@ -134,6 +134,13 @@ from ansible.module_utils.basic import AnsibleModule class Subversion(object): + + # Example text matched by the regexp: + # Révision : 1889134 + # 版本: 1889134 + # Revision: 1889134 + REVISION_RE = r'^\w+\s?:\s+\d+$' + def __init__(self, module, dest, repo, revision, username, password, svn_path, validate_certs): self.module = module self.dest = dest @@ -228,14 +235,28 @@ class Subversion(object): def get_revision(self): '''Revision and URL of subversion working directory.''' text = '\n'.join(self._exec(["info", self.dest])) - rev = re.search(r'^Revision:.*$', text, re.MULTILINE).group(0) - url = re.search(r'^URL:.*$', text, re.MULTILINE).group(0) + rev = re.search(self.REVISION_RE, text, re.MULTILINE) + if rev: + rev = rev.group(0) + else: + rev = 'Unable to get revision' + + url = re.search(r'^URL\s?:.*$', text, re.MULTILINE) + if url: + url = url.group(0) + else: + url = 'Unable to get URL' + return rev, url def get_remote_revision(self): '''Revision and URL of subversion working directory.''' text = '\n'.join(self._exec(["info", self.repo])) - rev = re.search(r'^Revision:.*$', text, re.MULTILINE).group(0) + rev = re.search(self.REVISION_RE, text, re.MULTILINE) + if rev: + rev = rev.group(0) + else: + rev = 'Unable to get remote revision' return rev def has_local_mods(self): @@ -250,7 +271,11 @@ class Subversion(object): def needs_update(self): curr, url = self.get_revision() out2 = '\n'.join(self._exec(["info", "-r", self.revision, self.dest])) - head = re.search(r'^Revision:.*$', out2, re.MULTILINE).group(0) + head = re.search(self.REVISION_RE, out2, re.MULTILINE) + if head: + head = head.group(0) + else: + head = 'Unable to get revision' rev1 = int(curr.split(':')[1].strip()) rev2 = int(head.split(':')[1].strip()) change = False