Skip to content

Commit

Permalink
Create directory and then file
Browse files Browse the repository at this point in the history
  • Loading branch information
mrocklin committed May 1, 2018
1 parent 8629ea7 commit 5e414fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 10 additions & 6 deletions dask/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def ensure_config_file(
source,
destination=os.path.join(os.path.expanduser('~'), '.config', 'dask'),
comment=True):
""" Copy file to default location if it does not already exist
"""
Copy file to default location if it does not already exist
This tries to move a default configuration file to a default location if
if does not already exist. It also comments out that file by default.
Expand All @@ -154,13 +155,13 @@ def ensure_config_file(
comment: bool, True by default
Whether or not to comment out the config file when copying
"""
if not os.path.exists(os.path.dirname(destination)):
os.makedirs(os.path.dirname(destination), exists_ok=True)

if os.path.isdir(destination):
if not os.path.splitext(destination)[1].strip('.'):
_, filename = os.path.split(source)
destination = os.path.join(destination, filename)

if not os.path.exists(os.path.dirname(destination)):
os.makedirs(os.path.dirname(destination))

if not os.path.exists(destination):
# Atomically create destination. Parallel testing discovered
# a race condition where a process can be busy creating the
Expand All @@ -170,7 +171,10 @@ def ensure_config_file(
lines = list(f)

if comment:
lines = ['#' + line if line else line for line in lines]
lines = ['# ' + line
if line.strip() and not line.startswith('#')
else line
for line in lines]

with open(tmp, 'w') as f:
f.write(''.join(lines))
Expand Down
6 changes: 4 additions & 2 deletions dask/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ def test_set_config():
assert config['abc'] == {'x': 1, 'y': 2, 'z': {'a': 3}}


def test_ensure_config_file_directory():
@pytest.mark.parametrize('mkdir', [True, False])
def test_ensure_config_file_directory(mkdir):
a = {'x': 1, 'y': {'a': 1}}
with tmpfile(extension='yaml') as source:
with tmpfile() as destination:
os.mkdir(destination)
if mkdir:
os.mkdir(destination)
with open(source, 'w') as f:
yaml.dump(a, f)

Expand Down

0 comments on commit 5e414fd

Please sign in to comment.