diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6937a4d..2a78057 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,38 +1,62 @@ repos: -- repo: https://github.com/sirosen/texthooks - rev: 0.6.7 - hooks: - - id: alphabetize-codeowners -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 - hooks: - - id: check-merge-conflict - - id: trailing-whitespace -- repo: https://github.com/psf/black - rev: 24.8.0 - hooks: - - id: black -- repo: https://github.com/PyCQA/flake8 - rev: 7.1.1 - hooks: - - id: flake8 - additional_dependencies: - - 'flake8-bugbear==24.8.19' -- repo: https://github.com/asottile/pyupgrade - rev: v3.17.0 - hooks: - - id: pyupgrade - args: ["--py38-plus"] -- repo: https://github.com/PyCQA/isort - rev: 5.13.2 - hooks: - - id: isort -- repo: https://github.com/PyCQA/bandit - rev: 1.7.9 - hooks: - - id: bandit - exclude: ^test/.*$ -- repo: https://github.com/koalaman/shellcheck-precommit - rev: v0.10.0 - hooks: - - id: shellcheck + + - repo: https://github.com/pre-commit/pre-commit-hooks.git + rev: v4.6.0 + hooks: + - id: check-merge-conflict + - id: trailing-whitespace + # ensure JSON files are well-formed and formatted + - id: check-json + - id: pretty-format-json + args: ["--autofix", "--no-sort-keys"] + + - repo: https://github.com/sirosen/texthooks + rev: 0.6.7 + hooks: + - id: alphabetize-codeowners + + - repo: https://github.com/python-jsonschema/check-jsonschema + rev: 0.29.1 + hooks: + - id: check-github-workflows + - id: check-dependabot + + - repo: https://github.com/asottile/pyupgrade + rev: v3.17.0 + hooks: + - id: pyupgrade + args: ["--py38-plus"] + + - repo: https://github.com/sirosen/slyp + rev: 0.7.1 + hooks: + - id: slyp + + - repo: https://github.com/PyCQA/isort + rev: 5.13.2 + hooks: + - id: isort + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: check-merge-conflict + - id: trailing-whitespace + + - repo: https://github.com/psf/black-pre-commit-mirror + rev: 24.8.0 + hooks: + - id: black + + - repo: https://github.com/PyCQA/flake8 + rev: 7.1.1 + hooks: + - id: flake8 + additional_dependencies: + - 'flake8-bugbear==24.8.19' + - 'flake8-typing-as-t==0.0.3' + + - repo: https://github.com/koalaman/shellcheck-precommit + rev: v0.10.0 + hooks: + - id: shellcheck diff --git a/client/globus_cw_client/client.py b/client/globus_cw_client/client.py index c6cf407..ff5e10d 100644 --- a/client/globus_cw_client/client.py +++ b/client/globus_cw_client/client.py @@ -36,7 +36,7 @@ def log_event(message, retries=10, wait=0.1): if wait < 0: raise ValueError("wait must be non-negative") - req = dict() + req = {} req["message"] = message req["timestamp"] = int(time.time() * 1000) return _request(req, retries, wait) diff --git a/daemon/globus_cw_daemon/cwlogs.py b/daemon/globus_cw_daemon/cwlogs.py index 09137a5..e0fe6a8 100644 --- a/daemon/globus_cw_daemon/cwlogs.py +++ b/daemon/globus_cw_daemon/cwlogs.py @@ -83,7 +83,7 @@ def add(self, record): def get_records_for_boto(self): ret = [] for r in self.records: - ret.append(dict(timestamp=r.timestamp, message=r.unicode_message)) + ret.append({"timestamp": r.timestamp, "message": r.unicode_message}) return ret @staticmethod @@ -150,11 +150,11 @@ def _flush_events(self, events): raise ValueError("cannot flush with no events") while True: try: - kwargs = dict( - logGroupName=self.group_name, - logStreamName=self.stream_name, - logEvents=events, - ) + kwargs = { + "logGroupName": self.group_name, + "logStreamName": self.stream_name, + "logEvents": events, + } if self.sequence_token: kwargs["sequenceToken"] = self.sequence_token ret = self.client.put_log_events(**kwargs) diff --git a/daemon/globus_cw_daemon/daemon.py b/daemon/globus_cw_daemon/daemon.py index 3593c5a..ccf0c5f 100644 --- a/daemon/globus_cw_daemon/daemon.py +++ b/daemon/globus_cw_daemon/daemon.py @@ -10,7 +10,7 @@ import sys import threading import time -import typing +import typing as t import globus_cw_daemon.config as config import globus_cw_daemon.cwlogs as cwlogs @@ -32,14 +32,14 @@ # Data shared with flush thread _g_lock = threading.Lock() -_g_queue: typing.List[cwlogs.Event] = [] # List of Events +_g_queue: t.List[cwlogs.Event] = [] # List of Events _g_nr_dropped = 0 # get constant instance_id on start try: - INSTANCE_ID: typing.Union[str, None] = os.readlink("/var/lib/cloud/instance").split( - "/" - )[-1] + INSTANCE_ID: t.Union[str, None] = os.readlink("/var/lib/cloud/instance").split("/")[ + -1 + ] except OSError: INSTANCE_ID = None @@ -98,12 +98,12 @@ def _flush_thread_main(writer): def _get_drop_event(nr_dropped): - data = dict( - type="audit", - subtype="cwlogs.dropped", - dropped=nr_dropped, - instance_id=INSTANCE_ID, - ) + data = { + "type": "audit", + "subtype": "cwlogs.dropped", + "dropped": nr_dropped, + "instance_id": INSTANCE_ID, + } ret = cwlogs.Event(timestamp=None, message=json.dumps(data)) return ret @@ -119,16 +119,16 @@ def _health_info(q_len=None): if q_len is None: q_len = len(_g_queue) # no lock, but safe q_pct = (q_len / float(MAX_EVENT_QUEUE_LEN)) * 100 - return dict(queue_length=q_len, queue_percent_full=q_pct) + return {"queue_length": q_len, "queue_percent_full": q_pct} def _get_heartbeat_event(nr_found): - data = dict( - type="audit", - subtype="cwlogs.heartbeat", - instance_id=INSTANCE_ID, - health=_health_info(nr_found), - ) + data = { + "type": "audit", + "subtype": "cwlogs.heartbeat", + "instance_id": INSTANCE_ID, + "health": _health_info(nr_found), + } ret = cwlogs.Event(timestamp=None, message=json.dumps(data)) return ret @@ -153,10 +153,10 @@ def do_request(sock): try: _handle_request(d) - response = dict(status="ok", health=_health_info()) + response = {"status": "ok", "health": _health_info()} except Exception as e: _log.exception("error %r", e) - response = dict(status="error", message=repr(e)) + response = {"status": "error", "message": repr(e)} _log.debug("response: %r", response) buf = json.dumps(response, indent=None).encode("utf-8") + b"\n" diff --git a/daemon/globus_cw_daemon_install/install.py b/daemon/globus_cw_daemon_install/install.py index 208c45f..18d8c0f 100644 --- a/daemon/globus_cw_daemon_install/install.py +++ b/daemon/globus_cw_daemon_install/install.py @@ -18,17 +18,16 @@ def main(): # get group name argument parser = argparse.ArgumentParser() parser.add_argument( - "group_name", help="Name of the existing CloudWatch log group " "to log to." + "group_name", help="Name of the existing CloudWatch log group to log to." ) parser.add_argument( "--stream-name", - help="Specify a stream name. Default is the current " "ec2 instance id.", + help="Specify a stream name. Default is the current ec2 instance id.", ) parser.add_argument( "--heartbeat-interval", type=int, - help="Specify the time in seconds between heartbeats. " - "Default is 60 seconds.", + help="Specify the time in seconds between heartbeats. Default is 60 seconds.", ) parser.add_argument( "--no-heartbeats", action="store_true", help="Turn off heartbeats."