From 72c1532284bad2978c2a0d3731714dbdaa5dd281 Mon Sep 17 00:00:00 2001 From: brent s Date: Fri, 15 Sep 2017 10:24:06 -0400 Subject: [PATCH] error handling --- arch/repoclone.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/arch/repoclone.py b/arch/repoclone.py index 8e73f31..425af91 100755 --- a/arch/repoclone.py +++ b/arch/repoclone.py @@ -59,16 +59,24 @@ def sync(args): with open(args['lockfile'], 'w') as f: f.write(str(os.getpid())) with open(args['logfile'], 'a') as log: -# try: - # Disabled the try/except because otherwise it complains on broken symlinks. Gah! - subprocess.call(cmd, stdout = log, stderr = subprocess.STDOUT) + c = subprocess.run(cmd, stdout = log, stderr = subprocess.PIPE) now = int(datetime.datetime.timestamp(datetime.datetime.utcnow())) with open(os.path.join(args['destination'], 'lastsync'), 'w') as f: f.write(str(now) + '\n') os.remove(args['lockfile']) -# except: -# os.remove(args['lockfile']) -# exit('!! The rsync has failed. See {0} for more details. !!'.format(args['logfile'])) + # Only report errors at the end of the run if we aren't running in cron. Otherwise, log them. + errors = c.stderr.decode('utf-8').splitlines() + if os.isatty(sys.stdin.fileno()): + print('We encountered some errors:') + for e in errors: + if e.startswith('symlink has no referent: '): + print('Broken upstream symlink: {0}'.format(e.split()[1].replace('"', ''))) + else: + print(e) + else: + with open(args['logfile'], 'a') as f: + for e in errors: + f.write('{0}\n'.format(e)) return() def getDefaults():