1. Moises Henriquez
  2. vasm

Source

vasm / src / vasm / ui / tui / modules / tui_partitioning.py

#!/usr/bin/env python

#    This file is part of VASM.
#
#    VASM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    VASM is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty 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 VASM.  If not, see <http://www.gnu.org/licenses/>.

""" tui_partitioning.py
    Launches a cfdisk instance from the running urwid interface.
    """

__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]

import subprocess as sp
import os
import urwid
import parted
from vasm.ui.tui.support import widgets, dialogs
from vasm.ui.tui.support.tui_utils import run_external_tui
from vasm.backend.utils import _
from vasm.backend import FSTAB
import logging

logger = logging.getLogger('vasm')

VASM_CATEGORY = "System"
VASM_LABEL = "System Disk Partitions"

class DrivePickerDialog(widgets.TuiModule):
    def __init__(self, parent=None):
        self.parent = parent
        self._drive = None
        msg = (_("There is more than one drive installed on your system."),"  ",
            _("Select which one you would like to partition."))
        _radios = []
        drives = FSTAB.list_all_system_drives()
        model = urwid.SimpleListWalker([])
        # create the radio buttons
        for drive in drives:
            dev = parted.Device(drive)
            label = "%s [ %s MB - %s ]"% ( dev.path, str(round(dev.getSize('MB'), 2)),
                dev.model)
            radio = urwid.RadioButton(group=_radios,
                label = label, user_data = dev.path, state=False,
                on_state_change=self.radiobutton_toggle_event)
            wrap = urwid.AttrWrap(radio, 'button', 'button sel')
            model.append(wrap)
        
        listbox = urwid.ListBox(model)
        body = urwid.Pile([
            ('flow', widgets.blank),
            listbox,
            ('flow',urwid.Divider('_')),
            ])
        
        #_body = [urwid.Text("Body content")]        
        widgets.TuiModule.__init__(
            self,
            header = _("Select which drive to partition"),
            desc = [widgets.blank, ''.join(msg), urwid.Divider('_'),widgets.blank],
            parent = self.parent,
            body = body,
            buttons = [("OK", self.module_ok_event),
                ("Cancel", self.parent.return_home)])
        
        self.set_focus('body')
        # set the focus to the left button on the bottom toolbar
        bar = self._get_action_area()
    
    def radiobutton_toggle_event(self, radio, newstate, user_data):
        if newstate:
            self._drive = user_data
        return
    
    def module_ok_event(self, widget=None, data=None):
        if not self._drive:
            dia = dialogs.Error(
                parent = self,
                message = _("You must select a hard disk before continuing."),
                buttons = [("OK", self.return_to_module)],
                )
            return self.pop_dialog(dia)
        
        return self.run_cfdisk(self._drive)
        dia = dialogs.Info(
            parent = self,
            message = self._drive,
            buttons = [("OK", self.return_to_module)]
            )
        return self.pop_dialog(dia)
    
    def run_cfdisk(self, disk_drive):
        """ Launch cfdisk on the selected drive """
        logger.debug('Launching cfdisk on %s'% disk_drive)
        cfpath = _get_cfdisk_path()
        self.close_module()
        return run_external_tui(self.parent, [cfpath, disk_drive])
        

def _get_cfdisk_path():
    """ Return the path to cfdisk """
    _paths = ('/sbin/cfdisk',
              '/usr/sbin/cfdisk')
    for item in _paths:
        if os.path.exists(item):
            return item
    return None

def __vasm_test__():
    """ Set of tests to anounce this module to vasm """
    if os.geteuid() > 0:
        return False    
    return _get_cfdisk_path() is not None


def __run__(parent):
    # Execute cfdisk in the running terminal
    # If we only find one disk, go straight to it,
    # otherwise, intercept the call with a dialog asking which drive to partition.
    drives = FSTAB.list_all_system_drives()
    if len(drives) > 1:
        logger.debug('Multiple drives found.... Asking user to select one')
        dia = DrivePickerDialog(parent)
        return parent.pop_module(dia)
    else:
        logger.debug('Launching cfdisk')
        run_external_tui(parent, _get_cfdisk_path())