comparison venv/lib/python2.7/site-packages/pip/utils/appdirs.py @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 """
2 This code was taken from https://github.com/ActiveState/appdirs and modified
3 to suite our purposes.
4 """
5 from __future__ import absolute_import
6
7 import os
8 import sys
9
10 from pip.compat import WINDOWS
11
12
13 def user_cache_dir(appname):
14 r"""
15 Return full path to the user-specific cache dir for this application.
16
17 "appname" is the name of application.
18
19 Typical user cache directories are:
20 Mac OS X: ~/Library/Caches/<AppName>
21 Unix: ~/.cache/<AppName> (XDG default)
22 Windows: C:\Users\<username>\AppData\Local\<AppName>\Cache
23
24 On Windows the only suggestion in the MSDN docs is that local settings go
25 in the `CSIDL_LOCAL_APPDATA` directory. This is identical to the
26 non-roaming app data dir (the default returned by `user_data_dir`). Apps
27 typically put cache data somewhere *under* the given dir here. Some
28 examples:
29 ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache
30 ...\Acme\SuperApp\Cache\1.0
31
32 OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value.
33 """
34 if WINDOWS:
35 # Get the base path
36 path = os.path.normpath(_get_win_folder("CSIDL_LOCAL_APPDATA"))
37
38 # Add our app name and Cache directory to it
39 path = os.path.join(path, appname, "Cache")
40 elif sys.platform == "darwin":
41 # Get the base path
42 path = os.path.expanduser("~/Library/Caches")
43
44 # Add our app name to it
45 path = os.path.join(path, appname)
46 else:
47 # Get the base path
48 path = os.getenv("XDG_CACHE_HOME", os.path.expanduser("~/.cache"))
49
50 # Add our app name to it
51 path = os.path.join(path, appname)
52
53 return path
54
55
56 def user_data_dir(appname, roaming=False):
57 """
58 Return full path to the user-specific data dir for this application.
59
60 "appname" is the name of application.
61 If None, just the system directory is returned.
62 "roaming" (boolean, default False) can be set True to use the Windows
63 roaming appdata directory. That means that for users on a Windows
64 network setup for roaming profiles, this user data will be
65 sync'd on login. See
66 <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
67 for a discussion of issues.
68
69 Typical user data directories are:
70 Mac OS X: ~/Library/Application Support/<AppName>
71 Unix: ~/.local/share/<AppName> # or in
72 $XDG_DATA_HOME, if defined
73 Win XP (not roaming): C:\Documents and Settings\<username>\ ...
74 ...Application Data\<AppName>
75 Win XP (roaming): C:\Documents and Settings\<username>\Local ...
76 ...Settings\Application Data\<AppName>
77 Win 7 (not roaming): C:\\Users\<username>\AppData\Local\<AppName>
78 Win 7 (roaming): C:\\Users\<username>\AppData\Roaming\<AppName>
79
80 For Unix, we follow the XDG spec and support $XDG_DATA_HOME.
81 That means, by default "~/.local/share/<AppName>".
82 """
83 if WINDOWS:
84 const = roaming and "CSIDL_APPDATA" or "CSIDL_LOCAL_APPDATA"
85 path = os.path.join(os.path.normpath(_get_win_folder(const)), appname)
86 elif sys.platform == "darwin":
87 path = os.path.join(
88 os.path.expanduser('~/Library/Application Support/'),
89 appname,
90 )
91 else:
92 path = os.path.join(
93 os.getenv('XDG_DATA_HOME', os.path.expanduser("~/.local/share")),
94 appname,
95 )
96
97 return path
98
99
100 def user_log_dir(appname):
101 """
102 Return full path to the user-specific log dir for this application.
103
104 "appname" is the name of application.
105 If None, just the system directory is returned.
106
107 Typical user cache directories are:
108 Mac OS X: ~/Library/Logs/<AppName>
109 Unix: ~/.cache/<AppName>/log # or under $XDG_CACHE_HOME if
110 defined
111 Win XP: C:\Documents and Settings\<username>\Local Settings\ ...
112 ...Application Data\<AppName>\Logs
113 Vista: C:\\Users\<username>\AppData\Local\<AppName>\Logs
114
115 On Windows the only suggestion in the MSDN docs is that local settings
116 go in the `CSIDL_LOCAL_APPDATA` directory. (Note: I'm interested in
117 examples of what some windows apps use for a logs dir.)
118
119 OPINION: This function appends "Logs" to the `CSIDL_LOCAL_APPDATA`
120 value for Windows and appends "log" to the user cache dir for Unix.
121 """
122 if WINDOWS:
123 path = os.path.join(user_data_dir(appname), "Logs")
124 elif sys.platform == "darwin":
125 path = os.path.join(os.path.expanduser('~/Library/Logs'), appname)
126 else:
127 path = os.path.join(user_cache_dir(appname), "log")
128
129 return path
130
131
132 def user_config_dir(appname, roaming=True):
133 """Return full path to the user-specific config dir for this application.
134
135 "appname" is the name of application.
136 If None, just the system directory is returned.
137 "roaming" (boolean, default True) can be set False to not use the
138 Windows roaming appdata directory. That means that for users on a
139 Windows network setup for roaming profiles, this user data will be
140 sync'd on login. See
141 <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
142 for a discussion of issues.
143
144 Typical user data directories are:
145 Mac OS X: same as user_data_dir
146 Unix: ~/.config/<AppName>
147 Win *: same as user_data_dir
148
149 For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME.
150 That means, by deafult "~/.config/<AppName>".
151 """
152 if WINDOWS:
153 path = user_data_dir(appname, roaming=roaming)
154 elif sys.platform == "darwin":
155 path = user_data_dir(appname)
156 else:
157 path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config"))
158 path = os.path.join(path, appname)
159
160 return path
161
162
163 # for the discussion regarding site_config_dirs locations
164 # see <https://github.com/pypa/pip/issues/1733>
165 def site_config_dirs(appname):
166 """Return a list of potential user-shared config dirs for this application.
167
168 "appname" is the name of application.
169
170 Typical user config directories are:
171 Mac OS X: /Library/Application Support/<AppName>/
172 Unix: /etc or $XDG_CONFIG_DIRS[i]/<AppName>/ for each value in
173 $XDG_CONFIG_DIRS
174 Win XP: C:\Documents and Settings\All Users\Application ...
175 ...Data\<AppName>\
176 Vista: (Fail! "C:\ProgramData" is a hidden *system* directory
177 on Vista.)
178 Win 7: Hidden, but writeable on Win 7:
179 C:\ProgramData\<AppName>\
180 """
181 if WINDOWS:
182 path = os.path.normpath(_get_win_folder("CSIDL_COMMON_APPDATA"))
183 pathlist = [os.path.join(path, appname)]
184 elif sys.platform == 'darwin':
185 pathlist = [os.path.join('/Library/Application Support', appname)]
186 else:
187 # try looking in $XDG_CONFIG_DIRS
188 xdg_config_dirs = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg')
189 if xdg_config_dirs:
190 pathlist = [
191 os.sep.join([os.path.expanduser(x), appname])
192 for x in xdg_config_dirs.split(os.pathsep)
193 ]
194 else:
195 pathlist = []
196
197 # always look in /etc directly as well
198 pathlist.append('/etc')
199
200 return pathlist
201
202
203 # -- Windows support functions --
204
205 def _get_win_folder_from_registry(csidl_name):
206 """
207 This is a fallback technique at best. I'm not sure if using the
208 registry for this guarantees us the correct answer for all CSIDL_*
209 names.
210 """
211 import _winreg
212
213 shell_folder_name = {
214 "CSIDL_APPDATA": "AppData",
215 "CSIDL_COMMON_APPDATA": "Common AppData",
216 "CSIDL_LOCAL_APPDATA": "Local AppData",
217 }[csidl_name]
218
219 key = _winreg.OpenKey(
220 _winreg.HKEY_CURRENT_USER,
221 r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
222 )
223 directory, _type = _winreg.QueryValueEx(key, shell_folder_name)
224 return directory
225
226
227 def _get_win_folder_with_ctypes(csidl_name):
228 csidl_const = {
229 "CSIDL_APPDATA": 26,
230 "CSIDL_COMMON_APPDATA": 35,
231 "CSIDL_LOCAL_APPDATA": 28,
232 }[csidl_name]
233
234 buf = ctypes.create_unicode_buffer(1024)
235 ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf)
236
237 # Downgrade to short path name if have highbit chars. See
238 # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
239 has_high_char = False
240 for c in buf:
241 if ord(c) > 255:
242 has_high_char = True
243 break
244 if has_high_char:
245 buf2 = ctypes.create_unicode_buffer(1024)
246 if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024):
247 buf = buf2
248
249 return buf.value
250
251 if WINDOWS:
252 try:
253 import ctypes
254 _get_win_folder = _get_win_folder_with_ctypes
255 except ImportError:
256 _get_win_folder = _get_win_folder_from_registry