Skip to content

Commit

Permalink
fix: remove deprecated distutils imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mstyushin committed Apr 15, 2024
1 parent 63c2887 commit a2cd6b9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
8 changes: 4 additions & 4 deletions mamonsu/plugins/pgsql/driver/pg8000/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import defaultdict, deque
from datetime import datetime as Datetime
from decimal import Decimal
from distutils.version import LooseVersion
from pkg_resources import packaging
from hashlib import md5
from itertools import count, islice
from struct import Struct
Expand Down Expand Up @@ -1416,11 +1416,11 @@ def handle_PARAMETER_STATUS(self, data, ps):
pass

elif key == b"server_version":
self._server_version = LooseVersion(value.decode('ascii'))
if self._server_version < LooseVersion('8.2.0'):
self._server_version = packaging.version.parse(value.decode('ascii'))
if self._server_version < packaging.version.parse('8.2.0'):
self._commands_with_count = (
b"INSERT", b"DELETE", b"UPDATE", b"MOVE")
elif self._server_version < LooseVersion('9.0.0'):
elif self._server_version < packaging.version.parse('9.0.0'):
self._commands_with_count = (
b"INSERT", b"DELETE", b"UPDATE", b"MOVE", b"FETCH",
b"COPY")
Expand Down
38 changes: 19 additions & 19 deletions mamonsu/plugins/pgsql/driver/pool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from distutils.version import LooseVersion
from pkg_resources import packaging
from .connection import Connection, ConnectionInfo


Expand Down Expand Up @@ -132,19 +132,19 @@ def server_version(self, db=None):

def server_version_greater(self, version, db=None):
db = self._normalize_db(db)
return self.server_version(db) >= LooseVersion(version)
return packaging.version.parse(self.server_version(db)) >= packaging.version.parse(version)

def server_version_less(self, version, db=None):
db = self._normalize_db(db)
return self.server_version(db) <= LooseVersion(version)
return packaging.version.parse(self.server_version(db)) <= packaging.version.parse(version)

def bootstrap_version_greater(self, version):
return str(
self._cache["bootstrap"]["version"]) >= LooseVersion(version)
return packaging.version.parse(
str(self._cache["bootstrap"]["version"])) >= packaging.version.parse(version)

def bootstrap_version_less(self, version):
return str(
self._cache["bootstrap"]["version"]) <= LooseVersion(version)
return packaging.version.parse(
str(self._cache["bootstrap"]["version"])) <= packaging.version.parse(version)

def in_recovery(self, db=None):
db = self._normalize_db(db)
Expand All @@ -166,16 +166,16 @@ def is_bootstraped(self, db=None):
self._cache["bootstrap"]["counter"] = 0
# TODO: изменить на нормальное название, 'config' слишком общее
sql = """
SELECT count(*)
FROM pg_catalog.pg_class
SELECT count(*)
FROM pg_catalog.pg_class
WHERE relname = 'config';
"""
result = int(self.query(sql, db)[0][0])
self._cache["bootstrap"]["storage"][db] = (result == 1)
if self._cache["bootstrap"]["storage"][db]:
self._connections[db].log.info("Found mamonsu bootstrap")
sql = """
SELECT max(version)
SELECT max(version)
FROM mamonsu.config;
"""
self._cache["bootstrap"]["version"] = self.query(sql, db)[0][0]
Expand Down Expand Up @@ -227,8 +227,8 @@ def is_pgpro_ee(self, db=None):
def extension_installed(self, ext, db=None):
db = self._normalize_db(db)
result = self.query("""
SELECT count(*)
FROM pg_catalog.pg_extension
SELECT count(*)
FROM pg_catalog.pg_extension
WHERE lower(extname) = lower('{0}');
""".format(ext), db)
return (int(result[0][0])) == 1
Expand All @@ -239,9 +239,9 @@ def extension_schema(self, extension, db=None):
return self._cache["extension_schema"][extension][db]
try:
self._cache["extension_schema"][extension][db] = self.query("""
SELECT n.nspname
FROM pg_extension e
JOIN pg_namespace n ON e.extnamespace = n.oid
SELECT n.nspname
FROM pg_extension e
JOIN pg_namespace n ON e.extnamespace = n.oid
WHERE e.extname = '{0}'
""".format(extension), db)[0][0]
return self._cache["extension_schema"][extension][db]
Expand All @@ -250,7 +250,7 @@ def extension_schema(self, extension, db=None):

