chamesle

chamesle.org: the site of Chamesle, a small Linux distribution
git clone https://github.com/tortr-rs/chamesle
Log | Files | Refs | README

jiggity.py (10605B)


      1 #!/usr/bin/python3
      2 # jiggity - the chamesle installer
      3 import subprocess
      4 import sys
      5 import threading
      6 
      7 import gi
      8 gi.require_version("Gtk", "3.0")
      9 from gi.repository import Gtk, Gdk, GLib
     10 
     11 sys.path.insert(0, "/usr/lib/jiggity")
     12 import disks
     13 import install
     14 import ui
     15 from choices import LANGUAGES, STEPS, keyboards
     16 
     17 
     18 class Jiggity(ui.SetupWindow):
     19     def __init__(self):
     20         super().__init__("Chamesle Setup")
     21         self.rows = []
     22         self.pages = [self.page_language(), self.page_start(), self.page_where(), self.page_installing(), self.page_done()]
     23         self.show_page(0)
     24 
     25     # language and keyboard
     26     def page_language(self):
     27         box = ui.page("", "")
     28         box.pack_start(ui.logo_line("Chamesle"), False, False, 12)
     29         grid = Gtk.Grid(column_spacing=16, row_spacing=14, halign=Gtk.Align.CENTER, margin_top=24)
     30         self.language = Gtk.ComboBoxText()
     31         for name, code in LANGUAGES:
     32             self.language.append(code, name)
     33         self.language.set_active_id("en_US")
     34         self.keyboard = Gtk.ComboBoxText()
     35         self.kbds = keyboards()
     36         for i, (desc, layout, variant) in enumerate(self.kbds):
     37             self.keyboard.append(str(i), desc)
     38             if layout == "us" and not variant:
     39                 self.keyboard.set_active(i)
     40         self.keyboard.connect("changed", self.on_keyboard)
     41         for r, (text, w) in enumerate([("Language to install:", self.language), ("Keyboard or input method:", self.keyboard)]):
     42             l = Gtk.Label(label=text, xalign=1)
     43             grid.attach(l, 0, r, 1, 1)
     44             w.set_size_request(320, -1)
     45             grid.attach(w, 1, r, 1, 1)
     46         box.pack_start(grid, False, False, 0)
     47         hint = Gtk.Label(label="Enter your language and other preferences and click \"Next\" to continue.", margin_top=40)
     48         hint.get_style_context().add_class("dim")
     49         box.pack_start(hint, False, False, 0)
     50         return box, [("Next", lambda *a: self.show_page(1))]
     51 
     52     def on_keyboard(self, combo):
     53         desc, layout, variant = self.kbds[int(combo.get_active_id())]
     54         subprocess.run(["setxkbmap", layout] + (["-variant", variant] if variant else []))
     55 
     56     # install now
     57     def page_start(self):
     58         box = ui.page("", "")
     59         box.pack_start(ui.logo_line("Chamesle"), False, False, 40)
     60         button = Gtk.Button(label="Install now", halign=Gtk.Align.CENTER)
     61         button.get_style_context().add_class("big")
     62         button.connect("clicked", lambda *a: self.show_page(2))
     63         box.pack_start(button, False, False, 0)
     64         return box, [("Back", lambda *a: self.show_page(0))]
     65 
     66     # where
     67     def page_where(self):
     68         box = ui.page("Where do you want to install Chamesle?", "")
     69         self.store = Gtk.ListStore(str, str, str, str, bool, int)
     70         view = Gtk.TreeView(model=self.store, headers_visible=True)
     71         for i, (title, xalign) in enumerate([("Name", 0), ("Total size", 1), ("Free space", 1), ("Type", 0)]):
     72             cell = Gtk.CellRendererText(xalign=xalign)
     73             col = Gtk.TreeViewColumn(title, cell, text=i, sensitive=4)
     74             col.set_expand(i == 0)
     75             view.append_column(col)
     76         self.selection = view.get_selection()
     77         self.selection.set_select_function(lambda sel, model, path, selected: model[path][4])
     78         self.selection.connect("changed", self.on_where)
     79         scroll = Gtk.ScrolledWindow(min_content_height=220)
     80         scroll.add(view)
     81         scroll.get_style_context().add_class("table")
     82         box.pack_start(scroll, True, True, 0)
     83         tools = Gtk.Box(spacing=18, margin_top=6)
     84         refresh = Gtk.LinkButton(label="Refresh", uri="")
     85         refresh.connect("activate-link", lambda *a: self.fill_where() or True)
     86         tools.pack_start(refresh, False, False, 0)
     87         box.pack_start(tools, False, False, 0)
     88         self.where_note = Gtk.Label(xalign=0, wrap=True, margin_top=6)
     89         self.where_note.get_style_context().add_class("note")
     90         box.pack_start(self.where_note, False, False, 0)
     91         self.encrypt = Gtk.CheckButton(label="Encrypt Chamesle with a passphrase", margin_top=10)
     92         box.pack_start(self.encrypt, False, False, 0)
     93         self.pass_grid = Gtk.Grid(column_spacing=12, row_spacing=8, margin_start=28, no_show_all=True)
     94         self.pass1 = Gtk.Entry(visibility=False, width_chars=28)
     95         self.pass2 = Gtk.Entry(visibility=False, width_chars=28)
     96         for r, (text, e) in enumerate([("Passphrase:", self.pass1), ("Confirm:", self.pass2)]):
     97             self.pass_grid.attach(Gtk.Label(label=text, xalign=1), 0, r, 1, 1)
     98             self.pass_grid.attach(e, 1, r, 1, 1)
     99             e.connect("changed", self.on_where)
    100         warn = Gtk.Label(label="You type it every time the computer starts. If you forget it, your files can't be "
    101                                "recovered.", xalign=0, wrap=True, max_width_chars=60)
    102         warn.get_style_context().add_class("note")
    103         self.pass_grid.attach(warn, 0, 2, 2, 1)
    104         box.pack_start(self.pass_grid, False, False, 0)
    105         self.encrypt.connect("toggled", self.on_encrypt)
    106         return box, [("Next", self.on_install), ("Back", lambda *a: self.show_page(1))]
    107 
    108     def fill_where(self):
    109         self.store.clear()
    110         self.rows = disks.scan()
    111         for i, r in enumerate(self.rows):
    112             name = r.label if r.kind == "drive" else "    " + r.label
    113             free = disks.size_text(r.free) if r.kind != "drive" else ""
    114             self.store.append([name, disks.size_text(r.total), free, r.type_text, r.usable, i])
    115         if not self.rows:
    116             self.where_note.set_text("No drives were found. Check that the drive is connected, then click Refresh.")
    117         self.on_where()
    118 
    119     def on_encrypt(self, check):
    120         self.pass_grid.set_visible(check.get_active())
    121         if check.get_active():
    122             self.pass_grid.show_all()
    123         self.on_where()
    124 
    125     def chosen(self):
    126         model, it = self.selection.get_selected()
    127         return self.rows[model[it][5]] if it else None
    128 
    129     def on_where(self, *a):
    130         row = self.chosen()
    131         ok = row is not None
    132         if row is None:
    133             if self.rows:
    134                 self.where_note.set_text("Pick a drive (to use all of it) or its unallocated space (to keep what's on "
    135                                          "the drive). Chamesle needs at least %s." % disks.size_text(disks.MIN_SIZE))
    136         elif row.kind == "drive":
    137             self.where_note.set_text("Everything on %s will be erased." % row.label.split(":")[0])
    138         else:
    139             self.where_note.set_text("Chamesle goes in the unallocated space. Nothing else on the drive changes. "
    140                                      "When the computer starts, you choose between Chamesle and what's already there.")
    141         if self.encrypt.get_active():
    142             p1, p2 = self.pass1.get_text(), self.pass2.get_text()
    143             ok = ok and len(p1) >= 8 and p1 == p2
    144             if p1 and len(p1) < 8:
    145                 self.where_note.set_text("The passphrase needs at least 8 characters.")
    146             elif p2 and p1 != p2:
    147                 self.where_note.set_text("The passphrases don't match.")
    148         self.set_button_sensitive("Next", ok)
    149 
    150     def on_install(self, *a):
    151         row = self.chosen()
    152         if row.kind == "drive":
    153             if not ui.confirm(self, "Erase %s?" % row.label.split(":")[0],
    154                               "Everything on this drive will be deleted, and Chamesle will be installed on it. "
    155                               "This can't be undone."):
    156                 return
    157         passphrase = self.pass1.get_text() if self.encrypt.get_active() else None
    158         language = self.language.get_active_id()
    159         desc, layout, variant = self.kbds[int(self.keyboard.get_active_id())]
    160         self.show_page(3)
    161         threading.Thread(target=self.run_install, args=(row, passphrase, language, (layout, variant)), daemon=True).start()
    162 
    163     # installing
    164     def page_installing(self):
    165         box = ui.page("Installing Chamesle", "")
    166         box.pack_start(Gtk.Label(label="Status", xalign=0, margin_top=10), False, False, 0)
    167         self.step_labels = []
    168         for s in STEPS:
    169             l = Gtk.Label(label=s, xalign=0, margin_start=12, margin_top=6)
    170             l.get_style_context().add_class("step")
    171             box.pack_start(l, False, False, 0)
    172             self.step_labels.append(l)
    173         return box, []
    174 
    175     def progress(self, step, percent):
    176         def show():
    177             for i, l in enumerate(self.step_labels):
    178                 ctx = l.get_style_context()
    179                 ctx.remove_class("now")
    180                 if i < step or (i == step and percent >= 100):
    181                     l.set_text("✓  " + STEPS[i])
    182                     ctx.add_class("done")
    183                 elif i == step:
    184                     l.set_text("      " + STEPS[i] + (" (%d%%)" % percent if i == 1 else "..."))
    185                     ctx.add_class("now")
    186                 else:
    187                     l.set_text("      " + STEPS[i])
    188         GLib.idle_add(show)
    189 
    190     def run_install(self, *args):
    191         try:
    192             install.install(*args, progress=self.progress)
    193         except Exception as e:
    194             print("jiggity: the installation failed:", e)
    195             GLib.idle_add(self.failed, str(e))
    196             return
    197         GLib.idle_add(self.show_page, 4)
    198         GLib.idle_add(self.countdown, 10)
    199 
    200     def failed(self, why):
    201         ui.message(self, "Chamesle couldn't be installed",
    202                    "%s\n\nThe details are in /var/log/jiggity.log. Click OK to restart." % why)
    203         subprocess.run(["reboot"])
    204 
    205     # restart
    206     def page_done(self):
    207         box = ui.page("Chamesle needs to restart to continue", "")
    208         self.count_label = Gtk.Label(xalign=0, margin_top=20)
    209         box.pack_start(self.count_label, False, False, 0)
    210         note = Gtk.Label(label="Take out the USB stick when the screen goes dark. After the restart you'll set up "
    211                                "your account.", xalign=0, wrap=True, margin_top=12)
    212         note.get_style_context().add_class("dim")
    213         box.pack_start(note, False, False, 0)
    214         return box, [("Restart now", lambda *a: subprocess.run(["reboot"]))]
    215 
    216     def countdown(self, n):
    217         self.count_label.set_text("Restarting in %d seconds" % n)
    218         if n == 0:
    219             subprocess.run(["reboot"])
    220             return False
    221         GLib.timeout_add_seconds(1, self.countdown, n - 1)
    222         return False
    223 
    224     def show_page(self, n):
    225         super().show_page(n)
    226         if n == 2:
    227             self.fill_where()
    228 
    229 
    230 if __name__ == "__main__":
    231     Jiggity()
    232     Gtk.main()