|
|
|
@ -9,17 +9,10 @@ import os
|
|
|
|
|
import types
|
|
|
|
|
from time import sleep
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
# TODO: temporary code
|
|
|
|
|
import sys
|
|
|
|
|
sys.path.insert(0,'/usr/lib/calculate-2.2/calculate-lib/pym/')
|
|
|
|
|
# ENDTODO
|
|
|
|
|
from cl_utils import runOsCommand,isMount,removeDir
|
|
|
|
|
from shutil import copyfile
|
|
|
|
|
|
|
|
|
|
def debug(s):
|
|
|
|
|
print s
|
|
|
|
|
# os.system(s)
|
|
|
|
|
|
|
|
|
|
class DistributiveError(Exception):
|
|
|
|
|
def __init__(self,value,reason=None):
|
|
|
|
@ -57,7 +50,7 @@ class Distributive(object):
|
|
|
|
|
DirectoryDistributive object
|
|
|
|
|
|
|
|
|
|
dist1 = ArchiveDistributive(file="/os.tar.bz2",mdirectory="/tmp/test")
|
|
|
|
|
dist2 = dist1.getAs(DirectoryDistributive)
|
|
|
|
|
dist2 = dist1.convertTo(DirectoryDistributive)
|
|
|
|
|
dist2.detach()
|
|
|
|
|
dist1.close()
|
|
|
|
|
...
|
|
|
|
@ -72,7 +65,7 @@ class Distributive(object):
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
dist1 = PartitionDistributive(partition="/dev/sda2")
|
|
|
|
|
dist2 = dist1.getAs(DirectoryDistributive)
|
|
|
|
|
dist2 = dist1.convertTo(DirectoryDistributive)
|
|
|
|
|
dist1.close()
|
|
|
|
|
"""
|
|
|
|
|
# close all child
|
|
|
|
@ -95,11 +88,19 @@ class Distributive(object):
|
|
|
|
|
unmounted."""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def getAs(self,typeDistributive):
|
|
|
|
|
"""Default getAs raise error about impossible convert object"""
|
|
|
|
|
raise DistributiveError("Cann't convert '%s' to '%s'" %
|
|
|
|
|
(self.__class__.__name__,
|
|
|
|
|
typeDistributive.__name__))
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
"""Overridden"""
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def convertTo(self,typeDistributive):
|
|
|
|
|
"""Default c raise error about impossible convert object"""
|
|
|
|
|
obj = self._convertTo(typeDistributive)
|
|
|
|
|
if obj:
|
|
|
|
|
return obj
|
|
|
|
|
else:
|
|
|
|
|
raise DistributiveError("Cann't convert '%s' to '%s'" %
|
|
|
|
|
(self.__class__.__name__,
|
|
|
|
|
typeDistributive.__name__))
|
|
|
|
|
|
|
|
|
|
# def __del__(self):
|
|
|
|
|
# """Uncomment this method for automaticaly release all distributive
|
|
|
|
@ -110,7 +111,6 @@ class Distributive(object):
|
|
|
|
|
"""Make directory and parent.
|
|
|
|
|
|
|
|
|
|
If directory exists then return False else True"""
|
|
|
|
|
print "makeDirectory:",path
|
|
|
|
|
try:
|
|
|
|
|
parent = os.path.split(path)[0]
|
|
|
|
|
if not pathexists(parent):
|
|
|
|
@ -120,7 +120,7 @@ class Distributive(object):
|
|
|
|
|
return False
|
|
|
|
|
os.mkdir(path)
|
|
|
|
|
return True
|
|
|
|
|
except Exception,e:
|
|
|
|
|
except (Exception,KeyboardInterrupt),e:
|
|
|
|
|
raise DistributiveError("Cann't create directory '%s'"%path,
|
|
|
|
|
reason=str(e))
|
|
|
|
|
|
|
|
|
@ -129,14 +129,14 @@ class Distributive(object):
|
|
|
|
|
print "removeDirectory:",directory
|
|
|
|
|
try:
|
|
|
|
|
removeDir(directory)
|
|
|
|
|
except Exception,e:
|
|
|
|
|
except (Exception,KeyboardInterrupt),e:
|
|
|
|
|
raise DistributiveError("Unable remove directory from '%s'"
|
|
|
|
|
%directory, reason=str(e))
|
|
|
|
|
|
|
|
|
|
def _copyfile(self,infile,outfile):
|
|
|
|
|
try:
|
|
|
|
|
copyfile(infile,outfile)
|
|
|
|
|
except Exception,e:
|
|
|
|
|
except (Exception,KeyboardInterrupt),e:
|
|
|
|
|
raise DistributiveError("Cann't copy '%s' to '%s'"%
|
|
|
|
|
(infile,outfile),reason=str(e))
|
|
|
|
|
|
|
|
|
@ -200,11 +200,9 @@ class DirectoryDistributive(Distributive):
|
|
|
|
|
if not parent:
|
|
|
|
|
self._makeDirectory(self.directory)
|
|
|
|
|
|
|
|
|
|
def getAs(self,typeDistributive):
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self
|
|
|
|
|
else:
|
|
|
|
|
return super(DirectoryDistributive,self).getAs(typeDistributive)
|
|
|
|
|
|
|
|
|
|
def installFrom(self, source):
|
|
|
|
|
"""Install distributive to directory from source distributive"""
|
|
|
|
@ -212,7 +210,7 @@ class DirectoryDistributive(Distributive):
|
|
|
|
|
source.unpackTo(self.directory)
|
|
|
|
|
else:
|
|
|
|
|
# get source distributive as directory distributive
|
|
|
|
|
dFrom = source.getAs(DirectoryDistributive)
|
|
|
|
|
dFrom = source.convertTo(DirectoryDistributive)
|
|
|
|
|
# copy distributive from source to this
|
|
|
|
|
self.copy(dFrom.directory,self.directory)
|
|
|
|
|
|
|
|
|
@ -238,7 +236,7 @@ class PartitionDistributive(Distributive):
|
|
|
|
|
self._umountPartition(child.directory)
|
|
|
|
|
child.directory = None
|
|
|
|
|
|
|
|
|
|
def _getAsDirectory(self):
|
|
|
|
|
def _convertToDirectory(self):
|
|
|
|
|
mdirectory = self.mdirectory
|
|
|
|
|
for child in self.childs:
|
|
|
|
|
if isinstance(child,DirectoryDistributive) and \
|
|
|
|
@ -248,23 +246,21 @@ class PartitionDistributive(Distributive):
|
|
|
|
|
self._mountPartition(self.partition,mdirectory)
|
|
|
|
|
return DirectoryDistributive(directory=mdirectory,parent=self)
|
|
|
|
|
|
|
|
|
|
def getAs(self,typeDistributive):
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self._getAsDirectory()
|
|
|
|
|
return self._convertToDirectory()
|
|
|
|
|
if typeDistributive == PartitionDistributive:
|
|
|
|
|
return self
|
|
|
|
|
else:
|
|
|
|
|
return super(PartitionDistributive,self).getAs(typeDistributive)
|
|
|
|
|
|
|
|
|
|
def installFrom(self, source):
|
|
|
|
|
"""Install distributive to partition from source distributive"""
|
|
|
|
|
# get currect partition as directory
|
|
|
|
|
distrTo = self.getAs(DirectoryDistributive)
|
|
|
|
|
distrTo = self.convertTo(DirectoryDistributive)
|
|
|
|
|
# install into directroy distributive from source
|
|
|
|
|
distrTo.installFrom(source)
|
|
|
|
|
|
|
|
|
|
class ArchiveDistributive(Distributive):
|
|
|
|
|
def __init__(self,file=None,parent=None,mdirectory="/mnt/calculate"):
|
|
|
|
|
def __init__(self,file=None,parent=None,mdirectory="/var/calculate/tmp/stage"):
|
|
|
|
|
super(ArchiveDistributive,self).__init__(parent=parent)
|
|
|
|
|
self.file = file
|
|
|
|
|
self.mdirectory = mdirectory
|
|
|
|
@ -309,7 +305,7 @@ class ArchiveDistributive(Distributive):
|
|
|
|
|
"""Unpack currect archive to directory"""
|
|
|
|
|
self._unpackArchive(self.file,directory)
|
|
|
|
|
|
|
|
|
|
def _getAsDirectory(self):
|
|
|
|
|
def _convertToDirectory(self):
|
|
|
|
|
"""Get archive as directory (unpack to directory)"""
|
|
|
|
|
# check may be the archive already unpacked
|
|
|
|
|
mdirectory = self.mdirectory
|
|
|
|
@ -319,8 +315,9 @@ class ArchiveDistributive(Distributive):
|
|
|
|
|
return child
|
|
|
|
|
# get temporary directory for unpacking
|
|
|
|
|
mdirectory = self.getMntDirectory(mdirectory)
|
|
|
|
|
dirdist = DirectoryDistributive(directory=mdirectory,parent=self)
|
|
|
|
|
self._unpackArchive(self.file,mdirectory)
|
|
|
|
|
return DirectoryDistributive(directory=mdirectory,parent=self)
|
|
|
|
|
return dirdist
|
|
|
|
|
|
|
|
|
|
def releaseChild(self,child):
|
|
|
|
|
"""Remove child Directory distributive"""
|
|
|
|
@ -328,14 +325,12 @@ class ArchiveDistributive(Distributive):
|
|
|
|
|
self._removeDirectory(child.directory)
|
|
|
|
|
child.directory = None
|
|
|
|
|
|
|
|
|
|
def getAs(self,typeDistributive):
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
"""Support DirectroyDistributive and ArchiveDistributive"""
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self._getAsDirectory()
|
|
|
|
|
return self._convertToDirectory()
|
|
|
|
|
if typeDistributive == ArchiveDistributive:
|
|
|
|
|
return self
|
|
|
|
|
else:
|
|
|
|
|
return super(ArchiveDistributive,self).getAs(typeDistributive)
|
|
|
|
|
|
|
|
|
|
def packToArchive(self,directory,file):
|
|
|
|
|
res,errmes = runOsCommand("tar cf %s -C %s ."%(file,directory))
|
|
|
|
@ -346,7 +341,7 @@ class ArchiveDistributive(Distributive):
|
|
|
|
|
def installFrom(self, source):
|
|
|
|
|
"""Install distributive to partition from source distributive"""
|
|
|
|
|
# get source distributive as directory distributive
|
|
|
|
|
dFrom = source.getAs(DirectoryDistributive)
|
|
|
|
|
dFrom = source.convertTo(DirectoryDistributive)
|
|
|
|
|
# install into directroy distributive from source
|
|
|
|
|
self.packToArchive(dFrom.directory, self.file)
|
|
|
|
|
|
|
|
|
@ -365,7 +360,7 @@ class SquashDistributive(Distributive):
|
|
|
|
|
self._umountDirectory(directory)
|
|
|
|
|
self._removeDirectory(directory)
|
|
|
|
|
|
|
|
|
|
def _getAsDirectory(self):
|
|
|
|
|
def _convertToDirectory(self):
|
|
|
|
|
mdirectory = self.mdirectory
|
|
|
|
|
for child in self.childs:
|
|
|
|
|
if isinstance(child,DirectoryDistributive) and \
|
|
|
|
@ -381,13 +376,11 @@ class SquashDistributive(Distributive):
|
|
|
|
|
self._umountSquash(child.directory)
|
|
|
|
|
child.directory = None
|
|
|
|
|
|
|
|
|
|
def getAs(self,typeDistributive):
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self._getAsDirectory()
|
|
|
|
|
if typeDistributive == SquashDistributive:
|
|
|
|
|
return self._convertToDirectory()
|
|
|
|
|
elif typeDistributive == SquashDistributive:
|
|
|
|
|
return self
|
|
|
|
|
else:
|
|
|
|
|
return super(SquashDistributive,self).getAs(typeDistributive)
|
|
|
|
|
|
|
|
|
|
def packToSquash(self,directory,file):
|
|
|
|
|
res,errmes = runOsCommand("/usr/bin/mksquashfs %s/ %s"%
|
|
|
|
@ -399,7 +392,7 @@ class SquashDistributive(Distributive):
|
|
|
|
|
def installFrom(self, source):
|
|
|
|
|
"""Install distributive to partition from source distributive"""
|
|
|
|
|
# get source distributive as directory distributive
|
|
|
|
|
dFrom = source.getAs(DirectoryDistributive)
|
|
|
|
|
dFrom = source.convertTo(DirectoryDistributive)
|
|
|
|
|
# install into directroy distributive from source
|
|
|
|
|
self.packToSquash(dFrom.directory, self.file)
|
|
|
|
|
|
|
|
|
@ -436,7 +429,7 @@ class IsoDistributive(Distributive):
|
|
|
|
|
curnum = num
|
|
|
|
|
return curfile
|
|
|
|
|
|
|
|
|
|
def _getAsSquash(self):
|
|
|
|
|
def _convertToSquash(self):
|
|
|
|
|
mdirectory = self.mdirectory
|
|
|
|
|
for child in self.childs:
|
|
|
|
|
if isinstance(child,SquashDistributive) and \
|
|
|
|
@ -457,15 +450,14 @@ class IsoDistributive(Distributive):
|
|
|
|
|
self._umountIso(pathdirname(child.file))
|
|
|
|
|
child.directory = None
|
|
|
|
|
|
|
|
|
|
def getAs(self,typeDistributive):
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self.getAs(SquashDistributive).getAs(DirectoryDistributive)
|
|
|
|
|
return self.convertTo(SquashDistributive).\
|
|
|
|
|
convertTo(DirectoryDistributive)
|
|
|
|
|
if typeDistributive == SquashDistributive:
|
|
|
|
|
return self._getAsSquash()
|
|
|
|
|
return self._Squash()
|
|
|
|
|
if typeDistributive == IsoDistributive:
|
|
|
|
|
return self
|
|
|
|
|
else:
|
|
|
|
|
return super(IsoDistributive,self).getAs(typeDistributive)
|
|
|
|
|
|
|
|
|
|
def prepareIso(self,directory):
|
|
|
|
|
print("apply iso templates to %s/"%directory)
|
|
|
|
@ -474,12 +466,11 @@ class IsoDistributive(Distributive):
|
|
|
|
|
pathjoin(directory,"isolinux/isolinux.bin"))
|
|
|
|
|
|
|
|
|
|
def packToIso(self,directory,file):
|
|
|
|
|
self.prepareIso(directory)
|
|
|
|
|
# remove previous version of iso
|
|
|
|
|
try:
|
|
|
|
|
if pathexists(file):
|
|
|
|
|
os.unlink(file)
|
|
|
|
|
except Exception,e:
|
|
|
|
|
except (Exception,KeyboardInterrupt),e:
|
|
|
|
|
raise DistributiveError("Cann't remove %s"%file,reason=str(e))
|
|
|
|
|
|
|
|
|
|
res,errmes = runOsCommand("/usr/bin/mkisofs -b isolinux/isolinux.bin -no-emul-boot -boot-load-size 4 -boot-info-table -iso-level 4 -hide boot.catalog -o %s %s/"%
|
|
|
|
@ -497,34 +488,26 @@ class IsoDistributive(Distributive):
|
|
|
|
|
isoDirectory = self.getMntDirectory(isoDirectory)
|
|
|
|
|
self._makeDirectory(isoDirectory)
|
|
|
|
|
|
|
|
|
|
# getting squash from source
|
|
|
|
|
try:
|
|
|
|
|
# getting squash from source
|
|
|
|
|
if isinstance(source,SquashDistributive):
|
|
|
|
|
self._copyfile(source.file,
|
|
|
|
|
pathjoin(isoDirectory,"livecd.squashfs"))
|
|
|
|
|
else:
|
|
|
|
|
distDirectory = source.getAs(DirectoryDistributive)
|
|
|
|
|
distDirectory = source.convertTo(DirectoryDistributive)
|
|
|
|
|
squashDistr = SquashDistributive(
|
|
|
|
|
file=pathjoin(isoDirectory,"livecd.squashfs"))
|
|
|
|
|
file=pathjoin(isoDirectory,"livecd.squashfs"))
|
|
|
|
|
squashDistr.installFrom(distDirectory)
|
|
|
|
|
|
|
|
|
|
# prepare iso
|
|
|
|
|
self.prepareIso(directory)
|
|
|
|
|
# pack iso
|
|
|
|
|
self.packToIso(isoDirectory, self.file)
|
|
|
|
|
# remove temporary directory after creating iso image
|
|
|
|
|
self._removeDirectory(isoDirectory)
|
|
|
|
|
except DistributiveError,e:
|
|
|
|
|
self._removeDirectory(isoDirectory)
|
|
|
|
|
raise e
|
|
|
|
|
# remove temporary directory after creating iso image
|
|
|
|
|
self._removeDirectory(isoDirectory)
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
q = PartitionDistributive(partition="/dev/sda2")
|
|
|
|
|
#q = IsoDistributive(file="/tmp/my.iso")
|
|
|
|
|
q2 = ArchiveDistributive(file="/usr/calculate/share/stages/stage3-i686-20100601.tar.bz2")
|
|
|
|
|
try:
|
|
|
|
|
q.installFrom(q2)
|
|
|
|
|
except DistributiveError,e:
|
|
|
|
|
print "%s,%s"% (e.value, e.reason)
|
|
|
|
|
q2.close()
|
|
|
|
|
q.close()
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|
|
|
|
|
except KeyboardInterrupt,e:
|
|
|
|
|
self._removeDirectory(isoDirectory)
|
|
|
|
|
raise DistributiveError("Keyboard interrupt")
|
|
|
|
|