Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/utils/process.py: 41%

29 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1# encoding: utf-8 

2""" 

3Utilities for working with external processes. 

4""" 

5 

6# Copyright (c) IPython Development Team. 

7# Distributed under the terms of the Modified BSD License. 

8 

9 

10import os 

11import shutil 

12import sys 

13 

14if sys.platform == 'win32': 

15 from ._process_win32 import system, getoutput, arg_split, check_pid 

16elif sys.platform == 'cli': 

17 from ._process_cli import system, getoutput, arg_split, check_pid 

18else: 

19 from ._process_posix import system, getoutput, arg_split, check_pid 

20 

21from ._process_common import getoutputerror, get_output_error_code, process_handler 

22 

23 

24class FindCmdError(Exception): 

25 pass 

26 

27 

28def find_cmd(cmd): 

29 """Find absolute path to executable cmd in a cross platform manner. 

30 

31 This function tries to determine the full path to a command line program 

32 using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the 

33 time it will use the version that is first on the users `PATH`. 

34 

35 Warning, don't use this to find IPython command line programs as there 

36 is a risk you will find the wrong one. Instead find those using the 

37 following code and looking for the application itself:: 

38 

39 import sys 

40 argv = [sys.executable, '-m', 'IPython'] 

41 

42 Parameters 

43 ---------- 

44 cmd : str 

45 The command line program to look for. 

46 """ 

47 path = shutil.which(cmd) 

48 if path is None: 

49 raise FindCmdError('command could not be found: %s' % cmd) 

50 return path 

51 

52 

53def abbrev_cwd(): 

54 """ Return abbreviated version of cwd, e.g. d:mydir """ 

55 cwd = os.getcwd().replace('\\','/') 

56 drivepart = '' 

57 tail = cwd 

58 if sys.platform == 'win32': 

59 if len(cwd) < 4: 

60 return cwd 

61 drivepart,tail = os.path.splitdrive(cwd) 

62 

63 

64 parts = tail.split('/') 

65 if len(parts) > 2: 

66 tail = '/'.join(parts[-2:]) 

67 

68 return (drivepart + ( 

69 cwd == '/' and '/' or tail))