Coverage for src/edwh_restic_plugin/helpers.py: 45%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-28 16:28 +0100

1import sys 

2import typing 

3 

4import invoke 

5from edwh.tasks import require_sudo 

6 

7T = typing.TypeVar("T") 

8 

9 

10def fix_tags(tags: typing.Iterable[T | None]) -> list[T]: 

11 """ 

12 Removes all None type elements from the input list. 

13 

14 :param tags: list of strings, some elements may be None 

15 :return: list of strings with all None elements removed 

16 """ 

17 return [tag for tag in tags if tag is not None] 

18 

19 

20def camel_to_snake(s: str) -> str: 

21 return "".join([f"_{c.lower()}" if c.isupper() else c for c in s]).lstrip("_") 

22 

23 

24def _require_restic(c: invoke.Context = None) -> bool: 

25 """ 

26 Checks if 'restic' is installed in the system. If not, it installs 'restic' using the 'apt' package manager 

27 and updates it to the latest version. The function returns False if 'restic' is already installed, 

28 and True after successfully installing and updating 'restic'. 

29 

30 :param c: An optional Invoke context. If not provided, a new context will be created. 

31 :return: False if 'restic' is already installed, True otherwise. 

32 """ 

33 c = c or invoke.Context() # type: invoke.Context 

34 if c.run("which restic", warn=True, hide=True).ok: 

35 # restic already exists, do nothing 

36 return False 

37 

38 if not require_sudo(c): 

39 return False 

40 

41 # sudo available 

42 print("Restic missing from this system! Installing now...", file=sys.stderr) 

43 c.sudo("apt install -y restic", hide=True) 

44 c.sudo("restic self-update", hide=True) 

45 print("Restic installed and updated!", file=sys.stderr) 

46 return True