mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
Move makedirs to fs.py
This commit is contained in:
+2
-1
@@ -16,7 +16,8 @@ from dvc.exceptions import NotDvcRepoError
|
||||
from dvc.lock import Lock, LockError
|
||||
from dvc.repo import Repo
|
||||
from dvc.scm import SCM
|
||||
from dvc.utils import env2bool, is_binary, makedirs
|
||||
from dvc.utils import env2bool, is_binary
|
||||
from dvc.utils.fs import makedirs
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
+2
-2
@@ -24,8 +24,8 @@ from dvc.path_info import PathInfo, URLInfo
|
||||
from dvc.progress import Tqdm
|
||||
from dvc.remote.slow_link_detection import slow_link_guard
|
||||
from dvc.state import StateNoop
|
||||
from dvc.utils import makedirs, relpath, tmp_fname
|
||||
from dvc.utils.fs import move
|
||||
from dvc.utils import relpath, tmp_fname
|
||||
from dvc.utils.fs import move, makedirs
|
||||
from dvc.utils.http import open_url
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
+1
-3
@@ -23,13 +23,11 @@ from dvc.scm.tree import is_working_tree
|
||||
from dvc.system import System
|
||||
from dvc.utils import copyfile
|
||||
from dvc.utils import file_md5
|
||||
from dvc.utils import makedirs
|
||||
from dvc.utils import relpath
|
||||
from dvc.utils import tmp_fname
|
||||
from dvc.utils import walk_files
|
||||
from dvc.compat import fspath_py35
|
||||
from dvc.utils.fs import move
|
||||
from dvc.utils.fs import remove
|
||||
from dvc.utils.fs import move, makedirs, remove
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ class Repo(object):
|
||||
from dvc.repo.metrics import Metrics
|
||||
from dvc.scm.tree import WorkingTree
|
||||
from dvc.repo.tag import Tag
|
||||
from dvc.utils import makedirs
|
||||
from dvc.utils.fs import makedirs
|
||||
|
||||
root_dir = self.find_root(root_dir)
|
||||
|
||||
|
||||
@@ -135,23 +135,6 @@ def copyfile(src, dest, no_progress_bar=False, name=None):
|
||||
pbar.update(len(buf))
|
||||
|
||||
|
||||
def makedirs(path, exist_ok=False, mode=None):
|
||||
path = fspath_py35(path)
|
||||
|
||||
if mode is None:
|
||||
os.makedirs(path, exist_ok=exist_ok)
|
||||
return
|
||||
|
||||
# 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)
|
||||
try:
|
||||
os.makedirs(path, exist_ok=exist_ok)
|
||||
finally:
|
||||
os.umask(umask)
|
||||
|
||||
|
||||
def _split(list_to_split, chunk_size):
|
||||
return [
|
||||
list_to_split[i : i + chunk_size]
|
||||
|
||||
@@ -152,3 +152,20 @@ def path_isin(child, parent):
|
||||
parent = os.path.join(normalize_path(parent), "")
|
||||
child = normalize_path(child)
|
||||
return child != parent and child.startswith(parent)
|
||||
|
||||
|
||||
def makedirs(path, exist_ok=False, mode=None):
|
||||
path = fspath_py35(path)
|
||||
|
||||
if mode is None:
|
||||
os.makedirs(path, exist_ok=exist_ok)
|
||||
return
|
||||
|
||||
# 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)
|
||||
try:
|
||||
os.makedirs(path, exist_ok=exist_ok)
|
||||
finally:
|
||||
os.umask(umask)
|
||||
|
||||
@@ -50,7 +50,7 @@ from contextlib import contextmanager
|
||||
import pytest
|
||||
from funcy.py3 import lmap, retry
|
||||
|
||||
from dvc.utils import makedirs
|
||||
from dvc.utils.fs import makedirs
|
||||
from dvc.compat import fspath, fspath_py35
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
import stat
|
||||
|
||||
import pytest
|
||||
|
||||
from dvc.utils.fs import makedirs
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name == "nt", reason="Not supported for Windows.")
|
||||
def test_makedirs_permissions(tmp_dir):
|
||||
dir_mode = 0o755
|
||||
intermediate_dir = "тестовая-директория"
|
||||
test_dir = os.path.join(intermediate_dir, "data")
|
||||
|
||||
assert not os.path.exists(intermediate_dir)
|
||||
|
||||
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
|
||||
@@ -9,7 +9,7 @@ from dvc.exceptions import UrlNotDvcRepoError
|
||||
from dvc.repo.get import GetDVCFileError, PathMissingError
|
||||
from dvc.repo import Repo
|
||||
from dvc.system import System
|
||||
from dvc.utils import makedirs
|
||||
from dvc.utils.fs import makedirs
|
||||
from dvc.compat import fspath
|
||||
from tests.utils import trees_equal
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import os
|
||||
import pytest
|
||||
|
||||
from dvc.repo import Repo
|
||||
from dvc.utils import makedirs
|
||||
from dvc.utils.fs import makedirs
|
||||
|
||||
|
||||
def test_get_file(repo_dir):
|
||||
|
||||
@@ -12,7 +12,7 @@ from dvc.exceptions import PathMissingError
|
||||
from dvc.exceptions import NoOutputInExternalRepoError
|
||||
from dvc.stage import Stage
|
||||
from dvc.system import System
|
||||
from dvc.utils import makedirs
|
||||
from dvc.utils.fs import makedirs
|
||||
from dvc.compat import fspath
|
||||
from tests.utils import trees_equal
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ from mock import patch
|
||||
|
||||
import dvc
|
||||
from dvc.main import main
|
||||
from dvc.utils import makedirs
|
||||
from dvc.utils.fs import makedirs
|
||||
from tests.basic_env import TestDvc
|
||||
from tests.utils import spy
|
||||
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
# encoding: utf-8
|
||||
import os
|
||||
import stat
|
||||
|
||||
import pytest
|
||||
|
||||
from dvc import utils
|
||||
|
||||
@@ -62,17 +59,3 @@ def test_boxify():
|
||||
)
|
||||
|
||||
assert expected == utils.boxify("message")
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name == "nt", reason="Not supported for Windows.")
|
||||
def test_makedirs_permissions(tmp_dir):
|
||||
dir_mode = 0o755
|
||||
intermediate_dir = "тестовая-директория"
|
||||
test_dir = os.path.join(intermediate_dir, "data")
|
||||
|
||||
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
|
||||
|
||||
@@ -16,6 +16,7 @@ from dvc.utils.fs import get_inode
|
||||
from dvc.utils.fs import get_mtime_and_size
|
||||
from dvc.utils.fs import move
|
||||
from dvc.utils.fs import path_isin, remove
|
||||
from dvc.utils.fs import makedirs
|
||||
from tests.basic_env import TestDir
|
||||
from tests.utils import spy
|
||||
|
||||
@@ -202,3 +203,16 @@ def test_path_isin_with_absolute_path():
|
||||
child = os.path.join(parent, "to", "folder")
|
||||
|
||||
assert path_isin(child, parent)
|
||||
|
||||
|
||||
def test_makedirs(repo_dir):
|
||||
path = os.path.join(repo_dir.root_dir, "directory")
|
||||
path_info = PathInfo(
|
||||
os.path.join(repo_dir.root_dir, "another", "directory")
|
||||
)
|
||||
|
||||
makedirs(path)
|
||||
assert os.path.isdir(path)
|
||||
|
||||
makedirs(path_info)
|
||||
assert os.path.isdir(path_info.fspath)
|
||||
|
||||
@@ -8,7 +8,6 @@ from dvc.path_info import PathInfo
|
||||
from dvc.utils import copyfile
|
||||
from dvc.utils import file_md5
|
||||
from dvc.utils import fix_env
|
||||
from dvc.utils import makedirs
|
||||
from dvc.utils import relpath
|
||||
from dvc.utils import to_chunks
|
||||
from dvc.utils import tmp_fname
|
||||
@@ -115,19 +114,6 @@ def test_copyfile(path, repo_dir):
|
||||
assert filecmp.cmp(src_info.fspath, dest_info.fspath, shallow=False)
|
||||
|
||||
|
||||
def test_makedirs(repo_dir):
|
||||
path = os.path.join(repo_dir.root_dir, "directory")
|
||||
path_info = PathInfo(
|
||||
os.path.join(repo_dir.root_dir, "another", "directory")
|
||||
)
|
||||
|
||||
makedirs(path)
|
||||
assert os.path.isdir(path)
|
||||
|
||||
makedirs(path_info)
|
||||
assert os.path.isdir(path_info.fspath)
|
||||
|
||||
|
||||
def test_tmp_fname():
|
||||
file_path = os.path.join("path", "to", "file")
|
||||
file_path_info = PathInfo(file_path)
|
||||
|
||||
Reference in New Issue
Block a user