-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Support environments without a home dir or writable file system #1824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8083f25
a697a26
4d65400
21921a3
4987dcd
64c797b
1dbd6de
1adfc85
941efd4
ca6cd19
cc8cd1b
f01ebe1
018ce26
6a4f1e7
4f55a27
81639a1
8335773
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
matplotlib.get_home now returns None if there is no home directory, instead of raising RuntimeError. Updated code in several places to handle this gracefully. matplotlib.get_configdir now returns a temporary directory if there is no home directory, instead of raising RuntimeError.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -470,26 +470,25 @@ def checkdep_usetex(s): | |
|
||
def _get_home(): | ||
"""Find user's home directory if possible. | ||
Otherwise raise error. | ||
Otherwise, returns None. | ||
|
||
:see: http://mail.python.org/pipermail/python-list/2005-February/263921.html | ||
:see: http://mail.python.org/pipermail/python-list/2005-February/325395.html | ||
""" | ||
path='' | ||
try: | ||
path=os.path.expanduser("~") | ||
path = os.path.expanduser("~") | ||
except: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather remove this bare except. Would you mind adding the specific exception(s) types that might occur? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. Well I hope I didn't miss any... but the documentation for both |
||
pass | ||
if not os.path.isdir(path): | ||
for evar in ('HOME', 'USERPROFILE', 'TMP'): | ||
try: | ||
path = os.environ[evar] | ||
if os.path.isdir(path): | ||
break | ||
except: pass | ||
if path: | ||
return path | ||
else: | ||
raise RuntimeError('please define environment variable $HOME') | ||
if os.path.isdir(path): | ||
return path | ||
for evar in ('HOME', 'USERPROFILE', 'TMP'): | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The try except seems superfluous to me, I'd change this to:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
path = os.environ[evar] | ||
if os.path.isdir(path): | ||
return path | ||
except: | ||
pass | ||
return None | ||
|
||
|
||
def _create_tmp_config_dir(): | ||
< 10000 tool-tip id="tooltip-d9d2d84b-e15a-4db8-92e7-f76a6d6d6e7a" for="expand-link-40-diff-47339c58ad4d3bf37dfc5302e1c27245645ed38258d9b146a3f4d47274ef054d" popover="manual" data-direction="ne" data-type="label" data-view-component="true" class="sr-only position-absolute">Expand All | @@ -513,12 +512,14 @@ def _get_configdir(): | |
""" | ||
Return the string representing the configuration directory. | ||
|
||
Default is HOME/.matplotlib. You can override this with the | ||
MPLCONFIGDIR environment variable. If the default is not | ||
writable, and MPLCONFIGDIR is not set, then | ||
tempfile.gettempdir() is used to provide a directory in | ||
which a matplotlib subdirectory is created as the configuration | ||
directory. | ||
The directory is chosen as follows: | ||
|
||
1. If the MPLCONFIGDIR environment variable is supplied, choose that. Else, | ||
choose the '.matplotlib' subdirectory of the user's home directory (and | ||
create it if necessary). | ||
2. If the chosen directory exists and is writable, use that as the | ||
configuration directory. | ||
3. Create a temporary directory, and use it as the configuration directory. | ||
""" | ||
|
||
configdir = os.environ.get('MPLCONFIGDIR') | ||
|
@@ -530,18 +531,21 @@ def _get_configdir(): | |
return configdir | ||
|
||
h = get_home() | ||
p = os.path.join(get_home(), '.matplotlib') | ||
if h is not None: | ||
p = os.path.join(h, '.matplotlib') | ||
|
||
if os.path.exists(p): | ||
if not _is_writable_dir(p): | ||
return _create_tmp_config_dir() | ||
else: | ||
if not _is_writable_dir(h): | ||
return _create_tmp_config_dir() | ||
from matplotlib.cbook import mkdirs | ||
mkdirs(p) | ||
if os.path.exists(p): | ||
if not _is_writable_dir(p): | ||
return _create_tmp_config_dir() | ||
else: | ||
if not _is_writable_dir(h): | ||
return _create_tmp_config_dir() | ||
from matplotlib.cbook import mkdirs | ||
mkdirs(p) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There must be a subtlety that I'm missing here - I can't see the difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The difference is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cbook.mkdirs() is even safer than os.makedirs(). Imagine two processes, one needing to make A/B, and the other needing to make A/C. There is a race condition in os.makedirs() where one of the two processes will fail to make their directory because the exception was thrown while dealing with the parent directory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So why does it use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes -- let's change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
return p | ||
|
||
return p | ||
return _create_tmp_config_dir() | ||
get_configdir = verbose.wrap('CONFIGDIR=%s', _get_configdir, always=False) | ||
|
||
|
||
|
@@ -643,18 +647,20 @@ def matplotlib_fname(): | |
print("WARNING: File could not be renamed: %s" % e, file=sys.stderr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you turn this into a real warning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done (and the rest). |
||
|
||
home = get_home() | ||
oldname = os.path.join( home, '.matplotlibrc') | ||
if os.path.exists(oldname): | ||
configdir = get_configdir() | ||
newname = os.path.join(configdir, 'matplotlibrc') | ||
print("""\ | ||
if home: | ||
oldname = os.path.join( home, '.matplotlibrc') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the space before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done (and the rest). |
||
if os.path.exists(oldname): | ||
configdir = get_configdir() | ||
newname = os.path.join(configdir, 'matplotlibrc') | ||
print("""\ | ||
WARNING: Old rc filename "%s" found and renamed to | ||
new default rc file name "%s"."""%(oldname, newname), file=sys.stderr) | ||
|
||
try: | ||
shutil.move(oldname, newname) | ||
except IOError as e: | ||
print("WARNING: File could not be renamed: %s" % e, file=sys.stderr) | ||
try: | ||
shutil.move(oldname, newname) | ||
except IOError as e: | ||
print("WARNING: File could not be renamed: %s" % e, | ||
file=sys.stderr) | ||
|
||
|
||
fname = os.path.join( os.getcwd(), 'matplotlibrc') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind removing a newline before this line & getting rid of the space before |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