Make the PEP8 gods less angry

This commit is contained in:
Alexandre Aubin 2019-07-14 00:45:09 +02:00
parent 5f4450ab87
commit aed53786f2
3 changed files with 51 additions and 58 deletions

View file

@ -3,13 +3,13 @@
import os import os
import random import random
from moulinette import m18n
from moulinette.utils.network import download_text from moulinette.utils.network import download_text
from moulinette.utils.process import check_output from moulinette.utils.process import check_output
from moulinette.utils.filesystem import read_file from moulinette.utils.filesystem import read_file
from yunohost.diagnosis import Diagnoser from yunohost.diagnosis import Diagnoser
class IPDiagnoser(Diagnoser): class IPDiagnoser(Diagnoser):
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
@ -17,10 +17,10 @@ class IPDiagnoser(Diagnoser):
def validate_args(self, args): def validate_args(self, args):
if "version" not in args.keys(): if "version" not in args.keys():
return { "versions" : [4, 6] } return {"versions": [4, 6]}
else: else:
assert str(args["version"]) in ["4", "6"], "Invalid version, should be 4 or 6." assert str(args["version"]) in ["4", "6"], "Invalid version, should be 4 or 6."
return { "versions" : [int(args["version"])] } return {"versions": [int(args["version"])]}
def run(self): def run(self):
@ -46,26 +46,26 @@ class IPDiagnoser(Diagnoser):
# If it turns out that at the same time, resolvconf is bad, that's probably # If it turns out that at the same time, resolvconf is bad, that's probably
# the cause of this, so we use a different message in that case # the cause of this, so we use a different message in that case
if not can_resolve_dns: if not can_resolve_dns:
yield dict(meta = {"name": "dnsresolution"}, yield dict(meta={"name": "dnsresolution"},
status = "ERROR", status="ERROR",
summary = ("diagnosis_ip_broken_dnsresolution", {}) if good_resolvconf summary=("diagnosis_ip_broken_dnsresolution", {}) if good_resolvconf
else ("diagnosis_ip_broken_resolvconf", {})) else ("diagnosis_ip_broken_resolvconf", {}))
# Otherwise, if the resolv conf is bad but we were able to resolve domain name, # Otherwise, if the resolv conf is bad but we were able to resolve domain name,
# still warn that we're using a weird resolv conf ... # still warn that we're using a weird resolv conf ...
elif not good_resolvconf: elif not good_resolvconf:
yield dict(meta = {"name": "dnsresolution"}, yield dict(meta={"name": "dnsresolution"},
status = "WARNING", status="WARNING",
summary = ("diagnosis_ip_weird_resolvconf", {})) summary=("diagnosis_ip_weird_resolvconf", {}))
else: else:
# Well, maybe we could report a "success", "dns resolution is working", idk if it's worth it # Well, maybe we could report a "success", "dns resolution is working", idk if it's worth it
pass pass
# And finally, we actually report the ipv4 connectivity stuff # And finally, we actually report the ipv4 connectivity stuff
yield dict(meta = {"version": 4}, yield dict(meta={"version": 4},
data = ipv4, data=ipv4,
status = "SUCCESS" if ipv4 else "ERROR", status="SUCCESS" if ipv4 else "ERROR",
summary = ("diagnosis_ip_connected_ipv4", {}) if ipv4 \ summary=("diagnosis_ip_connected_ipv4", {}) if ipv4
else ("diagnosis_ip_no_ipv4", {})) else ("diagnosis_ip_no_ipv4", {}))
if 6 in versions: if 6 in versions:
@ -74,12 +74,11 @@ class IPDiagnoser(Diagnoser):
else: else:
ipv6 = self.get_public_ip(6) ipv6 = self.get_public_ip(6)
yield dict(meta = {"version": 6}, yield dict(meta={"version": 6},
data = ipv6, data=ipv6,
status = "SUCCESS" if ipv6 else "WARNING", status="SUCCESS" if ipv6 else "WARNING",
summary = ("diagnosis_ip_connected_ipv6", {}) if ipv6 \ summary=("diagnosis_ip_connected_ipv6", {}) if ipv6
else ("diagnosis_ip_no_ipv6", {})) else ("diagnosis_ip_no_ipv6", {}))
def can_ping_outside(self, protocol=4): def can_ping_outside(self, protocol=4):
@ -113,11 +112,9 @@ class IPDiagnoser(Diagnoser):
random.shuffle(resolvers) random.shuffle(resolvers)
return any(ping(protocol, resolver) for resolver in resolvers[:5]) return any(ping(protocol, resolver) for resolver in resolvers[:5])
def can_resolve_dns(self): def can_resolve_dns(self):
return os.system("dig +short ip.yunohost.org >/dev/null 2>/dev/null") == 0 return os.system("dig +short ip.yunohost.org >/dev/null 2>/dev/null") == 0
def resolvconf_is_symlink(self): def resolvconf_is_symlink(self):
return os.path.realpath("/etc/resolv.conf") == "/run/resolvconf/resolv.conf" return os.path.realpath("/etc/resolv.conf") == "/run/resolvconf/resolv.conf"
@ -126,7 +123,6 @@ class IPDiagnoser(Diagnoser):
resolvers = [r.split(" ")[1] for r in read_file(file_).split("\n") if r.startswith("nameserver")] resolvers = [r.split(" ")[1] for r in read_file(file_).split("\n") if r.startswith("nameserver")]
return resolvers == ["127.0.0.1"] return resolvers == ["127.0.0.1"]
def get_public_ip(self, protocol=4): def get_public_ip(self, protocol=4):
# FIXME - TODO : here we assume that DNS resolution for ip.yunohost.org is working # FIXME - TODO : here we assume that DNS resolution for ip.yunohost.org is working
@ -149,4 +145,3 @@ class IPDiagnoser(Diagnoser):
def main(args, env, loggers): def main(args, env, loggers):
return IPDiagnoser(args, env, loggers).diagnose() return IPDiagnoser(args, env, loggers).diagnose()

