Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Functions to get a status on the distribution 

4 

5.. versionadded:: 1.1 

6""" 

7import os 

8 

9import time 

10from .module_install_version import get_pypi_version 

11from ..packaged import all_set 

12from ..packaged.packaged_config import classifiers2string 

13 

14 

15def get_installed_modules(pypi=False, skip_betas=False, fLOG=None, stop=-1, short_list=None): 

16 """ 

17 Returns all modules recored in this modules. 

18 Adds installed modules. 

19 

20 @param pypi add pypi version 

21 @param skip_betas skip the intermediate functions 

22 @param stop stop after *stop* (or -1 for all) 

23 @param short_list short list of modules or None for all 

24 @param fLOG logging function 

25 @return list of dictionaries 

26 """ 

27 mod = all_set() 

28 mod.sort() 

29 rows = [_.as_dict(rst_link=True) for _ in mod] 

30 for row in rows: 

31 row["classifier"] = classifiers2string(row["classifier"]) 

32 keys = {mod["name"].lower(): mod for mod in rows} 

33 keys.update({mod["mname"].lower(): mod for mod in rows if mod["mname"]}) 

34 

35 from pip._internal.utils.misc import get_installed_distributions 

36 all_installed = [] 

37 dists = get_installed_distributions() 

38 if short_list: 

39 dists = [_ for _ in dists if _.project_name in short_list] 

40 pack = map(lambda d: (d.project_name.lower(), d), dists) 

41 for i, (_, dist) in enumerate(sorted(pack)): 

42 if i >= stop >= 0: 

43 break 

44 res = {} 

45 key = dist.project_name.lower() 

46 if key in keys: 

47 res.update(keys[key]) 

48 

49 names = [dist.key, dist.project_name, dist.key.replace("-", "_"), 

50 res.get("mname", None), res.get("name", None)] 

51 date = "" 

52 location = None 

53 for name in names: 

54 if name is None: 

55 continue 

56 ini = os.path.join(dist.location, name, "__init__.py") 

57 if os.path.exists(ini): 

58 date = time.ctime(os.path.getctime(ini)) 

59 location = os.path.dirname(ini) 

60 break 

61 ini = os.path.join(dist.location, name + ".py") 

62 if os.path.exists(ini): 

63 date = time.ctime(os.path.getctime(ini)) 

64 location = ini 

65 break 

66 res["installed"] = dist.project_name 

67 if ini is not None: 

68 res["location"] = location 

69 res["installed_date"] = date 

70 res["installed_version"] = dist.version 

71 

72 # update? 

73 if pypi: 

74 available = get_pypi_version( 

75 dist.project_name, skip_betas=skip_betas, full_list=True) 

76 

77 if not available: 

78 msg = '-' 

79 elif available[0] != dist.version: 

80 msg = '!=' 

81 else: 

82 msg = "==" 

83 res["available"] = available 

84 res["pypi"] = available[0] 

85 else: 

86 msg = '' 

87 available = None 

88 

89 res["diff_pypi"] = msg 

90 

91 if fLOG: 

92 pkg_info = '{dist.project_name:30} {dist.version:20}'.format( 

93 dist=dist) 

94 key = key if dist.key != dist.project_name else "" 

95 mes = '{pkg_info} {msg:3} - {date:20} --- {available} - {location}' 

96 mes = mes.format(pkg_info=pkg_info, msg=msg, date=date, dist=dist, 

97 available=available, location=res.get('location', '')) 

98 fLOG("[pymy] {0}/{1} - {2}".format(i, len(dists), mes)) 

99 

100 all_installed.append(res) 

101 return all_installed