Coverage for C:\Repos\ekr-pylint\pylint\lint\caching.py: 29%

34 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html 

2# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE 

3# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt 

4 

5from __future__ import annotations 

6 

7import pickle 

8import sys 

9import warnings 

10from pathlib import Path 

11 

12from pylint.constants import PYLINT_HOME 

13from pylint.utils import LinterStats 

14 

15 

16def _get_pdata_path( 

17 base_name: Path, recurs: int, pylint_home: Path = Path(PYLINT_HOME) 

18) -> Path: 

19 # We strip all characters that can't be used in a filename 

20 # Also strip '/' and '\\' because we want to create a single file, not sub-directories 

21 underscored_name = "_".join( 

22 str(p.replace(":", "_").replace("/", "_").replace("\\", "_")) 

23 for p in base_name.parts 

24 ) 

25 return pylint_home / f"{underscored_name}_{recurs}.stats" 

26 

27 

28def load_results( 

29 base: str | Path, pylint_home: str | Path = PYLINT_HOME 

30) -> LinterStats | None: 

31 base = Path(base) 

32 pylint_home = Path(pylint_home) 

33 data_file = _get_pdata_path(base, 1, pylint_home) 

34 

35 if not data_file.exists(): 

36 return None 

37 

38 try: 

39 with open(data_file, "rb") as stream: 

40 data = pickle.load(stream) 

41 if not isinstance(data, LinterStats): 

42 warnings.warn( 

43 "You're using an old pylint cache with invalid data following " 

44 f"an upgrade, please delete '{data_file}'.", 

45 UserWarning, 

46 ) 

47 raise TypeError 

48 return data 

49 except Exception: # pylint: disable=broad-except 

50 # There's an issue with the cache but we just continue as if it isn't there 

51 return None 

52 

53 

54def save_results( 

55 results: LinterStats, base: str | Path, pylint_home: str | Path = PYLINT_HOME 

56) -> None: 

57 base = Path(base) 

58 pylint_home = Path(pylint_home) 

59 try: 

60 pylint_home.mkdir(parents=True, exist_ok=True) 

61 except OSError: # pragma: no cover 

62 print(f"Unable to create directory {pylint_home}", file=sys.stderr) 

63 data_file = _get_pdata_path(base, 1) 

64 try: 

65 with open(data_file, "wb") as stream: 

66 pickle.dump(results, stream) 

67 except OSError as ex: # pragma: no cover 

68 print(f"Unable to create file {data_file}: {ex}", file=sys.stderr)