From 04bd9e23a2a98604592e744f6374718dc0ed098c Mon Sep 17 00:00:00 2001 From: Elisa Sohier Date: Thu, 1 Aug 2019 16:15:35 +0200 Subject: Better error handling --- autosync/__init__.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'autosync/__init__.py') diff --git a/autosync/__init__.py b/autosync/__init__.py index 6415880..ec688f8 100644 --- a/autosync/__init__.py +++ b/autosync/__init__.py @@ -29,10 +29,23 @@ def getTab(name="", elements=[]): else: tabFile = os.path.join(os.environ["HOME"], ".autoSync.{}.tab".format(name)) - with open(tabFile, "r") as f: - for line in f.readlines(): - name, src, dst = filter(lambda i: len(i) > 0, line.rstrip("\n").split("\t")) - tabData[name] = (os.path.expandvars(os.path.expanduser(src)), os.path.expandvars(os.path.expanduser(dst))) + try: + with open(tabFile, "r") as f: + for line in f.readlines(): + name, src, dst = filter(lambda i: len(i) > 0, line.rstrip("\n").split("\t")) + tabData[name] = (os.path.expandvars(os.path.expanduser(src)), os.path.expandvars(os.path.expanduser(dst))) + except FileNotFoundError: + print("The tabfile @{} (path '{}') could not be read.".format( + name, + tabFile + )) + return [] + except PermissionError: + print("The tabfile @{} (path '{}') is not readable to me.".format( + name, + tabFile + )) + return [] if len(elements) < 1: elements = list(tabData.keys()) @@ -54,7 +67,13 @@ def getTimestamp(source, dest): f.write("0") with open("/tmp/source.ts", "r") as sourceF, open("/tmp/dest.ts", "r") as destF: - return (float(sourceF.read()), float(destF.read())) + tsTuple = [] + for f in [sourceF, destF]: + try: + tsTuple.append(float(f.read())) + except ValueError: + tsTuple.append(0) + return tsTuple def run(): global server @@ -104,6 +123,7 @@ def run(): ignoreFile = None if tabname == "": tabname = "default" + status = None if srcT >= dstT: msg("[{} @ {}] sending data to destination".format(name, tabname)) if not server: @@ -111,15 +131,18 @@ def run(): f.write(str(time.time())) else: print("\033[2;3;37mServer mode, won't update timestamp\033[0m") - sync(src, dst, ignoreFile, True) + status = sync(src, dst, ignoreFile, True) else: if server: print("\033[2;3;37mServer mode, won't update timestamp\033[0m") msg("[{} @ {}] gathering data from destination".format(name, tabname)) - sync(dst, src, ignoreFile, True) - if not server: + status = sync(dst, src, ignoreFile, True) + if not server and status == 0: with open(os.path.join(src, ".lastsync"), "w") as f: f.write(str(time.time())) + if status != 0 and status is not None: + print("Error while syncing target {} @ {}, aborting".format(name, tabname)) + return if os.path.exists("/tmp/source.ts"): os.unlink("/tmp/source.ts") -- cgit v1.2.3