This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
executable file
·77 lines (67 loc) · 2.62 KB
/
upload.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
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" perform recursive uploads using cadaver
cadaver does not support recursive directory upload.
This script will upload a given path to a given URL
"""
import argparse
import os
import tempfile
import subprocess
import urlparse
def create_script(dir_given, base_path):
"""create the script"""
upload_script = []
parent_directory = os.path.abspath(dir_given)
def remove_first_dir(directory_path):
"""remove common ancestors of directory_path with parent_directory
those parts should only exist locally."""
len_common_ancestors = len(
os.path.commonprefix([directory_path, parent_directory])
)
return directory_path[len_common_ancestors + 1:]
for (path, subdirs, files) in os.walk(parent_directory):
for subdir in subdirs:
directory_path = os.path.join(path, subdir)
upload_script.append(
"mkcol %s" % remove_first_dir(directory_path)
)
if files:
if remove_first_dir(path):
upload_script.append(
"cd %s" % remove_first_dir(path)
)
for file_path in files:
upload_script.append(
"put %s" % os.path.join(path, file_path)
)
upload_script.append("cd %s" % base_path)
upload_script.append("quit")
return upload_script
def run_script(script, server, base_path):
"""pass an upload script into cadaver run"""
file_handle, file_name = tempfile.mkstemp(dir='.', prefix='davput')
with open(file_name, 'w') as script_file:
script_file.write("cd %s\n" % base_path)
for line in script:
script_file.write('%s\n' % line)
subprocess.call(['cadaver', server, '-r', file_name])
os.unlink(file_name)
if __name__ == '__main__':
ARG_PARSER = argparse.ArgumentParser(
description='Create recursive upload Script'
)
ARG_PARSER.add_argument('directory', metavar='dir', type=str,
help='Directory to upload')
ARG_PARSER.add_argument('server', metavar='URL', type=str,
help='URL of webdav target directory')
ARGS_GIVEN = ARG_PARSER.parse_args()
BASE_PATH = urlparse.urlsplit(ARGS_GIVEN.server).path
if not os.path.isdir(ARGS_GIVEN.directory):
raise IOError('First argument must be directory name')
else:
OLD_CWD = os.getcwd()
SCRIPT = create_script(ARGS_GIVEN.directory, BASE_PATH)
os.chdir(ARGS_GIVEN.directory)
run_script(SCRIPT, ARGS_GIVEN.server, BASE_PATH)
os.chdir(OLD_CWD)