From 49d9ca7c7444cbb2b1c92eed3eb555b8a40b1193 Mon Sep 17 00:00:00 2001 From: Ping Zhang Date: Wed, 14 Dec 2022 14:11:22 -0800 Subject: [PATCH] mkdirs should set mode correctly With Path.mkdirs, if parents is true, any missing parents of this path are created as needed; they are created with the default permissions without taking mode into account see: https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir --- airflow/utils/file.py | 16 ++++++++-------- tests/utils/test_file.py | 13 ++++++++++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/airflow/utils/file.py b/airflow/utils/file.py index 102be16e289f5..8357a72790a60 100644 --- a/airflow/utils/file.py +++ b/airflow/utils/file.py @@ -143,14 +143,14 @@ def mkdirs(path, mode): :param path: The directory to create :param mode: The mode to give to the directory e.g. 0o755, ignores umask """ - import warnings - - warnings.warn( - f"This function is deprecated. Please use `pathlib.Path({path}).mkdir`", - RemovedInAirflow3Warning, - stacklevel=2, - ) - Path(path).mkdir(mode=mode, parents=True, exist_ok=True) + try: + o_umask = os.umask(0) + os.makedirs(path, mode) + except OSError: + if not os.path.isdir(path): + raise + finally: + os.umask(o_umask) ZIP_REGEX = re.compile(rf"((.*\.zip){re.escape(os.sep)})?(.*)") diff --git a/tests/utils/test_file.py b/tests/utils/test_file.py index 9a82a113b66d9..4bda96987242c 100644 --- a/tests/utils/test_file.py +++ b/tests/utils/test_file.py @@ -20,14 +20,25 @@ import os import os.path from pathlib import Path +from tempfile import TemporaryDirectory from unittest import mock +from uuid import uuid4 import pytest -from airflow.utils.file import correct_maybe_zipped, find_path_from_directory, open_maybe_zipped +from airflow.utils.file import correct_maybe_zipped, find_path_from_directory, mkdirs, open_maybe_zipped from tests.models import TEST_DAGS_FOLDER +def test_mkdirs(): + with TemporaryDirectory() as tmpdir: + path = os.path.join(tmpdir, str(uuid4()), str(uuid4())) + mkdirs(path, 0o777) + + mode = oct(os.stat(path).st_mode)[-3:] + assert mode == "777" + + class TestCorrectMaybeZipped: @mock.patch("zipfile.is_zipfile") def test_correct_maybe_zipped_normal_file(self, mocked_is_zipfile):