plugin interface changed: now we use classes (inherited by CryptoBoxPlugin) instead of plain scripts
added some log entries use threading module instead of "fork" for background formatting redirection for "network" plugin fixed empty return value of plugins defaults to plugin overview page
This commit is contained in:
parent
52ccaeb530
commit
56e954d1c4
11 changed files with 545 additions and 436 deletions
|
@ -1,49 +1,64 @@
|
|||
import subprocess
|
||||
import os
|
||||
import CryptoBoxPlugin
|
||||
|
||||
|
||||
def doAction(hdf, cbox, store=None, year=0, month=0, day=0, hour=0, minute=0):
|
||||
class date(CryptoBoxPlugin.CryptoBoxPlugin):
|
||||
|
||||
def doAction(self, store=None, year=0, month=0, day=0, hour=0, minute=0):
|
||||
import datetime
|
||||
__prepareFormData(hdf, cbox)
|
||||
if store:
|
||||
try:
|
||||
year, month, day = int(year), int(month), int(day)
|
||||
hour, minute = int(hour), int(minute)
|
||||
new_date = datetime.datetime(year, month, day, hour, minute)
|
||||
except ValueError:
|
||||
hdf["Data.Warning"] = "Plugins.date.InvalidDate"
|
||||
proc = subprocess.Popen(
|
||||
shell = False,
|
||||
args = [
|
||||
cbox.prefs["Programs"]["super"],
|
||||
cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
"plugin",
|
||||
os.path.join(os.path.dirname(__file__), "root_action.py"),
|
||||
"%02d%02d%02d%02d%d" % (month, day, hour, minute, year)])
|
||||
proc.communicate()
|
||||
if proc.returncode == 0:
|
||||
return "form_system"
|
||||
self.hdf["Data.Warning"] = "Plugins.date.InvalidDate"
|
||||
self.__prepareFormData()
|
||||
return "form_date"
|
||||
date = "%02d%02d%02d%02d%d" % (month, day, hour, minute, year)
|
||||
if self.__setDate(date):
|
||||
self.cbox.log.info("changed date to: %s" % date)
|
||||
self.hdf["Data.Success"] = "Plugins.date.DateChanged"
|
||||
return None
|
||||
else:
|
||||
hdf["Data.Warning"] = "Plugins.date.InvalidDate"
|
||||
## a failure should usually be an invalid date (we do not check it really)
|
||||
self.cbox.log.info("failed to set date: %s" % date)
|
||||
self.hdf["Data.Warning"] = "Plugins.date.InvalidDate"
|
||||
self.__prepareFormData()
|
||||
return "form_date"
|
||||
else:
|
||||
self.__prepareFormData()
|
||||
return "form_date"
|
||||
|
||||
|
||||
def getStatus(cbox):
|
||||
return str(__getCurrentDate())
|
||||
def getStatus(self):
|
||||
return str(self.__getCurrentDate())
|
||||
|
||||
|
||||
def __prepareFormData(hdf, cbox):
|
||||
date = __getCurrentDate()
|
||||
hdf["Data.Plugins.date.year"] = date.year
|
||||
hdf["Data.Plugins.date.month"] = date.month
|
||||
hdf["Data.Plugins.date.day"] = date.day
|
||||
hdf["Data.Plugins.date.hour"] = date.hour
|
||||
hdf["Data.Plugins.date.minute"] = date.minute
|
||||
def __prepareFormData(self):
|
||||
date = self.__getCurrentDate()
|
||||
self.hdf[self.hdf_prefix + "year"] = date.year
|
||||
self.hdf[self.hdf_prefix + "month"] = date.month
|
||||
self.hdf[self.hdf_prefix + "day"] = date.day
|
||||
self.hdf[self.hdf_prefix + "hour"] = date.hour
|
||||
self.hdf[self.hdf_prefix + "minute"] = date.minute
|
||||
|
||||
|
||||
def __getCurrentDate():
|
||||
def __getCurrentDate(self):
|
||||
import datetime
|
||||
return datetime.datetime(2000,1,1).now()
|
||||
|
||||
|
||||
def __setDate(self, date):
|
||||
import subprocess
|
||||
import os
|
||||
proc = subprocess.Popen(
|
||||
shell = False,
|
||||
args = [
|
||||
self.cbox.prefs["Programs"]["super"],
|
||||
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
"plugin",
|
||||
os.path.join(self.pluginDir, "root_action.py"),
|
||||
date])
|
||||
proc.communicate()
|
||||
return proc.returncode == 0
|
||||
|
||||
|
|
|
@ -23,7 +23,14 @@ Text.Months {
|
|||
12 = December
|
||||
}
|
||||
|
||||
WarningMessage.InvalidDate {
|
||||
SuccessMessage.DateChanged {
|
||||
Title = Date changed
|
||||
Text = The date was changed successfully.
|
||||
}
|
||||
|
||||
WarningMessage {
|
||||
InvalidDate {
|
||||
Title = Invalid value
|
||||
Text = An invalid value for date or time was supplied. Please try again.
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,23 @@
|
|||
import CryptoBoxPlugin
|
||||
|
||||
def doAction(hdf, cbox):
|
||||
__prepareFormData(hdf,cbox)
|
||||
class logs(CryptoBoxPlugin.CryptoBoxPlugin):
|
||||
|
||||
def doAction(self):
|
||||
self.__prepareFormData()
|
||||
return "show_log"
|
||||
|
||||
|
||||
def getStatus(cbox):
|
||||
def getStatus(self):
|
||||
return "%s:%s:%s" % (
|
||||
cbox.prefs["Log"]["Level"],
|
||||
cbox.prefs["Log"]["Destination"],
|
||||
cbox.prefs["Log"]["Details"])
|
||||
self.cbox.prefs["Log"]["Level"],
|
||||
self.cbox.prefs["Log"]["Destination"],
|
||||
self.cbox.prefs["Log"]["Details"])
|
||||
|
||||
|
||||
def __prepareFormData(hdf, cbox):
|
||||
hdf["Data.Plugins.logs.Content"] = __getLogContent(cbox)
|
||||
def __prepareFormData(self):
|
||||
self.hdf[self.hdf_prefix + "Content"] = self.__getLogContent()
|
||||
|
||||
|
||||
def __getLogContent(cbox, lines=30, maxSize=2000):
|
||||
return "<br/>".join(cbox.getLogData(lines, maxSize))
|
||||
def __getLogContent(self, lines=30, maxSize=2000):
|
||||
return "<br/>".join(self.cbox.getLogData(lines, maxSize))
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ Rank = 30
|
|||
|
||||
Title.Network = Network settings
|
||||
|
||||
Button.Network = Update settings
|
||||
Button.Network = Update network settings
|
||||
|
||||
Text.IP = Network address
|
||||
|
||||
|
|
|
@ -1,47 +1,73 @@
|
|||
import re
|
||||
import subprocess
|
||||
import imp
|
||||
import os
|
||||
import CryptoBoxPlugin
|
||||
|
||||
## specify (in seconds), how long we should wait before redirecting and ip change
|
||||
REDIRECT_DELAY=20
|
||||
CHANGE_IP_DELAY=5
|
||||
|
||||
def doAction(hdf, cbox, store=None, ip1="", ip2="", ip3="", ip4=""):
|
||||
__prepareFormData(hdf, cbox)
|
||||
class network(CryptoBoxPlugin.CryptoBoxPlugin):
|
||||
|
||||
def doAction(self, store=None, redirected="", ip1="", ip2="", ip3="", ip4=""):
|
||||
## if we were redirected, then we should display the default page
|
||||
if redirected == "1":
|
||||
return None
|
||||
## request for IP change?
|
||||
if store:
|
||||
try:
|
||||
for ip_in in (ip1, ip2, ip3, ip4):
|
||||
if (int(ip_in) < 0) or (int(ip_in) > 255):
|
||||
cbox.log.debug("invalid IP supplied: %s" % str((ip1,ip2,ip3,ip4)))
|
||||
self.cbox.log.info("invalid IP supplied: %s" % str((ip1,ip2,ip3,ip4)))
|
||||
raise ValueError
|
||||
ip = "%d.%d.%d.%d" % (int(ip1), int(ip2), int(ip3), int(ip4))
|
||||
except ValueError:
|
||||
hdf["Data.Warning"] = "Plugins.network.InvalidIP"
|
||||
return "form_system"
|
||||
if __setIP(cbox, ip):
|
||||
hdf["Data.Success"] = "Plugins.network.IPChanged"
|
||||
hdf["Data.Redirect.URL"] = ""
|
||||
hdf["Data.Redirect.Delay"] = 30
|
||||
return "form_system"
|
||||
self.hdf["Data.Warning"] = "Plugins.network.InvalidIP"
|
||||
return None
|
||||
if self.__setIP(ip):
|
||||
self.cbox.log.info("the IP was successfully changed: %s" % ip)
|
||||
self.hdf["Data.Success"] = "Plugins.network.IPChanged"
|
||||
self.hdf["Data.Redirect.URL"] = self.__getRedirectDestination(ip)
|
||||
self.hdf["Data.Redirect.Delay"] = REDIRECT_DELAY
|
||||
return None
|
||||
else:
|
||||
hdf["Data.Warning"] = "Plugins.network.InvalidIP"
|
||||
self.cbox.log.warn("failed to change IP address to: %s" % ip)
|
||||
self.hdf["Data.Warning"] = "Plugins.network.InvalidIP"
|
||||
self.__prepareFormData()
|
||||
return "form_network"
|
||||
else:
|
||||
## just show the form
|
||||
self.__prepareFormData()
|
||||
return "form_network"
|
||||
|
||||
|
||||
def getStatus(cbox):
|
||||
return "%d.%d.%d.%d" % __getCurrentIP(cbox)
|
||||
def getStatus(self):
|
||||
return "%d.%d.%d.%d" % self.__getCurrentIP()
|
||||
|
||||
|
||||
def __prepareFormData(hdf, cbox):
|
||||
(oc1, oc2, oc3, oc4) = __getCurrentIP(cbox)
|
||||
hdf["Data.Plugins.network.ip.oc1"] = oc1
|
||||
hdf["Data.Plugins.network.ip.oc2"] = oc2
|
||||
hdf["Data.Plugins.network.ip.oc3"] = oc3
|
||||
hdf["Data.Plugins.network.ip.oc4"] = oc4
|
||||
def __getRedirectDestionation(self, ip):
|
||||
import cherrypy
|
||||
req = cherrypy.request
|
||||
dest = ip
|
||||
base_parts = req.base.split(":")
|
||||
if len(base_parts) == 3:
|
||||
dest += ":%s" % base_parts[2]
|
||||
return dest
|
||||
|
||||
|
||||
def __getCurrentIP(cbox):
|
||||
root_action_plug = imp.load_source("root_action", os.path.join(os.path.dirname(__file__), "root_action.py"))
|
||||
def __prepareFormData(self):
|
||||
(oc1, oc2, oc3, oc4) = self.__getCurrentIP()
|
||||
self.hdf[self.hdf_prefix + "ip.oc1"] = oc1
|
||||
self.hdf[self.hdf_prefix + "ip.oc2"] = oc2
|
||||
self.hdf[self.hdf_prefix + "ip.oc3"] = oc3
|
||||
self.hdf[self.hdf_prefix + "ip.oc4"] = oc4
|
||||
|
||||
|
||||
def __getCurrentIP(self):
|
||||
import re
|
||||
import imp
|
||||
## load some values from the root_action.py script
|
||||
root_action_plug = imp.load_source("root_action", os.path.join(self.pluginDir, "root_action.py"))
|
||||
## get the current IP of the network interface
|
||||
proc = subprocess.Popen(
|
||||
shell = False,
|
||||
stdout = subprocess.PIPE,
|
||||
|
@ -50,22 +76,35 @@ def __getCurrentIP(cbox):
|
|||
root_action_plug.IFACE])
|
||||
(output, error) = proc.communicate()
|
||||
if proc.returncode != 0: return (0,0,0,0)
|
||||
## this regex matches the four numbers of the IP
|
||||
match = re.search(u'inet [\w]+:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\s',output)
|
||||
if match:
|
||||
## use the previously matched numbers
|
||||
return tuple([int(e) for e in match.groups()])
|
||||
else:
|
||||
return (0,0,0,0)
|
||||
|
||||
|
||||
def __setIP(cbox, ip):
|
||||
def __setIP(self, ip):
|
||||
import threading
|
||||
## call the root_action script after some seconds - so we can deliver the page before
|
||||
def delayedIPchange():
|
||||
import time
|
||||
time.sleep(CHANGE_IP_DELAY)
|
||||
proc = subprocess.Popen(
|
||||
shell = False,
|
||||
args = [
|
||||
cbox.prefs["Programs"]["super"],
|
||||
cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
self.cbox.prefs["Programs"]["super"],
|
||||
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
"plugin",
|
||||
os.path.join(os.path.dirname(__file__), "root_action.py"),
|
||||
os.path.join(self.pluginDir, "root_action.py"),
|
||||
ip])
|
||||
proc.communicate()
|
||||
return proc.returncode == 0
|
||||
return
|
||||
thread = threading.Thread()
|
||||
thread.run = delayedIPchange
|
||||
thread.setDaemon(True)
|
||||
thread.start()
|
||||
# TODO: how could we guess, if it failed?
|
||||
return True
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ Text {
|
|||
Size = Size (MB)
|
||||
SelectDevice = Choose a disk for partitioning
|
||||
WarningMessage = If you continue, you will destroy all data on the choosen disk. Please be careful!
|
||||
ProgressInfo = Progress of formatting:
|
||||
}
|
||||
|
||||
SuccessMessage {
|
||||
|
|
|
@ -2,6 +2,9 @@ import subprocess
|
|||
import os
|
||||
import logging
|
||||
import CryptoBoxTools
|
||||
import CryptoBoxPlugin
|
||||
|
||||
class partition(CryptoBoxPlugin.CryptoBoxPlugin):
|
||||
|
||||
PartTypes = {
|
||||
"windows" : ["0xC", "vfat"],
|
||||
|
@ -12,150 +15,169 @@ ConfigPartition = {
|
|||
"type" : "L",
|
||||
"fs" : "ext2"}
|
||||
|
||||
logger = logging.getLogger("CryptoBox")
|
||||
|
||||
|
||||
def doAction(hdf, cbox, **args):
|
||||
def doAction(self, **args):
|
||||
## retrieve some values from 'args' - defaults are empty
|
||||
self.withConfigPartition = self.__isWithConfigPartition(args)
|
||||
self.device = self.__getSelectedDevice(args)
|
||||
self.deviceSize = self.__getAvailableDeviceSize(self.device)
|
||||
try:
|
||||
step = args["step"]
|
||||
del args["step"]
|
||||
except KeyError:
|
||||
step = "select_device"
|
||||
## no (or invalid) device was supplied
|
||||
if not self.device:
|
||||
step == "select_device"
|
||||
if step == "add_partition":
|
||||
return __actionAddPartition(hdf, cbox, args)
|
||||
return self.__actionAddPartition(args)
|
||||
if step == "del_partition":
|
||||
return __actionDelPartition(hdf, cbox, args)
|
||||
return self.__actionDelPartition(args)
|
||||
elif step == "finish":
|
||||
return __actionFinish(hdf, cbox, args)
|
||||
return self.__actionFinish(args)
|
||||
else: # for "select_device" and for invalid targets
|
||||
return __actionSelectDevice(hdf, cbox, args)
|
||||
return self.__actionSelectDevice(args)
|
||||
|
||||
|
||||
def getStatus(cbox):
|
||||
return "TODO"
|
||||
def getStatus(self):
|
||||
return "%s / %s / %s" % (self.device, self.deviceSize, self.withConfigPartition)
|
||||
|
||||
|
||||
def __isDeviceValid(device, cbox):
|
||||
if not cbox.isDeviceAllowed(device):
|
||||
def __getSelectedDevice(self, args):
|
||||
try:
|
||||
device = args["block_device"]
|
||||
except KeyError:
|
||||
return None
|
||||
if not self.__isDeviceValid(device):
|
||||
return None
|
||||
if self.__isDeviceBusy(device):
|
||||
self.hdf["Data.Warning"] = "Plugins.partition.DiskIsBusy"
|
||||
return None
|
||||
return device
|
||||
|
||||
|
||||
def __isDeviceValid(self, device):
|
||||
if not device:
|
||||
return False
|
||||
if not self.cbox.isDeviceAllowed(device):
|
||||
return False
|
||||
if not device in CryptoBoxTools.getParentBlockDevices():
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def __isDeviceBusy(device, cbox):
|
||||
def __isDeviceBusy(self, device):
|
||||
"""check if the device (or one of its partitions) is mounted"""
|
||||
# TODO: the config partition is ignored, as it is not part of the container list - that is not good
|
||||
import re
|
||||
for c in cbox.getContainerList():
|
||||
for c in self.cbox.getContainerList():
|
||||
if re.match(device + "\d*$", c.getDevice()):
|
||||
if c.isMounted(): return True
|
||||
return False
|
||||
|
||||
|
||||
def __actionSelectDevice(hdf, cbox, args):
|
||||
def __actionSelectDevice(self, args):
|
||||
block_devices = [e
|
||||
for e in CryptoBoxTools.getParentBlockDevices()
|
||||
if cbox.isDeviceAllowed(e)]
|
||||
if self.cbox.isDeviceAllowed(e)]
|
||||
counter = 0
|
||||
for a in block_devices:
|
||||
hdf["Data.Plugins.partition.BlockDevices.%d" % counter] = a
|
||||
cbox.log.debug("found a suitable block device: %s" % a)
|
||||
self.hdf[self.hdf_prefix + "BlockDevices.%d" % counter] = a
|
||||
self.cbox.log.debug("found a suitable block device: %s" % a)
|
||||
counter += 1
|
||||
if __withConfigPartition(args):
|
||||
hdf["Data.Plugins.partition.CreateConfigPartition"] = "1"
|
||||
if self.withConfigPartition:
|
||||
self.hdf[self.hdf_prefix + "CreateConfigPartition"] = "1"
|
||||
## there is no disk available
|
||||
if not block_devices:
|
||||
hdf["Data.Warning"] = "Plugins.partition.NoDisksAvailable"
|
||||
self.hdf["Data.Warning"] = "Plugins.partition.NoDisksAvailable"
|
||||
return "select_device"
|
||||
|
||||
|
||||
def __actionAddPartition(hdf, cbox, args):
|
||||
try:
|
||||
device = args["block_device"]
|
||||
except KeyError:
|
||||
return __actionSelectDevice(hdf, cbox, args)
|
||||
if not __isDeviceValid(device, cbox): return __actionSelectDevice(hdf, cbox, args)
|
||||
if __isDeviceBusy(device, cbox):
|
||||
hdf["Data.Warning"] = "Plugins.partition.DiskIsBusy"
|
||||
return __actionSelectDevice(hdf, cbox, args)
|
||||
size = __getAvailableDeviceSize(device, __withConfigPartition(args))
|
||||
hdf["Data.Plugins.partition.Device"] = device
|
||||
hdf["Data.Plugins.partition.Device.Size"] = size
|
||||
parts = __getPartitionsFromArgs(hdf, args, size)
|
||||
__setPartitionData(hdf, parts, size, __withConfigPartition(args))
|
||||
def __actionAddPartition(self, args):
|
||||
self.hdf[self.hdf_prefix + "Device"] = self.device
|
||||
self.hdf[self.hdf_prefix + "Device.Size"] = self.deviceSize
|
||||
parts = self.__getPartitionsFromArgs(args)
|
||||
self.__setPartitionData(parts)
|
||||
return "set_partitions"
|
||||
|
||||
|
||||
def __actionDelPartition(hdf, cbox, args):
|
||||
def __actionDelPartition(self, args):
|
||||
try:
|
||||
device = args["block_device"]
|
||||
part_num = int(args["del_num"])
|
||||
except (TypeError,KeyError):
|
||||
return __actionSelectDevice(hdf, cbox, args)
|
||||
if not __isDeviceValid(device, cbox): return __actionSelectDevice(hdf, cbox, args)
|
||||
if __isDeviceBusy(device, cbox):
|
||||
hdf["Data.Warning"] = "Plugins.partition.DiskIsBusy"
|
||||
return __actionSelectDevice(hdf, cbox, args)
|
||||
size = __getAvailableDeviceSize(device, __withConfigPartition(args))
|
||||
hdf["Data.Plugins.partition.Device"] = device
|
||||
hdf["Data.Plugins.partition.Device.Size"] = size
|
||||
parts = __getPartitionsFromArgs(hdf, args, size)
|
||||
return self.__actionAddPartition(args)
|
||||
self.hdf[self.hdf_prefix + "Device"] = self.device
|
||||
self.hdf[self.hdf_prefix + "Device.Size"] = self.deviceSize
|
||||
parts = self.__getPartitionsFromArgs(args)
|
||||
## valid partition number to be deleted?
|
||||
if part_num < len(parts):
|
||||
del parts[part_num]
|
||||
else:
|
||||
return __actionSelectDevice(hdf, cbox, args)
|
||||
__setPartitionData(hdf, parts, size, __withConfigPartition(args))
|
||||
return self.__actionAddPartition(args)
|
||||
self.__setPartitionData(parts)
|
||||
return "set_partitions"
|
||||
|
||||
|
||||
def __actionFinish(hdf, cbox, args):
|
||||
try:
|
||||
device = args["block_device"]
|
||||
except KeyError:
|
||||
return __actionSelectDevice(hdf, cbox, args)
|
||||
if not __isDeviceValid(device, cbox): return __actionSelectDevice(hdf, cbox, args)
|
||||
if __isDeviceBusy(device, cbox):
|
||||
hdf["Data.Warning"] = "Plugins.partition.DiskIsBusy"
|
||||
return __actionSelectDevice(hdf, cbox, args)
|
||||
size = __getAvailableDeviceSize(device, __withConfigPartition(args))
|
||||
parts = __getPartitionsFromArgs(hdf, args, size)
|
||||
def __actionFinish(self, args):
|
||||
parts = self.__getPartitionsFromArgs(args)
|
||||
if parts:
|
||||
if not __runFDisk(cbox, device, parts, __withConfigPartition(args)):
|
||||
hdf["Data.Warning"] = "Plugins.partition.PartitioningFailed"
|
||||
return __actionAddPartition(hdf, cbox, args)
|
||||
self.__setPartitionData(parts)
|
||||
if not self.__runFDisk(parts):
|
||||
self.hdf["Data.Warning"] = "Plugins.partition.PartitioningFailed"
|
||||
return self.__actionAddPartition(args)
|
||||
else:
|
||||
if __formatPartitions(cbox, device, parts, __withConfigPartition(args)):
|
||||
hdf["Data.Success"] = "Plugins.partition.Partitioned"
|
||||
# TODO: will we use the "yield"-style? If yes, then remove these messages (success and warning)
|
||||
#if self.__formatPartitions(parts):
|
||||
# self.hdf["Data.Success"] = "Plugins.partition.Partitioned"
|
||||
#else:
|
||||
# self.hdf["Data.Warning"] = "Plugins.partition.FormattingFailed"
|
||||
"""
|
||||
tricky problem: if the device was partitioned, then a created config partition is still part of the containerlist, as the label is not checked again - very ugly!!! So we will call reReadContainerList after formatting the last partition - see below
|
||||
"""
|
||||
self.cbox.reReadContainerList()
|
||||
def result_generator():
|
||||
counter = 0
|
||||
## initialize the generator
|
||||
formatPart_gen = self.__formatPartitions(parts)
|
||||
while counter < len(parts):
|
||||
## first part: get the device name
|
||||
yield formatPart_gen.next()
|
||||
counter += 1
|
||||
## second part: do the real formatting of a partition
|
||||
result = formatPart_gen.next()
|
||||
## after the first partiton, we can reRead the containerList (as the possible config partition was already created)
|
||||
if self.withConfigPartition and (counter == 1):
|
||||
## important: reRead the containerList
|
||||
self.cbox.reReadContainerList()
|
||||
## return the result
|
||||
yield result
|
||||
return {
|
||||
"template": "show_format_progress",
|
||||
"generator": result_generator}
|
||||
else:
|
||||
hdf["Data.Warning"] = "Plugins.partition.FormattingFailed"
|
||||
cbox.reReadContainerList()
|
||||
return "form_system"
|
||||
else:
|
||||
return __actionSelectDevice(hdf, cbox, args)
|
||||
return self.__actionAddPartition(args)
|
||||
|
||||
|
||||
def __setPartitionData(hdf, parts, size, withConfigPartition):
|
||||
availSize = size
|
||||
def __setPartitionData(self, parts):
|
||||
availSize = self.deviceSize
|
||||
i = 0
|
||||
for part in parts:
|
||||
logger.debug(part)
|
||||
hdf["Data.Plugins.partition.Parts.%d.Size" % i] = part["size"]
|
||||
hdf["Data.Plugins.partition.Parts.%d.Type" % i] = part["type"]
|
||||
self.cbox.log.debug(part)
|
||||
self.hdf[self.hdf_prefix + "Parts.%d.Size" % i] = part["size"]
|
||||
self.hdf[self.hdf_prefix + "Parts.%d.Type" % i] = part["type"]
|
||||
availSize -= part["size"]
|
||||
i += 1
|
||||
hdf["Data.Plugins.partition.availSize"] = availSize
|
||||
if withConfigPartition:
|
||||
hdf["Data.Plugins.partition.CreateConfigPartition"] = "1"
|
||||
for t in PartTypes.keys():
|
||||
hdf["Data.Plugins.partition.Types.%s" % t] = t
|
||||
self.hdf[self.hdf_prefix + "availSize"] = availSize
|
||||
if self.withConfigPartition:
|
||||
self.hdf[self.hdf_prefix + "CreateConfigPartition"] = "1"
|
||||
for t in self.PartTypes.keys():
|
||||
self.hdf[self.hdf_prefix + "Types.%s" % t] = t
|
||||
|
||||
|
||||
def __getPartitionsFromArgs(hdf, args, maxSize):
|
||||
def __getPartitionsFromArgs(self, args):
|
||||
parts = []
|
||||
done = False
|
||||
availSize = maxSize
|
||||
availSize = self.deviceSize
|
||||
i = -1
|
||||
while not done:
|
||||
i += 1
|
||||
|
@ -163,12 +185,12 @@ def __getPartitionsFromArgs(hdf, args, maxSize):
|
|||
size = int(args["part%d_size" % i])
|
||||
partType = args["part%d_type" % i]
|
||||
if int(size) > availSize:
|
||||
hdf["Data.Warning"] = "Plugins.partition.PartitionTooBig"
|
||||
self.hdf["Data.Warning"] = "Plugins.partition.PartitionTooBig"
|
||||
continue
|
||||
if int(size) < 10:
|
||||
hdf["Data.Warning"] = "Plugins.partition.PartitionTooSmall"
|
||||
self.hdf["Data.Warning"] = "Plugins.partition.PartitionTooSmall"
|
||||
continue
|
||||
if not partType in PartTypes.keys(): continue
|
||||
if not partType in self.PartTypes.keys(): continue
|
||||
parts.append({"size":size, "type":partType})
|
||||
availSize -= size
|
||||
except TypeError:
|
||||
|
@ -178,9 +200,10 @@ def __getPartitionsFromArgs(hdf, args, maxSize):
|
|||
return parts
|
||||
|
||||
|
||||
def __getAvailableDeviceSize(device, withConfigPartition):
|
||||
def __getAvailableDeviceSize(self, device):
|
||||
"""calculate the available size (MB) of the device
|
||||
also consider a (possible) configuration partition"""
|
||||
if not device: return 0
|
||||
rdev = os.stat(device).st_rdev
|
||||
minor = os.minor(rdev)
|
||||
major = os.major(rdev)
|
||||
|
@ -190,14 +213,15 @@ def __getAvailableDeviceSize(device, withConfigPartition):
|
|||
if len(elements) != 4: continue
|
||||
if (int(elements[0]) == major) and (int(elements[1]) == minor):
|
||||
deviceSize = int(elements[2])/1024
|
||||
if withConfigPartition: deviceSize -= ConfigPartition["size"]
|
||||
if self.withConfigPartition:
|
||||
deviceSize -= self.ConfigPartition["size"]
|
||||
return deviceSize
|
||||
except ValueError:
|
||||
pass
|
||||
return 0
|
||||
|
||||
|
||||
def __withConfigPartition(args):
|
||||
def __isWithConfigPartition(self, args):
|
||||
try:
|
||||
if args["create_config_partition"]:
|
||||
createConfig = True
|
||||
|
@ -208,9 +232,9 @@ def __withConfigPartition(args):
|
|||
return createConfig
|
||||
|
||||
|
||||
def __runFDisk(cbox, device, parts, withConfigPartition):
|
||||
def __runFDisk(self, parts):
|
||||
## check if the device is completely filled (to avoid some empty last blocks)
|
||||
avail_size = __getAvailableDeviceSize(device, withConfigPartition)
|
||||
avail_size = self.deviceSize
|
||||
for d in parts: avail_size -= d["size"]
|
||||
isFilled = avail_size == 0
|
||||
proc = subprocess.Popen(
|
||||
|
@ -219,27 +243,27 @@ def __runFDisk(cbox, device, parts, withConfigPartition):
|
|||
stdout = subprocess.PIPE,
|
||||
stderr = subprocess.PIPE,
|
||||
args = [
|
||||
cbox.prefs["Programs"]["super"],
|
||||
cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
self.cbox.prefs["Programs"]["super"],
|
||||
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
"plugin",
|
||||
os.path.join(os.path.dirname(__file__), "root_action.py"),
|
||||
"partition",
|
||||
device])
|
||||
for line in __getSFDiskLayout(parts, withConfigPartition, isFilled):
|
||||
self.device])
|
||||
for line in self.__getSFDiskLayout(parts, isFilled):
|
||||
proc.stdin.write(line + "\n")
|
||||
(output, error) = proc.communicate()
|
||||
if proc.returncode != 0: logger.debug("partitioning failed: %s" % error)
|
||||
if proc.returncode != 0: self.cbox.log.debug("partitioning failed: %s" % error)
|
||||
return proc.returncode == 0
|
||||
|
||||
|
||||
def __getSFDiskLayout(paramParts, withConfigPartition, isFilled):
|
||||
def __getSFDiskLayout(self, paramParts, isFilled):
|
||||
parts = paramParts[:]
|
||||
## first a (possible) configuration partition - so it will be reusable
|
||||
if withConfigPartition:
|
||||
if self.withConfigPartition:
|
||||
## fill the main table (including a config partition)
|
||||
yield ",%d,%s" % (ConfigPartition["size"], ConfigPartition["type"])
|
||||
yield ",%d,%s" % (self.ConfigPartition["size"], self.ConfigPartition["type"])
|
||||
## one primary partition
|
||||
yield ",%d,%s,*" % (parts[0]["size"], PartTypes[parts[0]["type"]][0])
|
||||
yield ",%d,%s,*" % (parts[0]["size"], self.PartTypes[parts[0]["type"]][0])
|
||||
del parts[0]
|
||||
## no extended partition, if there is only one disk
|
||||
if not parts: return
|
||||
|
@ -248,62 +272,58 @@ def __getSFDiskLayout(paramParts, withConfigPartition, isFilled):
|
|||
## an empty partition in main table
|
||||
yield ";"
|
||||
## maybe another empty partition if there is no config partition
|
||||
if not withConfigPartition: yield ";"
|
||||
if not self.withConfigPartition: yield ";"
|
||||
while parts:
|
||||
if isFilled and (len(parts) == 1):
|
||||
yield ",,%s" % (PartTypes[parts[0]["type"]][0],)
|
||||
yield ",,%s" % (self.PartTypes[parts[0]["type"]][0],)
|
||||
else:
|
||||
yield ",%d,%s" % (parts[0]["size"], PartTypes[parts[0]["type"]][0])
|
||||
yield ",%d,%s" % (parts[0]["size"], self.PartTypes[parts[0]["type"]][0])
|
||||
del parts[0]
|
||||
|
||||
|
||||
def __formatPartitions(cbox, device, paramParts, withConfigPartition):
|
||||
success = True
|
||||
def __formatPartitions(self, paramParts):
|
||||
import threading
|
||||
parts = paramParts[:]
|
||||
part_num = 1
|
||||
## maybe a config partition?
|
||||
if withConfigPartition:
|
||||
dev_name = device + str(part_num)
|
||||
logger.info("formatting config partition (%s)" % dev_name)
|
||||
if __formatOnePartition(cbox, dev_name, ConfigPartition["fs"]):
|
||||
__setLabelOfPartition(cbox, dev_name, cbox.prefs["Main"]["ConfigVolumeLabel"])
|
||||
else:
|
||||
success = False
|
||||
if self.withConfigPartition:
|
||||
dev_name = self.device + str(part_num)
|
||||
self.cbox.log.info("formatting config partition (%s)" % dev_name)
|
||||
if self.__formatOnePartition(dev_name, self.ConfigPartition["fs"]):
|
||||
self.__setLabelOfPartition(dev_name, self.cbox.prefs["Main"]["ConfigVolumeLabel"])
|
||||
part_num += 1
|
||||
## the first data partition
|
||||
dev_name = device + str(part_num)
|
||||
partType = PartTypes[parts[0]["type"]][1]
|
||||
logger.info("formatting partition (%s) as '%s'" % (dev_name, partType))
|
||||
if not __formatOnePartition(cbox, dev_name, partType):
|
||||
success = False
|
||||
dev_name = self.device + str(part_num)
|
||||
partType = self.PartTypes[parts[0]["type"]][1]
|
||||
self.cbox.log.info("formatting partition (%s) as '%s'" % (dev_name, partType))
|
||||
yield dev_name
|
||||
if self.__formatOnePartition(dev_name, partType):
|
||||
yield "OK"
|
||||
else:
|
||||
yield "<b>Failed!</b>"
|
||||
del parts[0]
|
||||
## other data partitions
|
||||
part_num = 5
|
||||
while parts:
|
||||
dev_name = device + str(part_num)
|
||||
partType = PartTypes[parts[0]["type"]][1]
|
||||
logger.info("formatting partition (%s) as '%s'" % (dev_name, partType))
|
||||
if not __formatOnePartition(cbox, dev_name, partType):
|
||||
success = False
|
||||
dev_name = self.device + str(part_num)
|
||||
partType = self.PartTypes[parts[0]["type"]][1]
|
||||
self.cbox.log.info("formatting partition (%s) as '%s'" % (dev_name, partType))
|
||||
yield dev_name
|
||||
if self.__formatOnePartition(dev_name, partType):
|
||||
yield "OK"
|
||||
else:
|
||||
yield "<b>Failed!</b>"
|
||||
part_num += 1
|
||||
del parts[0]
|
||||
return success
|
||||
return
|
||||
|
||||
|
||||
def __formatOnePartition(cbox, dev_name, type):
|
||||
import time, sys
|
||||
child_pid = os.fork()
|
||||
## we run formatting as a parallel thread
|
||||
## TODO: the parent thread still waits for the last child - that is not good for big harddisks
|
||||
if child_pid == 0:
|
||||
## we are the child process
|
||||
def __formatOnePartition(self, dev_name, type):
|
||||
proc = subprocess.Popen(
|
||||
shell = False,
|
||||
stdout = subprocess.PIPE,
|
||||
stderr = subprocess.PIPE,
|
||||
args = [
|
||||
cbox.prefs["Programs"]["super"],
|
||||
cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
self.cbox.prefs["Programs"]["super"],
|
||||
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
"plugin",
|
||||
os.path.join(os.path.dirname(__file__), "root_action.py"),
|
||||
"format",
|
||||
|
@ -311,28 +331,20 @@ def __formatOnePartition(cbox, dev_name, type):
|
|||
type])
|
||||
(output, error) = proc.communicate()
|
||||
if proc.returncode != 0:
|
||||
logger.warn("failed to create filesystem on %s: %s" % (dev_name, error))
|
||||
sys.exit(1)
|
||||
else:
|
||||
sys.exit(0)
|
||||
else:
|
||||
time.sleep(1)
|
||||
(pid, exit_state) = os.waitpid(child_pid, os.WNOHANG)
|
||||
if ((pid == 0) and (exit_state == 0)) \
|
||||
or ((pid == child_pid) and (exit_state == 0)):
|
||||
return True
|
||||
else:
|
||||
self.cbox.log.warn("failed to create filesystem on %s: %s" % (dev_name, error))
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
def __setLabelOfPartition(cbox, dev_name, label):
|
||||
def __setLabelOfPartition(self, dev_name, label):
|
||||
proc = subprocess.Popen(
|
||||
shell = False,
|
||||
stdout = subprocess.PIPE,
|
||||
stderr = subprocess.PIPE,
|
||||
args = [
|
||||
cbox.prefs["Programs"]["super"],
|
||||
cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
self.cbox.prefs["Programs"]["super"],
|
||||
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||
"plugin",
|
||||
os.path.join(os.path.dirname(__file__), "root_action.py"),
|
||||
"label",
|
||||
|
@ -342,6 +354,6 @@ def __setLabelOfPartition(cbox, dev_name, label):
|
|||
if proc.returncode == 0:
|
||||
return True
|
||||
else:
|
||||
logger.warn("failed to create filesystem on %s: %s" % (device + str(part_num), error))
|
||||
self.cbox.log.warn("failed to create filesystem on %s: %s" % (dev_name, error))
|
||||
return False
|
||||
|
||||
|
|
|
@ -26,14 +26,28 @@ def __partitionDevice(device):
|
|||
|
||||
|
||||
def __formatPartition(device, type):
|
||||
import time, threading
|
||||
result = True
|
||||
def formatting():
|
||||
proc = subprocess.Popen(
|
||||
shell = False,
|
||||
stdin = subprocess.PIPE,
|
||||
stdout = subprocess.PIPE,
|
||||
stderr = subprocess.PIPE,
|
||||
args = [
|
||||
MKFS_BIN,
|
||||
"-t", type,
|
||||
device])
|
||||
proc.communicate()
|
||||
return proc.returncode == 0
|
||||
## TODO: very ugly way of communication: it assumes, that failures are fast - success is slow
|
||||
if proc.returncode == 0:
|
||||
time.sleep(1)
|
||||
thread = threading.Thread()
|
||||
thread.setDaemon(True)
|
||||
thread.run = formatting
|
||||
thread.start()
|
||||
time.sleep(0.5)
|
||||
return thread.isAlive()
|
||||
|
||||
|
||||
def __labelPartition(device, label):
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
<input type="hidden" name="step" value="add_partition" />
|
||||
<?cs set: x = subcount(Data.Plugins.partition.Parts) ?>
|
||||
<td><?cs var:x ?></td>
|
||||
<td><input type="text" name="part<?cs var:x ?>_size" size="8" value="<?cs var:Data.Plugins.partition.availSize ?>" /></td>
|
||||
<td><input type="text" name="part<?cs var:x ?>_size" size="8" value="<?cs var:Data.Plugins.partition.availSize ?>" tabindex="0" /></td>
|
||||
<td><select name="part<?cs var:x ?>_type" size="0"><?cs each: t = Data.Plugins.partition.Types ?><option <?cs if:t == "windows" ?>selected="selected"<?cs /if ?>><?cs var:t ?></option>
|
||||
<?cs /each ?></select></td>
|
||||
<td>
|
||||
|
|
13
pythonrewrite/plugins/partition/show_format_progress.cs
Normal file
13
pythonrewrite/plugins/partition/show_format_progress.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?cs # $Id$ ?>
|
||||
|
||||
<h1><?cs var:html_escape(Lang.Plugins.partition.Title.Partition) ?></h1>
|
||||
|
||||
<p><?cs var:html_escape(Lang.Plugins.partition.Text.ProgressInfo) ?>
|
||||
<ul>
|
||||
<?cs loop: x = #0, subcount(Data.Plugins.partition.Parts)-1, #1 ?>
|
||||
<li><!-- CONTENT_DUMMY -->:
|
||||
<!-- CONTENT_DUMMY --></li>
|
||||
<?cs /loop ?>
|
||||
</ul>
|
||||
</p>
|
||||
|
|
@ -5,15 +5,20 @@ The following directory structure is required:
|
|||
|
||||
|
||||
Python code interface:
|
||||
def doAction(hdf, cbox, store=None, ???):
|
||||
- create a class with the same name as the plugin - it has to inherit CryptoBoxPlugin
|
||||
- function "doAction":
|
||||
- this function will get called whenever this plugins is involved in a request
|
||||
- all arguments should be optional (e.g. for displaying a form without previous input values)
|
||||
- the argument "store" should be used to process a form submission (just a recommendation)
|
||||
- if the processing failed for some reason (invalid input, ...), it should manually set the "Data.Warning" (resp. "Data.Error" or "Data.Success") to a value of your choice (preferably you may want to use messages of your namespace (e.g. "Plugins.PLUGINNAME.InvalidInput"))
|
||||
- the return value should be the name of the template that should be displayed after processing (a template file in the plugin directory takes precedence over global template files)
|
||||
- the return value may also be a dictionary with the following elements:
|
||||
* template: the name of the template file (mandatory)
|
||||
* generator: a generator object ("yield") - its content will replace every occourence of "<!-- CONTENT_DUMMY -->" in the template (useful for pages that are displayed step by step (as for formatting of filesystems))
|
||||
- an empty (e.g. None) return value can be used to go return to the plugin page
|
||||
|
||||
def def getStatus(cbox):
|
||||
- returns a string, that described a state connected to this plugin (e.g. the current date and time (for the "date" plugin)
|
||||
- function "getStatus":
|
||||
- returns a string, that describes a state connected to this plugin (e.g. the current date and time (for the "date" plugin))
|
||||
|
||||
|
||||
Language file structure:
|
||||
|
|
Loading…
Reference in a new issue