From f01a184b2694d7aa2bc153641b44f89ffcf47687 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 31 Mar 2015 15:12:34 +0100 Subject: Remove hash file on subsequent error If an error occurs after the hash has been saved to a file then delete the file so that it's possible to try again Also, fix file descriptor leaks. Change-Id: Ibe3edab5310357b23c64d8afccab53bf88db888c --- firehose/plugin/firehose_plugin.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/firehose/plugin/firehose_plugin.py b/firehose/plugin/firehose_plugin.py index 200467e..dbce376 100644 --- a/firehose/plugin/firehose_plugin.py +++ b/firehose/plugin/firehose_plugin.py @@ -39,6 +39,8 @@ def firehose_git(app): app.runcmd(["git", "config", "--global", "user.email", email]) class FirehosePlugin(cliapp.Plugin): + sha_filename="" + def enable(self): logging.basicConfig(filename='/var/log/firehose.log',level=logging.INFO) logging.info("Firehose plugin enabled") @@ -138,15 +140,17 @@ class FirehosePlugin(cliapp.Plugin): def log_sha(self, name): sha = self.app.runcmd(['git', 'rev-parse', 'HEAD'], cwd=self.gitpath).strip() - filename = '/var/lib/firehose/'+name+'_log.json' - if (os.path.exists(os.path.abspath(filename))): - log = open(filename).read() - if sha in log: - logging.info('Everything up-to-date') - print 'Everything up-to-date' - exit(0) - log = open(filename, 'w') - log.write('{\"sha\":\"'+sha+'\"}') + self.sha_filename = '/var/lib/firehose/'+name+'_log.json' + if (os.path.exists(os.path.abspath(self.sha_filename))): + with open(self.sha_filename) as f: + log = f.read() + if sha in log: + logging.info('Everything up-to-date') + print 'Everything up-to-date' + exit(0) + # Save the hash to a file + with open(self.sha_filename, 'w') as log: + log.write('{\"sha\":\"'+sha+'\"}') logging.info('log_sha: wrote {\"sha\":\"'+sha+'\"}') def insert_githook(self): @@ -194,6 +198,9 @@ class FirehosePlugin(cliapp.Plugin): return False self.morphs.traverse_specs(process_spec, wanted_spec) if len(urls) != 1: + # Remove the previously recorded hash + if self.sha_filename != "": + os.remove(self.sha_filename) raise cliapp.AppException( "Woah! expected 1 chunk matching %s:%s (got %d)" % ( stratum, chunk, len(urls))) -- cgit v1.2.1