Skip to content
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

makedirs: fix mode flag is being ignored starting from Python 3.7 #2790

Merged
merged 6 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dvc/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ def makedirs(path, exist_ok=False, mode=None):
_makedirs(path, exist_ok=exist_ok)
return

umask = os.umask(0)
# utilize umask to set proper permissions since Python 3.7 the `mode`
# `makedirs` argument no longer affects the file permission bits of
# newly-created intermediate-level directories.
umask = os.umask(0o777 - mode)
shcheklein marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put a comment explaining what is going on :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

try:
_makedirs(path, exist_ok=exist_ok, mode=mode)
_makedirs(path, exist_ok=exist_ok)
finally:
os.umask(umask)

Expand Down
2 changes: 0 additions & 2 deletions dvc/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ def _makedirs(name, mode=0o777, exist_ok=False):
if e.errno != errno.EEXIST:
raise
cdir = os.curdir
if isinstance(tail, bytes):
shcheklein marked this conversation as resolved.
Show resolved Hide resolved
cdir = bytes(os.curdir, "ASCII")
if tail == cdir:
return
try:
Expand Down
19 changes: 19 additions & 0 deletions tests/func/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# encoding: utf-8
import filecmp
import os
import shutil
import stat

import pytest

from dvc import utils
from tests.basic_env import TestDvc
Expand Down Expand Up @@ -69,3 +73,18 @@ def test_boxify(self):
)

assert expected == utils.boxify("message")


@pytest.mark.skipif(os.name == "nt", reason="Not supported for Windows.")
def test_makedirs_permissions(tmpdir):
dir_mode = 0o755
os.chdir(str(tmpdir))
intermediate_dir = "тестовая-директория"
test_dir = os.path.join(intermediate_dir, "data")
Copy link
Contributor

@efiop efiop Nov 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are creating this directory in the project's root, which is not nice. Please use tmpdir fixture or something like that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, simplified using tmpdir


assert not os.path.exists(intermediate_dir)

utils.makedirs(test_dir, mode=dir_mode)

assert stat.S_IMODE(os.stat(test_dir).st_mode) == dir_mode
assert stat.S_IMODE(os.stat(intermediate_dir).st_mode) == dir_mode