-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_tool.py
More file actions
55 lines (43 loc) · 1.46 KB
/
upload_tool.py
File metadata and controls
55 lines (43 loc) · 1.46 KB
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
import pysftp
import random
import os
def recursive_upload(server, local, remote) -> None:
for name in os.listdir(local):
rpath = remote + "/" + name
lpath = os.path.join(local, name)
if not os.path.isfile(lpath):
try:
server.mkdir(rpath)
except OSError:
pass
recursive_upload(server, lpath, rpath)
else:
server.put(lpath, rpath)
def recursive_remove(server, rpath) -> None:
file_list = server.listdir(rpath)
for name in file_list:
current_path = os.path.join(rpath, name)
try:
server.remove(current_path)
except IOError:
recursive_remove(server, current_path)
server.rmdir(rpath)
def upload_all(HOSTNAME, USERNAME, DRIFT_KEY, LOCAL_DIR):
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
srv = pysftp.Connection(host=HOSTNAME, username=USERNAME, password=DRIFT_KEY, log="pysftp.log", cnopts=cnopts)
new_folder = "tiles" + str(random.randrange(1,100))
srv.mkdir(new_folder, mode=774)
with srv.cd(new_folder):
current_dir = srv.pwd
print(srv.pwd)
local_dir = LOCAL_DIR
recursive_upload(srv, local_dir, current_dir)
old_tiles = "tiles"
with srv.cd(old_tiles):
current_dir = srv.pwd
print(current_dir)
recursive_remove(srv, current_dir)
srv.rename(new_folder, "tiles")
current_dir = srv.pwd
srv.close()