create: add --json option

This commit is contained in:
Marian Beermann 2017-02-23 11:54:57 +01:00
parent cc26bdf810
commit 7cbade2f8c
2 changed files with 38 additions and 8 deletions

View File

@ -68,6 +68,14 @@ class Statistics:
return "<{cls} object at {hash:#x} ({self.osize}, {self.csize}, {self.usize})>".format(
cls=type(self).__name__, hash=id(self), self=self)
def as_dict(self):
return {
'original_size': self.osize,
'compressed_size': self.csize,
'deduplicated_size': self.usize,
'nfiles': self.nfiles,
}
@property
def osize_fmt(self):
return format_file_size(self.osize)
@ -343,6 +351,19 @@ class Archive:
def duration_from_meta(self):
return format_timedelta(self.ts_end - self.ts)
def info(self):
return {
'name': self.name,
'id': self.fpr,
'start': format_time(to_localtime(self.start.replace(tzinfo=timezone.utc))),
'end': format_time(to_localtime(self.end.replace(tzinfo=timezone.utc))),
'duration': (self.end - self.start).total_seconds(),
'nfiles': self.stats.nfiles,
'limits': {
'max_archive_size': self.cache.chunks[self.id].csize / MAX_DATA_SIZE,
},
}
def __str__(self):
return '''\
Archive name: {0.name}

View File

@ -369,13 +369,20 @@ class Archiver:
if args.progress:
archive.stats.show_progress(final=True)
if args.stats:
log_multi(DASHES,
str(archive),
DASHES,
STATS_HEADER,
str(archive.stats),
str(cache),
DASHES, logger=logging.getLogger('borg.output.stats'))
if args.json:
print_as_json({
'cache_stats': cache.stats(),
'stats': archive.stats.as_dict(),
'archive': archive.info(),
})
else:
log_multi(DASHES,
str(archive),
DASHES,
STATS_HEADER,
str(archive.stats),
str(cache),
DASHES, logger=logging.getLogger('borg.output.stats'))
self.output_filter = args.output_filter
self.output_list = args.output_list
@ -1027,7 +1034,7 @@ class Archiver:
}
if args.json:
info['cache-stats'] = cache.stats()
info['cache_stats'] = cache.stats()
print_as_json(info)
else:
print(textwrap.dedent("""
@ -2174,6 +2181,8 @@ class Archiver:
help='output verbose list of items (files, dirs, ...)')
subparser.add_argument('--filter', dest='output_filter', metavar='STATUSCHARS',
help='only display items with the given status characters')
subparser.add_argument('--json', action='store_true',
help='output stats as JSON')
exclude_group = subparser.add_argument_group('Exclusion options')
exclude_group.add_argument('-e', '--exclude', dest='patterns',