Debian Codegraph @

src/devedeng/runner.py

raw
1 # Copyright 2014 (C) Raster Software Vigo (Sergio Costas)
2 #
3 # This file is part of DeVeDe-NG
4 #
5 # DeVeDe-NG is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 3
7 # as published by the Free Software Foundation.
8 #
9 # DeVeDe-NG is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>
16
17 from gi.repository import Gtk, GObject
18 import os
19 import devedeng.configuration_data
20 import devedeng.error
21 import devedeng.ask
22
23
24 class runner(GObject.GObject):
25
26 __gsignals__ = {'done': (GObject.SIGNAL_RUN_FIRST, None, (int,))}
27
28 def __init__(self, show_window=True):
29
30 GObject.GObject.__init__(self)
31
32 self.config = devedeng.configuration_data.configuration.get_config()
33
34 if (self.config.multicore > 0):
35 if self.config.cores < self.config.multicore:
36 self.cores = self.config.cores
37 else:
38 self.cores = self.config.multicore
39 else:
40 self.cores = self.config.cores - self.config.multicore
41 if (self.cores <= 0):
42 self.cores = 1
43
44 self.proc_list = []
45 self.running = 0
46
47 self.builder = Gtk.Builder()
48 self.builder.set_translation_domain(self.config.gettext_domain)
49
50 self.builder.add_from_file(os.path.join(
51 self.config.glade, "wprogress.ui"))
52 self.builder.connect_signals(self)
53 self.wprogress = self.builder.get_object("progress")
54 if show_window:
55 self.wprogress.show_all()
56 self.wtotal = self.builder.get_object("progress_total")
57
58 progress_frame = self.builder.get_object("progress_frame")
59 box = Gtk.Box(Gtk.Orientation.VERTICAL, 0)
60 progress_frame.add(box)
61 box.show()
62 self.progress_bars = []
63 self.used_progress_bars = []
64 for c in range(0, self.cores):
65 f = Gtk.Frame()
66 p = Gtk.ProgressBar()
67 p.set_orientation(Gtk.Orientation.HORIZONTAL)
68 p.set_show_text(True)
69 f.add(p)
70 # A frame, a progress bar, and the process running in that bar
71 self.progress_bars.append([f, p, None])
72 box.pack_start(f, True, True, 0)
73 box.set_orientation(Gtk.Orientation.VERTICAL)
74 self.total_processes = 0
75 self.error = False
76
77 def add_process(self, process):
78
79 if (self.proc_list.count(process) == 0):
80 self.proc_list.append(process)
81
82 for p in process.childs:
83 self.add_process(p)
84
85 self.total_processes = len(self.proc_list)
86
87 def on_cancel_clicked(self, b):
88
89 ask_w = devedeng.ask.ask_window()
90 retval = ask_w.run(_("Cancel the current job?"),
91 _("Cancel the current job?"))
92 if retval:
93 self.error = True
94 for element in self.proc_list:
95 element.cancel()
96 self.wprogress.destroy()
97 self.emit("done", 1) # there was an error
98 return
99
100 def run(self, clear_log=True):
101
102 if clear_log:
103 self.config.clear_log()
104
105 for element in self.proc_list:
106 # each element has three items:
107 # * the process object
108 # * the list of dependencies, or None if there are no more dependencies
109 # * the progress bar being used by this process
110 if (element.dependencies is None) and (element.progress_bar is None):
111 element.connect("ended", self.process_ended)
112 element.run(self.progress_bars[0])
113 element.progress_bar = self.progress_bars[0]
114 self.used_progress_bars.append(self.progress_bars[0])
115 if (len(self.progress_bars) > 1):
116 self.progress_bars = self.progress_bars[1:]
117 else:
118 self.progress_bars = []
119 break
120 self.wtotal.set_text(
121 str(self.total_processes - len(self.proc_list)) + "/" + str(self.total_processes))
122 self.wtotal.set_fraction(
123 (float(self.total_processes - len(self.proc_list))) / (float(self.total_processes)))
124
125 def process_ended(self, process, retval):
126
127 if self.error:
128 return
129
130 if retval != 0:
131 self.error = True
132 for element in self.proc_list:
133 element.cancel()
134 self.wprogress.destroy()
135 devedeng.error.error_window()
136 self.emit("done", 1) # there was an error
137 return
138
139 # move the progress bar used by this process to the list of available
140 # progress bars
141 tmp = []
142 for e in self.used_progress_bars:
143 if (process.progress_bar == e):
144 self.progress_bars.append(e)
145 e[0].hide()
146 else:
147 tmp.append(e)
148 self.used_progress_bars = tmp
149
150 # remove this process from the list of processes, and remove it from
151 # the dependencies in other processes
152 tmp = []
153 for e in self.proc_list:
154 if (e != process):
155 tmp.append(e)
156 e.remove_dependency(process)
157 self.proc_list = tmp
158
159 # launch a new process
160 if (len(self.proc_list) != 0):
161 self.run(False)
162 else:
163 self.wprogress.destroy()
164 self.emit("done", 0) # no error