paths.py 4.3 KB

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