def databases(self):
result, databases = self.query("""
SELECT datname
SELECT datname
FROM pg_catalog.pg_database;
"""), []
for row in result:
Expand Down Expand Up @@ -309,13 +309,13 @@ def get_sys_param(self, param, db=None):
db = self._normalize_db(db)
if self.is_bootstraped() and self.bootstrap_version_greater("2.3.4"):
result = self.query("""
SELECT *
SELECT *
FROM mamonsu.get_sys_param('{0}');
""".format(param))[0][0]
else:
result = self.query("""
SELECT setting
FROM pg_catalog.pg_settings
SELECT setting
FROM pg_catalog.pg_settings
WHERE name = '{0}';
""".format(param), db)[0][0]
return result
4 changes: 2 additions & 2 deletions mamonsu/plugins/pgsql/memory_leak_diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
from .pool import Pooler
import re
from distutils.version import LooseVersion
from pkg_resources import packaging
import mamonsu.lib.platform as platform
import posix

Expand Down Expand Up @@ -91,7 +91,7 @@ def run(self, zbx):
for row in Pooler.query(query=self.query):
pids.append(row[0])

if (LooseVersion(self.os_release) < LooseVersion("4.5")
if (packaging.version.parse(self.os_release) < packaging.version.parse("4.5")
and not (self.os_name == "centos" and self.os_version == "7")) \
or (not self.os_name and not self.os_version):
for pid in pids:
Expand Down
4 changes: 2 additions & 2 deletions mamonsu/plugins/pgsql/replication.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

from mamonsu.plugins.pgsql.plugin import PgsqlPlugin as Plugin
from distutils.version import LooseVersion
from pkg_resources import packaging
from .pool import Pooler
from mamonsu.lib.zbx_template import ZbxTemplate

Expand Down Expand Up @@ -201,7 +201,7 @@ def discovery_rules(self, template, dashboard=False):

def keys_and_queries(self, template_zabbix):
result = []
if LooseVersion(self.VersionPG) < LooseVersion("10"):
if packaging.version.parse(self.VersionPG) < packaging.version.parse("10"):
result.append("{0},$2 $1 -c \"{1}\"".format("pgsql.replication_lag.sec[*]",
self.query_agent_replication_lag.format(
self.plugin_config("interval"), "xlog_receive_location",
Expand Down
8 changes: 4 additions & 4 deletions mamonsu/tools/zabbix_cli/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
from mamonsu.tools.zabbix_cli.request import Request
from mamonsu.lib.parser import zabbix_msg
from distutils.version import LooseVersion
from pkg_resources import packaging
from mamonsu.tools.zabbix_cli.dashboard import generate_dashboard


Expand Down Expand Up @@ -171,10 +171,10 @@ def template(self, args):
}
},
'source': open(file).read()}
if LooseVersion(zabbix_version) < LooseVersion('5.4'):
if packaging.version.parse(zabbix_version) < packaging.version.parse('5.4'):
params['rules']['applications'] = {'createMissing': True,
'deleteMissing': True}
if LooseVersion(zabbix_version) < LooseVersion('5.2'):
if packaging.version.parse(zabbix_version) < packaging.version.parse('5.2'):
params['rules']['templateScreens'] = {'createMissing': True,
'updateExisting': False,
'deleteMissing': True}
Expand Down Expand Up @@ -329,7 +329,7 @@ def dashboard(self, args):
if not len(args) == 2:
return self._print_help()
zabbix_version = str(self.req.post(method='apiinfo.version', params=[]))
if LooseVersion(zabbix_version) < LooseVersion('6.0'):
if packaging.version.parse(zabbix_version) < packaging.version.parse('6.0'):
print("You can import Mamonsu dashboard only on Zabbix 6.0+.")
return
else:
Expand Down
4 changes: 2 additions & 2 deletions mamonsu/tools/zabbix_cli/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections import OrderedDict


from distutils.version import LooseVersion
from pkg_resources import packaging
import urllib.request as urllib2


Expand All @@ -26,7 +26,7 @@ def _auth(self):
if self._auth_tocken is None:
if not self._user:
return None
user_field = 'user' if LooseVersion(self._api_version) < LooseVersion('6.4') else 'username'
user_field = 'user' if packaging.version.parse(self._api_version) < packaging.version.parse('6.4') else 'username'
self._auth_tocken = self.post(
'user.login',
{user_field: self._user, 'password': self._passwd})
Expand Down

0 comments on commit a2cd6b9

Please sign in to comment.