paths.py 4.2 KB

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