From 5ff79e49ba7f6dfa8f343eb9ac9aed4ca37b328f Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Wed, 3 Dec 2014 20:36:00 -0600 Subject: [PATCH] backup: Close file handles --HG-- extra : amend_source : 8ee363811f104459f8346df1fccb1950c3ffc76c --- backup.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/backup.py b/backup.py index 45cc40e..23de3eb 100755 --- a/backup.py +++ b/backup.py @@ -24,10 +24,11 @@ class Backup(object): def __init__(self, destination, config=None, pretend=False): self.config = configparser.ConfigParser() - print(config) - if not config: - config = open(self.default_config) - self.config.read_file(config) + if config: + self.config.read_file(config) + else: + with open(self.default_config) as config: + self.config.read_file(config) self.config.filename = config.name self.destination = destination self.pretend = pretend @@ -126,13 +127,16 @@ class Backup(object): def _run(self, *cmd): self.log.debug('Running command: {}'.format(' '.join(cmd))) + devnull = open(os.devnull) try: - subprocess.check_call(cmd, stdin=open(os.devnull), + subprocess.check_call(cmd, stdin=devnull, stdout=self.stdout, stderr=self.stderr) except (OSError, subprocess.CalledProcessError) as e: raise BackupError('Error executing rsync: {}'.format(e)) except KeyboardInterrupt: raise BackupError('rsync interrupted') + finally: + devnull.close() def _parse_args(): @@ -160,6 +164,8 @@ def _parse_args(): def main(): args = _parse_args() backup = Backup(args.destination, args.config, args.pretend) + if args.config: + args.config.close() if not args.quiet: print('Backing up to {} using configuration from {}'.format( args.destination, backup.config.filename))