Coverage for linuxpy/proc.py: 98%

34 statements  

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

1from pathlib import Path 

2 

3from linuxpy.util import try_numeric 

4 

5PROC_PATH = Path("/proc") 

6 

7CPU_INFO_PATH: Path = PROC_PATH / "cpuinfo" 

8MEM_INFO_PATH: Path = PROC_PATH / "meminfo" 

9MODULES_PATH: Path = PROC_PATH / "modules" 

10 

11 

12def iter_cpu_info(): 

13 data = CPU_INFO_PATH.read_text() 

14 for cpu in data.split("\n\n"): 

15 info = {} 

16 for line in cpu.splitlines(): 

17 key, value = map(str.strip, line.split(":", 1)) 

18 if "flags" in key or key == "bugs": 

19 value = value.split() 

20 else: 

21 value = try_numeric(value) 

22 info[key] = value 

23 yield info 

24 

25 

26def iter_mem_info(): 

27 data = MEM_INFO_PATH.read_text() 

28 for line in data.splitlines(): 

29 key, value = map(str.strip, line.split(":", 1)) 

30 if value.endswith(" kB"): 

31 value = try_numeric(value[:-3]) * 1024 

32 else: 

33 value = try_numeric(value) 

34 yield key, value 

35 

36 

37def iter_modules(): 

38 data = MODULES_PATH.read_text() 

39 for line in data.splitlines(): 

40 fields = line.split() 

41 mod = { 

42 "name": fields[0], 

43 "size": int(fields[1]), 

44 "use_count": int(fields[2]), 

45 "dependencies": [] if fields[3] == "-" else [dep for dep in fields[3].split(",") if dep], 

46 } 

47 if len(fields) > 5: 47 ↛ 50line 47 didn't jump to line 50 because the condition on line 47 was always true

48 mod["state"] = fields[4] 

49 mod["offset"] = int(fields[5], 16) 

50 yield mod