Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/utils/sysinfo.py: 42%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

26 statements  

1""" 

2Utilities for getting information about IPython and the system it's running in. 

3""" 

4from __future__ import annotations 

5 

6#----------------------------------------------------------------------------- 

7# Copyright (C) 2008-2011 The IPython Development Team 

8# 

9# Distributed under the terms of the BSD License. The full license is in 

10# the file COPYING, distributed as part of this software. 

11#----------------------------------------------------------------------------- 

12 

13#----------------------------------------------------------------------------- 

14# Imports 

15#----------------------------------------------------------------------------- 

16 

17import os 

18import sys 

19 

20from pathlib import Path 

21 

22from IPython.core import release 

23from IPython.utils import _sysinfo, encoding 

24 

25#----------------------------------------------------------------------------- 

26# Code 

27#----------------------------------------------------------------------------- 

28 

29def pkg_commit_hash(pkg_path: str) -> tuple[str, str]: 

30 """Get short form of commit hash given directory `pkg_path` 

31 

32 We get the commit hash from (in order of preference): 

33 

34 * IPython.utils._sysinfo.commit 

35 * git output, if we are in a git repository 

36 

37 If these fail, we return a not-found placeholder tuple 

38 

39 Parameters 

40 ---------- 

41 pkg_path : str 

42 directory containing package 

43 only used for getting commit from active repo 

44 

45 Returns 

46 ------- 

47 hash_from : str 

48 Where we got the hash from - description 

49 hash_str : str 

50 short form of hash 

51 """ 

52 # Try and get commit from written commit text file 

53 if _sysinfo.commit: 

54 return "installation", _sysinfo.commit 

55 

56 # maybe we are in a repository 

57 import subprocess 

58 proc = subprocess.Popen('git rev-parse --short HEAD'.split(' '), 

59 stdout=subprocess.PIPE, 

60 stderr=subprocess.PIPE, 

61 cwd=pkg_path) 

62 repo_commit, _ = proc.communicate() 

63 if repo_commit: 

64 return 'repository', repo_commit.strip().decode('ascii') 

65 return '(none found)', '<not found>' 

66 

67 

68def pkg_info(pkg_path: str) -> dict: 

69 """Return dict describing the context of this package 

70 

71 Parameters 

72 ---------- 

73 pkg_path : str 

74 path containing __init__.py for package 

75 

76 Returns 

77 ------- 

78 context : dict 

79 with named parameters of interest 

80 """ 

81 import platform 

82 

83 src, hsh = pkg_commit_hash(pkg_path) 

84 return dict( 

85 ipython_version=release.version, 

86 ipython_path=pkg_path, 

87 commit_source=src, 

88 commit_hash=hsh, 

89 sys_version=sys.version, 

90 sys_executable=sys.executable, 

91 sys_platform=sys.platform, 

92 platform=platform.platform(), 

93 os_name=os.name, 

94 default_encoding=encoding.DEFAULT_ENCODING, 

95 ) 

96 

97def get_sys_info() -> dict: 

98 """Return useful information about IPython and the system, as a dict.""" 

99 path = Path(__file__, "..").resolve().parent 

100 return pkg_info(str(path)) 

101 

102def sys_info() -> str: 

103 """Return useful information about IPython and the system, as a string. 

104 

105 Examples 

106 -------- 

107 :: 

108 

109 In [2]: print(sys_info()) 

110 {'commit_hash': '144fdae', # random 

111 'commit_source': 'repository', 

112 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython', 

113 'ipython_version': '0.11.dev', 

114 'os_name': 'posix', 

115 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick', 

116 'sys_executable': '/usr/bin/python', 

117 'sys_platform': 'linux2', 

118 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'} 

119 """ 

120 import pprint 

121 

122 return pprint.pformat(get_sys_info())