Skip to content

Commit 223719f

Browse files
Revert "Added support for Windows paths in SambaHook (#54459)"
This reverts commit 53ec9e1.
1 parent 400871f commit 223719f

3 files changed

Lines changed: 10 additions & 75 deletions

File tree

providers/samba/docs/connections.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,3 @@ Login
4242

4343
Password
4444
The password of the user that will be used for authentication against the Samba server.
45-
46-
Share Type
47-
The share OS type (``posix`` or ``windows``). Used to determine the formatting of file and folder paths.

providers/samba/src/airflow/providers/samba/hooks/samba.py

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
# under the License.
1818
from __future__ import annotations
1919

20+
import posixpath
2021
from functools import wraps
21-
from pathlib import PurePosixPath, PureWindowsPath
2222
from shutil import copyfileobj
23-
from typing import TYPE_CHECKING, Any, Literal
23+
from typing import TYPE_CHECKING, Any
2424

2525
import smbclient
2626

@@ -41,21 +41,14 @@ class SambaHook(BaseHook):
4141
:param share:
4242
An optional share name. If this is unset then the "schema" field of
4343
the connection is used in its place.
44-
:param share_type:
45-
An optional share type name. If this is unset then it will assume a posix share type.
4644
"""
4745

4846
conn_name_attr = "samba_conn_id"
4947
default_conn_name = "samba_default"
5048
conn_type = "samba"
5149
hook_name = "Samba"
5250

53-
def __init__(
54-
self,
55-
samba_conn_id: str = default_conn_name,
56-
share: str | None = None,
57-
share_type: Literal["posix", "windows"] | None = None,
58-
) -> None:
51+
def __init__(self, samba_conn_id: str = default_conn_name, share: str | None = None) -> None:
5952
super().__init__()
6053
conn = self.get_connection(samba_conn_id)
6154

@@ -65,13 +58,6 @@ def __init__(
6558
if not conn.password:
6659
self.log.info("Password not provided")
6760

68-
self._share_type = share_type or conn.extra_dejson.get("share_type", "posix")
69-
if self._share_type not in {"posix", "windows"}:
70-
self._share_type = "posix"
71-
self.log.warning(
72-
"Invalid share_type specified. It must be either 'posix' or 'windows'. Falling back to 'posix'"
73-
)
74-
7561
connection_cache: dict[str, smbprotocol.connection.Connection] = {}
7662

7763
self._host = conn.host
@@ -98,18 +84,8 @@ def __exit__(self, exc_type, exc_value, traceback):
9884
connection.disconnect()
9985
self._connection_cache.clear()
10086

101-
@staticmethod
102-
def _join_posix_path(host: str, share: str, path: str) -> str:
103-
return str(PurePosixPath("//" + host, share, path.lstrip("/")))
104-
105-
@staticmethod
106-
def _join_windows_path(host: str, share: str, path: str) -> str:
107-
return "\\{}".format(PureWindowsPath(rf"\\{host}\\{share}", path.lstrip(r"\/")))
108-
10987
def _join_path(self, path):
110-
if self._share_type == "windows":
111-
return self._join_windows_path(self._host, self._share, path)
112-
return self._join_posix_path(self._host, self._share, path)
88+
return f"//{posixpath.join(self._host, self._share, path.lstrip('/'))}"
11389

11490
@wraps(smbclient.link)
11591
def link(self, src, dst, follow_symlinks=True):
@@ -317,20 +293,6 @@ def push_from_local(self, destination_filepath: str, local_filepath: str, buffer
317293
def get_ui_field_behaviour(cls) -> dict[str, Any]:
318294
"""Return custom field behaviour."""
319295
return {
320-
"hidden_fields": [],
296+
"hidden_fields": ["extra"],
321297
"relabeling": {"schema": "Share"},
322298
}
323-
324-
@classmethod
325-
def get_connection_form_widgets(cls) -> dict[str, Any]:
326-
"""Return connection widgets to add to connection form."""
327-
from flask_babel import lazy_gettext
328-
from wtforms import StringField
329-
330-
return {
331-
"share_type": StringField(
332-
label=lazy_gettext("Share Type"),
333-
description="The share OS type (`posix` or `windows`). Used to determine the formatting of file and folder paths.",
334-
default="posix",
335-
)
336-
}

providers/samba/tests/unit/samba/hooks/test_samba.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -148,38 +148,14 @@ def test_method(self, get_conn_mock, name):
148148
assert dict(kwargs, **connection_settings) == p_kwargs
149149

150150
@pytest.mark.parametrize(
151-
"path, path_type, full_path",
151+
"path, full_path",
152152
[
153-
# Linux path -> Linux path, no path_type (default)
154-
("/start/path/with/slash", None, "//ip/share/start/path/with/slash"),
155-
("start/path/without/slash", None, "//ip/share/start/path/without/slash"),
156-
# Linux path -> Linux path, explicit path_type (posix)
157-
("/start/path/with/slash/posix", "posix", "//ip/share/start/path/with/slash/posix"),
158-
("start/path/without/slash/posix", "posix", "//ip/share/start/path/without/slash/posix"),
159-
# Linux path -> Windows path, explicit path_type (windows)
160-
("/start/path/with/slash/windows", "windows", r"\\ip\share\start\path\with\slash\windows"),
161-
("start/path/without/slash/windows", "windows", r"\\ip\share\start\path\without\slash\windows"),
162-
# Windows path -> Windows path, explicit path_type (windows)
163-
(
164-
r"\start\path\with\backslash\windows",
165-
"windows",
166-
r"\\ip\share\start\path\with\backslash\windows",
167-
),
168-
(
169-
r"start\path\without\backslash\windows",
170-
"windows",
171-
r"\\ip\share\start\path\without\backslash\windows",
172-
),
153+
("/start/path/with/slash", "//ip/share/start/path/with/slash"),
154+
("start/path/without/slash", "//ip/share/start/path/without/slash"),
173155
],
174156
)
175157
@mock.patch(f"{BASEHOOK_PATCH_PATH}.get_connection")
176-
def test__join_path(
177-
self,
178-
get_conn_mock,
179-
path,
180-
path_type,
181-
full_path,
182-
):
158+
def test__join_path(self, get_conn_mock, path, full_path):
183159
CONNECTION = Connection(
184160
host="ip",
185161
schema="share",
@@ -188,7 +164,7 @@ def test__join_path(
188164
)
189165

190166
get_conn_mock.return_value = CONNECTION
191-
hook = SambaHook("samba_default", share_type=path_type)
167+
hook = SambaHook("samba_default")
192168
assert hook._join_path(path) == full_path
193169

194170
@mock.patch("airflow.providers.samba.hooks.samba.smbclient.open_file", return_value=mock.Mock())

0 commit comments

Comments
 (0)