-
Notifications
You must be signed in to change notification settings - Fork 1
/
pySync.py
executable file
·158 lines (141 loc) · 4.92 KB
/
pySync.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/local/bin/python3
import sys, shutil, os, time, configparser
from os.path import *
ignoreFiles = ("Thumbs.db",".DS_Store")
#this feature is not yet implemented
ignorePaths = []
if os.name == 'nt':
#msvcrt can't function correctly in IDLE
if 'idlelib.run' in sys.modules:
print("Please don't run this script in IDLE.")
sys.exit(0)
import msvcrt
def flush_input(str):
while msvcrt.kbhit():
ch = msvcrt.getch()
if ch == '\xff':
print("msvcrt is broken, this is weird.")
sys.exit(0)
return input(str)
else:
import select
def flush_input(str):
while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0])>0:
os.read(sys.stdin.fileno(), 4096)
return input(str)
def compare(fa,fb):
if isfile(fa)==isfile(fb):
if isdir(fa):
walktree(fa,fb)
elif isfile(fa):
if getsize(fa)!=getsize(fb) or int(getmtime(fa))!=int(getmtime(fb)):
print(fa,': size=',getsize(fa),'mtime=',time.asctime(time.localtime(getmtime(fa))))
print(fb,': size=',getsize(fb),'mtime=',time.asctime(time.localtime(getmtime(fb))))
if getmtime(fa)>getmtime(fb):
act='>'
else:
act='<'
s = flush_input('What to do?(>,<,r,n)['+act+']')
if len(s)>0:
act=s[0]
if act=='>':
shutil.copy2(fa,fb)
elif act=='<':
shutil.copy2(fb,fa)
elif act=='r':
if isdir(fa):
shutil.rmtree(fa)
elif isfile(fa):
os.remove(fa)
else:
print('Remove: Skipping', fa)
if isdir(fb):
shutil.rmtree(fb)
elif isfile(fb):
os.remove(fb)
else:
print('Remove: Skipping', fb)
else:
print('Compare: Skipping non-dir and non-file',fa)
else:
print('Error:',fa,',',fb,'have different file type')
def copy(fa,fb):
s = flush_input('Copy '+fa+' to another side?(r,y,n)[y]')
if len(s)>0:
act=s[0]
else:
act='y'
if act =='y':
if isdir(fa):
shutil.copytree(fa,fb)
elif isfile(fa):
shutil.copy2(fa,fb)
else:
print('Copy: Skipping ',fa)
elif act =='r':
if isdir(fa):
shutil.rmtree(fa)
elif isfile(fa):
os.remove(fa)
else:
print('Remove: Skipping ',fa)
stoentry = []
tarentry = []
def walktree(source,target):
srclist = os.listdir(source)
tarlist = os.listdir(target)
if '!sync' in srclist: return
if '!sync' in tarlist: return
#files in source dir...
for f in srclist:
if f in ignoreFiles: continue
spath = join(source,f)
tpath = join(target,f)
if spath in ignorePaths: continue
if spath in stoentry:
#just in case target also have this one
if f in tarlist: del tarlist[tarlist.index(f)]
continue
#if also exists in target dir
if f in tarlist:
del tarlist[tarlist.index(f)]
compare(spath,tpath)
#exists in source dir only
else:
copy(spath,tpath)
#exists in target dir only
for f in tarlist:
if f in ignoreFiles: continue
spath = join(source,f)
tpath = join(target,f)
if tpath in ignorePaths: continue
if tpath in tarentry:
continue
copy(tpath,spath)
if __name__ == '__main__':
stoconf = configparser.RawConfigParser()
tarconf = configparser.RawConfigParser()
stoconf.read("pySync.ini")
tarconf.read(expanduser("~/.pysync"))
stoname = stoconf.sections()[0]
tarname = tarconf.sections()[0]
#calculate storage's base folder
if stoconf.has_option(stoname,'BASE'):
stobase=abspath(stoconf.get(stoname,'BASE'))
stoconf.remove_option(stoname,'BASE')
else: stobase=os.getcwd()
#same, for target's base folder
if tarconf.has_option(tarname,'BASE'):
tarbase=abspath(tarconf.get(tarname,'BASE'))
tarconf.remove_option(tarname,'BASE')
else: tarbase=expanduser('~/')
print("Syncing between",stoname,"and",tarname)
sto_content = {x:realpath(join(stobase,stoconf.get(stoname,x))) for x in stoconf.options(stoname)}
tar_content = {x:realpath(join(tarbase,tarconf.get(tarname,x))) for x in tarconf.options(tarname)}
stoentry = [sto_content[x] for x in sto_content]
tarentry = [tar_content[x] for x in tar_content]
for folder in sto_content:
if folder in tar_content:
print('Processing',folder)
walktree(sto_content[folder],tar_content[folder])
print("Done.")