-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncdevice.py
More file actions
103 lines (89 loc) · 2.89 KB
/
syncdevice.py
File metadata and controls
103 lines (89 loc) · 2.89 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
import glob
import os
import subprocess
from syncfile import SyncFile
import sys
# Return the IP address if it can be turned into one,
# otherwise return None.
def is_ip_addr(dst):
assert type(dst) == str
if len(dst) <= 3 and dst.isdigit(): # It's a single number
return "192.168.245." + dst
if dst.count(".") != 3:
return None
for i in dst:
if not (i.isdigit() or i == "."):
return None
return dst # It's a valid entire IP address
def is_mounted(ip):
procmounts = open("/proc/mounts", "r")
for i in procmounts:
i = i.strip()
fields = i.split()
mount_src = "root@" + ip + ":/"
if mount_src == fields[0]:
procmounts.close()
return fields[1]
procmounts.close()
return None
def create_mountpoint():
n = 0
while True:
mp = os.path.expanduser("~/mp" + str(n))
if os.path.exists(mp):
if os.path.isdir(mp) and not os.path.ismount(mp):
if not os.listdir(mp): # Check it's empty
return mp
else:
print("Does not exist:", mp)
try:
os.makedirs(mp)
except FileExistsError:
subprocess.call(["fusermount", "-u", mp])
return mp
# See if the mountpoint has been mounted but then
# expired. When this happens there is a "Transport
# endpoint is not connected" error.
def endpoint_not_connected(mp):
out = subprocess.check_output(["ls", mp])
errmsg = "ls: cannot access " + mp + ": Transport endpoint is not connected"
if out.strip() == errmsg:
return True
return False
def mount(ip):
mp = is_mounted(ip)
if mp != None: # It's already mounted
print("%s already mounted at %s" % (ip, mp))
if endpoint_not_connected(mp):
print("But endpoint not connected: Unmounting.")
subprocess.call(["fusermount", "-u", mp])
else:
return mp
mp = create_mountpoint()
mnt = ["sshfs", "root@" + ip + ":/", mp]
mnt += ["-o", "ConnectTimeout=20"]
print("Mounting", ip, "on", mp)
retcode = subprocess.call(mnt)
print("Done.")
if retcode != 0:
print("Error: Could not mount", ip, "on", mp)
os.removedirs(mp)
sys.exit(retcode)
return mp
# Check that dst exists and is a directory, or is an
# IP address, in which case mount it using sshfs
def check_exists_or_ip(dst):
if not os.path.exists(dst):
ip = is_ip_addr(dst)
if ip != None:
mp = mount(ip)
dst = os.path.join(mp, "storage/emulated/legacy")
else:
print("Error: " + dst + " does not exist.")
sys.exit(1)
else: # The path exists - check it's a directory
if not os.path.isdir(dst):
print("Error: " + dst + " exists but is not a directory")
sys.exit(1)
return dst