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.

appdirs.py 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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._vendor.six import PY2, text_type
  9. from pip._internal.compat import WINDOWS, expanduser
  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. r"""
  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. if it exists, else ~/.config/<AppName>
  61. Unix: ~/.local/share/<AppName> # or in
  62. $XDG_DATA_HOME, if defined
  63. Win XP (not roaming): C:\Documents and Settings\<username>\ ...
  64. ...Application Data\<AppName>
  65. Win XP (roaming): C:\Documents and Settings\<username>\Local ...
  66. ...Settings\Application Data\<AppName>
  67. Win 7 (not roaming): C:\\Users\<username>\AppData\Local\<AppName>
  68. Win 7 (roaming): C:\\Users\<username>\AppData\Roaming\<AppName>
  69. For Unix, we follow the XDG spec and support $XDG_DATA_HOME.
  70. That means, by default "~/.local/share/<AppName>".
  71. """
  72. if WINDOWS:
  73. const = roaming and "CSIDL_APPDATA" or "CSIDL_LOCAL_APPDATA"
  74. path = os.path.join(os.path.normpath(_get_win_folder(const)), appname)
  75. elif sys.platform == "darwin":
  76. path = os.path.join(
  77. expanduser('~/Library/Application Support/'),
  78. appname,
  79. ) if os.path.isdir(os.path.join(
  80. expanduser('~/Library/Application Support/'),
  81. appname,
  82. )
  83. ) else os.path.join(
  84. expanduser('~/.config/'),
  85. appname,
  86. )
  87. else:
  88. path = os.path.join(
  89. os.getenv('XDG_DATA_HOME', expanduser("~/.local/share")),
  90. appname,
  91. )
  92. return path
  93. def user_config_dir(appname, roaming=True):
  94. """Return full path to the user-specific config dir for this application.
  95. "appname" is the name of application.
  96. If None, just the system directory is returned.
  97. "roaming" (boolean, default True) can be set False to not use the
  98. Windows roaming appdata directory. That means that for users on a
  99. Windows network setup for roaming profiles, this user data will be
  100. sync'd on login. See
  101. <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
  102. for a discussion of issues.
  103. Typical user data directories are:
  104. macOS: same as user_data_dir
  105. Unix: ~/.config/<AppName>
  106. Win *: same as user_data_dir
  107. For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME.
  108. That means, by default "~/.config/<AppName>".
  109. """
  110. if WINDOWS:
  111. path = user_data_dir(appname, roaming=roaming)
  112. elif sys.platform == "darwin":
  113. path = user_data_dir(appname)
  114. else:
  115. path = os.getenv('XDG_CONFIG_HOME', expanduser("~/.config"))
  116. path = os.path.join(path, appname)
  117. return path
  118. # for the discussion regarding site_config_dirs locations
  119. # see <https://github.com/pypa/pip/issues/1733>
  120. def site_config_dirs(appname):
  121. r"""Return a list of potential user-shared config dirs for this application.
  122. "appname" is the name of application.
  123. Typical user config directories are:
  124. macOS: /Library/Application Support/<AppName>/
  125. Unix: /etc or $XDG_CONFIG_DIRS[i]/<AppName>/ for each value in
  126. $XDG_CONFIG_DIRS
  127. Win XP: C:\Documents and Settings\All Users\Application ...
  128. ...Data\<AppName>\
  129. Vista: (Fail! "C:\ProgramData" is a hidden *system* directory
  130. on Vista.)
  131. Win 7: Hidden, but writeable on Win 7:
  132. C:\ProgramData\<AppName>\
  133. """
  134. if WINDOWS:
  135. path = os.path.normpath(_get_win_folder("CSIDL_COMMON_APPDATA"))
  136. pathlist = [os.path.join(path, appname)]
  137. elif sys.platform == 'darwin':
  138. pathlist = [os.path.join('/Library/Application Support', appname)]
  139. else:
  140. # try looking in $XDG_CONFIG_DIRS
  141. xdg_config_dirs = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg')
  142. if xdg_config_dirs:
  143. pathlist = [
  144. os.path.join(expanduser(x), appname)
  145. for x in xdg_config_dirs.split(os.pathsep)
  146. ]
  147. else:
  148. pathlist = []
  149. # always look in /etc directly as well
  150. pathlist.append('/etc')
  151. return pathlist
  152. # -- Windows support functions --
  153. def _get_win_folder_from_registry(csidl_name):
  154. """
  155. This is a fallback technique at best. I'm not sure if using the
  156. registry for this guarantees us the correct answer for all CSIDL_*
  157. names.
  158. """
  159. import _winreg
  160. shell_folder_name = {
  161. "CSIDL_APPDATA": "AppData",
  162. "CSIDL_COMMON_APPDATA": "Common AppData",
  163. "CSIDL_LOCAL_APPDATA": "Local AppData",
  164. }[csidl_name]
  165. key = _winreg.OpenKey(
  166. _winreg.HKEY_CURRENT_USER,
  167. r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
  168. )
  169. directory, _type = _winreg.QueryValueEx(key, shell_folder_name)
  170. return directory
  171. def _get_win_folder_with_ctypes(csidl_name):
  172. csidl_const = {
  173. "CSIDL_APPDATA": 26,
  174. "CSIDL_COMMON_APPDATA": 35,
  175. "CSIDL_LOCAL_APPDATA": 28,
  176. }[csidl_name]
  177. buf = ctypes.create_unicode_buffer(1024)
  178. ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf)
  179. # Downgrade to short path name if have highbit chars. See
  180. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  181. has_high_char = False
  182. for c in buf:
  183. if ord(c) > 255:
  184. has_high_char = True
  185. break
  186. if has_high_char:
  187. buf2 = ctypes.create_unicode_buffer(1024)
  188. if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024):
  189. buf = buf2
  190. return buf.value
  191. if WINDOWS:
  192. try:
  193. import ctypes
  194. _get_win_folder = _get_win_folder_with_ctypes
  195. except ImportError:
  196. _get_win_folder = _get_win_folder_from_registry
  197. def _win_path_to_bytes(path):
  198. """Encode Windows paths to bytes. Only used on Python 2.
  199. Motivation is to be consistent with other operating systems where paths
  200. are also returned as bytes. This avoids problems mixing bytes and Unicode
  201. elsewhere in the codebase. For more details and discussion see
  202. <https://github.com/pypa/pip/issues/3463>.
  203. If encoding using ASCII and MBCS fails, return the original Unicode path.
  204. """
  205. for encoding in ('ASCII', 'MBCS'):
  206. try:
  207. return path.encode(encoding)
  208. except (UnicodeEncodeError, LookupError):
  209. pass
  210. return path