-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·138 lines (114 loc) · 3.76 KB
/
main.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python2.6
# -*- coding: iso-8859-15 -*-
from constants import *
from fs import *
from os.path import join
from utils import format
from transfer import scp
from threading import Thread
import settings as x
def hash_both(source, target):
def HashSource():
x.sourcehash = md5sum(source)
print '\t'+source+': '+x.sourcehash
thread1 = Thread( target=HashSource )
def HashTarget():
x.targethash = md5sum(target)
print '\t'+target+': '+x.targethash
thread2 = Thread( target=HashTarget )
thread1.start()
thread2.start()
thread1.join()
thread2.join()
def transfer_to_target(source, target):
print '--- '+source+' ---'
corresponding_target = join(target, fname(source))
print '\tTarget: '+corresponding_target
if isdir(source):
print '\tSource is a directory. Entering ...'
for item in listdir(source):
transfer_to_target(join(source, item), corresponding_target)
print '\tLeaving '+source+' ...'
if x.behavior in [smv, smerge]:
try:
rmdir(source)
except:
print 'FAILED TO REMOVE DIRECTORY'
return
elif isfile(source):
print '\tSource is a file.'
if not exists(corresponding_target):
print '\tCorresponding target doesn\'t exist.'
if not exists(target):
mkdir(target)
scp(source, target)
hash_both(source, corresponding_target)
if x.sourcehash == x.targethash and x.behavior in [smv, smerge]:
remove(source)
return
elif isdir(corresponding_target):
print 'Aborting: Unmergeable: Source File and Target Directory.'
return
elif isfile(corresponding_target):
print '\tTarget exists and is a file.'
sourcesize = getsize(source)
print '\t'+source+': '+format(sourcesize)
targetsize = getsize(corresponding_target)
print '\t'+corresponding_target+': '+format(targetsize)
if sourcesize == targetsize:
hash_both(source, corresponding_target)
if x.sourcehash != x.targethash:
if x.behavior != smerge:
print 'Aborting: Same filesize but content binary differs.'
return
else:
_source += '.'+x.sourcehash
print '\tDifferent file. Saving as '+_source+'.'
move(source, _source)
transfer_to_target(_source, target)
return
else:
print '\tSuccess: File complete. Content binary equal.'
if x.behavior in [smv, smerge]:
remove(source)
return
elif sourcesize > targetsize: # smaller
blocksize = 256*1024
pos = (targetsize / blocksize) * blocksize
def HashSource():
x.sourcehash = md5sum(source, end=targetsize)
print '\t'+source+' (partial): '+x.sourcehash
thread1 = Thread( target=HashSource )
def HashTarget():
x.targethash = md5sum(corresponding_target)
print '\t'+corresponding_target+': '+x.targethash
thread2 = Thread( target=HashTarget )
thread1.start()
thread2.start()
thread1.join()
thread2.join()
if x.sourcehash != x.targethash:
print 'Aborting: Partial content binary differs.'
return
else:
from utils import run
from fsremote import login, path
if pos == 0:
print '\tRestarting transfer ...'
remove(corresponding_target)
scp(source, target)
hash_both(source, corresponding_target)
if x.sourcehash == x.targethash and x.behavior in [smv, smerge]:
remove(source)
return
else:
print '\tContinuing transfer ...'
truncate(corresponding_target, pos)
run('dd if="'+source+'" bs='+str(blocksize)+' skip='+str(pos/blocksize)+' | ssh -C '+login(target)+' dd of="'+path(corresponding_target)+'" bs='+str(blocksize)+' seek='+str(pos/blocksize))
hash_both(source, corresponding_target)
if x.sourcehash == x.targethash and x.behavior in [smv, smerge]:
remove(source)
return
elif sourcesize < targetsize: # bigger
print 'Aborting: Target file is bigger.'
return