From 142c12f2224351c76b68f7257ce1d0822769a53a Mon Sep 17 00:00:00 2001 From: MrBesen Date: Thu, 14 Jun 2018 12:57:22 +0200 Subject: [PATCH] minor improvements --- ownssync.py | 66 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/ownssync.py b/ownssync.py index 2559fb4..33e2cf2 100644 --- a/ownssync.py +++ b/ownssync.py @@ -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/')) \ No newline at end of file +#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.')