Write installation

netsetup
Mike Hiretsky 14 years ago
parent 57c3394fe7
commit 15d1d79989

@ -10,7 +10,7 @@ import types
from time import sleep
import re
import sys
from cl_utils import runOsCommand,isMount,removeDir
from cl_utils import runOsCommand,isMount,removeDir,typeFile
from shutil import copyfile
@ -42,7 +42,6 @@ class DistributiveRepository:
'march':"|".join(marches),
'ext':"|".join(extensiton)
}, re.X)
def __init__(self,directory):
self.root = directory
@ -63,10 +62,31 @@ class DistributiveRepository:
return True
return [ dist for dist in os.listdir(self.root) if distfilter(dist) ]
def _getDistributiveByFile(self,filename):
# MAGIC_COMPRESS 0x000004 Check inside compressed files
tf = typeFile(magic=0x4)
ftype = tf.getMType(filename)
if "ISO 9660 CD-ROM" in ftype:
return IsoDistributive(filename)
elif "7-zip" in ftype or \
"POSIX tar archive" in ftype:
return ArchiveDistributive(filename)
elif "Squashfs filesystem" in ftype:
return SquashDistributive(filename)
else:
raise DistributiveError("Wrong distributive '%s':\n%s"%
(filename,ftype))
def getLastDistributive(self,system=None,shortname=None,march=None,
version=None):
return self._getAvailableDistributives(system,shortname,march,
version)[0]
availDistrs = self._getAvailableDistributives(system,shortname,
march,version)
# TODO: add priority for select actualy distributive
if availDistrs:
return self._getDistributiveByFile(
pathjoin(self.root,availDistrs[0]))
else:
return None
class Distributive(object):
"""Distributive object. Parent object for all distributive."""
@ -118,7 +138,7 @@ class Distributive(object):
self.childs = []
# if has parent
if self.parent:
self.parent().releaseChild(self)
self.parent(None).releaseChild(self)
self.parent = None
def releaseChild(self,child):
@ -148,6 +168,9 @@ class Distributive(object):
# instance"""
# self.close()
def getDirectory(self):
return self.convertTo(DirectoryDistributive).directory
def _makeDirectory(self,path):
"""Make directory and parent.
@ -258,12 +281,20 @@ class DirectoryDistributive(Distributive):
class PartitionDistributive(Distributive):
reRightPartition = re.compile(r"^/dev/(sd[a-z]\d+|cciss/c\d+d\d+)$")
def __init__(self,partition,parent=None,mdirectory="/mnt/calculate"):
"""Raise DistributiveError if partition has bad name"""
def __init__(self,partition,parent=None,mdirectory="/mnt/calculate",
check=False):
"""Initialize partition distributive
mdirectory - directory for mount
check - check partition name and raise DistributiveError if partition
has bad name
"""
Distributive.__init__(self,parent=parent)
self.partition = partition
self.mdirectory = mdirectory
if not self.reRightPartition.match(partition):
if check and not partition is None and \
(not self.reRightPartition.match(partition) or \
not pathexists(partition)):
raise DistributiveError("Wrong partition name '%s'"%partition)
def _mountPartition(self,partition,directory):
@ -326,26 +357,30 @@ class ArchiveDistributive(Distributive):
else:
return None
def _unpackArchive(self,file,directory):
def _unpackArchive(self,archfile,directory):
"""Unpack archive"""
# archive is exists
if not pathexists(file):
raise DistributiveError("File '%s' not found"%file)
if not pathexists(archfile):
raise DistributiveError("File '%s' not found"%archfile)
# detect type archive
archiveType = self._detectArchive(file)
archiveType = self._detectArchive(archfile)
# make directory if archive was detected normally
if archiveType:
self._makeDirectory(directory)
# unpack archive
if archiveType == "7z":
res,mes = runOsCommand("7za x -so %s | tar xf - -C %s/"%
(file,directory))
(archfile,directory))
elif archiveType == "bzip2":
res,mes = runOsCommand("tar xjf %s -C %s/"% (file,directory))
res,mes = runOsCommand("tar xjf %s -C %s/"%
(archfile,directory))
elif archiveType == "gzip":
res,mes = runOsCommand("tar xf %s -C %s/"%(file,directory))
res,mes = runOsCommand("tar xf %s -C %s/"%
(archfile,directory))
else:
raise DistributiveError("Unknown archive type by '%s'"%file)
if res != 0:
raise DistributiveError("Error during unpacking\n%s"%mes)
def unpackTo(self,directory):
"""Unpack currect archive to directory"""
@ -400,7 +435,7 @@ class SquashDistributive(Distributive):
def _mountSquash(self,file,directory):
"""Mount squashfs to directory"""
self._makeDirectory(directory)
self._mountPartition(file,directory,mountopts="-o loop -t squashfs")
self._mountToDirectory(file,directory,mountopts="-o loop -t squashfs")
def _umountSquash(self,directory):
self._umountDirectory(directory)
@ -464,7 +499,7 @@ class IsoDistributive(Distributive):
curfile = None
curnum = -1
for file in os.listdir(directory):
res = self.reLive.search(i)
res = self.reLive.search(file)
if res:
if res.groups()[1]:
num = int(res.groups()[1])
@ -557,3 +592,6 @@ class IsoDistributive(Distributive):
except KeyboardInterrupt,e:
self._removeDirectory(isoDirectory)
raise DistributiveError("Keyboard interrupt")
class ScratchDistributive(Distributive):
pass

@ -15,7 +15,6 @@
# limitations under the License.
__version__ = "2.2.0"
__version_info__ = tuple([ int(num) for num in __version__.split('.')])
__app__ = "calculate-install"
import os
@ -27,7 +26,9 @@ from cl_template import template
from cl_fill import clLocale
from cl_datavars import DataVars
from cl_print import color_print
from cl_distr import PartitionDistributive,DistributiveRepository
from cl_distr import PartitionDistributive,DistributiveRepository,\
DistributiveError, ScratchDistributive
from time import sleep
tr = lang()
tr.setGlobalDomain('cl_install')
@ -59,11 +60,11 @@ class cl_install(color_print):
self.clTemp = None
self.color = True
def colorPrint(self,attr,fg,bg,string):
def colorPrint(self,*argv,**kwarg):
if self.color:
color_print.colorPrint(self,attr,fg,bg,string)
color_print.colorPrint(self,*argv,**kwarg)
else:
sys.stdout.write(string)
sys.stdout.write(argv[-1])
sys.stdout.flush()
def setNoColor(self):
@ -86,13 +87,71 @@ class cl_install(color_print):
else:
return dirsFiles
def installSystem(self,disk=""):
def printInfo(self,sourceDistr,targetDistr):
self.printSUCCESS(_("Machine hardware name: %s")%
self.clVars.Get('os_arch_machine'))
self.printSUCCESS("Found update: %s"%self.clVars.Get('os_linux_name'))
def wait(self,sec=10):
for i in xrange(sec,0,-1):
self.printSUCCESS(_("Press %s to cancel")%"Ctrl+C"+"... %d"%i)
sleep(1)
def prepareBoot(self):
pass
def getTargetDistributive(self,disk,buildermode):
if buildermode:
return ScratchDistributive(disk,mdirectory="/mnt/install",
check=True)
else:
return PartitionDistributive(disk,mdirectory="/mnt/install",
check=True)
def joinTemplates(self,directory):
pass
def installSystem(self,disk="",buildermode=False):
"""install System by current variable enviroment"""
targetDistr = PartitionDistributive(disk,mdirectory="/mnt/install")
distRep = DistributiveRepository('/usr/calculate/share/linux')
distRep.getLastDistributive(
march=self.clVars.Get('os_arch_machine'),
shortname=self.clVars.Get('os_linux_shortname').lower())
sourceDistr = None
targetDistr = None
error = None
try:
targetDistr = self.getTargetDistributive(disk,buildermode)
distRep = DistributiveRepository('/usr/calculate/share/linux')
sourceDistr = distRep.getLastDistributive(
march=self.clVars.Get('os_arch_machine'),
shortname=self.clVars.Get('os_linux_shortname').lower())
if sourceDistr:
# print info
self.printInfo(sourceDistr,targetDistr)
# wait 10 sec
self.wait(3)
# install distributive
self.printSUCCESS(_("Unpacking system image into target"))
targetDistr.installFrom(sourceDistr)
self.printOK("Unpacking complete")
# join templates
self.printSUCCESS(_("Update config"))
self.joinTemplates(targetDistr.getDirectory())
# change boot config
self.printSUCCESS(_("Prepare system for reboot"))
self.prepareBoot()
else:
self.printWARNING("No update available.")
except DistributiveError,e:
error = e
except KeyboardInterrupt,e:
error = _("Installation manually interrupt")
if sourceDistr:
sourceDistr.close()
if targetDistr:
targetDistr.close()
if error:
self.printERROR(str(error))
return False
return False
def setAllLocaleByLang(self,lang):
"""Set all locale variable by specified lang"""

@ -35,6 +35,10 @@ CMD_OPTIONS = [{'shortOption':"T",
'longOption':"disk",
'optVal':"DISK",
'help':_("the disk for installation")
},
{'shortOption':"b",
'longOption':"build",
'help':_("installation for assembling")
}]
class install_cmd(cl_install,opt,share_cmd):
@ -103,10 +107,9 @@ class install_cmd(cl_install,opt,share_cmd):
return False
def installSystem(self):
try:
cl_install.installSystem(self,disk=self.values.d)
except InstallError,e:
print e
if cl_install.installSystem(self,disk=self.values.d,
buildermode=self.values.b):
print "installation complete"
return True
else:
return False
print "installation complete"
return True

@ -35,3 +35,4 @@ class Data:
cl_ver = {'value':'2.2.0'}
os_arch_machine = {'mode':'w'}
os_linux_shortname = {'mode':'w'}

@ -2,7 +2,7 @@
# setup.py --- Setup script for calculate-install
#Copyright 2008 Calculate Pack, http://www.calculate-linux.org
# Copyright 2010 Mir Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

Loading…
Cancel
Save