forked from astropy/astropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.py
More file actions
174 lines (142 loc) · 5.73 KB
/
Copy pathpaths.py
File metadata and controls
174 lines (142 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Licensed under a 3-clause BSD style license - see LICENSE.rst
""" This module contains functions to determine where configuration and
data/cache files used by Astropy should be placed.
"""
from __future__ import division
__all__ = ['get_config_dir', 'get_cache_dir']
def _find_home():
""" Locates and return the home directory (or best approximation) on this
system.
Raises
------
OSError
If the home directory cannot be located - usually means you are running
Astropy on some obscure platform that doesn't have standard home
directories.
"""
import os
import sys
from os import environ as env
# this is used below to make fix up encoding issues that sometimes crop up
# in py2.x but not in py3.x
if sys.version_info[0] < 3: # pragma: py3
decodepath = lambda pth: pth.decode(sys.getfilesystemencoding())
else: # pragma: py2
decodepath = lambda pth: pth
#First find the home directory - this is inspired by the scheme ipython
#uses to identify "home"
if os.name == 'posix':
# Linux, Unix, AIX, OS X
if 'HOME' in env:
homedir = decodepath(env['HOME'])
else:
raise OSError('Could not find unix home directory to search for' +\
' astropy config dir')
elif os.name == 'nt': # This is for all modern Windows (NT or after)
#Try for a network home first
if 'HOMESHARE' in env:
homedir = decodepath(env['HOMESHARE'])
#See if there's a local home
elif 'HOMEDRIVE' in env and 'HOMEPATH' in env:
homedir = os.path.join(env['HOMEDRIVE'], env['HOMEPATH'])
homedir = decodepath(homedir)
#Maybe a user profile?
elif 'USERPROFILE' in env:
homedir = decodepath(os.path.join(env['USERPROFILE']))
else:
try:
import _winreg as wreg
key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
homedir = wreg.QueryValueEx(key, 'Personal')[0]
homedir = decodepath(homedir)
key.Close()
except:
#As a final possible resort, see if HOME is present
if 'HOME' in env:
homedir = decodepath(env['HOME'])
else:
raise OSError('Could not find windows home directory to' +\
' search for astropy config dir')
else:
#for other platforms, try HOME, although it probably isn't there
if 'HOME' in env:
homedir = decodepath(env['HOME'])
else:
raise OSError('Could not find a home directory to search for ' +\
'astropy config dir - are you on an unspported platform?')
return homedir
def get_config_dir(create=True):
"""
Determines the Astropy configuration directory name and creates the
directory if it doesn't exist.
This directory is typically ``$HOME/.astropy/config``, but if the
XDG_CONFIG_HOME environment variable is set and the
``$XDG_CONFIG_HOME/astropy`` directory exists, it will be that directory.
If neither exists, the former will be created and symlinked to the latter.
Returns
-------
configdir : str
The absolute path to the configuration directory.
"""
from os import path, environ
#symlink will be set to this if the directory is created
linkto = None
#first look for XDG_CONFIG_HOME
xch = environ.get('XDG_CONFIG_HOME')
if xch is not None:
xchpth = path.join(xch, 'astropy')
if not path.islink(xchpth):
if path.exists(xchpth):
return path.abspath(xchpth)
else:
linkto = xchpth
return path.abspath(_find_or_create_astropy_dir('config', linkto))
def get_cache_dir():
"""
Determines the Astropy cache directory name and creates the directory if it
doesn't exist.
This directory is typically ``$HOME/.astropy/cache``, but if the
XDG_CACHE_HOME environment variable is set and the
``$XDG_CACHE_HOME/astropy`` directory exists, it will be that directory.
If neither exists, the former will be created and symlinked to the latter.
Returns
-------
cachedir : str
The absolute path to the cache directory.
"""
from os import path, environ
#symlink will be set to this if the directory is created
linkto = None
#first look for XDG_CACHE_HOME
xch = environ.get('XDG_CACHE_HOME')
if xch is not None:
xchpth = path.join(xch, 'astropy')
if not path.islink(xchpth):
if path.exists(xchpth):
return path.abspath(xchpth)
else:
linkto = xchpth
return path.abspath(_find_or_create_astropy_dir('cache', linkto))
def _find_or_create_astropy_dir(dirnm, linkto):
from os import path, mkdir
import sys
innerdir = path.join(_find_home(), '.astropy')
maindir = path.join(_find_home(), '.astropy', dirnm)
if not path.exists(maindir):
#first create .astropy dir if needed
if not path.exists(innerdir):
mkdir(innerdir)
elif not path.isdir(innerdir):
msg = 'Intended Astropy directory {0} is actually a file.'
raise IOError(msg.format(innerdir))
mkdir(maindir)
if (not sys.platform.startswith('win') and
linkto is not None and
not path.exists(linkto)):
from os import symlink
symlink(maindir, linkto)
elif not path.isdir(maindir):
msg = 'Intended Astropy {0} directory {1} is actually a file.'
raise IOError(msg.format(dirnm, maindir))
return path.abspath(maindir)