View file

@ -2,25 +2,25 @@
import os import os
from moulinette.utils.network import download_text
from moulinette.utils.process import check_output from moulinette.utils.process import check_output
from moulinette.utils.filesystem import read_file from moulinette.utils.filesystem import read_file
from yunohost.diagnosis import Diagnoser from yunohost.diagnosis import Diagnoser
from yunohost.domain import domain_list, _build_dns_conf, _get_maindomain from yunohost.domain import domain_list, _build_dns_conf, _get_maindomain
class DNSRecordsDiagnoser(Diagnoser): class DNSRecordsDiagnoser(Diagnoser):
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
cache_duration = 3600*24 cache_duration = 3600 * 24
def validate_args(self, args): def validate_args(self, args):
all_domains = domain_list()["domains"] all_domains = domain_list()["domains"]
if "domain" not in args.keys(): if "domain" not in args.keys():
return { "domains" : all_domains } return {"domains": all_domains}
else: else:
assert args["domain"] in all_domains, "Unknown domain" assert args["domain"] in all_domains, "Unknown domain"
return { "domains" : [ args["domain"] ] } return {"domains": [args["domain"]]}
def run(self): def run(self):
@ -34,7 +34,7 @@ class DNSRecordsDiagnoser(Diagnoser):
for domain in self.args["domains"]: for domain in self.args["domains"]:
self.logger_debug("Diagnosing DNS conf for %s" % domain) self.logger_debug("Diagnosing DNS conf for %s" % domain)
for report in self.check_domain(domain, domain==main_domain): for report in self.check_domain(domain, domain == main_domain):
yield report yield report
def check_domain(self, domain, is_main_domain): def check_domain(self, domain, is_main_domain):
@ -44,13 +44,13 @@ class DNSRecordsDiagnoser(Diagnoser):
# Here if there are no AAAA record, we should add something to expect "no" AAAA record # Here if there are no AAAA record, we should add something to expect "no" AAAA record
# to properly diagnose situations where people have a AAAA record but no IPv6 # to properly diagnose situations where people have a AAAA record but no IPv6
for category, records in expected_configuration.items(): for category, records in expected_configuration.items():
discrepancies = [] discrepancies = []
for r in records: for r in records:
current_value = self.get_current_record(domain, r["name"], r["type"]) or "None" current_value = self.get_current_record(domain, r["name"], r["type"]) or "None"
expected_value = r["value"] if r["value"] != "@" else domain+"." expected_value = r["value"] if r["value"] != "@" else domain + "."
if current_value == "None": if current_value == "None":
discrepancies.append(("diagnosis_dns_missing_record", (r["type"], r["name"], expected_value))) discrepancies.append(("diagnosis_dns_missing_record", (r["type"], r["name"], expected_value)))
@ -64,16 +64,15 @@ class DNSRecordsDiagnoser(Diagnoser):
status = "SUCCESS" status = "SUCCESS"
summary = ("diagnosis_dns_good_conf", {"domain": domain, "category": category}) summary = ("diagnosis_dns_good_conf", {"domain": domain, "category": category})
output = dict(meta = {"domain": domain, "category": category}, output = dict(meta={"domain": domain, "category": category},
status = status, status=status,
summary = summary) summary=summary)
if discrepancies: if discrepancies:
output["details"] = discrepancies output["details"] = discrepancies
yield output yield output
def get_current_record(self, domain, name, type_): def get_current_record(self, domain, name, type_):
if name == "@": if name == "@":
command = "dig +short @%s %s %s" % (self.resolver, type_, domain) command = "dig +short @%s %s %s" % (self.resolver, type_, domain)
@ -83,12 +82,10 @@ class DNSRecordsDiagnoser(Diagnoser):
# e.g. no internet connectivity (dependency mechanism to good result from 'ip' diagosis ?) # e.g. no internet connectivity (dependency mechanism to good result from 'ip' diagosis ?)
# or the resolver is unavailable for some reason # or the resolver is unavailable for some reason
output = check_output(command).strip() output = check_output(command).strip()
output = output.replace("\;",";")
if output.startswith('"') and output.endswith('"'): if output.startswith('"') and output.endswith('"'):
output = '"' + ' '.join(output.replace('"',' ').split()) + '"' output = '"' + ' '.join(output.replace('"', ' ').split()) + '"'
return output return output
def main(args, env, loggers): def main(args, env, loggers):
return DNSRecordsDiagnoser(args, env, loggers).diagnose() return DNSRecordsDiagnoser(args, env, loggers).diagnose()

