OSDN Git Service

[fix] : Lock Screen command in power menu on i3
[alterlinux/alterlinux.git] / build-wizard.py
1 #!/usr/bin/env python3
2 #== Copyright ==#
3 # (C) 2019-2020 Fascode Network
4
5 #== Import ==#
6 import gi, subprocess, sys, threading
7 gi.require_version("Gtk", "3.0")
8 from gi.repository import GLib, Gtk, GObject
9
10 #== Main Window ==#
11 class MainWindow(Gtk.Window):
12     #= Fuction =#
13     def __init__(self):
14         def yn(name):
15             y = Gtk.RadioButton.new_with_label_from_widget(None, "Yes")
16             y.connect("toggled", self.on_button_toggled, name, True)
17             n = Gtk.RadioButton.new_with_mnemonic_from_widget(y, "No")
18             n.connect("toggled", self.on_button_toggled, name, False)
19             return y, n
20
21         def combobox(name, list):
22             list_store = Gtk.ListStore(int, str)
23             for num in range(len(list)):
24                 list_store.append([num, list[num]])
25
26             combo = Gtk.ComboBox.new_with_model_and_entry(list_store)
27             combo.connect("changed", self.on_combo_changed, name)
28             combo.set_entry_text_column(1)
29             combo.set_active(0)
30             return combo
31
32         #-- Create Window --#
33         Gtk.Window.__init__(self, title="build wizard")
34
35         #-- Define --#
36         self.bool = {"plymouth": True, "japanese": True}
37         self.selected = {"build": "native"}
38
39         #-- Sub Layout 1 --#
40         #- Create -#
41         sub_layout1 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5)
42
43         #- Labels -#
44         label1 = Gtk.Label("select desktop environment")
45         label2 = Gtk.Label("select kernel")
46
47         #- Comboboxes -#
48         de = combobox("de", ["xfce", "plasma", "lxde"])
49         kernel= combobox("kernel", ["zen", "linux", "lts", "lqx", "ck", "rt", "rt-lts", "xanmod-lts"])
50
51         #- Buttons & Add -#
52         for name in "plymouth", "japanese":
53             y, n = yn(name)
54             subs_layout = Gtk.Box(spacing=5)
55             subs_layout.pack_start(y, True, True, 30)
56             subs_layout.pack_start(n, True, True, 30)
57             label = Gtk.Label("Enable" + " " + name + "?")
58             sub_layout1.pack_start(label,       True, True, 5)
59             sub_layout1.pack_start(subs_layout, True, True, 5)
60
61         #- Add -#
62         for name in label1, de, label2, kernel:
63             sub_layout1.pack_start(name, True, True, 5)
64
65         #-- Sub Layout 2 --#
66         #- Create -#
67         sub_layout2 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5)
68
69         #- Labels -#
70         label1 = Gtk.Label("select compression")
71         label2 = Gtk.Label("user name")
72         label3 = Gtk.Label("password")
73
74         #- Combobox -#
75         comp = combobox("comp", ["zstd", "lzma", "lzo", "lz4", "xz", "gzip"])
76
77         #- Entrys -#
78         self.usr = Gtk.Entry()
79         self.usr.set_text("alter")
80         self.passwd = Gtk.Entry()
81         self.passwd.set_visibility(False)
82         self.passwd.set_text("alter")
83
84         #- Button -#
85         showpasswd = Gtk.ToggleButton("Show password")
86         showpasswd.connect("toggled", self.on_button_toggled, "1")
87
88         #- Add -#
89         for name in label1, comp, label2, self.usr, label3, self.passwd, showpasswd:
90             sub_layout2.pack_start(name, True, True, 5)
91
92         #-- Sub Layout 3 --#
93         #- Create -#
94         sub_layout3 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5)
95
96         #- Label -#
97         label = Gtk.Label("select build type")
98
99         #- Button -#
100         button1 = Gtk.RadioButton.new_with_label_from_widget(None, "native")
101         button1.connect("toggled", self.select_build, "native")
102
103         #- Add -#
104         sub_layout3.pack_start(label, True, True, 5)
105         sub_layout3.pack_start(button1, True, True, 5)
106
107         #- Buttons & Add -#
108         for name in "docker", "ssh":
109             button = Gtk.RadioButton.new_with_mnemonic_from_widget(button1, name)
110             button.connect("toggled", self.select_build, name)
111             sub_layout3.pack_start(button, True, True, 5)
112
113         #-- Grid --#
114         #- Create -#
115         grid = Gtk.Grid(column_spacing=10, row_spacing=10)
116
117         #- Progressbar -#
118         self.progressbar = Gtk.ProgressBar()
119
120         #- Button -#
121         build = Gtk.Button("build")
122         build.connect("clicked", self.on_click_build)
123
124         #- Add -#
125         grid.attach(sub_layout1,      0, 0, 1, 1)
126         grid.attach(sub_layout2,      1, 0, 1, 1)
127         grid.attach(sub_layout3,      2, 0, 1, 1)
128         grid.attach(self.progressbar, 0, 1, 2, 1)
129         grid.attach(build,            2, 1, 1, 1)
130
131         #-- Layout --#
132         #- Create -#
133         layout = Gtk.Box(spacing=10)
134
135         #- Add -#
136         layout.pack_start(grid, True, True, 10)
137
138         #-- Main Layout --#
139         #- Create -#
140         main_layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
141
142         #- Add -#
143         main_layout.pack_start(layout, True, True, 10)
144
145         #-- Add Main Window --#
146         self.add(main_layout)
147
148     def on_button_toggled(self, button, name):
149         if button.get_active():
150             self.passwd.set_visibility(True)
151         else:
152             self.passwd.set_visibility(False)
153
154     def on_combo_changed(self, combo, name):
155         self.selected[name] = combo.get_model()[combo.get_active_iter()][1]
156
157     def select_build(self, button, name):
158         self.selected["build"] = name
159
160     def on_click_build(self, button):
161         cmd = ["sudo", "./build.sh"]
162
163         if self.selected["build"] == "native":
164             if self.bool["plymouth"]:
165                 cmd.append("-b")
166
167             if self.bool["japanese"]:
168                 cmd.append("-j")
169
170             for name in "-k", self.selected["kernel"], "-c", self.selected["comp"], "-u", self.usr.get_text(), "-p", self.passwd.get_text():
171                 cmd.append(name)
172
173             self.run_cmd(cmd)
174         else:
175             self.progressbar.set_show_text(1)
176             self.progressbar.set_text("not supported!")
177             print("not supported!")
178
179     def run_cmd(self, cmd):
180         def update(line):
181             self.progressbar.pulse()
182             self.progressbar.set_show_text(1)
183             self.progressbar.set_ellipsize(True)
184             self.progressbar.set_text(line)
185             return False
186
187         def run():
188             run = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr = subprocess.STDOUT)
189             while run.poll() is None:
190                 line = run.stdout.readline().decode('utf-8')
191                 if line:
192                     yield line
193
194         def echo():
195             for line in run():
196                 GLib.idle_add(update, line)
197                 sys.stdout.write(line)
198
199         thread = threading.Thread(target=echo)
200         thread.daemon = True
201         thread.start()
202
203 #== Run ==#
204 if __name__ == "__main__":
205     win = MainWindow()
206     win.show_all()
207     win.connect("destroy", Gtk.main_quit)
208     Gtk.main()