-
Notifications
You must be signed in to change notification settings - Fork 1
/
webdav2unicore.py
64 lines (50 loc) · 2.04 KB
/
webdav2unicore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import tempfile
import pendulum
from airflow.decorators import dag, task
from airflow.models import Variable
from airflow.models.param import Param
from utils import setup_webdav, walk_dir, get_unicore_client
@dag(
default_args={"owner": "airflow",},
schedule=None,
start_date=pendulum.yesterday(),
tags=["testing", "unicore"],
params={
"target": Param("/tmp/", type="string"),
"oid": Param("", description="id of the dataset in datacat", type="string"),
"working_dir": Param("/tmp/", type="string", description="local working dir"),
"site_url": Param("", description="Unicore site url", type="string"),
"user": Param("", description="Unicore user", type="string"),
"password": Param("", description="Unicore password", type="string"),
},
)
def webdav2unicore():
@task
#.virtualenv(requirements=["pyunicore"], system_site_packages=False)
def load(**kwargs):
params = kwargs["params"]
target = params.get("target", "/tmp/")
working_dir = Variable.get("working_dir", default_var="/tmp/")
# setup webdav
client, dirname, prefix = setup_webdav(params=params)
print(f"WebDAV dirname {dirname}")
abso, _ = os.path.split(dirname[:-1])
# setup unicore
uc_client = get_unicore_client(**params)
uc_client.mkdir(target)
cnt = 0
for fname in walk_dir(client=client, prefix=prefix, path=dirname):
print(f"Processing {fname}")
target_path = os.path.join(target, fname[len(abso) + 1 :])
target_dir = os.path.dirname(target_path)
uc_client.mkdir(target_dir)
with tempfile.NamedTemporaryFile(dir=working_dir) as tmp:
print(f"Downloading {fname} --> {tmp.name}")
client.download_file(fname, tmp.name)
print(f"Uploading -> {target_path}")
uc_client.upload(tmp.name, target_path)
cnt += 1
return f"Copied {cnt} files"
load()
dag = webdav2unicore()