Skip to content

Commit

Permalink
code cleanup: dropped python2 support in libs, code formating in libs…
Browse files Browse the repository at this point in the history
…. min pylint level set to 6
  • Loading branch information
vasiliyk committed May 16, 2024
1 parent dbb7d47 commit 4040d84
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests-pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
python -m pip install pylint==2.17.7 pylint_runner flask
- name: Lint with pyLint
run: |
pylint $(git ls-files '*.py') --fail-under 4
pylint $(git ls-files '*.py') --fail-under 6
- name: Check with Unitests
run: |
./tests.py
2 changes: 1 addition & 1 deletion .github/workflows/tests-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
#if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with pyLint
run: |
pylint $(git ls-files '*.py') --fail-under 4
pylint $(git ls-files '*.py') --fail-under 6
- name: Check with Unitest
run: |
./tests.py
3 changes: 2 additions & 1 deletion collectors/lib/docker_engine/docker_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
from __future__ import print_function
import time
import requests
from prometheus_client.parser import text_string_to_metric_families
from prometheus_client.parser import text_string_to_metric_families # pylint: disable=import-error
from collectors.lib.docker_engine.metric import Metric


class DockerMetrics(object):
def __init__(self, url):
self._url = url
Expand Down
70 changes: 35 additions & 35 deletions collectors/lib/postgresqlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,61 @@
Collector Utilities for PostgreSQL.
"""

import sys
import os
import time
import socket
import errno

try:
import psycopg2
import psycopg2
except ImportError:
psycopg2 = None # handled in main()
psycopg2 = None # handled in main()

CONNECT_TIMEOUT = 2 # seconds
CONNECT_TIMEOUT = 2 # seconds

from collectors.lib import utils
from collectors.etc import postgresqlconf

# Directories under which to search socket files
SEARCH_DIRS = frozenset([
"/var/run/postgresql", # Debian default
"/var/pgsql_socket", # MacOS default
"/usr/local/var/postgres", # custom compilation
"/tmp", # custom compilation
"/var/run/postgresql", # Debian default
"/var/pgsql_socket", # MacOS default
"/usr/local/var/postgres", # custom compilation
"/tmp", # custom compilation
])


def find_sockdir():
"""Returns a path to PostgreSQL socket file to monitor."""
for dir in SEARCH_DIRS:
for dirpath, dirnames, dirfiles in os.walk(dir, followlinks=True):
for name in dirfiles:
# ensure selection of PostgreSQL socket only
if (utils.is_sockfile(os.path.join(dirpath, name)) and "PGSQL" in name):
return(dirpath)
"""Returns a path to PostgreSQL socket file to monitor."""
for dir in SEARCH_DIRS:
for dirpath, dirnames, dirfiles in os.walk(dir, followlinks=True):
for name in dirfiles:
# ensure selection of PostgreSQL socket only
if (utils.is_sockfile(os.path.join(dirpath, name)) and "PGSQL" in name):
return dirpath


def postgres_connect(sockdir):
"""Connects to the PostgreSQL server using the specified socket file."""
user, password = postgresqlconf.get_user_password()
"""Connects to the PostgreSQL server using the specified socket file."""
user, password = postgresqlconf.get_user_password()

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

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

def connect():
"""Returns an initialized connection to Postgresql."""
if psycopg2 is None:
raise RuntimeError("error: Python module 'psycopg2' is missing")
"""Returns an initialized connection to Postgresql."""
if psycopg2 is None:
raise RuntimeError("error: Python module 'psycopg2' is missing")

sockdir = find_sockdir()
if not sockdir: # Nothing to monitor
raise RuntimeError("error: Can't find postgresql socket file")
sockdir = find_sockdir()
if not sockdir: # Nothing to monitor
raise RuntimeError("error: Can't find postgresql socket file")

db = postgres_connect(sockdir)
db.autocommit=True
db = postgres_connect(sockdir)
db.autocommit = True

return db
return db
17 changes: 3 additions & 14 deletions collectors/lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# This file is part of tcollector.
# Copyright (C) 2013 The tcollector Authors.
# Copyright (C) 2013-2024 The tcollector Authors.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
Expand All @@ -22,8 +22,6 @@
import errno
import sys

PY3 = sys.version_info[0] > 2

# If we're running as root and this user exists, we'll drop privileges.
USER = "nobody"

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


if PY3:
def is_numeric(value):
return isinstance(value, (int, float))
else:
def is_numeric(value):
try:
float(str(value))
return True
except ValueError:
pass
return False
def is_numeric(value):
return isinstance(value, (int, float))
1 change: 1 addition & 0 deletions collectors/test/docker_engine/test_docker_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ def test_eval_prometheus_line(self):
provided_line = provided.get_metric_lines()
self.assertEqual(expected_line, provided_line)


if __name__ == '__main__':
unittest.main()
14 changes: 9 additions & 5 deletions mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
# for debugging
real_stderr = sys.stderr


class SocketDone(Exception):
pass


class Socket():
def __init__(self):
self.AF_INET = 0
Expand All @@ -42,13 +44,14 @@ def close(self):
return None

def recvfrom(self, inBytes):
if (len(self.state['udp_in']) > 0):
if len(self.state['udp_in']) > 0:
line = self.state['udp_in'].pop(0)
return (line, None)
else:
raise SocketDone('stop reading from socket')

class Sys():

class Sys:
def __init__(self):
self.stderr = self.Stderr()
self.stdout = self.Stdout()
Expand All @@ -59,21 +62,22 @@ def exit(self, exitCode):
msg = 'exit called with code %s\n stderr: %s\n trace: %s'
raise Exception(msg % (exitCode, err, trace))

class Stderr():
class Stderr:
def __init__(self):
self.lines = []

def write(self, outString):
self.lines.append(outString)

class Stdout():
class Stdout:
def __init__(self):
self.lines = []

def write(self, outString):
self.lines.append(outString)

class Utils():

class Utils:
def __init__(self):
self.drop_privileges = lambda: None

Expand Down
8 changes: 1 addition & 7 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
import tcollector


PY3 = sys.version_info[0] > 2


def return_none(x):
return None

Expand Down Expand Up @@ -113,10 +110,7 @@ def check_access_rights(top):
elif S_ISREG(mode):
# file, check permissions
permissions = oct(os.stat(pathname)[ST_MODE])
if PY3:
self.assertEqual("0o100755", permissions)
else:
self.assertEqual("0100755", permissions)
self.assertEqual("0o100755", permissions)
else:
# unknown file type
pass
Expand Down

0 comments on commit 4040d84

Please sign in to comment.