-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
remote: add support for WebDAV #3647
Changes from 13 commits
12a0a7a
9e8a97b
5dc4dd8
4d0d7bb
441e9f4
74e8f53
a997811
a057c1a
87b2d23
0908005
df3dd7b
4110f0e
52e8d5a
3411f34
0d3424e
64b5129
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -312,3 +312,33 @@ def __eq__(self, other): | |
and self._path == other._path | ||
and self._extra_parts == other._extra_parts | ||
) | ||
|
||
|
||
class WebdavURLInfo(HTTPURLInfo): | ||
def __init__(self, url): | ||
super().__init__(url) | ||
|
||
@cached_property | ||
def url(self): | ||
return "{}://{}{}{}{}{}".format( | ||
self.scheme.replace("webdav", "http"), | ||
self.netloc, | ||
self._spath, | ||
(";" + self.params) if self.params else "", | ||
("?" + self.query) if self.query else "", | ||
("#" + self.fragment) if self.fragment else "", | ||
) | ||
|
||
def get_collections(self) -> list: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I wonder how is this different from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shizacat ^ ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This me need only one thing - replace scheme |
||
def pcol(path): | ||
return "{}://{}{}".format( | ||
self.scheme.replace("webdav", "http"), self.netloc, path, | ||
) | ||
|
||
p = self.path.split("/")[1:-1] | ||
if not p: | ||
return [] | ||
r = [] | ||
for i in range(len(p)): | ||
r.append(pcol("/{}/".format("/".join(p[: i + 1])))) | ||
return r |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os.path | ||
|
||
from .http import RemoteHTTP | ||
from dvc.scheme import Schemes | ||
from dvc.progress import Tqdm | ||
from dvc.exceptions import HTTPError | ||
from dvc.path_info import WebdavURLInfo | ||
|
||
|
||
class RemoteWEBDAV(RemoteHTTP): | ||
scheme = Schemes.WEBDAV | ||
path_cls = WebdavURLInfo | ||
REQUEST_TIMEOUT = 20 | ||
|
||
def _upload(self, from_file, to_info, name=None, no_progress_bar=False): | ||
def chunks(): | ||
with open(from_file, "rb") as fd: | ||
with Tqdm.wrapattr( | ||
fd, | ||
"read", | ||
total=None | ||
if no_progress_bar | ||
else os.path.getsize(from_file), | ||
leave=False, | ||
desc=to_info.url if name is None else name, | ||
disable=no_progress_bar, | ||
) as fd_wrapped: | ||
while True: | ||
chunk = fd_wrapped.read(self.CHUNK_SIZE) | ||
if not chunk: | ||
break | ||
yield chunk | ||
|
||
self._create_collections(to_info) | ||
response = self._request("PUT", to_info.url, data=chunks()) | ||
shizacat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if response.status_code not in (200, 201): | ||
raise HTTPError(response.status_code, response.reason) | ||
|
||
def _create_collections(self, to_info): | ||
url_cols = to_info.get_collections() | ||
from_idx = 0 | ||
for idx in reversed(range(len(url_cols) + 1)): | ||
from_idx = idx | ||
if bool(self._request("HEAD", url_cols[idx - 1])): | ||
break | ||
for idx in range(from_idx, len(url_cols)): | ||
response = self._request("MKCOL", url_cols[idx]) | ||
if response.status_code not in (200, 201): | ||
if bool(self._request("HEAD", url_cols[idx])): | ||
continue | ||
raise HTTPError(response.status_code, response.reason) | ||
|
||
def gc(self): | ||
raise NotImplementedError | ||
|
||
def list_cache_paths(self, prefix=None, progress_callback=None): | ||
raise NotImplementedError | ||
|
||
def walk_files(self, path_info): | ||
raise NotImplementedError |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from .webdav import RemoteWEBDAV | ||
from dvc.scheme import Schemes | ||
|
||
|
||
class RemoteWEBDAVS(RemoteWEBDAV): | ||
scheme = Schemes.WEBDAVS | ||
|
||
def gc(self): | ||
raise NotImplementedError | ||
shcheklein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def list_cache_paths(self, prefix=None, progress_callback=None): | ||
pmrowla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise NotImplementedError | ||
|
||
def walk_files(self, path_info): | ||
raise NotImplementedError |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ class Schemes: | |
GDRIVE = "gdrive" | ||
LOCAL = "local" | ||
OSS = "oss" | ||
WEBDAV = "webdav" | ||
WEBDAVS = "webdavs" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pytest | ||
|
||
from dvc.exceptions import HTTPError | ||
from dvc.path_info import WebdavURLInfo | ||
from dvc.remote.webdav import RemoteWEBDAV | ||
from tests.utils.httpd import StaticFileServer, WebDavSimpleHandler | ||
|
||
|
||
def test_create_collections(dvc): | ||
with StaticFileServer(handler_class=WebDavSimpleHandler) as httpd: | ||
url0 = "webdav://localhost:{}/a/b/file.txt".format(httpd.server_port) | ||
url1 = "webdav://localhost:{}/a/c/file.txt".format(httpd.server_port) | ||
config = {"url": url0} | ||
|
||
remote = RemoteWEBDAV(dvc, config) | ||
|
||
remote._create_collections(WebdavURLInfo(url0)) | ||
|
||
with pytest.raises(HTTPError): | ||
remote._create_collections(WebdavURLInfo(url1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about https? webdavs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If replace in "webdavS" webdav on http will be "httpS"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, 🤦 sorry :)
Ok, a few concerns still:
url
like this means loosing information. E.g. when we write in logs we will be getting http instead. In general feels like not a good idea creating a discrepancy between what we have inside class and what we return here.Feels like we should be doing it on the Remote class level, or there should be some separate method - access url or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me to do a separate method is over head. It will be necessary to rewrite the entire code from HTTP with one fix, and at the same time, there will still be the possibility use in logs the wrong URL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was merely a random suggestion. The whole approach with overriding
url
feels like a hack and can hit us somewhere down the road. There should be a better way of doing this - e.g. RemoteWebDav class should understand how to deal with those URLs (translate them if needed and pass to HTTPRemote), etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, makes me think if we should tackle it the other way around by simply adding some flag that will tell HTTP(s) remote to use webdav to access this remote that would otherwise have a normal HTTP(s) URL. Something like
and then
or
or something else. Not sure if this makes sense. It kinda goes against current conventions, but also I'm not sure if webdav is unique enough for a separate remote, maybe it is better to handle it as HTTP with some special tweaks. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hack - not sure. The same DEFAULT_PORTS also change the state of the object without notice. The same S3 is HTTP.
Leaving url unchanged - then the purpose of the _BasePath class is not very clear. It seemed to me that he should return the ready-made address format for working with him.
As for - as an extension of http. The more I work with him, the more he is everyday. The same gc. I don’t know how it works, but if only the remote method is needed for it, it’s easy to add and now a big difference from HTTP is already obtained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default ports logic is part of the protocols ... e.g. we want to detect that
example.com:80
is the same asexample.com
. In your case you create an object that provides an inconsistent interface - some methods (and some logic internally) might still rely onwebdav
, while url hashttp
. It can be very confusing - you ask and object to print its name - it prints Duck but then behave like Dog.not sure I got this point, could you clarify, please?
Again, not sure I understood this. Btw, we can jump on Discord - dvc.org/chat and discuss this with me and Ruslan (ivan and ruslan there). Please, feel free to ping me/us.