appdirs.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. """
  2. This code was taken from https://github.com/ActiveState/appdirs and modified
  3. to suit our purposes.
  4. """
  5. from __future__ import absolute_import
  6. import os
  7. import sys
  8. from pip.compat import WINDOWS, expanduser
  9. from pip._vendor.six import PY2, text_type
  10. def user_cache_dir(appname):
  11. r"""
  12. Return full path to the user-specific cache dir for this application.
  13. "appname" is the name of application.
  14. Typical user cache directories are:
  15. macOS: ~/Library/Caches/<AppName>
  16. Unix: ~/.cache/<AppName> (XDG default)
  17. Windows: C:\Users\<username>\AppData\Local\<AppName>\Cache
  18. On Windows the only suggestion in the MSDN docs is that local settings go
  19. in the `CSIDL_LOCAL_APPDATA` directory. This is identical to the
  20. non-roaming app data dir (the default returned by `user_data_dir`). Apps
  21. typically put cache data somewhere *under* the given dir here. Some
  22. examples:
  23. ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache
  24. ...\Acme\SuperApp\Cache\1.0
  25. OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value.
  26. """
  27. if WINDOWS:
  28. # Get the base path
  29. path = os.path.normpath(_get_win_folder("CSIDL_LOCAL_APPDATA"))
  30. # When using Python 2, return paths as bytes on Windows like we do on
  31. # other operating systems. See helper function docs for more details.
  32. if PY2 and isinstance(path, text_type):
  33. path = _win_path_to_bytes(path)
  34. # Add our app name and Cache directory to it
  35. path = os.path.join(path, appname, "Cache")
  36. elif sys.platform == "darwin":
  37. # Get the base path
  38. path = expanduser("~/Library/Caches")
  39. # Add our app name to it
  40. path = os.path.join(path, appname)
  41. else:
  42. # Get the base path
  43. path = os.getenv("XDG_CACHE_HOME", expanduser("~/.cache"))
  44. # Add our app name to it
  45. path = os.path.join(path, appname)
  46. return path
  47. def user_data_dir(appname, roaming=False):
  48. """
  49. Return full path to the user-specific data dir for this application.
  50. "appname" is the name of application.
  51. If None, just the system directory is returned.
  52. "roaming" (boolean, default False) can be set True to use the Windows
  53. roaming appdata directory. That means that for users on a Windows
  54. network setup for roaming profiles, this user data will be
  55. sync'd on login. See
  56. <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
  57. for a discussion of issues.
  58. Typical user data directories are:
  59. macOS: ~/Library/Application Support/<AppName>
  60. Unix: ~/.local/share/<AppName> # or in
  61. $XDG_DATA_HOME, if defined
  62. Win XP (not roaming): C:\Documents and Settings\<username>\ ...
  63. ...Application Data\<AppName>
  64. Win XP (roaming): C:\Documents and Settings\<username>\Local ...
  65. ...Settings\Application Data\<AppName>
  66. Win 7 (not roaming): C:\\Users\<username>\AppData\Local\<AppName>
  67. Win 7 (roaming): C:\\Users\<username>\AppData\Roaming\<AppName>
  68. For Unix, we follow the XDG spec and support $XDG_DATA_HOME.
  69. That means, by default "~/.local/share/<AppName>".
  70. """
  71. if WINDOWS:
  72. const = roaming and "CSIDL_APPDATA" or "CSIDL_LOCAL_APPDATA"
  73. path = os.path.join(os.path.normpath(_get_win_folder(const)), appname)
  74. elif sys.platform == "darwin":
  75. path = os.path.join(
  76. expanduser('~/Library/Application Support/'),
  77. appname,
  78. )
  79. else:
  80. path = os.path.join(
  81. os.getenv('XDG_DATA_HOME', expanduser("~/.local/share")),
  82. appname,
  83. )
  84. return path
  85. def user_config_dir(appname, roaming=True):
  86. """Return full path to the user-specific config dir for this application.
  87. "appname" is the name of application.
  88. If None, just the system directory is returned.
  89. "roaming" (boolean, default True) can be set False to not use the
  90. Windows roaming appdata directory. That means that for users on a
  91. Windows network setup for roaming profiles, this user data will be
  92. sync'd on login. See
  93. <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
  94. for a discussion of issues.
  95. Typical user data directories are:
  96. macOS: same as user_data_dir
  97. Unix: ~/.config/<AppName>
  98. Win *: same as user_data_dir
  99. For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME.
  100. That means, by default "~/.config/<AppName>".
  101. """
  102. if WINDOWS:
  103. path = user_data_dir(appname, roaming=roaming)
  104. elif sys.platform == "darwin":
  105. path = user_data_dir(appname)
  106. else:
  107. path = os.getenv('XDG_CONFIG_HOME', expanduser("~/.config"))
  108. path = os.path.join(path, appname)
  109. return path
  110. # for the discussion regarding site_config_dirs locations
  111. # see <https://github.com/pypa/pip/issues/1733>
  112. def site_config_dirs(appname):
  113. """Return a list of potential user-shared config dirs for this application.
  114. "appname" is the name of application.
  115. Typical user config directories are:
  116. macOS: /Library/Application Support/<AppName>/
  117. Unix: /etc or $XDG_CONFIG_DIRS[i]/<AppName>/ for each value in
  118. $XDG_CONFIG_DIRS
  119. Win XP: C:\Documents and Settings\All Users\Application ...
  120. ...Data\<AppName>\
  121. Vista: (Fail! "C:\ProgramData" is a hidden *system* directory
  122. on Vista.)
  123. Win 7: Hidden, but writeable on Win 7:
  124. C:\ProgramData\<AppName>\
  125. """
  126. if WINDOWS:
  127. path = os.path.normpath(_get_win_folder("CSIDL_COMMON_APPDATA"))
  128. pathlist = [os.path.join(path, appname)]
  129. elif sys.platform == 'darwin':
  130. pathlist = [os.path.join('/Library/Application Support', appname)]
  131. else:
  132. # try looking in $XDG_CONFIG_DIRS
  133. xdg_config_dirs = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg')
  134. if xdg_config_dirs:
  135. pathlist = [
  136. os.path.join(expanduser(x), appname)
  137. for x in xdg_config_dirs.split(os.pathsep)
  138. ]
  139. else:
  140. pathlist = []
  141. # always look in /etc directly as well
  142. pathlist.append('/etc')
  143. return pathlist
  144. # -- Windows support functions --
  145. def _get_win_folder_from_registry(csidl_name):
  146. """
  147. This is a fallback technique at best. I'm not sure if using the
  148. registry for this guarantees us the correct answer for all CSIDL_*
  149. names.
  150. """
  151. import _winreg
  152. shell_folder_name = {
  153. "CSIDL_APPDATA": "AppData",
  154. "CSIDL_COMMON_APPDATA": "Common AppData",
  155. "CSIDL_LOCAL_APPDATA": "Local AppData",
  156. }[csidl_name]
  157. key = _winreg.OpenKey(
  158. _winreg.HKEY_CURRENT_USER,
  159. r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
  160. )
  161. directory, _type = _winreg.QueryValueEx(key, shell_folder_name)
  162. return directory
  163. def _get_win_folder_with_ctypes(csidl_name):
  164. csidl_const = {
  165. "CSIDL_APPDATA": 26,
  166. "CSIDL_COMMON_APPDATA": 35,
  167. "CSIDL_LOCAL_APPDATA": 28,
  168. }[csidl_name]
  169. buf = ctypes.create_unicode_buffer(1024)
  170. ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf)
  171. # Downgrade to short path name if have highbit chars. See
  172. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  173. has_high_char = False
  174. for c in buf:
  175. if ord(c) > 255:
  176. has_high_char = True
  177. break
  178. if has_high_char:
  179. buf2 = ctypes.create_unicode_buffer(1024)
  180. if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024):
  181. buf = buf2
  182. return buf.value
  183. if WINDOWS:
  184. try:
  185. import ctypes
  186. _get_win_folder = _get_win_folder_with_ctypes
  187. except ImportError:
  188. _get_win_folder = _get_win_folder_from_registry
  189. def _win_path_to_bytes(path):
  190. """Encode Windows paths to bytes. Only used on Python 2.
  191. Motivation is to be consistent with other operating systems where paths
  192. are also returned as bytes. This avoids problems mixing bytes and Unicode
  193. elsewhere in the codebase. For more details and discussion see
  194. <https://github.com/pypa/pip/issues/3463>.
  195. If encoding using ASCII and MBCS fails, return the original Unicode path.
  196. """
  197. for encoding in ('ASCII', 'MBCS'):
  198. try:
  199. return path.encode(encoding)
  200. except (UnicodeEncodeError, LookupError):
  201. pass
  202. return path