diff --git a/debian/further-link.lintian-overrides b/debian/further-link.lintian-overrides index 0bb0be66..210bbf69 100644 --- a/debian/further-link.lintian-overrides +++ b/debian/further-link.lintian-overrides @@ -2,3 +2,4 @@ further-link: no-manual-page usr/bin/further-link further-link: no-manual-page usr/bin/start-further further-link: no-manual-page usr/bin/further-link-bluetooth-pairing further-link: no-manual-page usr/bin/further-link-bluetooth-encryption +further-link: no-manual-page usr/bin/further-link-set-pretty-hostname diff --git a/debian/further-link.service b/debian/further-link.service index 9201aa60..832a7348 100644 --- a/debian/further-link.service +++ b/debian/further-link.service @@ -7,6 +7,7 @@ After=network.target Type=simple Environment="PYTHONUNBUFFERED=1" Environment="PYTHONDONTWRITEBYTECODE=1" +ExecStartPre=/usr/bin/further-link-set-pretty-hostname ExecStart=/usr/bin/further-link [Install] diff --git a/further_link/endpoint/apt_version.py b/further_link/endpoint/apt_version.py index a7da3b5b..fd885efd 100644 --- a/further_link/endpoint/apt_version.py +++ b/further_link/endpoint/apt_version.py @@ -1,11 +1,10 @@ import json import re -from os import environ -from shlex import split -from subprocess import run from aiohttp import web +from ..util.sdk import run_command + def _apt_version_dict(package): return {"version": apt_cache_installed(package)} @@ -29,23 +28,3 @@ def apt_cache_installed(pkg_name): return match.group(1) if match else None except Exception: return None - - -def run_command(command_str): - def __get_env(): - env = environ.copy() - # Print output of commands in english - env["LANG"] = "en_US.UTF-8" - return env - - try: - resp = run( - split(command_str), - check=False, - capture_output=True, - timeout=5, - env=__get_env(), - ) - return str(resp.stdout, "utf8") - except Exception: - return None diff --git a/further_link/util/hostname.py b/further_link/util/hostname.py new file mode 100644 index 00000000..b19ffcb0 --- /dev/null +++ b/further_link/util/hostname.py @@ -0,0 +1,15 @@ +import logging + +import click + +from .bluetooth.utils import get_bluetooth_server_name +from .sdk import run_command + + +@click.command() +def set_pretty_hostname() -> None: + try: + name = get_bluetooth_server_name() + run_command(f"hostnamectl set-hostname --pretty {name}") + except Exception as e: + logging.error(f"Error setting pretty hostname: {e}") diff --git a/further_link/util/sdk.py b/further_link/util/sdk.py index 25b1ab68..65ef839d 100644 --- a/further_link/util/sdk.py +++ b/further_link/util/sdk.py @@ -7,6 +7,7 @@ from glob import glob from os import environ +from shlex import split from subprocess import run from typing import List, Optional @@ -80,3 +81,23 @@ def __call__(cls, *args, **kwargs): if cls.instance is None: cls.instance = super(Singleton, cls).__call__(*args, **kwargs) return cls.instance + + +def run_command(command_str): + def __get_env(): + env = environ.copy() + # Print output of commands in english + env["LANG"] = "en_US.UTF-8" + return env + + try: + resp = run( + split(command_str), + check=False, + capture_output=True, + timeout=5, + env=__get_env(), + ) + return str(resp.stdout, "utf8") + except Exception: + return None diff --git a/setup.cfg b/setup.cfg index 84e1c9c8..17d8dece 100644 --- a/setup.cfg +++ b/setup.cfg @@ -60,6 +60,7 @@ console_scripts = start-further=further_link.start_further:start_further further-link-bluetooth-pairing=further_link.util.bluetooth.pairing:main further-link-bluetooth-encryption=further_link.util.bluetooth.encryption:main + further-link-set-pretty-hostname=further_link.util.hostname:set_pretty_hostname [bdist_wheel] universal = 1