Skip to content

Commit

Permalink
Support distro.linux_distribution
Browse files Browse the repository at this point in the history
Salt fails on Python 3.8:

```
======================================================================
ERROR: unit.grains.test_core (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: unit.grains.test_core
Traceback (most recent call last):
  File "/usr/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "tests/unit/grains/test_core.py", line 37, in <module>
    import salt.grains.core as core
  File "salt/grains/core.py", line 40, in <module>
    from platform import _supported_dists
ImportError: cannot import name '_supported_dists' from 'platform' (/usr/lib/python3.8/platform.py)
```

So only try to import `_supported_dists` from `platform` for Python <=
3.7. Otherwise rely on the external `distro` module to  not need any
special handling.

Addresses parts of saltstack#55835
Signed-off-by: Benjamin Drung <[email protected]>
  • Loading branch information
bdrung authored and andzn committed Nov 12, 2021
1 parent 44a1496 commit 7301ff2
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions salt/grains/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,36 @@
__proxyenabled__ = ['*']
__FQDN__ = None

# Extend the default list of supported distros. This will be used for the
# /etc/DISTRO-release checking that is part of linux_distribution()
from platform import _supported_dists
_supported_dists += ('arch', 'mageia', 'meego', 'vmware', 'bluewhite64',
'slamd64', 'ovs', 'system', 'mint', 'oracle', 'void')

# linux_distribution deprecated in py3.7
try:
from platform import linux_distribution as _deprecated_linux_distribution

# Extend the default list of supported distros. This will be used for the
# /etc/DISTRO-release checking that is part of linux_distribution()
from platform import _supported_dists

_supported_dists += (
"arch",
"mageia",
"meego",
"vmware",
"bluewhite64",
"slamd64",
"ovs",
"system",
"mint",
"oracle",
"void",
)

def linux_distribution(**kwargs):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
return _deprecated_linux_distribution(**kwargs)
return _deprecated_linux_distribution(
supported_dists=_supported_dists, **kwargs
)


except ImportError:
from distro import linux_distribution

Expand Down Expand Up @@ -1937,9 +1953,9 @@ def os_data():
'Getting OS name, release, and codename from '
'platform.linux_distribution()'
)
(osname, osrelease, oscodename) = \
[x.strip('"').strip("'") for x in
linux_distribution(supported_dists=_supported_dists)]
(osname, osrelease, oscodename) = [
x.strip('"').strip("'") for x in linux_distribution()
]
# Try to assign these three names based on the lsb info, they tend to
# be more accurate than what python gets from /etc/DISTRO-release.
# It's worth noting that Ubuntu has patched their Python distribution
Expand Down

0 comments on commit 7301ff2

Please sign in to comment.