cli: output JSON error object in MI mode
[deliverable/lttng-analyses.git] / lttnganalyses / cli / progressbar.py
CommitLineData
4ed24f86
JD
1# The MIT License (MIT)
2#
a3fa57c0 3# Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com>
4ed24f86
JD
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
418527ca
JD
23import os
24import sys
25
26try:
27 from progressbar import ETA, Bar, Percentage, ProgressBar
28 progressbar_available = True
29except ImportError:
30 progressbar_available = False
31
32# approximation for the progress bar
33BYTES_PER_EVENT = 30
34
35
b6d9132b 36def get_folder_size(folder):
418527ca
JD
37 total_size = os.path.getsize(folder)
38 for item in os.listdir(folder):
39 itempath = os.path.join(folder, item)
40 if os.path.isfile(itempath):
41 total_size += os.path.getsize(itempath)
42 elif os.path.isdir(itempath):
b6d9132b 43 total_size += get_folder_size(itempath)
418527ca
JD
44 return total_size
45
46
47def progressbar_setup(obj):
b6d9132b 48 if obj._args.no_progress:
418527ca
JD
49 obj.pbar = None
50 return
51
52 if progressbar_available:
b6d9132b 53 size = get_folder_size(obj._args.path)
418527ca
JD
54 widgets = ['Processing the trace: ', Percentage(), ' ',
55 Bar(marker='#', left='[', right=']'),
56 ' ', ETA(), ' '] # see docs for other options
57 obj.pbar = ProgressBar(widgets=widgets,
58 maxval=size/BYTES_PER_EVENT)
59 obj.pbar.start()
60 else:
73b71522
AB
61 print('Warning: progressbar module not available, '
62 'using --no-progress.', file=sys.stderr)
b6d9132b 63 obj._args.no_progress = True
418527ca
JD
64 obj.pbar = None
65 obj.event_count = 0
66
67
68def progressbar_update(obj):
b6d9132b 69 if obj._args.no_progress or obj.pbar is None:
418527ca 70 return
b6d9132b 71
418527ca
JD
72 try:
73 obj.pbar.update(obj.event_count)
74 except ValueError:
75 pass
76 obj.event_count += 1
77
78
79def progressbar_finish(obj):
b6d9132b 80 if obj._args.no_progress:
418527ca
JD
81 return
82 obj.pbar.finish()
This page took 0.029386 seconds and 5 git commands to generate.