backup: Close file handles

--HG--
extra : amend_source : 8ee363811f104459f8346df1fccb1950c3ffc76c
master
Dustin C. Hatch 2014-12-03 20:36:00 -06:00
parent 71ca5624cb
commit 5ff79e49ba
1 changed files with 11 additions and 5 deletions

View File

@ -24,9 +24,10 @@ 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)
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
@ -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))