minor improvements
This commit is contained in:
parent
65ef5db5f1
commit
142c12f222
66
ownssync.py
66
ownssync.py
|
@ -22,6 +22,11 @@ sync1 = '/home/yannis/Projects/ownsync/test/s1/'
|
||||||
sync2 = '/home/yannis/Projects/ownsync/test/s2/'
|
sync2 = '/home/yannis/Projects/ownsync/test/s2/'
|
||||||
dataname = '.sync'
|
dataname = '.sync'
|
||||||
|
|
||||||
|
def endwithslash(str):
|
||||||
|
if str[:-1] == '/':
|
||||||
|
return str
|
||||||
|
return str + '/'
|
||||||
|
|
||||||
|
|
||||||
def readfiletoarray(file):
|
def readfiletoarray(file):
|
||||||
try:
|
try:
|
||||||
|
@ -67,6 +72,16 @@ def rm(path):
|
||||||
os.rmdir(path)
|
os.rmdir(path)
|
||||||
|
|
||||||
|
|
||||||
|
def updatedata(path):
|
||||||
|
data = scandir(path)
|
||||||
|
f = open(path + dataname, 'w+')
|
||||||
|
for d in data:
|
||||||
|
d = d[len(path):]
|
||||||
|
if d != dataname:
|
||||||
|
f.write(d + '\n')
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
def syncnew(fro, to):
|
def syncnew(fro, to):
|
||||||
#read data
|
#read data
|
||||||
dataf = readfiletoarray(fro + dataname)
|
dataf = readfiletoarray(fro + dataname)
|
||||||
|
@ -78,7 +93,7 @@ def syncnew(fro, to):
|
||||||
pos = -1
|
pos = -1
|
||||||
for i in range(0, len(listffiles)):
|
for i in range(0, len(listffiles)):
|
||||||
listffiles[i] = listffiles[i][len(fro):]
|
listffiles[i] = listffiles[i][len(fro):]
|
||||||
if listffiles[i] == dataname:
|
if listffiles[i] == dataname: # remove .sync file from list
|
||||||
pos = i
|
pos = i
|
||||||
|
|
||||||
if pos > -1:
|
if pos > -1:
|
||||||
|
@ -86,7 +101,7 @@ def syncnew(fro, to):
|
||||||
|
|
||||||
for f in listffiles:
|
for f in listffiles:
|
||||||
print('syncing: ', f)
|
print('syncing: ', f)
|
||||||
if (not os.path.isfile(fro + f)) and os.path.exists(fro + f): # TODO: check if folder was deleted at target.
|
if (not os.path.isfile(fro + f)) and os.path.exists(fro + f): # TODO: check if folder was deleted at target.
|
||||||
if not os.path.exists(to + f): # seperate if to not enter elif
|
if not os.path.exists(to + f): # seperate if to not enter elif
|
||||||
# print('make dir:', to + f)
|
# print('make dir:', to + f)
|
||||||
#if is folder and exists in from then create folder in to.
|
#if is folder and exists in from then create folder in to.
|
||||||
|
@ -98,7 +113,7 @@ def syncnew(fro, to):
|
||||||
if not os.path.exists(folder):
|
if not os.path.exists(folder):
|
||||||
os.makedirs(folder)
|
os.makedirs(folder)
|
||||||
|
|
||||||
print( fro + f, '->', to + f)
|
print(fro + f, '->', to + f)
|
||||||
copy2(fro + f, to + f)
|
copy2(fro + f, to + f)
|
||||||
else:
|
else:
|
||||||
print('skipping', f)
|
print('skipping', f)
|
||||||
|
@ -110,17 +125,38 @@ def syncnew(fro, to):
|
||||||
print('delete file', to + f)
|
print('delete file', to + f)
|
||||||
# rm(fro + f)
|
# rm(fro + f)
|
||||||
|
|
||||||
#store to file
|
|
||||||
print('writing', len(listffiles), 'files')
|
|
||||||
f = open(fro + dataname, 'w+')
|
|
||||||
for line in listffiles:
|
|
||||||
f.write(line + '\n')
|
|
||||||
#f.writelines(listffiles)
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#MAIN
|
#MAIN
|
||||||
syncnew(sync1, sync2)
|
#setup
|
||||||
#syncnew(sync2, sync1)
|
sync1 = endwithslash(sync1)
|
||||||
#print(scandir('/home/yannis/Projects/telegramchatcopy/'))
|
sync2 = endwithslash(sync2)
|
||||||
|
|
||||||
|
sync1ex = True
|
||||||
|
if os.path.exists(sync1):
|
||||||
|
updatedata(sync1)
|
||||||
|
else:
|
||||||
|
sync1ex = False
|
||||||
|
|
||||||
|
sync2ex = True
|
||||||
|
if os.path.exists(sync2):
|
||||||
|
updatedata(sync2)
|
||||||
|
else:
|
||||||
|
sync2ex = False
|
||||||
|
|
||||||
|
if not sync1ex and not sync2ex:
|
||||||
|
print('both folders do not exists, exiting.')
|
||||||
|
else:
|
||||||
|
print('Location A: ', sync1, '\nLocation B: ', sync2)
|
||||||
|
if sync1ex:
|
||||||
|
print('Sync A -> B')
|
||||||
|
syncnew(sync1, sync2)
|
||||||
|
print('update changes.')
|
||||||
|
updatedata(sync2) # update changes
|
||||||
|
print('Done.')
|
||||||
|
if sync2ex:
|
||||||
|
print('Sync B -> A')
|
||||||
|
syncnew(sync2, sync1)
|
||||||
|
print('update changes.')
|
||||||
|
updatedata(sync1) # update changes
|
||||||
|
print('Done.')
|
||||||
|
print('Done.')
|
||||||
|
|
Loading…
Reference in New Issue