minor improvements

This commit is contained in:
MrBesen 2018-06-14 12:57:22 +02:00
parent 65ef5db5f1
commit 142c12f222
1 changed files with 51 additions and 15 deletions

View File

@ -22,6 +22,11 @@ sync1 = '/home/yannis/Projects/ownsync/test/s1/'
sync2 = '/home/yannis/Projects/ownsync/test/s2/'
dataname = '.sync'
def endwithslash(str):
if str[:-1] == '/':
return str
return str + '/'
def readfiletoarray(file):
try:
@ -67,6 +72,16 @@ def rm(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):
#read data
dataf = readfiletoarray(fro + dataname)
@ -78,7 +93,7 @@ def syncnew(fro, to):
pos = -1
for i in range(0, len(listffiles)):
listffiles[i] = listffiles[i][len(fro):]
if listffiles[i] == dataname:
if listffiles[i] == dataname: # remove .sync file from list
pos = i
if pos > -1:
@ -86,7 +101,7 @@ def syncnew(fro, to):
for f in listffiles:
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
# print('make dir:', to + f)
#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):
os.makedirs(folder)
print( fro + f, '->', to + f)
print(fro + f, '->', to + f)
copy2(fro + f, to + f)
else:
print('skipping', f)
@ -110,17 +125,38 @@ def syncnew(fro, to):
print('delete file', to + 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
syncnew(sync1, sync2)
#syncnew(sync2, sync1)
#print(scandir('/home/yannis/Projects/telegramchatcopy/'))
#setup
sync1 = endwithslash(sync1)
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.')