-
Notifications
You must be signed in to change notification settings - Fork 347
/
entropy_dialog.py
100 lines (80 loc) · 3.71 KB
/
entropy_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Dialog for waiting for enough random data entropy
#
# Copyright (C) 2013 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
import time
import math
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from pyanaconda.core.i18n import P_
from pyanaconda.core.constants import MAX_ENTROPY_WAIT
from pyanaconda.core.timer import Timer
from pyanaconda.ui.gui import GUIObject
from pyanaconda.core.async_utils import async_action_wait
from blivet.util import get_current_entropy
__all__ = ["run_entropy_dialog"]
# in milliseconds
LOOP_TIMEOUT = 250
@async_action_wait
def run_entropy_dialog(ksdata, desired_entropy):
"""Show dialog with waiting for entropy"""
dialog = EntropyDialog(ksdata, desired_entropy)
dialog.run()
return dialog.force_cont
class EntropyDialog(GUIObject):
builderObjects = ["entropyDialog"]
mainWidgetName = "entropyDialog"
uiFile = "spokes/lib/entropy_dialog.glade"
def __init__(self, data, desired_entropy):
super().__init__(data)
self._desired_entropy = desired_entropy
self._progress_bar = self.builder.get_object("progressBar")
self._terminate = False
self.force_cont = False
self._num_loops = 0
def run(self):
self.window.show_all()
# XXX: Is it better to rely on Gtk running the self._update_progress
# method every ~250msec or rely on NTP not changing system time right
# now and use time.time()?
self._num_loops = 0
Timer().timeout_msec(LOOP_TIMEOUT, self._update_progress)
Gtk.main()
self.window.destroy()
def _update_progress(self):
if self._terminate:
# give users time to realize they should stop typing
time.sleep(3)
Gtk.main_quit()
# remove the method from idle queue
return False
else:
self._num_loops += 1
current_entropy = get_current_entropy()
current_fraction = min(float(current_entropy) / self._desired_entropy, 1.0)
remaining = (MAX_ENTROPY_WAIT * 1000 - self._num_loops * LOOP_TIMEOUT) / 1000 / 60.0
self._progress_bar.set_fraction(current_fraction)
self._progress_bar.set_text("%(pct)d %% (%(rem)d %(min)s remaining)" % {"pct": (int(current_fraction * 100)),
"rem": math.ceil(remaining),
"min": P_("minute", "minutes", int(remaining))})
# if we have enough our time ran out, terminate the dialog, but let
# the progress_bar refresh in the main loop
self._terminate = (current_entropy >= self._desired_entropy) or (remaining <= 0)
self.force_cont = (remaining <= 0)
# keep updating
return True