Skip to content

Restyle remote: add support for WebDAV #3648

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

Closed
wants to merge 8 commits into from
Closed
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
4 changes: 4 additions & 0 deletions dvc/remote/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from dvc.remote.oss import RemoteOSS
from dvc.remote.s3 import RemoteS3
from dvc.remote.ssh import RemoteSSH
from dvc.remote.webdav import RemoteWEBDAV
from dvc.remote.webdavs import RemoteWEBDAVS


REMOTES = [
Expand All @@ -23,6 +25,8 @@
RemoteS3,
RemoteSSH,
RemoteOSS,
RemoteWEBDAV,
RemoteWEBDAVS,
# NOTE: RemoteLOCAL is the default
]

Expand Down
89 changes: 89 additions & 0 deletions dvc/remote/webdav.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import os.path

from funcy import cached_property

from .http import RemoteHTTP
from dvc.scheme import Schemes
from dvc.path_info import HTTPURLInfo
from dvc.progress import Tqdm
from dvc.exceptions import HTTPError


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:
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


class RemoteWEBDAV(RemoteHTTP):
scheme = Schemes.WEBDAV
path_cls = WebdavURLInfo

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())
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):
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
15 changes: 15 additions & 0 deletions dvc/remote/webdavs.py
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

def list_cache_paths(self, prefix=None, progress_callback=None):
raise NotImplementedError

def walk_files(self, path_info):
raise NotImplementedError
2 changes: 2 additions & 0 deletions dvc/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ class Schemes:
GDRIVE = "gdrive"
LOCAL = "local"
OSS = "oss"
WEBDAV = "webdav"
WEBDAVS = "webdavs"