|
|
|
@ -12,6 +12,7 @@ import re
|
|
|
|
|
import sys
|
|
|
|
|
from cl_utils import runOsCommand,isMount,removeDir,typeFile
|
|
|
|
|
from shutil import copyfile
|
|
|
|
|
from cl_template import _terms
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DistributiveError(Exception):
|
|
|
|
@ -47,6 +48,7 @@ class DistributiveRepository:
|
|
|
|
|
|
|
|
|
|
def _getAvailableDistributives(self,system=None,shortname=None,march=None,
|
|
|
|
|
version=None):
|
|
|
|
|
"""Get all distributives by filter"""
|
|
|
|
|
def distfilter(dist):
|
|
|
|
|
match = self.reDistName.match(dist)
|
|
|
|
|
if not match:
|
|
|
|
@ -63,6 +65,7 @@ class DistributiveRepository:
|
|
|
|
|
return [ dist for dist in os.listdir(self.root) if distfilter(dist) ]
|
|
|
|
|
|
|
|
|
|
def _getDistributiveByFile(self,filename):
|
|
|
|
|
"""Get Distributive object by filename"""
|
|
|
|
|
# MAGIC_COMPRESS 0x000004 Check inside compressed files
|
|
|
|
|
tf = typeFile(magic=0x4)
|
|
|
|
|
ftype = tf.getMType(filename)
|
|
|
|
@ -77,14 +80,27 @@ class DistributiveRepository:
|
|
|
|
|
raise DistributiveError("Wrong distributive '%s':\n%s"%
|
|
|
|
|
(filename,ftype))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getLastDistributive(self,system=None,shortname=None,march=None,
|
|
|
|
|
version=None):
|
|
|
|
|
"""Get the actualest distributive"""
|
|
|
|
|
availDistrs = self._getAvailableDistributives(system,shortname,
|
|
|
|
|
march,version)
|
|
|
|
|
# TODO: add priority for select actualy distributive
|
|
|
|
|
# TODO: improve selecting best distributive
|
|
|
|
|
if availDistrs:
|
|
|
|
|
term = _terms()
|
|
|
|
|
bestDistr = availDistrs[0]
|
|
|
|
|
bestDict = self.reDistName.match(bestDistr).groupdict()
|
|
|
|
|
for dist in availDistrs[1:]:
|
|
|
|
|
match = self.reDistName.match(dist)
|
|
|
|
|
d = match.groupdict()
|
|
|
|
|
v1,v2 = term._convertVers(d['ver'],bestDict['ver'])
|
|
|
|
|
if v1 > v2 or \
|
|
|
|
|
bestDict['ext'] != 'iso' and d['ext'] == 'iso':
|
|
|
|
|
bestDistr = dist
|
|
|
|
|
bestDict = d
|
|
|
|
|
return self._getDistributiveByFile(
|
|
|
|
|
pathjoin(self.root,availDistrs[0]))
|
|
|
|
|
pathjoin(self.root,bestDistr))
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
@ -111,7 +127,7 @@ class Distributive(object):
|
|
|
|
|
DirectoryDistributive object
|
|
|
|
|
|
|
|
|
|
dist1 = ArchiveDistributive(file="/os.tar.bz2",mdirectory="/tmp/test")
|
|
|
|
|
dist2 = dist1.convertTo(DirectoryDistributive)
|
|
|
|
|
dist2 = dist1.convertToDirectory()
|
|
|
|
|
dist2.detach()
|
|
|
|
|
dist1.close()
|
|
|
|
|
...
|
|
|
|
@ -126,7 +142,7 @@ class Distributive(object):
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
dist1 = PartitionDistributive(partition="/dev/sda2")
|
|
|
|
|
dist2 = dist1.convertTo(DirectoryDistributive)
|
|
|
|
|
dist2 = dist1.convertToDirectory()
|
|
|
|
|
dist1.close()
|
|
|
|
|
"""
|
|
|
|
|
# close all child
|
|
|
|
@ -149,27 +165,25 @@ class Distributive(object):
|
|
|
|
|
unmounted."""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
"""Overridden"""
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def convertTo(self,typeDistributive):
|
|
|
|
|
def convertToDirectory(self):
|
|
|
|
|
"""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'" %
|
|
|
|
|
raise DistributiveError("Cann't convert '%s' to '%s'" %
|
|
|
|
|
(self.__class__.__name__,
|
|
|
|
|
typeDistributive.__name__))
|
|
|
|
|
"DirectoryDistributive"))
|
|
|
|
|
|
|
|
|
|
# def __del__(self):
|
|
|
|
|
# """Uncomment this method for automaticaly release all distributive
|
|
|
|
|
# instance"""
|
|
|
|
|
# self.close()
|
|
|
|
|
|
|
|
|
|
def runOsCommand(self, *argv, **kwarg):
|
|
|
|
|
res,mes = runOsCommand(*argv,**kwarg)
|
|
|
|
|
if type(mes) == types.ListType:
|
|
|
|
|
mes = "\n".join(map(lambda x: x.strip(), mes))
|
|
|
|
|
return res,mes
|
|
|
|
|
|
|
|
|
|
def getDirectory(self):
|
|
|
|
|
return self.convertTo(DirectoryDistributive).directory
|
|
|
|
|
return self.convertToDirectory().directory
|
|
|
|
|
|
|
|
|
|
def _makeDirectory(self,path):
|
|
|
|
|
"""Make directory and parent.
|
|
|
|
@ -206,7 +220,7 @@ class Distributive(object):
|
|
|
|
|
|
|
|
|
|
def copy(self,fromdir,todir):
|
|
|
|
|
"""Copy files from 'fromdir' directory to 'todir' directory"""
|
|
|
|
|
res,errmes = runOsCommand("rsync -a -x %s/ %s/" % (fromdir,todir))
|
|
|
|
|
res,errmes = self.runOsCommand("rsync -a -x %s/ %s/" % (fromdir,todir))
|
|
|
|
|
if res == 0:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
@ -223,7 +237,7 @@ class Distributive(object):
|
|
|
|
|
if isMount(directory):
|
|
|
|
|
raise DistributiveError("Cann't mount to directory:\n"+
|
|
|
|
|
"Directory already mounted")
|
|
|
|
|
res,errmes = runOsCommand("/bin/mount %s %s %s"%
|
|
|
|
|
res,errmes = self.runOsCommand("/bin/mount %s %s %s"%
|
|
|
|
|
(mountopts,file,directory))
|
|
|
|
|
if res == 0:
|
|
|
|
|
return True
|
|
|
|
@ -239,7 +253,7 @@ class Distributive(object):
|
|
|
|
|
if isMount(directory):
|
|
|
|
|
for wait in [0,0.5,2,5]:
|
|
|
|
|
sleep(wait)
|
|
|
|
|
res,errmes = runOsCommand("/bin/umount %s"%directory)
|
|
|
|
|
res,errmes = self.runOsCommand("/bin/umount %s"%directory)
|
|
|
|
|
if res == 0:
|
|
|
|
|
return True
|
|
|
|
|
raise DistributiveError("Cann't umount %s:\n%s"%
|
|
|
|
@ -264,9 +278,8 @@ class DirectoryDistributive(Distributive):
|
|
|
|
|
if not parent:
|
|
|
|
|
self._makeDirectory(self.directory)
|
|
|
|
|
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self
|
|
|
|
|
def convertToDirectory(self):
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def installFrom(self, source):
|
|
|
|
|
"""Install distributive to directory from source distributive"""
|
|
|
|
@ -274,7 +287,7 @@ class DirectoryDistributive(Distributive):
|
|
|
|
|
source.unpackTo(self.directory)
|
|
|
|
|
else:
|
|
|
|
|
# get source distributive as directory distributive
|
|
|
|
|
dFrom = source.convertTo(DirectoryDistributive)
|
|
|
|
|
dFrom = source.convertToDirectory()
|
|
|
|
|
# copy distributive from source to this
|
|
|
|
|
self.copy(dFrom.directory,self.directory)
|
|
|
|
|
|
|
|
|
@ -313,7 +326,7 @@ class PartitionDistributive(Distributive):
|
|
|
|
|
self._umountPartition(child.directory)
|
|
|
|
|
child.directory = None
|
|
|
|
|
|
|
|
|
|
def _convertToDirectory(self):
|
|
|
|
|
def convertToDirectory(self):
|
|
|
|
|
mdirectory = self.mdirectory
|
|
|
|
|
for child in self.childs:
|
|
|
|
|
if isinstance(child,DirectoryDistributive) and \
|
|
|
|
@ -323,16 +336,19 @@ class PartitionDistributive(Distributive):
|
|
|
|
|
self._mountPartition(self.partition,mdirectory)
|
|
|
|
|
return DirectoryDistributive(mdirectory,parent=self)
|
|
|
|
|
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self._convertToDirectory()
|
|
|
|
|
if typeDistributive == PartitionDistributive:
|
|
|
|
|
return self
|
|
|
|
|
def formatPartition(self,format="reiserfs"):
|
|
|
|
|
res,errmes = self.runOsCommand("mkfs.reiserfs -f %s" % self.partition)
|
|
|
|
|
if res == 0:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
raise DistributiveError("Cann't format partition %s:\n%s"%
|
|
|
|
|
(self.partition,errmes))
|
|
|
|
|
|
|
|
|
|
def installFrom(self, source):
|
|
|
|
|
"""Install distributive to partition from source distributive"""
|
|
|
|
|
# get currect partition as directory
|
|
|
|
|
distrTo = self.convertTo(DirectoryDistributive)
|
|
|
|
|
self.formatPartition()
|
|
|
|
|
distrTo = self.convertToDirectory()
|
|
|
|
|
# install into directroy distributive from source
|
|
|
|
|
distrTo.installFrom(source)
|
|
|
|
|
|
|
|
|
@ -347,7 +363,7 @@ class ArchiveDistributive(Distributive):
|
|
|
|
|
|
|
|
|
|
Return bzip2,gzip,7z or None
|
|
|
|
|
"""
|
|
|
|
|
res,mes = runOsCommand("/usr/bin/file %s"%file)
|
|
|
|
|
res,mes = self.runOsCommand("/usr/bin/file %s"%file)
|
|
|
|
|
if "bzip2 compressed data" in mes:
|
|
|
|
|
return "bzip2"
|
|
|
|
|
elif "gzip compressed data" in mes:
|
|
|
|
@ -369,13 +385,13 @@ class ArchiveDistributive(Distributive):
|
|
|
|
|
self._makeDirectory(directory)
|
|
|
|
|
# unpack archive
|
|
|
|
|
if archiveType == "7z":
|
|
|
|
|
res,mes = runOsCommand("7za x -so %s | tar xf - -C %s/"%
|
|
|
|
|
res,mes = self.runOsCommand("7za x -so %s | tar xf - -C %s/"%
|
|
|
|
|
(archfile,directory))
|
|
|
|
|
elif archiveType == "bzip2":
|
|
|
|
|
res,mes = runOsCommand("tar xjf %s -C %s/"%
|
|
|
|
|
res,mes = self.runOsCommand("tar xjf %s -C %s/"%
|
|
|
|
|
(archfile,directory))
|
|
|
|
|
elif archiveType == "gzip":
|
|
|
|
|
res,mes = runOsCommand("tar xf %s -C %s/"%
|
|
|
|
|
res,mes = self.runOsCommand("tar xf %s -C %s/"%
|
|
|
|
|
(archfile,directory))
|
|
|
|
|
else:
|
|
|
|
|
raise DistributiveError("Unknown archive type by '%s'"%file)
|
|
|
|
@ -386,7 +402,7 @@ class ArchiveDistributive(Distributive):
|
|
|
|
|
"""Unpack currect archive to directory"""
|
|
|
|
|
self._unpackArchive(self.file,directory)
|
|
|
|
|
|
|
|
|
|
def _convertToDirectory(self):
|
|
|
|
|
def convertToDirectory(self):
|
|
|
|
|
"""Get archive as directory (unpack to directory)"""
|
|
|
|
|
# check may be the archive already unpacked
|
|
|
|
|
mdirectory = self.mdirectory
|
|
|
|
@ -406,15 +422,8 @@ class ArchiveDistributive(Distributive):
|
|
|
|
|
self._removeDirectory(child.directory)
|
|
|
|
|
child.directory = None
|
|
|
|
|
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
"""Support DirectroyDistributive and ArchiveDistributive"""
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self._convertToDirectory()
|
|
|
|
|
if typeDistributive == ArchiveDistributive:
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def packToArchive(self,directory,file):
|
|
|
|
|
res,errmes = runOsCommand("tar cf %s -C %s ."%(file,directory))
|
|
|
|
|
res,errmes = self.runOsCommand("tar cf %s -C %s ."%(file,directory))
|
|
|
|
|
if res != 0:
|
|
|
|
|
raise DistributiveError("Cann't create archive '%s':\n%s"%
|
|
|
|
|
(file,errmes))
|
|
|
|
@ -422,7 +431,7 @@ class ArchiveDistributive(Distributive):
|
|
|
|
|
def installFrom(self, source):
|
|
|
|
|
"""Install distributive to partition from source distributive"""
|
|
|
|
|
# get source distributive as directory distributive
|
|
|
|
|
dFrom = source.convertTo(DirectoryDistributive)
|
|
|
|
|
dFrom = source.convertToDirectory()
|
|
|
|
|
# install into directroy distributive from source
|
|
|
|
|
self.packToArchive(dFrom.directory, self.file)
|
|
|
|
|
|
|
|
|
@ -441,7 +450,7 @@ class SquashDistributive(Distributive):
|
|
|
|
|
self._umountDirectory(directory)
|
|
|
|
|
self._removeDirectory(directory)
|
|
|
|
|
|
|
|
|
|
def _convertToDirectory(self):
|
|
|
|
|
def convertToDirectory(self):
|
|
|
|
|
mdirectory = self.mdirectory
|
|
|
|
|
for child in self.childs:
|
|
|
|
|
if isinstance(child,DirectoryDistributive) and \
|
|
|
|
@ -457,14 +466,8 @@ class SquashDistributive(Distributive):
|
|
|
|
|
self._umountSquash(child.directory)
|
|
|
|
|
child.directory = None
|
|
|
|
|
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self._convertToDirectory()
|
|
|
|
|
elif typeDistributive == SquashDistributive:
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def packToSquash(self,directory,file):
|
|
|
|
|
res,errmes = runOsCommand("/usr/bin/mksquashfs %s/ %s"%
|
|
|
|
|
res,errmes = self.runOsCommand("/usr/bin/mksquashfs %s/ %s"%
|
|
|
|
|
(directory,file))
|
|
|
|
|
if res != 0:
|
|
|
|
|
raise DistributiveError("Cann't create squashfs '%s':\n%s"%
|
|
|
|
@ -473,7 +476,7 @@ class SquashDistributive(Distributive):
|
|
|
|
|
def installFrom(self, source):
|
|
|
|
|
"""Install distributive to partition from source distributive"""
|
|
|
|
|
# get source distributive as directory distributive
|
|
|
|
|
dFrom = source.convertTo(DirectoryDistributive)
|
|
|
|
|
dFrom = source.convertToDirectory()
|
|
|
|
|
# install into directroy distributive from source
|
|
|
|
|
self.packToSquash(dFrom.directory, self.file)
|
|
|
|
|
|
|
|
|
@ -510,7 +513,7 @@ class IsoDistributive(Distributive):
|
|
|
|
|
curnum = num
|
|
|
|
|
return curfile
|
|
|
|
|
|
|
|
|
|
def _convertToSquash(self):
|
|
|
|
|
def convertToSquash(self):
|
|
|
|
|
mdirectory = self.mdirectory
|
|
|
|
|
for child in self.childs:
|
|
|
|
|
if isinstance(child,SquashDistributive) and \
|
|
|
|
@ -531,14 +534,8 @@ class IsoDistributive(Distributive):
|
|
|
|
|
self._umountIso(pathdirname(child.file))
|
|
|
|
|
child.directory = None
|
|
|
|
|
|
|
|
|
|
def _convertTo(self,typeDistributive):
|
|
|
|
|
if typeDistributive == DirectoryDistributive:
|
|
|
|
|
return self.convertTo(SquashDistributive).\
|
|
|
|
|
convertTo(DirectoryDistributive)
|
|
|
|
|
if typeDistributive == SquashDistributive:
|
|
|
|
|
return self._convertToSquash()
|
|
|
|
|
if typeDistributive == IsoDistributive:
|
|
|
|
|
return self
|
|
|
|
|
def convertToDirectory(self):
|
|
|
|
|
return self.convertToSquash().convertToDirectory()
|
|
|
|
|
|
|
|
|
|
def prepareIso(self,directory):
|
|
|
|
|
print("apply iso templates to %s/"%directory)
|
|
|
|
@ -554,8 +551,14 @@ class IsoDistributive(Distributive):
|
|
|
|
|
except (Exception,KeyboardInterrupt),e:
|
|
|
|
|
raise DistributiveError("Cann't remove %s:\n%s"%(file,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/"%
|
|
|
|
|
(file,directory))
|
|
|
|
|
res,errmes = self.runOsCommand(
|
|
|
|
|
"%(progname)s %(params)s -o %(target)s %(source)s/"%
|
|
|
|
|
{'progname':'/usr/bin/mkisofs',
|
|
|
|
|
'params':" ".join(["-b isolinux/isolinux.bin","-no-emul-boot",
|
|
|
|
|
"-boot-load-size 4","-boot-info-table","-iso-level 4",
|
|
|
|
|
"-hide boot.catalog"]),
|
|
|
|
|
'target':file,
|
|
|
|
|
'source':directory})
|
|
|
|
|
if res == 0:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
@ -575,7 +578,7 @@ class IsoDistributive(Distributive):
|
|
|
|
|
self._copyfile(source.file,
|
|
|
|
|
pathjoin(isoDirectory,"livecd.squashfs"))
|
|
|
|
|
else:
|
|
|
|
|
distDirectory = source.convertTo(DirectoryDistributive)
|
|
|
|
|
distDirectory = source.convertToDirectory()
|
|
|
|
|
squashDistr = SquashDistributive(
|
|
|
|
|
pathjoin(isoDirectory,"livecd.squashfs"))
|
|
|
|
|
squashDistr.installFrom(distDirectory)
|
|
|
|
|