1 """Find files and directories which IPython uses. 2 """ 3 import os.path 4 import tempfile 5 from warnings import warn 6 7 import IPython 8 from IPython.utils.importstring import import_item 9 from IPython.utils.path import ( 10 get_home_dir, 11 get_xdg_dir, 12 get_xdg_cache_dir, 13 compress_user, 14 _writable_dir, 15 ensure_dir_exists, 16 ) 17 18 19 def get_ipython_dir() -> str: 20 """Get the IPython directory for this platform and user. 21 22 This uses the logic in `get_home_dir` to find the home directory 23 and then adds .ipython to the end of the path. 24 """ 25 26 env = os.environ 27 pjoin = os.path.join 28 29 30 ipdir_def = '.ipython' 31 32 home_dir = get_home_dir() 33 xdg_dir = get_xdg_dir() 34 35 if 'IPYTHON_DIR' in env: 36 warn('The environment variable IPYTHON_DIR is deprecated since IPython 3.0. ' 37 'Please use IPYTHONDIR instead.', DeprecationWarning) 38 ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None)) 39 if ipdir is None: 40 # not set explicitly, use ~/.ipython 41 ipdir = pjoin(home_dir, ipdir_def) 42 if xdg_dir: 43 # Several IPython versions (up to 1.x) defaulted to .config/ipython 44 # on Linux. We have decided to go back to using .ipython everywhere 45 xdg_ipdir = pjoin(xdg_dir, 'ipython') 46 47 if _writable_dir(xdg_ipdir): 48 cu = compress_user 49 if os.path.exists(ipdir): 50 warn(('Ignoring {0} in favour of {1}. Remove {0} to ' 51 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir))) 52 elif os.path.islink(xdg_ipdir): 53 warn(('{0} is deprecated. Move link to {1} to ' 54 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir))) 55 else: 56 ipdir = xdg_ipdir 57 58 ipdir = os.path.normpath(os.path.expanduser(ipdir)) 59 60 if os.path.exists(ipdir) and not _writable_dir(ipdir): 61 # ipdir exists, but is not writable 62 warn("IPython dir '{0}' is not a writable location," 63 " using a temp directory.".format(ipdir)) 64 ipdir = tempfile.mkdtemp() 65 elif not os.path.exists(ipdir): 66 parent = os.path.dirname(ipdir) 67 if not _writable_dir(parent): 68 # ipdir does not exist and parent isn't writable 69 warn("IPython parent '{0}' is not a writable location," 70 " using a temp directory.".format(parent)) 71 ipdir = tempfile.mkdtemp() 72 else: 73 os.makedirs(ipdir, exist_ok=True) 74 assert isinstance(ipdir, str), "all path manipulation should be str(unicode), but are not." 75 return ipdir 76 77 78 def get_ipython_cache_dir() -> str: 79 """Get the cache directory it is created if it does not exist.""" 80 xdgdir = get_xdg_cache_dir() 81 if xdgdir is None: 82 return get_ipython_dir() 83 ipdir = os.path.join(xdgdir, "ipython") 84 if not os.path.exists(ipdir) and _writable_dir(xdgdir): 85 ensure_dir_exists(ipdir) 86 elif not _writable_dir(xdgdir): 87 return get_ipython_dir() 88 89 return ipdir 90 91 92 def get_ipython_package_dir() -> str: 93 """Get the base directory where IPython itself is installed.""" 94 ipdir = os.path.dirname(IPython.__file__) 95 assert isinstance(ipdir, str) 96 return ipdir 97 98 99 def get_ipython_module_path(module_str): 100 """Find the path to an IPython module in this version of IPython. 101 102 This will always find the version of the module that is in this importable 103 IPython package. This will always return the path to the ``.py`` 104 version of the module. 105 """ 106 if module_str == 'IPython': 107 return os.path.join(get_ipython_package_dir(), '__init__.py') 108 mod = import_item(module_str) 109 the_path = mod.__file__.replace('.pyc', '.py') 110 the_path = the_path.replace('.pyo', '.py') 111 return the_path 112 113 114 def locate_profile(profile='default'): 115 """Find the path to the folder associated with a given profile. 116 117 I.e. find $IPYTHONDIR/profile_whatever. 118 """ 119 from IPython.core.profiledir import ProfileDir, ProfileDirError 120 try: 121 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile) 122 except ProfileDirError as e: 123 # IOError makes more sense when people are expecting a path 124 raise IOError("Couldn't find profile %r" % profile) from e 125 return pd.location 126