parted: fix regex for version match and partition size output (#1695) (#1732)

* Fix 2 regex in parted related to parted version string and to parsing partition size output.

* Added changelog fragment.

* Updated changelog as per recommendation.

* Fix the regex matching the parted version. The space character at the end of the string may or may not be always present

* provided sample version output and corrected regex to match

* add/correct changelog fragment

* split parted_version function to allow creating a test unit

* test unit for parted version info

* ansible-test sanity fixes

* review fix

* Update changelogs/fragments/1695-parted-updatedregex.yaml

Co-authored-by: Felix Fontein <felix@fontein.de>

* comment fixes

* better function name

* Update plugins/modules/system/parted.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* comment fixes

Co-authored-by: Claude Robitaille <claude@cbcr.me>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 43da5b88db)

Co-authored-by: Anatoly Pugachev <matorola@gmail.com>
This commit is contained in:
patchback[bot] 2021-02-05 07:51:43 +01:00 committed by GitHub
parent f613983cb4
commit a216f15dd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 14 deletions

View file

@ -241,7 +241,7 @@ def parse_unit(size_str, unit=''):
"""
Parses a string containing a size or boundary information
"""
matches = re.search(r'^(-?[\d.]+)([\w%]+)?$', size_str)
matches = re.search(r'^(-?[\d.]+) *([\w%]+)?$', size_str)
if matches is None:
# "<cylinder>,<head>,<sector>" format
matches = re.search(r'^(\d+),(\d+),(\d+)$', size_str)
@ -500,6 +500,33 @@ def check_parted_label(device):
return False
def parse_parted_version(out):
"""
Returns version tuple from the output of "parted --version" command
"""
lines = [x for x in out.split('\n') if x.strip() != '']
if len(lines) == 0:
return None, None, None
# Sample parted versions (see as well test unit):
# parted (GNU parted) 3.3
# parted (GNU parted) 3.4.5
# parted (GNU parted) 3.3.14-dfc61
matches = re.search(r'^parted.+\s(\d+)\.(\d+)(?:\.(\d+))?', lines[0].strip())
if matches is None:
return None, None, None
# Convert version to numbers
major = int(matches.group(1))
minor = int(matches.group(2))
rev = 0
if matches.group(3) is not None:
rev = int(matches.group(3))
return major, minor, rev
def parted_version():
"""
Returns the major and minor version of parted installed on the system.
@ -512,21 +539,10 @@ def parted_version():
msg="Failed to get parted version.", rc=rc, out=out, err=err
)
lines = [x for x in out.split('\n') if x.strip() != '']
if len(lines) == 0:
(major, minor, rev) = parse_parted_version(out)
if major is None:
module.fail_json(msg="Failed to get parted version.", rc=0, out=out)
matches = re.search(r'^parted.+(\d+)\.(\d+)(?:\.(\d+))?$', lines[0])
if matches is None:
module.fail_json(msg="Failed to get parted version.", rc=0, out=out)
# Convert version to numbers
major = int(matches.group(1))
minor = int(matches.group(2))
rev = 0
if matches.group(3) is not None:
rev = int(matches.group(3))
return major, minor, rev