1
0
mirror of https://github.com/ihabunek/toot synced 2024-12-23 07:27:12 +01:00

Rework the diag command a bit

This commit is contained in:
Ivan Habunek 2024-09-29 09:08:06 +02:00
parent fe29ac816b
commit 0d829e9495
No known key found for this signature in database
GPG Key ID: 01DB3DD0D824504C
3 changed files with 83 additions and 66 deletions

View File

@ -1,7 +1,7 @@
from typing import Optional from typing import Optional
import click import click
from toot import api, config from toot import api, config
from toot.entities import Data from toot.entities import Data, Instance, from_dict
from toot.output import print_diags from toot.output import print_diags
from toot.cli import cli from toot.cli import cli
@ -21,10 +21,11 @@ from toot.cli import cli
) )
def diag(files: bool, server: bool): def diag(files: bool, server: bool):
"""Display useful information for diagnosing problems""" """Display useful information for diagnosing problems"""
instance_dict: Optional[Data] = None instance: Optional[Instance] = None
if server: if server:
_, app = config.get_active_user_app() _, app = config.get_active_user_app()
if app: if app:
instance_dict = api.get_instance(app.base_url).json() response = api.get_instance(app.base_url)
instance = from_dict(Instance, response.json())
print_diags(instance_dict, files) print_diags(instance, files)

View File

@ -1,3 +1,5 @@
import json
from os import path
import click import click
import platform import platform
import re import re
@ -5,13 +7,11 @@ import shutil
import textwrap import textwrap
import typing as t import typing as t
from datetime import datetime, timezone
from importlib.metadata import version
from wcwidth import wcswidth from wcwidth import wcswidth
from toot import __version__, config, settings from toot import __version__, config, settings
from toot.entities import Account, Data, Instance, Notification, Poll, Status, List from toot.entities import Account, Data, Instance, Notification, Poll, Status, List
from toot.utils import get_text, html_to_paragraphs from toot.utils import get_distro_name, get_text, get_version, html_to_paragraphs
from toot.wcstring import wc_wrap from toot.wcstring import wc_wrap
@ -330,80 +330,79 @@ def format_account_name(account: Account) -> str:
return acct return acct
def print_diags(instance_dict: t.Optional[Data], include_files: bool): def print_diags(instance: t.Optional[Instance], include_files: bool):
click.echo(f'{green("## Toot Diagnostics")}') click.echo("## Toot Diagnostics")
click.echo() click.echo()
click.echo(f"toot {__version__}")
click.echo(f"Python {platform.python_version()}")
click.echo(platform.platform())
now = datetime.now(timezone.utc) distro = get_distro_name()
click.echo(f'{green("Current Date/Time:")} {now.strftime("%Y-%m-%d %H:%M:%S %Z")}') if distro:
click.echo(distro)
click.echo(f'{green("Toot version:")} {__version__}')
click.echo(f'{green("Platform:")} {platform.platform()}')
# print distro - only call if available (python 3.10+)
fd_os_release = getattr(platform, "freedesktop_os_release", None) # novermin
if callable(fd_os_release): # novermin
try:
name = platform.freedesktop_os_release()['PRETTY_NAME']
click.echo(f'{green("Distro:")} {name}')
except: # noqa
pass
click.echo(f'{green("Python version:")} {platform.python_version()}')
click.echo() click.echo()
click.secho(bold("Dependencies:"))
click.echo(green("Dependency versions:")) deps = [
"beautifulsoup4",
deps = sorted(['beautifulsoup4', 'click', 'requests', 'tomlkit', 'urwid', 'wcwidth', "click",
'pillow', 'term-image', 'urwidgets', 'flake8', 'pytest', 'setuptools', "pillow",
'vermin', 'typing-extensions']) "requests",
"setuptools",
"term-image",
"tomlkit",
"typing-extensions",
"urwid",
"urwidgets",
"wcwidth",
]
for dep in deps: for dep in deps:
try: version = get_version(dep) or yellow("not installed")
ver = version(dep) click.echo(f" * {dep}: {version}")
except: # noqa
ver = yellow("not installed") if instance:
click.echo()
click.echo(bold("Server:"))
click.echo(instance.title)
click.echo(instance.uri)
click.echo(f"version {instance.version}")
click.echo(f" * {dep}: {ver}")
click.echo() click.echo()
click.echo(f'{green("Settings file path:")} {settings.get_settings_path()}') settings_path = settings.get_settings_path()
click.echo(f'{green("Config file path:")} {config.get_config_file_path()}') if path.exists(settings_path):
click.echo(f"Settings file: {settings_path}")
if include_files:
with open(settings_path, "r") as f:
click.echo("\n```toml")
click.echo(f.read().strip())
click.echo("```\n")
else:
click.echo(f'Settings file: {yellow("not found")}')
if instance_dict: config_path = config.get_config_file_path()
click.echo(f'{green("Server URI:")} {instance_dict.get("uri")}') if path.exists(config_path):
click.echo(f'{green("Server version:")} {instance_dict.get("version")}') click.echo(f"Config file: {config_path}")
if include_files:
if include_files: with open(config_path, "r") as f:
click.echo(f'{green("Settings file contents:")}') content = json.load(f)
try: for app in content.get("apps", {}).values():
with open(settings.get_settings_path(), 'r') as f: app["client_id"] = "*****"
print("```toml") app["client_secret"] = "*****"
print(f.read()) for user in content.get("users", {}).values():
print("```") user["access_token"] = "*****"
except: # noqa click.echo("\n```json")
click.echo(f'{yellow("Could not open settings file")}') click.echo(json.dumps(content, indent=4))
click.echo() click.echo("```\n")
else:
click.echo(f'{green("Config file contents:")}') click.echo(f'Config file: {yellow("not found")}')
click.echo("```json")
try:
with open(config.get_config_file_path(), 'r') as f:
for line in f:
# Do not output client secret or access token lines
if "client_" in line or "token" in line:
click.echo(f'{yellow("***CONTENTS REDACTED***")}')
else:
click.echo(line, nl=False)
click.echo()
except: # noqa
click.echo(f'{yellow("Could not open config file")}')
click.echo("```")
# Shorthand functions for coloring output # Shorthand functions for coloring output
def blue(text: t.Any) -> str: def blue(text: t.Any) -> str:
return click.style(text, fg="blue") return click.style(text, fg="blue")

View File

@ -1,3 +1,4 @@
import platform
import click import click
import os import os
import re import re
@ -7,6 +8,7 @@ import unicodedata
import warnings import warnings
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from importlib.metadata import version
from typing import Any, Dict, Generator, List, Optional from typing import Any, Dict, Generator, List, Optional
from urllib.parse import urlparse, urlencode, quote, unquote from urllib.parse import urlparse, urlencode, quote, unquote
@ -147,3 +149,18 @@ def urlencode_url(url: str) -> str:
encoded_url = parsed_url._replace(path=encoded_path, params=encoded_query).geturl() encoded_url = parsed_url._replace(path=encoded_path, params=encoded_query).geturl()
return encoded_url return encoded_url
def get_distro_name() -> Optional[str]:
"""Attempt to get linux distro name from platform (requires python 3.10+)"""
try:
return platform.freedesktop_os_release()["PRETTY_NAME"] # type: ignore
except Exception:
pass
def get_version(name):
try:
return version(name)
except Exception:
return None