Skip to content

Commit

Permalink
Enable angry pylint (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekateluv authored Oct 10, 2024
1 parent f8b7de1 commit 20c8fab
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 50 deletions.
3 changes: 2 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
buildDebSbuild defaultTargets: "bullseye-host",
defaultRunPythonChecks: true,
defaultAngryPylint: true,
defaultRunCoverage: true,
defaultCoverageMin: "57"
defaultCoverageMin: "56"
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
wb-cloud-agent (1.5.10) stable; urgency=medium

* Enable anrgy pylint. No functional changes

-- Ekaterina Volkova <[email protected]> Wed, 09 Oct 2024 13:59:09 +0300

wb-cloud-agent (1.5.9) stable; urgency=medium

* Add mqtt device republish on mosquitto restart
Expand Down
2 changes: 1 addition & 1 deletion wb-cloud-agent
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# pylint:disable=invalid-name
import sys

from wb.cloud_agent import main
Expand Down
73 changes: 36 additions & 37 deletions wb/cloud_agent/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def start_service(service: str, restart=False):
def setup_log(settings: AppSettings):
numeric_level = getattr(logging, settings.LOG_LEVEL.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError("Invalid log level: %s" % settings.LOG_LEVEL)
raise ValueError(f"Invalid log level: {settings.LOG_LEVEL}")
logging.basicConfig(level=numeric_level, encoding="utf-8", format="%(message)s")


def update_providers_list(settings: AppSettings, mqtt):
# FIXME: Find a better way to update providers list (services enabled? services running?).
def update_providers_list(_settings: AppSettings, mqtt):
# Find a better way to update providers list (services enabled? services running?).
mqtt.publish_providers(",".join(get_providers()))


Expand Down Expand Up @@ -97,15 +97,15 @@ def do_curl(settings: AppSettings, method="get", endpoint="", params=None):

try:
status = int(json.loads(split_result[1])["code"])
except (KeyError, TypeError, ValueError, JSONDecodeError):
raise ValueError("Invalid data in response: " + str(split_result))
except (KeyError, TypeError, ValueError, JSONDecodeError) as e:
raise ValueError(f"Invalid data in response: {split_result}") from e

return data, status


def write_to_file(fpath: str, contents: str):
os.makedirs(os.path.dirname(fpath), exist_ok=True)
with open(fpath, mode="w") as file:
with open(fpath, mode="w", encoding="utf-8") as file:
file.write(contents)


Expand All @@ -117,7 +117,7 @@ def write_activation_link(settings: AppSettings, link, mqtt):
def read_activation_link(settings: AppSettings):
if not os.path.exists(settings.ACTIVATION_LINK_CONFIG):
return "unknown"
with open(settings.ACTIVATION_LINK_CONFIG, "r") as file:
with open(settings.ACTIVATION_LINK_CONFIG, "r", encoding="utf-8") as file:
return file.readline()


Expand Down Expand Up @@ -148,41 +148,40 @@ def upload_diagnostic(settings: AppSettings):
settings=settings, method="put", endpoint="diagnostic-status/", params={"status": "error"}
)
if http_status != HTTP_200_OK:
logging.error("Not a 200 status while updating diagnostic status: " + str(http_status))
logging.error("Not a 200 status while updating diagnostic status: %s", http_status)
return

last_diagnostic = files[-1]
logging.info(f"Diagnostics collected: {last_diagnostic}")
data, http_status = do_curl(
logging.info("Diagnostics collected: %s", last_diagnostic)
_data, http_status = do_curl(
settings=settings, method="multipart-post", endpoint="upload-diagnostic/", params=last_diagnostic
)
if http_status != HTTP_200_OK:
logging.error("Not a 200 status while making upload_diagnostic request: " + str(http_status))
logging.error("Not a 200 status while making upload_diagnostic request: %s", http_status)

os.remove(last_diagnostic)


def fetch_diagnostics(settings: AppSettings, payload, mqtt):
def fetch_diagnostics(settings: AppSettings, _payload, _mqtt):
# remove old diagnostics
try:
for fname in glob.glob(f"{DIAGNOSTIC_DIR}/diag_*.zip"):
os.remove(fname)
except OSError as e:
logging.warning(f"Erase diagnostic files failed: {e.strerror}")

process = subprocess.Popen(
"wb-diag-collect diag",
cwd=DIAGNOSTIC_DIR,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)

def process_waiter(p, callback):
p.wait()
callback(settings)

thread = threading.Thread(target=process_waiter, args=(process, upload_diagnostic))
logging.warning("Erase diagnostic files failed: %s", e.strerror)

def process_waiter():
with subprocess.Popen(
"wb-diag-collect diag",
cwd=DIAGNOSTIC_DIR,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
) as process:
process.wait()
upload_diagnostic(settings)

thread = threading.Thread(target=process_waiter)
thread.start()


Expand All @@ -196,7 +195,7 @@ def process_waiter(p, callback):

def make_event_request(settings: AppSettings, mqtt):
event_data, http_status = do_curl(settings=settings, method="get", endpoint="events/")
logging.debug("Checked for new events. Status " + str(http_status) + ". Data: " + str(event_data))
logging.debug("Checked for new events. Status %s. Data: %s", http_status, event_data)

if http_status == HTTP_204_NO_CONTENT:
return
Expand All @@ -218,9 +217,9 @@ def make_event_request(settings: AppSettings, mqtt):
if handler:
handler(settings, payload, mqtt)
else:
logging.warning("Got an unknown event '" + code + "'. Try to update wb-cloud-agent package.")
logging.warning("Got an unknown event '%s'. Try to update wb-cloud-agent package.", code)

logging.info("Event '" + code + "' handled successfully, event id " + str(event_id))
logging.info("Event '%s' handled successfully, event id %s", code, event_id)

_, http_status = do_curl(settings=settings, method="post", endpoint="events/" + event_id + "/confirm/")

Expand Down Expand Up @@ -248,18 +247,18 @@ def make_start_up_request(settings: AppSettings, mqtt):


def send_agent_version(settings: AppSettings):
status_data, http_status = do_curl(
_status_data, http_status = do_curl(
settings=settings,
method="put",
endpoint="update_device_data/",
params={"agent_version": package_version},
)
if http_status != HTTP_200_OK:
logging.error("Not a 200 status while making send_agent_version request: " + str(http_status))
logging.error("Not a 200 status while making send_agent_version request: %s", http_status)


def on_message(userdata, message):
status_data, http_status = do_curl(
_status_data, http_status = do_curl(
userdata.get("settings"),
method="put",
endpoint="update_device_data/",
Expand Down Expand Up @@ -296,7 +295,7 @@ def add_provider(options, settings, mqtt):
generate_config(options.provider_name, options.base_url, options.agent_url)
start_service(f"wb-cloud-agent@{options.provider_name}.service")
update_providers_list(settings, mqtt)
return
return 0


def show_activation_link(settings):
Expand All @@ -305,7 +304,6 @@ def show_activation_link(settings):
print(f">> {link}")
else:
print("No active link. Controller may be already connected")
return


def run_daemon(mqtt, settings):
Expand All @@ -321,13 +319,13 @@ def run_daemon(mqtt, settings):
make_event_request(settings, mqtt)
except subprocess.TimeoutExpired:
continue
except Exception as ex:
except Exception as ex: # pylint:disable=broad-exception-caught
logging.exception("Error making request to cloud!")
mqtt.publish_ctrl("status", "error: " + str(ex))
else:
mqtt.publish_ctrl("status", "ok")
request_time = time.perf_counter() - start
logging.debug("Done in: " + str(int(request_time * 1000)) + " ms.")
logging.debug("Done in: %s ms.", int(request_time * 1000))
time.sleep(settings.REQUEST_PERIOD_SECONDS)


Expand Down Expand Up @@ -360,3 +358,4 @@ def main():
update_providers_list(settings, mqtt)

run_daemon(mqtt, settings)
return 0
4 changes: 2 additions & 2 deletions wb/cloud_agent/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def start(self, update_status=False):
if update_status:
self.publish_ctrl("status", "starting")

def _on_connect(self, client, _userdata, _flags, reason_code, *_):
def _on_connect(self, _client, _userdata, _flags, reason_code, *_):
# 0: Connection successful
if reason_code != 0:
logging.error("Failed to connect: %d. loop_forever() will retry connection", reason_code)
Expand All @@ -42,7 +42,7 @@ def _on_connect(self, client, _userdata, _flags, reason_code, *_):

self.client.subscribe("/devices/system/controls/HW Revision", qos=2)

def _on_message(self, client, userdata, message):
def _on_message(self, _client, userdata, message):
assert "settings" in userdata, "No settings in userdata"
self.client.unsubscribe("/devices/system/controls/HW Revision")
if self.on_message:
Expand Down
19 changes: 10 additions & 9 deletions wb/cloud_agent/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import os
from json import JSONDecodeError
from pathlib import Path

from wb_common.mqtt_client import DEFAULT_BROKER_URL
Expand Down Expand Up @@ -32,14 +31,16 @@ class AppSettings: # pylint: disable=too-many-instance-attributes disable=too-f
REQUEST_PERIOD_SECONDS: int = 3

def __init__(self, provider: str = "default"):
self.PROVIDER = provider
self.PROVIDER = provider # pylint:disable=invalid-name
if provider == "default":
self.CONFIG_FILE: str = DEFAULT_CONF_FILE
self.FRP_SERVICE: str = "wb-cloud-agent-frpc.service"
self.TELEGRAF_SERVICE: str = "wb-cloud-agent-telegraf.service"
self.FRP_CONFIG: str = "/var/lib/wb-cloud-agent/frpc.conf"
self.TELEGRAF_CONFIG: str = "/var/lib/wb-cloud-agent/telegraf.conf"
self.ACTIVATION_LINK_CONFIG: str = "/var/lib/wb-cloud-agent/activation_link.conf"
self.CONFIG_FILE: str = DEFAULT_CONF_FILE # pylint:disable=invalid-name
self.FRP_SERVICE: str = "wb-cloud-agent-frpc.service" # pylint:disable=invalid-name
self.TELEGRAF_SERVICE: str = "wb-cloud-agent-telegraf.service" # pylint:disable=invalid-name
self.FRP_CONFIG: str = "/var/lib/wb-cloud-agent/frpc.conf" # pylint:disable=invalid-name
self.TELEGRAF_CONFIG: str = "/var/lib/wb-cloud-agent/telegraf.conf" # pylint:disable=invalid-name
self.ACTIVATION_LINK_CONFIG: str = ( # pylint:disable=invalid-name
"/var/lib/wb-cloud-agent/activation_link.conf"
)
else:
self.CONFIG_FILE: str = f"{PROVIDERS_CONF_DIR}/{provider}/wb-cloud-agent.conf"
self.FRP_SERVICE: str = f"wb-cloud-agent-frpc@{provider}.service"
Expand All @@ -49,7 +50,7 @@ def __init__(self, provider: str = "default"):
self.ACTIVATION_LINK_CONFIG: str = (
f"/var/lib/wb-cloud-agent/providers/{provider}/activation_link.conf"
)
self.MQTT_PREFIX: str = f"/devices/system__wb-cloud-agent__{provider}"
self.MQTT_PREFIX: str = f"/devices/system__wb-cloud-agent__{provider}" # pylint:disable=invalid-name

if os.path.exists(self.CONFIG_FILE):
self.apply_conf_file(self.CONFIG_FILE)
Expand Down

0 comments on commit 20c8fab

Please sign in to comment.