Skip to content

Commit 4040d84

Browse files
committed
code cleanup: dropped python2 support in libs, code formating in libs. min pylint level set to 6
1 parent dbb7d47 commit 4040d84

File tree

8 files changed

+53
-64
lines changed

8 files changed

+53
-64
lines changed

.github/workflows/tests-pull_request.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
python -m pip install pylint==2.17.7 pylint_runner flask
2626
- name: Lint with pyLint
2727
run: |
28-
pylint $(git ls-files '*.py') --fail-under 4
28+
pylint $(git ls-files '*.py') --fail-under 6
2929
- name: Check with Unitests
3030
run: |
3131
./tests.py

.github/workflows/tests-push.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
#if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
2828
- name: Lint with pyLint
2929
run: |
30-
pylint $(git ls-files '*.py') --fail-under 4
30+
pylint $(git ls-files '*.py') --fail-under 6
3131
- name: Check with Unitest
3232
run: |
3333
./tests.py

collectors/lib/docker_engine/docker_metrics.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
from __future__ import print_function
1616
import time
1717
import requests
18-
from prometheus_client.parser import text_string_to_metric_families
18+
from prometheus_client.parser import text_string_to_metric_families # pylint: disable=import-error
1919
from collectors.lib.docker_engine.metric import Metric
2020

21+
2122
class DockerMetrics(object):
2223
def __init__(self, url):
2324
self._url = url

collectors/lib/postgresqlutils.py

