mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
*: Utilize newer 3.10 syntax for type hints
Tests: - mypy does not show any errors. - Installing ejabberd app works. Privileged actions run fine. - Unit tests work. - No additional testing was done as type annotations don't have any effect at runtime. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
a86a86d605
commit
38ece87c6c
29 files changed
+116
-140
No files matched your search
@@ -7,7 +7,6 @@ import pathlib
|
||||
import re
|
||||
import subprocess
|
||||
import tarfile
|
||||
from typing import Optional, Union
|
||||
|
||||
from plinth.actions import privileged
|
||||
from plinth.utils import Version
|
||||
@@ -22,8 +21,8 @@ class AlreadyMountedError(Exception):
|
||||
|
||||
|
||||
@privileged
|
||||
def mount(mountpoint: str, remote_path: str, ssh_keyfile: Optional[str] = None,
|
||||
password: Optional[str] = None,
|
||||
def mount(mountpoint: str, remote_path: str, ssh_keyfile: str | None = None,
|
||||
password: str | None = None,
|
||||
user_known_hosts_file: str = '/dev/null'):
|
||||
"""Mount a remote ssh path via sshfs."""
|
||||
try:
|
||||
@@ -110,7 +109,7 @@ def setup(path: str):
|
||||
|
||||
|
||||
def _init_repository(path: str, encryption: str,
|
||||
encryption_passphrase: Optional[str] = None):
|
||||
encryption_passphrase: str | None = None):
|
||||
"""Initialize a local or remote borg repository."""
|
||||
if encryption != 'none':
|
||||
if not encryption_passphrase:
|
||||
@@ -121,14 +120,13 @@ def _init_repository(path: str, encryption: str,
|
||||
|
||||
|
||||
@privileged
|
||||
def init(path: str, encryption: str,
|
||||
encryption_passphrase: Optional[str] = None):
|
||||
def init(path: str, encryption: str, encryption_passphrase: str | None = None):
|
||||
"""Initialize the borg repository."""
|
||||
_init_repository(path, encryption, encryption_passphrase)
|
||||
|
||||
|
||||
@privileged
|
||||
def info(path: str, encryption_passphrase: Optional[str] = None) -> dict:
|
||||
def info(path: str, encryption_passphrase: str | None = None) -> dict:
|
||||
"""Show repository information."""
|
||||
process = _run(['borg', 'info', '--json', path], encryption_passphrase,
|
||||
stdout=subprocess.PIPE)
|
||||
@@ -136,7 +134,7 @@ def info(path: str, encryption_passphrase: Optional[str] = None) -> dict:
|
||||
|
||||
|
||||
@privileged
|
||||
def list_repo(path: str, encryption_passphrase: Optional[str] = None) -> dict:
|
||||
def list_repo(path: str, encryption_passphrase: str | None = None) -> dict:
|
||||
"""List repository contents."""
|
||||
process = _run(['borg', 'list', '--json', '--format="{comment}"', path],
|
||||
encryption_passphrase, stdout=subprocess.PIPE)
|
||||
@@ -150,8 +148,8 @@ def _get_borg_version():
|
||||
|
||||
|
||||
@privileged
|
||||
def create_archive(path: str, paths: list[str], comment: Optional[str] = None,
|
||||
encryption_passphrase: Optional[str] = None):
|
||||
def create_archive(path: str, paths: list[str], comment: str | None = None,
|
||||
encryption_passphrase: str | None = None):
|
||||
"""Create archive."""
|
||||
existing_paths = filter(os.path.exists, paths)
|
||||
command = ['borg', 'create', '--json']
|
||||
@@ -169,7 +167,7 @@ def create_archive(path: str, paths: list[str], comment: Optional[str] = None,
|
||||
|
||||
|
||||
@privileged
|
||||
def delete_archive(path: str, encryption_passphrase: Optional[str] = None):
|
||||
def delete_archive(path: str, encryption_passphrase: str | None = None):
|
||||
"""Delete archive."""
|
||||
_run(['borg', 'delete', path], encryption_passphrase)
|
||||
|
||||
@@ -199,7 +197,7 @@ def _extract(archive_path, destination, encryption_passphrase, locations=None):
|
||||
|
||||
|
||||
@privileged
|
||||
def export_tar(path: str, encryption_passphrase: Optional[str] = None):
|
||||
def export_tar(path: str, encryption_passphrase: str | None = None):
|
||||
"""Export archive contents as tar stream on stdout."""
|
||||
_run(['borg', 'export-tar', path, '-', '--tar-filter=gzip'],
|
||||
encryption_passphrase)
|
||||
@@ -214,7 +212,7 @@ def _read_archive_file(archive, filepath, encryption_passphrase):
|
||||
|
||||
@privileged
|
||||
def get_archive_apps(path: str,
|
||||
encryption_passphrase: Optional[str] = None) -> list[str]:
|
||||
encryption_passphrase: str | None = None) -> list[str]:
|
||||
"""Get list of apps included in archive."""
|
||||
manifest_folder = os.path.relpath(MANIFESTS_FOLDER, '/')
|
||||
borg_call = [
|
||||
@@ -286,7 +284,7 @@ def get_exported_archive_apps(path: str) -> list[str]:
|
||||
@privileged
|
||||
def restore_archive(archive_path: str, destination: str,
|
||||
directories: list[str], files: list[str],
|
||||
encryption_passphrase: Optional[str] = None):
|
||||
encryption_passphrase: str | None = None):
|
||||
"""Restore files from an archive."""
|
||||
locations_all = directories + files
|
||||
locations_all = [
|
||||
@@ -319,8 +317,7 @@ def _assert_app_id(app_id):
|
||||
|
||||
|
||||
@privileged
|
||||
def dump_settings(app_id: str, settings: dict[str, Union[int, float, bool,
|
||||
str]]):
|
||||
def dump_settings(app_id: str, settings: dict[str, int | float | bool | str]):
|
||||
"""Dump an app's settings to a JSON file."""
|
||||
_assert_app_id(app_id)
|
||||
BACKUPS_DATA_PATH.mkdir(exist_ok=True)
|
||||
@@ -329,7 +326,7 @@ def dump_settings(app_id: str, settings: dict[str, Union[int, float, bool,
|
||||
|
||||
|
||||
@privileged
|
||||
def load_settings(app_id: str) -> dict[str, Union[int, float, bool, str]]:
|
||||
def load_settings(app_id: str) -> dict[str, int | float | bool | str]:
|
||||
"""Load an app's settings from a JSON file."""
|
||||
_assert_app_id(app_id)
|
||||
settings_path = BACKUPS_DATA_PATH / f'{app_id}-settings.json'
|
||||
@@ -339,7 +336,7 @@ def load_settings(app_id: str) -> dict[str, Union[int, float, bool, str]]:
|
||||
return {}
|
||||
|
||||
|
||||
def _get_env(encryption_passphrase: Optional[str] = None):
|
||||
def _get_env(encryption_passphrase: str | None = None):
|
||||
"""Create encryption and ssh kwargs out of given arguments."""
|
||||
env = dict(os.environ, BORG_RELOCATED_REPO_ACCESS_IS_OK='yes',
|
||||
LANG='C.UTF-8')
|
||||
|
||||
Reference in new issue
Block a user