Move from platform.linux_distribution to distro.id

The builtin platform.linux_distribution[1] is deprecated and will be
removed in 3.8 and the recommended replacement is distro.
This also raises a "deprecated method" error in pylint.

This patch moves us over to the future by following the recommendation
in the python docs and switching to use the "distro" module.

[1] https://docs.python.org/2/library/platform.html \
    #platform.linux_distribution

Depends-On: https://review.openstack.org/578983
Change-Id: I29e2673572eab75b553da6b01143b007701808fd
This commit is contained in:
Michael Johnson 2018-06-29 14:30:21 -07:00
parent 0322cbc5c3
commit 14cb07f2d9
8 changed files with 32 additions and 32 deletions

View File

@ -54,8 +54,7 @@ disable=
too-many-statements,
multiple-statements,
duplicate-except,
keyword-arg-before-vararg,
deprecated-method
keyword-arg-before-vararg
[BASIC]
# Variable names can be 1 to 31 characters long, with lowercase and underscores

View File

@ -25,6 +25,7 @@ debtcollector==1.19.0
decorator==4.2.1
deprecation==2.0
diskimage-builder==1.1.2
distro===1.2.0
doc8==0.6.0
docutils==0.14
dogpile.cache==0.6.5

View File

@ -14,11 +14,11 @@
import ipaddress
import os
import platform
import shutil
import stat
import subprocess
import distro
import jinja2
from oslo_config import cfg
from oslo_log import log as logging
@ -45,7 +45,7 @@ class BaseOS(object):
@classmethod
def get_os_util(cls):
os_name = platform.linux_distribution(full_distribution_name=False)[0]
os_name = distro.id()
for subclass in BaseOS.__subclasses__():
if subclass.is_os_name(os_name):
return subclass(os_name)
@ -219,7 +219,7 @@ class Ubuntu(BaseOS):
@classmethod
def is_os_name(cls, os_name):
return os_name in ['Ubuntu']
return os_name in ['ubuntu']
def cmd_get_version_of_installed_package(self, package_name):
return "dpkg --status {name}".format(name=package_name)
@ -303,7 +303,7 @@ class RH(BaseOS):
@classmethod
def is_os_name(cls, os_name):
return os_name in ['fedora', 'redhat', 'centos']
return os_name in ['fedora', 'rhel', 'centos']
def cmd_get_version_of_installed_package(self, package_name):
return "rpm -qi {name}".format(name=package_name)

View File

@ -45,13 +45,13 @@ class TestServerTestCase(base.TestCase):
def setUp(self):
super(TestServerTestCase, self).setUp()
with mock.patch('platform.linux_distribution',
return_value=['Ubuntu', 'Foo', 'Bar']):
with mock.patch('distro.id',
return_value='ubuntu'):
self.ubuntu_test_server = server.Server()
self.ubuntu_app = self.ubuntu_test_server.app.test_client()
with mock.patch('platform.linux_distribution',
return_value=['centos', 'Foo', 'Bar']):
with mock.patch('distro.id',
return_value='centos'):
self.centos_test_server = server.Server()
self.centos_app = self.centos_test_server.app.test_client()

View File

@ -34,8 +34,8 @@ class ListenerTestCase(base.TestCase):
self.jinja_cfg = jinja_cfg.JinjaTemplater(
base_amp_path=BASE_AMP_PATH,
base_crt_dir=BASE_CRT_PATH)
self.mock_platform = mock.patch("platform.linux_distribution").start()
self.mock_platform.return_value = ("Ubuntu",)
self.mock_platform = mock.patch("distro.id").start()
self.mock_platform.return_value = "ubuntu"
self.test_listener = listener.Listener()
def test_parse_haproxy_config(self):

View File