View file

@ -38,21 +38,23 @@ logger = log.getActionLogger('yunohost.diagnosis')
DIAGNOSIS_CACHE = "/var/cache/yunohost/diagnosis/" DIAGNOSIS_CACHE = "/var/cache/yunohost/diagnosis/"
def diagnosis_list(): def diagnosis_list():
all_categories_names = [ h for h, _ in _list_diagnosis_categories() ] all_categories_names = [h for h, _ in _list_diagnosis_categories()]
return { "categories": all_categories_names } return {"categories": all_categories_names}
def diagnosis_show(categories=[], issues=False, full=False): def diagnosis_show(categories=[], issues=False, full=False):
# Get all the categories # Get all the categories
all_categories = _list_diagnosis_categories() all_categories = _list_diagnosis_categories()
all_categories_names = [ category for category, _ in all_categories ] all_categories_names = [category for category, _ in all_categories]
# Check the requested category makes sense # Check the requested category makes sense
if categories == []: if categories == []:
categories = all_categories_names categories = all_categories_names
else: else:
unknown_categories = [ c for c in categories if c not in all_categories_names ] unknown_categories = [c for c in categories if c not in all_categories_names]
if unknown_categories: if unknown_categories:
raise YunohostError('unknown_categories', categories=", ".join(categories)) raise YunohostError('unknown_categories', categories=", ".join(categories))
@ -62,7 +64,7 @@ def diagnosis_show(categories=[], issues=False, full=False):
try: try:
report = Diagnoser.get_cached_report(category) report = Diagnoser.get_cached_report(category)
except Exception as e: except Exception as e:
logger.error("Failed to fetch diagnosis result for category '%s' : %s" % (category, str(e))) # FIXME : i18n logger.error("Failed to fetch diagnosis result for category '%s' : %s" % (category, str(e))) # FIXME : i18n
else: else:
if not full: if not full:
del report["timestamp"] del report["timestamp"]
@ -72,33 +74,33 @@ def diagnosis_show(categories=[], issues=False, full=False):
if "data" in item: if "data" in item:
del item["data"] del item["data"]
if issues: if issues:
report["items"] = [ item for item in report["items"] if item["status"] != "SUCCESS" ] report["items"] = [item for item in report["items"] if item["status"] != "SUCCESS"]
# Ignore this category if no issue was found # Ignore this category if no issue was found
if not report["items"]: if not report["items"]:
continue continue
all_reports.append(report) all_reports.append(report)
return {"reports": all_reports} return {"reports": all_reports}
def diagnosis_run(categories=[], force=False, args=None): def diagnosis_run(categories=[], force=False, args=None):
# Get all the categories # Get all the categories
all_categories = _list_diagnosis_categories() all_categories = _list_diagnosis_categories()
all_categories_names = [ category for category, _ in all_categories ] all_categories_names = [category for category, _ in all_categories]
# Check the requested category makes sense # Check the requested category makes sense
if categories == []: if categories == []:
categories = all_categories_names categories = all_categories_names
else: else:
unknown_categories = [ c for c in categories if c not in all_categories_names ] unknown_categories = [c for c in categories if c not in all_categories_names]
if unknown_categories: if unknown_categories:
raise YunohostError('unknown_categories', categories=", ".join(unknown_categories)) raise YunohostError('unknown_categories', categories=", ".join(unknown_categories))
# Transform "arg1=val1&arg2=val2" to { "arg1": "val1", "arg2": "val2" } # Transform "arg1=val1&arg2=val2" to { "arg1": "val1", "arg2": "val2" }
if args is not None: if args is not None:
args = { arg.split("=")[0]: arg.split("=")[1] for arg in args.split("&") } args = {arg.split("=")[0]: arg.split("=")[1] for arg in args.split("&")}
else: else:
args = {} args = {}
args["force"] = force args["force"] = force
@ -108,12 +110,12 @@ def diagnosis_run(categories=[], force=False, args=None):
diagnosed_categories = [] diagnosed_categories = []
for category in categories: for category in categories:
logger.debug("Running diagnosis for %s ..." % category) logger.debug("Running diagnosis for %s ..." % category)
path = [p for n, p in all_categories if n == category ][0] path = [p for n, p in all_categories if n == category][0]
try: try:
code, report = hook_exec(path, args=args, env=None) code, report = hook_exec(path, args=args, env=None)
except Exception as e: except Exception as e:
logger.error("Diagnosis failed for category '%s' : %s" % (category, str(e)), exc_info=True) # FIXME : i18n logger.error("Diagnosis failed for category '%s' : %s" % (category, str(e)), exc_info=True) # FIXME : i18n
else: else:
diagnosed_categories.append(category) diagnosed_categories.append(category)
if report != {}: if report != {}:
@ -127,6 +129,7 @@ def diagnosis_run(categories=[], force=False, args=None):
return return
def diagnosis_ignore(category, args="", unignore=False): def diagnosis_ignore(category, args="", unignore=False):
pass pass
@ -149,7 +152,6 @@ class Diagnoser():
if self.description == descr_key: if self.description == descr_key:
self.description = self.id_ self.description = self.id_
def cached_time_ago(self): def cached_time_ago(self):
if not os.path.exists(self.cache_file): if not os.path.exists(self.cache_file):
@ -172,10 +174,9 @@ class Diagnoser():
items = list(self.run()) items = list(self.run())
new_report = { "id": self.id_, new_report = {"id": self.id_,
"cached_for": self.cache_duration, "cached_for": self.cache_duration,
"items": items "items": items}
}
# TODO / FIXME : should handle the case where we only did a partial diagnosis # TODO / FIXME : should handle the case where we only did a partial diagnosis
self.logger_debug("Updating cache %s" % self.cache_file) self.logger_debug("Updating cache %s" % self.cache_file)
@ -227,13 +228,13 @@ class Diagnoser():
item["summary"] = m18n.n(summary_key, **summary_args) item["summary"] = m18n.n(summary_key, **summary_args)
if "details" in item: if "details" in item:
item["details"] = [ m18n.n(key, *values) for key, values in item["details"] ] item["details"] = [m18n.n(key, *values) for key, values in item["details"]]
def _list_diagnosis_categories(): def _list_diagnosis_categories():
hooks_raw = hook_list("diagnosis", list_by="priority", show_info=True)["hooks"] hooks_raw = hook_list("diagnosis", list_by="priority", show_info=True)["hooks"]
hooks = [] hooks = []
for _, some_hooks in sorted(hooks_raw.items(), key=lambda h:int(h[0])): for _, some_hooks in sorted(hooks_raw.items(), key=lambda h: int(h[0])):
for name, info in some_hooks.items(): for name, info in some_hooks.items():
hooks.append((name, info["path"])) hooks.append((name, info["path"]))