Coverage for linuxpy/mounts.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-21 07:58 +0200

1# 

2# This file is part of the linuxpy project 

3# 

4# Copyright (c) 2024 Tiago Coutinho 

5# Distributed under the GPLv3 license. See LICENSE for more info. 

6 

7import functools 

8from pathlib import Path 

9 

10from .proc import PROC_PATH 

11from .types import Generator, NamedTuple, Optional 

12 

13MOUNTS_PATH: Path = PROC_PATH / "mounts" 

14 

15 

16class MountInfo(NamedTuple): 

17 dev_type: str 

18 mount_point: str 

19 fs_type: str 

20 attrs: list[str] 

21 

22 

23def gen_read() -> Generator[MountInfo, None, None]: 

24 data = MOUNTS_PATH.read_text() 

25 for line in data.splitlines(): 

26 dev_type, mount_point, fs_type, attrs, *_ = line.split() 

27 yield MountInfo(dev_type, mount_point, fs_type, attrs.split(",")) 

28 

29 

30@functools.cache 

31def cache() -> tuple[MountInfo, ...]: 

32 return tuple(gen_read()) 

33 

34 

35@functools.cache 

36def get_mount_point(dev_type, fs_type=None) -> Optional[Path]: 

37 if fs_type is None: 

38 fs_type = dev_type 

39 for _dev_type, mount_point, _fs_type, *_ in cache(): 

40 if dev_type == _dev_type and fs_type == _fs_type: 

41 return Path(mount_point) 

42 

43 

44def sysfs() -> Optional[Path]: 

45 return get_mount_point("sysfs") 

46 

47 

48def configfs() -> Optional[Path]: 

49 return get_mount_point("configfs")