You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.3 KiB

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