@ -32,33 +32,33 @@ class TestOSUtils(base.TestCase):
self.base_os_util = osutils.BaseOS('unknown')
with mock.patch('platform.linux_distribution',
return_value=['Ubuntu', 'Foo', 'Bar']):
with mock.patch('distro.id',
return_value='ubuntu'):
self.ubuntu_os_util = osutils.BaseOS.get_os_util()
with mock.patch('platform.linux_distribution',
return_value=['centos', 'Foo', 'Bar']):
with mock.patch('distro.id',
return_value='centos'):
self.rh_os_util = osutils.BaseOS.get_os_util()
def test_get_os_util(self):
with mock.patch('platform.linux_distribution',
return_value=['Ubuntu', 'Foo', 'Bar']):
with mock.patch('distro.id',
return_value='ubuntu'):
returned_cls = osutils.BaseOS.get_os_util()
self.assertIsInstance(returned_cls, osutils.Ubuntu)
with mock.patch('platform.linux_distribution',
return_value=['fedora', 'Foo', 'Bar']):
with mock.patch('distro.id',
return_value='fedora'):
returned_cls = osutils.BaseOS.get_os_util()
self.assertIsInstance(returned_cls, osutils.RH)
with mock.patch('platform.linux_distribution',
return_value=['redhat', 'Foo', 'Bar']):
with mock.patch('distro.id',
return_value='rhel'):
returned_cls = osutils.BaseOS.get_os_util()
self.assertIsInstance(returned_cls, osutils.RH)
with mock.patch('platform.linux_distribution',
return_value=['centos', 'Foo', 'Bar']):
with mock.patch('distro.id',
return_value='centos'):
returned_cls = osutils.BaseOS.get_os_util()
self.assertIsInstance(returned_cls, osutils.RH)
with mock.patch('platform.linux_distribution',
return_value=['FakeOS', 'Foo', 'Bar']):
with mock.patch('distro.id',
return_value='FakeOS'):
self.assertRaises(
octavia_exceptions.InvalidAmphoraOperatingSystem,
osutils.BaseOS.get_os_util)

View File

@ -37,8 +37,8 @@ class TestPlug(base.TestCase):
def setUp(self):
super(TestPlug, self).setUp()
self.mock_netifaces = mock.patch.object(plug, "netifaces").start()
self.mock_platform = mock.patch("platform.linux_distribution").start()
self.mock_platform.return_value = ("Ubuntu",)
self.mock_platform = mock.patch("distro.id").start()
self.mock_platform.return_value = "ubuntu"
self.osutil = osutils.BaseOS.get_os_util()
self.test_plug = plug.Plug(self.osutil)
self.addCleanup(self.mock_netifaces.stop)
@ -57,8 +57,7 @@ class TestPlug(base.TestCase):
self.assertEqual(FAKE_INTERFACE, interface)
def test__interface_by_mac_case_insensitive_rh(self):
with mock.patch('platform.linux_distribution',
return_value=['centos', 'Foo']):
with mock.patch('distro.id', return_value='centos'):
osutil = osutils.BaseOS.get_os_util()
self.test_plug = plug.Plug(osutil)
interface = self.test_plug._interface_by_mac(
@ -160,8 +159,8 @@ class TestPlug(base.TestCase):
class TestPlugNetwork(base.TestCase):
def setUp(self):
super(TestPlugNetwork, self).setUp()
self.mock_platform = mock.patch("platform.linux_distribution").start()
self.mock_platform.return_value = ("Ubuntu",)
self.mock_platform = mock.patch("distro.id").start()
self.mock_platform.return_value = "ubuntu"
self.osutil = osutils.BaseOS.get_os_util()
self.test_plug = plug.Plug(self.osutil)

View File

@ -40,6 +40,7 @@ diskimage-builder!=1.6.0,!=1.7.0,!=1.7.1,>=1.1.2 # Apache-2.0
futures>=3.0.0;python_version=='2.7' or python_version=='2.6' # BSD
castellan>=0.16.0 # Apache-2.0
tenacity>=4.9.0 # Apache-2.0
distro>=1.2.0 # Apache-2.0
#for the amphora api
Flask!=0.11,>=0.10 # BSD