1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-03-20 02:45:42 +00:00

implement and use context manager for Cache, partial fix for #285

also: make check in Lock.close more precise, check for "is not None".

note: a lot of blocks were just indented to be under the "with" statement,
in one case a block had to be moved into a function.
This commit is contained in:
Thomas Waldmann 2016-01-17 01:09:13 +01:00
parent 08e7c546ba
commit 22f218baef
2 changed files with 134 additions and 127 deletions

View file

@ -97,7 +97,8 @@ class Archiver:
manifest.key = key
manifest.write()
repository.commit()
Cache(repository, key, manifest, warn_if_unencrypted=False)
with Cache(repository, key, manifest, warn_if_unencrypted=False):
pass
return self.exit_code
def do_check(self, args):
@ -128,23 +129,7 @@ class Archiver:
def do_create(self, args):
"""Create new archive"""
self.output_filter = args.output_filter
self.output_list = args.output_list
dry_run = args.dry_run
t0 = datetime.now()
if not dry_run:
repository = self.open_repository(args, exclusive=True)
manifest, key = Manifest.load(repository)
compr_args = dict(buffer=COMPR_BUFFER)
compr_args.update(args.compression)
key.compressor = Compressor(**compr_args)
cache = Cache(repository, key, manifest, do_files=args.cache_files, lock_wait=self.lock_wait)
archive = Archive(repository, key, manifest, args.location.archive, cache=cache,
create=True, checkpoint_interval=args.checkpoint_interval,
numeric_owner=args.numeric_owner, progress=args.progress,
chunker_params=args.chunker_params, start=t0)
else:
archive = cache = None
def create_inner(archive, cache):
# Add cache dir to inode_skip list
skip_inodes = set()
try:
@ -196,6 +181,25 @@ class Archiver:
str(archive.stats),
str(cache),
DASHES)
self.output_filter = args.output_filter
self.output_list = args.output_list
dry_run = args.dry_run
t0 = datetime.now()
if not dry_run:
repository = self.open_repository(args, exclusive=True)
manifest, key = Manifest.load(repository)
compr_args = dict(buffer=COMPR_BUFFER)
compr_args.update(args.compression)
key.compressor = Compressor(**compr_args)
with Cache(repository, key, manifest, do_files=args.cache_files, lock_wait=self.lock_wait) as cache:
archive = Archive(repository, key, manifest, args.location.archive, cache=cache,
create=True, checkpoint_interval=args.checkpoint_interval,
numeric_owner=args.numeric_owner, progress=args.progress,
chunker_params=args.chunker_params, start=t0)
create_inner(archive, cache)
else:
create_inner(None, None)
return self.exit_code
def _process(self, archive, cache, excludes, exclude_caches, exclude_if_present,
@ -322,7 +326,7 @@ class Archiver:
"""Rename an existing archive"""
repository = self.open_repository(args, exclusive=True)
manifest, key = Manifest.load(repository)
cache = Cache(repository, key, manifest, lock_wait=self.lock_wait)
with Cache(repository, key, manifest, lock_wait=self.lock_wait) as cache:
archive = Archive(repository, key, manifest, args.location.archive, cache=cache)
archive.rename(args.name)
manifest.write()
@ -334,7 +338,7 @@ class Archiver:
"""Delete an existing repository or archive"""
repository = self.open_repository(args, exclusive=True)
manifest, key = Manifest.load(repository)
cache = Cache(repository, key, manifest, do_files=args.cache_files, lock_wait=self.lock_wait)
with Cache(repository, key, manifest, do_files=args.cache_files, lock_wait=self.lock_wait) as cache:
if args.location.archive:
archive = Archive(repository, key, manifest, args.location.archive, cache=cache)
stats = Statistics()
@ -444,7 +448,7 @@ class Archiver:
"""Show archive details such as disk space used"""
repository = self.open_repository(args)
manifest, key = Manifest.load(repository)
cache = Cache(repository, key, manifest, do_files=args.cache_files, lock_wait=self.lock_wait)
with Cache(repository, key, manifest, do_files=args.cache_files, lock_wait=self.lock_wait) as cache:
archive = Archive(repository, key, manifest, args.location.archive, cache=cache)
stats = archive.calc_stats(cache)
print('Name:', archive.name)
@ -463,7 +467,6 @@ class Archiver:
"""Prune repository archives according to specified rules"""
repository = self.open_repository(args, exclusive=True)
manifest, key = Manifest.load(repository)
cache = Cache(repository, key, manifest, do_files=args.cache_files, lock_wait=self.lock_wait)
archives = manifest.list_archive_infos(sort_by='ts', reverse=True) # just a ArchiveInfo list
if args.hourly + args.daily + args.weekly + args.monthly + args.yearly == 0 and args.within is None:
self.print_error('At least one of the "within", "keep-hourly", "keep-daily", "keep-weekly", '
@ -488,6 +491,7 @@ class Archiver:
keep.sort(key=attrgetter('ts'), reverse=True)
to_delete = [a for a in archives if a not in keep]
stats = Statistics()
with Cache(repository, key, manifest, do_files=args.cache_files, lock_wait=self.lock_wait) as cache:
for archive in keep:
logger.info('Keeping archive: %s' % format_archive(archive))
for archive in to_delete:

View file

@ -78,7 +78,10 @@ class Cache:
self.sync()
self.commit()
def __del__(self):
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def __str__(self):
@ -149,7 +152,7 @@ Chunk index: {0.total_unique_chunks:20d} {0.total_chunks:20d}"""
self.rollback()
def close(self):
if self.lock:
if self.lock is not None:
self.lock.release()
self.lock = None