Coverage for src/pymyinstall/installhelper/pip_helper.py: 68%

28 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-07-19 01:47 +0200

1""" 

2@file 

3@brief Helpers for pip. 

4 

5.. versionadded:: 1.5 

6""" 

7try: 

8 from pip._internal.utils.compat import stdlib_pkgs 

9except ImportError: 

10 stdlib_pkgs = None 

11 

12 

13class Distribution: 

14 """ 

15 Common interface for old and recent pip packages. 

16 

17 .. versionadded:: 1.5 

18 """ 

19 

20 def __init__(self, dist): 

21 self.dist = dist 

22 

23 def __getattr__(self, attr): 

24 if attr == 'key': 

25 if hasattr(self.__dict__['dist'], 'key'): 

26 return self.__dict__['dist'].key 

27 return self.__dict__['dist'].canonical_name 

28 if attr == 'dist': 

29 return self.__dict__['dist'] 

30 if attr in {'_get_metadata', 'requires', 'PKG_INFO', 'project_name'}: 

31 if hasattr(self.__dict__['dist'], attr): 

32 return getattr(self.__dict__['dist'], attr) 

33 return getattr(self.__dict__['dist']._dist, attr) 

34 return getattr(self.__dict__['dist'], attr) 

35 

36 

37def get_installed_distributions(local_only=True, skip=stdlib_pkgs, 

38 include_editables=True, editables_only=False, 

39 user_only=False, use_cmd=False): 

40 """ 

41 Directs call to function *get_installed_distributions* from :epkg:`pip`. 

42 

43 Return a list of installed Distribution objects. 

44 

45 :param local_only: if True (default), only return installations 

46 local to the current virtualenv, if in a virtualenv. 

47 :param skip: argument is an iterable of lower-case project names to 

48 ignore; defaults to ``pip.compat.stdlib_pkgs`` (if *skip* is None) 

49 :param editables: if False, don't report editables. 

50 :param editables_only: if True , only report editables. 

51 :param user_only: if True , only report installations in the user 

52 site directory. 

53 :param use_cmd: if True, use a different process (updated package list) 

54 :return: list of installed Distribution objects. 

55 

56 .. versionadded:: 1.5 

57 """ 

58 if use_cmd: 

59 raise NotImplementedError("use_cmd should be False") 

60 try: 

61 from pip._internal.metadata import get_default_environment 

62 return list(map(Distribution, 

63 get_default_environment().iter_installed_distributions( 

64 local_only=local_only, skip=skip, 

65 include_editables=include_editables, 

66 editables_only=editables_only, 

67 user_only=user_only))) 

68 

69 except ImportError: 

70 from pip._internal.utils.misc import get_installed_distributions as getd 

71 return list(map(Distribution, getd( 

72 local_only=local_only, skip=skip, 

73 include_editables=include_editables, 

74 editables_only=editables_only, 

75 user_only=user_only, use_cmd=use_cmd)))