diff options
Diffstat (limited to 'bitbake/lib/bb/progress.py')
-rw-r--r-- | bitbake/lib/bb/progress.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/bitbake/lib/bb/progress.py b/bitbake/lib/bb/progress.py new file mode 100644 index 0000000000..bab8e9465d --- /dev/null +++ b/bitbake/lib/bb/progress.py | |||
@@ -0,0 +1,86 @@ | |||
1 | """ | ||
2 | BitBake progress handling code | ||
3 | """ | ||
4 | |||
5 | # Copyright (C) 2016 Intel Corporation | ||
6 | # | ||
7 | # This program is free software; you can redistribute it and/or modify | ||
8 | # it under the terms of the GNU General Public License version 2 as | ||
9 | # published by the Free Software Foundation. | ||
10 | # | ||
11 | # This program is distributed in the hope that it will be useful, | ||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | # GNU General Public License for more details. | ||
15 | # | ||
16 | # You should have received a copy of the GNU General Public License along | ||
17 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | |||
20 | import sys | ||
21 | import re | ||
22 | import time | ||
23 | import bb.event | ||
24 | import bb.build | ||
25 | |||
26 | class ProgressHandler(object): | ||
27 | """ | ||
28 | Base class that can pretend to be a file object well enough to be | ||
29 | used to build objects to intercept console output and determine the | ||
30 | progress of some operation. | ||
31 | """ | ||
32 | def __init__(self, d, outfile=None): | ||
33 | self._progress = 0 | ||
34 | self._data = d | ||
35 | self._lastevent = 0 | ||
36 | if outfile: | ||
37 | self._outfile = outfile | ||
38 | else: | ||
39 | self._outfile = sys.stdout | ||
40 | |||
41 | def _fire_progress(self, taskprogress, rate=None): | ||
42 | """Internal function to fire the progress event""" | ||
43 | bb.event.fire(bb.build.TaskProgress(taskprogress, rate), self._data) | ||
44 | |||
45 | def write(self, string): | ||
46 | self._outfile.write(string) | ||
47 | |||
48 | def flush(self): | ||
49 | self._outfile.flush() | ||
50 | |||
51 | def update(self, progress, rate=None): | ||
52 | ts = time.time() | ||
53 | if progress > 100: | ||
54 | progress = 100 | ||
55 | if progress != self._progress or self._lastevent + 1 < ts: | ||
56 | self._fire_progress(progress, rate) | ||
57 | self._lastevent = ts | ||
58 | self._progress = progress | ||
59 | |||
60 | class BasicProgressHandler(ProgressHandler): | ||
61 | def __init__(self, d, regex=r'(\d+)%', outfile=None): | ||
62 | super(BasicProgressHandler, self).__init__(d, outfile) | ||
63 | self._regex = re.compile(regex) | ||
64 | # Send an initial progress event so the bar gets shown | ||
65 | self._fire_progress(0) | ||
66 | |||
67 | def write(self, string): | ||
68 | percs = self._regex.findall(string) | ||
69 | if percs: | ||
70 | progress = int(percs[-1]) | ||
71 | self.update(progress) | ||
72 | super(BasicProgressHandler, self).write(string) | ||
73 | |||
74 | class OutOfProgressHandler(ProgressHandler): | ||
75 | def __init__(self, d, regex, outfile=None): | ||
76 | super(OutOfProgressHandler, self).__init__(d, outfile) | ||
77 | self._regex = re.compile(regex) | ||
78 | # Send an initial progress event so the bar gets shown | ||
79 | self._fire_progress(0) | ||
80 | |||
81 | def write(self, string): | ||
82 | nums = self._regex.findall(string) | ||
83 | if nums: | ||
84 | progress = (float(nums[-1][0]) / float(nums[-1][1])) * 100 | ||
85 | self.update(progress) | ||
86 | super(OutOfProgressHandler, self).write(string) | ||