Skip to content

Commit f91ee90

Browse files
authored
Merge pull request #103 from itkovian/ruff-format-check
Ruff format check
2 parents 151d8b5 + 717e0b8 commit f91ee90

15 files changed

Lines changed: 260 additions & 176 deletions

.github/workflows/unittest.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
# This file was automatically generated using 'python -m vsc.install.ci'
33
# DO NOT EDIT MANUALLY
44
jobs:
5+
python_ruff_check: &id001
6+
runs-on: ubuntu-24.04
7+
steps:
8+
- name: Checkout code
9+
uses: actions/checkout@v4
10+
- name: Setup Python
11+
uses: actions/setup-python@v5
12+
with:
13+
python-version: ${{matrix.python}}
14+
- name: install ruff
15+
run: pip install 'ruff'
16+
- name: Run ruff format
17+
run: ruff format --check .
18+
- name: Run ruff
19+
run: ruff check .
20+
strategy:
21+
matrix:
22+
python:
23+
- 3.9
24+
python_ruff_format: *id001
525
python_unittests:
626
runs-on: ubuntu-24.04
727
steps:

Jenkinsfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ node {
88
// remove untracked files (*.pyc for example)
99
sh 'git clean -fxd'
1010
}
11+
stage ('ruff format') {
12+
sh 'curl -L --silent https://github.com/astral-sh/ruff/releases/download/0.13.1/ruff-x86_64-unknown-linux-gnu.tar.gz --output - | tar -xzv'
13+
sh 'cp ruff-x86_64-unknown-linux-gnu/ruff .'
14+
sh './ruff --version'
15+
sh './ruff format --check .'
16+
}
17+
stage ('ruff check') {
18+
sh 'curl -L --silent https://github.com/astral-sh/ruff/releases/download/0.13.1/ruff-x86_64-unknown-linux-gnu.tar.gz --output - | tar -xzv'
19+
sh 'cp ruff-x86_64-unknown-linux-gnu/ruff .'
20+
sh './ruff --version'
21+
sh './ruff check .'
22+
}
1123
stage('test') {
1224
sh 'pip3 install --ignore-installed --prefix $PWD/.vsc-tox tox'
1325
sh 'export PATH=$PWD/.vsc-tox/bin:$PATH && export PYTHONPATH=$PWD/.vsc-tox/lib/python$(python3 -c "import sys; print(\\"%s.%s\\" % sys.version_info[:2])")/site-packages:$PYTHONPATH && tox -v -c tox.ini'

lib/vsc/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@
2626
"""
2727
Allow other packages to extend this namespace, zip safe setuptools style
2828
"""
29+
2930
import pkg_resources
31+
3032
pkg_resources.declare_namespace(__name__)

lib/vsc/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@
2626
"""
2727
Allow other packages to extend this namespace, zip safe setuptools style
2828
"""
29+
2930
import pkg_resources
31+
3032
pkg_resources.declare_namespace(__name__)

lib/vsc/utils/availability.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def proceed_on_ha_service(host_ip):
4444

4545
machine_addresses = []
4646
for iface_name in interfaces():
47-
addresses = [i['addr'] for i in ifaddresses(iface_name).setdefault(AF_INET, [{'addr': None}]) if i['addr']]
47+
addresses = [i["addr"] for i in ifaddresses(iface_name).setdefault(AF_INET, [{"addr": None}]) if i["addr"]]
4848
machine_addresses.extend(addresses)
4949

5050
return host_ip in machine_addresses

lib/vsc/utils/cache.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
2929
@author: Andy Georges (Ghent University)
3030
"""
31+
3132
import gzip
3233
import os
3334
import time
@@ -37,6 +38,7 @@
3738

3839
from vsc.utils import fancylogger
3940

41+
4042
class FileCache:
4143
"""File cache with a timestamp safety.
4244
@@ -78,11 +80,11 @@ def __init__(self, filename, retain_old=True, raise_unpickable=False):
7880
return
7981

8082
try:
81-
with open(self.filename, 'rb') as f:
83+
with open(self.filename, "rb") as f:
8284
try:
83-
g = gzip.GzipFile(mode='rb', fileobj=f) # no context manager available in python 26 yet
85+
g = gzip.GzipFile(mode="rb", fileobj=f) # no context manager available in python 26 yet
8486
s = g.read()
85-
except (OSError) as _:
87+
except OSError as _:
8688
self.log.error("Cannot load data from cache file %s as gzipped json", self.filename)
8789
try:
8890
f.seek(0)
@@ -164,19 +166,19 @@ def close(self):
164166
dirname = os.path.dirname(self.filename)
165167
if not os.path.exists(dirname):
166168
os.makedirs(dirname)
167-
with open(self.filename, 'wb') as fih:
169+
with open(self.filename, "wb") as fih:
168170
if not fih:
169-
self.log.error('cannot open the file cache at %s for writing', self.filename)
171+
self.log.error("cannot open the file cache at %s for writing", self.filename)
170172
else:
171173
if self.retain_old:
172174
if self.shelf is None:
173175
self.shelf = {}
174176
self.shelf.update(self.new_shelf)
175177
self.new_shelf = self.shelf
176178

177-
with gzip.GzipFile(mode='wb', fileobj=fih) as zipf:
179+
with gzip.GzipFile(mode="wb", fileobj=fih) as zipf:
178180
pickled = jsonpickle.encode(self.new_shelf)
179181
# .encode() is required in Python 3, since we need to pass a bytestring
180182
zipf.write(pickled.encode())
181183

182-
self.log.info('closing the file cache at %s', self.filename)
184+
self.log.info("closing the file cache at %s", self.filename)

lib/vsc/utils/graphyte.py

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
import threading
4343
import time
4444

45-
__all__ = ['Sender', 'init', 'send']
45+
__all__ = ["Sender", "init", "send"]
4646

47-
__version__ = '1.7.1'
47+
__version__ = "1.7.1"
4848

4949
default_sender = None
5050
logger = logging.getLogger(__name__)
@@ -55,9 +55,20 @@ def _has_whitespace(value):
5555

5656

5757
class Sender:
58-
def __init__(self, host, port=2003, prefix=None, timeout=5, interval=None,
59-
queue_size=None, log_sends=False, protocol='tcp',
60-
batch_size=1000, tags=None, raise_send_errors=False):
58+
def __init__(
59+
self,
60+
host,
61+
port=2003,
62+
prefix=None,
63+
timeout=5,
64+
interval=None,
65+
queue_size=None,
66+
log_sends=False,
67+
protocol="tcp",
68+
batch_size=1000,
69+
tags=None,
70+
raise_send_errors=False,
71+
):
6172
"""Initialize a Sender instance, starting the background thread to
6273
send messages at given interval (in seconds) if "interval" is not
6374
None. Send at most "batch_size" messages per socket send operation.
@@ -83,7 +94,7 @@ def __init__(self, host, port=2003, prefix=None, timeout=5, interval=None,
8394

8495
if self.interval is not None:
8596
if raise_send_errors:
86-
raise ValueError('raise_send_errors must be disabled when interval is set')
97+
raise ValueError("raise_send_errors must be disabled when interval is set")
8798
if queue_size is None:
8899
queue_size = int(round(interval)) * 100
89100
self._queue = queue.Queue(maxsize=queue_size)
@@ -111,19 +122,18 @@ def build_message(self, metric, value, timestamp, tags=None):
111122
if _has_whitespace(metric):
112123
raise ValueError('"metric" must not have whitespace in it')
113124
if not isinstance(value, (int, float)):
114-
raise TypeError('"value" must be an int or a float, not a %s',
115-
type(value).__name__)
125+
raise TypeError('"value" must be an int or a float, not a %s', type(value).__name__)
116126

117127
all_tags = self.tags.copy()
118128
all_tags.update(tags)
119-
tags_strs = [f';{k}={v}' for k, v in sorted(all_tags.items())]
129+
tags_strs = [f";{k}={v}" for k, v in sorted(all_tags.items())]
120130
if any(_has_whitespace(t) for t in tags_strs):
121131
raise ValueError('"tags" keys and values must not have whitespace in them')
122-
tags_suffix = ''.join(tags_strs)
132+
tags_suffix = "".join(tags_strs)
123133

124-
prefix = self.prefix + '.' if self.prefix else ''
134+
prefix = self.prefix + "." if self.prefix else ""
125135
message = f"{prefix}{metric}{tags_suffix} {value} {int(round(timestamp))}\n"
126-
message = message.encode('utf-8')
136+
message = message.encode("utf-8")
127137
return message
128138

129139
def send(self, metric, value, timestamp=None, tags=None):
@@ -147,18 +157,18 @@ def send(self, metric, value, timestamp=None, tags=None):
147157
try:
148158
self._queue.put_nowait(message)
149159
except queue.Full:
150-
logger.error('queue full when sending %s', message)
160+
logger.error("queue full when sending %s", message)
151161

152162
def send_message(self, message):
153-
if self.protocol == 'tcp':
163+
if self.protocol == "tcp":
154164
with socket.create_connection((self.host, self.port), self.timeout) as sock:
155165
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
156166
sock.sendall(message)
157-
elif self.protocol == 'udp':
167+
elif self.protocol == "udp":
158168
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
159169
sock.sendto(message, (self.host, self.port))
160170
else:
161-
raise ValueError('"protocol" must be \'tcp\' or \'udp\', not %s', self.protocol)
171+
raise ValueError("\"protocol\" must be 'tcp' or 'udp', not %s", self.protocol)
162172

163173
def send_socket(self, message):
164174
"""Low-level function to send message bytes to this Sender's socket.
@@ -172,12 +182,11 @@ def send_socket(self, message):
172182
except Exception as error:
173183
if self.raise_send_errors:
174184
raise
175-
logger.error('error sending message %s: %s', message, error)
185+
logger.error("error sending message %s: %s", message, error)
176186
else:
177187
if self.log_sends:
178188
elapsed_time = time.time() - start_time
179-
logger.info('sent message %s to %s:%s in %s seconds',
180-
message, self.host, self.port, elapsed_time)
189+
logger.info("sent message %s to %s:%s in %s seconds", message, self.host, self.port, elapsed_time)
181190

182191
def _thread_loop(self):
183192
"""Background thread used when Sender is in asynchronous/interval mode."""
@@ -218,14 +227,15 @@ def _thread_loop(self):
218227
if current_time - last_check_time >= self.interval:
219228
last_check_time = current_time
220229
for i in range(0, len(messages), self.batch_size):
221-
batch = messages[i:i + self.batch_size]
222-
self.send_socket(b''.join(batch))
230+
batch = messages[i : i + self.batch_size]
231+
self.send_socket(b"".join(batch))
223232
messages = []
224233

225234
# Send any final messages before exiting thread
226235
for i in range(0, len(messages), self.batch_size):
227-
batch = messages[i:i + self.batch_size]
228-
self.send_socket(b''.join(batch))
236+
batch = messages[i : i + self.batch_size]
237+
self.send_socket(b"".join(batch))
238+
229239

230240
def init(*args, **kwargs):
231241
"""Initialize default Sender instance with given args."""
@@ -238,29 +248,23 @@ def send(*args, **kwargs):
238248
default_sender.send(*args, **kwargs)
239249

240250

241-
if __name__ == '__main__':
251+
if __name__ == "__main__":
242252
import argparse
243253

244254
parser = argparse.ArgumentParser()
245-
parser.add_argument('metric',
246-
help='name of metric to send')
247-
parser.add_argument('value', type=float,
248-
help='numeric value to send')
249-
parser.add_argument('-s', '--server', default='localhost',
250-
help='hostname of Graphite server to send to, default %(default)s')
251-
parser.add_argument('-p', '--port', type=int, default=2003,
252-
help='port to send message to, default %(default)d')
253-
parser.add_argument('-u', '--udp', action='store_true',
254-
help='send via UDP instead of TCP')
255-
parser.add_argument('-t', '--timestamp', type=int,
256-
help='Unix timestamp for message (defaults to current time)')
257-
parser.add_argument('-q', '--quiet', action='store_true',
258-
help="quiet mode (don't log send to stdout)")
255+
parser.add_argument("metric", help="name of metric to send")
256+
parser.add_argument("value", type=float, help="numeric value to send")
257+
parser.add_argument(
258+
"-s", "--server", default="localhost", help="hostname of Graphite server to send to, default %(default)s"
259+
)
260+
parser.add_argument("-p", "--port", type=int, default=2003, help="port to send message to, default %(default)d")
261+
parser.add_argument("-u", "--udp", action="store_true", help="send via UDP instead of TCP")
262+
parser.add_argument("-t", "--timestamp", type=int, help="Unix timestamp for message (defaults to current time)")
263+
parser.add_argument("-q", "--quiet", action="store_true", help="quiet mode (don't log send to stdout)")
259264
args = parser.parse_args()
260265

261266
if not args.quiet:
262-
logging.basicConfig(level=logging.INFO, format='%(message)s')
267+
logging.basicConfig(level=logging.INFO, format="%(message)s")
263268

264-
sender = Sender(args.server, port=args.port, log_sends=not args.quiet,
265-
protocol='udp' if args.udp else 'tcp')
269+
sender = Sender(args.server, port=args.port, log_sends=not args.quiet, protocol="udp" if args.udp else "tcp")
266270
sender.send(args.metric, args.value, timestamp=args.timestamp)

lib/vsc/utils/lock.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828
2929
@author Andy Georges (Ghent University)
3030
"""
31+
3132
import sys
3233

3334
from lockfile import LockFailed, NotLocked, NotMyLock, LockError
3435
from vsc.utils.fancylogger import getLogger
3536
from vsc.utils.nagios import NAGIOS_EXIT_CRITICAL
3637

37-
logger = getLogger('vsc.utils.lock')
38+
logger = getLogger("vsc.utils.lock")
3839

39-
LOCKFILE_DIR = '/var/lock'
40+
LOCKFILE_DIR = "/var/lock"
4041
LOCKFILE_FILENAME_TEMPLATE = "%s.lock"
4142

4243

@@ -54,7 +55,7 @@ def lock_or_bork(lockfile, simple_nagios):
5455
try:
5556
lockfile.acquire()
5657
except LockFailed:
57-
logger.critical('Unable to obtain lock: lock failed')
58+
logger.critical("Unable to obtain lock: lock failed")
5859
simple_nagios.critical(f"failed to take lock on {lockfile.path}")
5960
sys.exit(NAGIOS_EXIT_CRITICAL)
6061
except LockError:
@@ -64,7 +65,7 @@ def lock_or_bork(lockfile, simple_nagios):
6465

6566

6667
def release_or_bork(lockfile, simple_nagios):
67-
""" Release the lock on the given lockfile.
68+
"""Release the lock on the given lockfile.
6869
6970
@type lockfile: A LockFile instance
7071
@type simple_nagios: SimpleNagios instance
@@ -78,10 +79,10 @@ def release_or_bork(lockfile, simple_nagios):
7879
try:
7980
lockfile.release()
8081
except NotLocked:
81-
logger.critical('Lock release failed: was not locked.')
82+
logger.critical("Lock release failed: was not locked.")
8283
simple_nagios.critical(f"Lock release failed on {lockfile.path}")
8384
sys.exit(NAGIOS_EXIT_CRITICAL)
8485
except NotMyLock:
85-
logger.error('Lock release failed: not my lock')
86+
logger.error("Lock release failed: not my lock")
8687
simple_nagios.critical(f"Lock release failed on {lockfile.path}")
8788
sys.exit(NAGIOS_EXIT_CRITICAL)

0 commit comments

Comments
 (0)