backup: Close file handles
--HG-- extra : amend_source : 8ee363811f104459f8346df1fccb1950c3ffc76cmaster
parent
71ca5624cb
commit
5ff79e49ba
16
backup.py
16
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))
|
||||
|
|
Loading…
Reference in New Issue