+35-35
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,61 @@
1515
Collector Utilities for PostgreSQL.
1616
"""
1717

18-
import sys
1918
import os
20-
import time
2119
import socket
22-
import errno
2320

2421
try:
25-
import psycopg2
22+
import psycopg2
2623
except ImportError:
27-
psycopg2 = None # handled in main()
24+
psycopg2 = None # handled in main()
2825

29-
CONNECT_TIMEOUT = 2 # seconds
26+
CONNECT_TIMEOUT = 2 # seconds
3027

3128
from collectors.lib import utils
3229
from collectors.etc import postgresqlconf
3330

3431
# Directories under which to search socket files
3532
SEARCH_DIRS = frozenset([
36-
"/var/run/postgresql", # Debian default
37-
"/var/pgsql_socket", # MacOS default
38-
"/usr/local/var/postgres", # custom compilation
39-
"/tmp", # custom compilation
33+
"/var/run/postgresql", # Debian default
34+
"/var/pgsql_socket", # MacOS default
35+
"/usr/local/var/postgres", # custom compilation
36+
"/tmp", # custom compilation
4037
])
4138

39+
4240
def find_sockdir():
43-
"""Returns a path to PostgreSQL socket file to monitor."""
44-
for dir in SEARCH_DIRS:
45-
for dirpath, dirnames, dirfiles in os.walk(dir, followlinks=True):
46-
for name in dirfiles:
47-
# ensure selection of PostgreSQL socket only
48-
if (utils.is_sockfile(os.path.join(dirpath, name)) and "PGSQL" in name):
49-
return(dirpath)
41+
"""Returns a path to PostgreSQL socket file to monitor."""
42+
for dir in SEARCH_DIRS:
43+
for dirpath, dirnames, dirfiles in os.walk(dir, followlinks=True):
44+
for name in dirfiles:
45+
# ensure selection of PostgreSQL socket only
46+
if (utils.is_sockfile(os.path.join(dirpath, name)) and "PGSQL" in name):
47+
return dirpath
48+
5049

5150
def postgres_connect(sockdir):
52-
"""Connects to the PostgreSQL server using the specified socket file."""
53-
user, password = postgresqlconf.get_user_password()
51+
"""Connects to the PostgreSQL server using the specified socket file."""
52+
user, password = postgresqlconf.get_user_password()
53+
54+
try:
55+
return psycopg2.connect("host='%s' user='%s' password='%s' "
56+
"connect_timeout='%s' dbname=postgres"
57+
% (sockdir, user, password,
58+
CONNECT_TIMEOUT))
59+
except (EnvironmentError, EOFError, RuntimeError, socket.error) as e:
60+
utils.err("Couldn't connect to DB :%s" % (e))
5461

55-
try:
56-
return psycopg2.connect("host='%s' user='%s' password='%s' "
57-
"connect_timeout='%s' dbname=postgres"
58-
% (sockdir, user, password,
59-
CONNECT_TIMEOUT))
60-
except (EnvironmentError, EOFError, RuntimeError, socket.error) as e:
61-
utils.err("Couldn't connect to DB :%s" % (e))
6262

6363
def connect():
64-
"""Returns an initialized connection to Postgresql."""
65-
if psycopg2 is None:
66-
raise RuntimeError("error: Python module 'psycopg2' is missing")
64+
"""Returns an initialized connection to Postgresql."""
65+
if psycopg2 is None:
66+
raise RuntimeError("error: Python module 'psycopg2' is missing")
6767

68-
sockdir = find_sockdir()
69-
if not sockdir: # Nothing to monitor
70-
raise RuntimeError("error: Can't find postgresql socket file")
68+
sockdir = find_sockdir()
69+
if not sockdir: # Nothing to monitor
70+
raise RuntimeError("error: Can't find postgresql socket file")
7171

72-
db = postgres_connect(sockdir)
73-
db.autocommit=True
72+
db = postgres_connect(sockdir)
73+
db.autocommit = True
7474

75-
return db
75+
return db

collectors/lib/utils.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# This file is part of tcollector.
3-
# Copyright (C) 2013 The tcollector Authors.
3+
# Copyright (C) 2013-2024 The tcollector Authors.
44
#
55
# This program is free software: you can redistribute it and/or modify it
66
# under the terms of the GNU Lesser General Public License as published by
@@ -22,8 +22,6 @@
2222
import errno
2323
import sys
2424

25-
PY3 = sys.version_info[0] > 2
26-
2725
# If we're running as root and this user exists, we'll drop privileges.
2826
USER = "nobody"
2927

@@ -58,14 +56,5 @@ def err(msg):
5856
print(msg, file=sys.stderr)
5957

6058

61-
if PY3:
62-
def is_numeric(value):
63-
return isinstance(value, (int, float))
64-
else:
65-
def is_numeric(value):
66-
try:
67-
float(str(value))
68-
return True
69-
except ValueError:
70-
pass
71-
return False
59+
def is_numeric(value):
60+
return isinstance(value, (int, float))

collectors/test/docker_engine/test_docker_metrics.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ def test_eval_prometheus_line(self):
3333
provided_line = provided.get_metric_lines()
3434
self.assertEqual(expected_line, provided_line)
3535

36+
3637
if __name__ == '__main__':
3738
unittest.main()

mocks.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
# for debugging
1818
real_stderr = sys.stderr
1919

20+
2021
class SocketDone(Exception):
2122
pass
2223

24+
2325
class Socket():
2426
def __init__(self):
2527
self.AF_INET = 0
@@ -42,13 +44,14 @@ def close(self):
4244
return None
4345

4446
def recvfrom(self, inBytes):
45-
if (len(self.state['udp_in']) > 0):
47+
if len(self.state['udp_in']) > 0:
4648
line = self.state['udp_in'].pop(0)
4749
return (line, None)
4850
else:
4951
raise SocketDone('stop reading from socket')
5052

51-
class Sys():
53+
54+
class Sys:
5255
def __init__(self):
5356
self.stderr = self.Stderr()
5457
self.stdout = self.Stdout()
@@ -59,21 +62,22 @@ def exit(self, exitCode):
5962
msg = 'exit called with code %s\n stderr: %s\n trace: %s'
6063
raise Exception(msg % (exitCode, err, trace))
6164

62-
class Stderr():
65+
class Stderr:
6366
def __init__(self):
6467
self.lines = []
6568

6669
def write(self, outString):
6770
self.lines.append(outString)
6871

69-
class Stdout():
72+
class Stdout:
7073
def __init__(self):
7174
self.lines = []
7275

7376
def write(self, outString):
7477
self.lines.append(outString)
7578

76-
class Utils():
79+
80+
class Utils:
7781
def __init__(self):
7882
self.drop_privileges = lambda: None
7983

tests.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
import tcollector
3030

3131

32-
PY3 = sys.version_info[0] > 2
33-
34-
3532
def return_none(x):
3633
return None
3734

@@ -113,10 +110,7 @@ def check_access_rights(top):
113110
elif S_ISREG(mode):
114111
# file, check permissions
115112
permissions = oct(os.stat(pathname)[ST_MODE])
116-
if PY3:
117-
self.assertEqual("0o100755", permissions)
118-
else:
119-
self.assertEqual("0100755", permissions)
113+
self.assertEqual("0o100755", permissions)
120114
else:
121115
# unknown file type
122116
pass

0 commit comments

Comments
 (0)