#coding:utf-8
import os
import sys
import time
import sunhpc
import datetime
class Command(sunhpc.commands.create.command):
    """
    创建新的xml相关配置文件.
    <param type="str" name="name">
    Must specify the ISO/Roll name.
    </param>

    <param type="bool" name="boot">
    Def:0 Specifies whether the ISO supports bootin.
    </param>

    <param type="str" name="flag">
    Specifies boot extension flag.
    </param>

    <param type="bool" name="bin">
    Def:1 Specifies the file bin format in the ISO package.
    </param>

    <param type="bool" name="src">
    Def:0 Specifies whether the ISO contains source files.
    </param>

    <param type="bool" name="rolls">
    Def:1 Specifies the ISO for sunhpc rolls.
    </param>

    <param type="str" name="version">
    Specifies the ISO/Roll version.
    </param>

    <param type="str" name="release">
    Specifies the ISO/Roll release.
    </param>

    <param type="str" name="arch">
    Def:x86_64 Specifies the ISO/Roll arch.
    </param>

    <param type="str" name="os">
    Def:linux Specifies the ISO/Roll os.
    </param>

    <example cmd='create xml name=CentOS version=7.9.2009 boot=1'>
    Create Roll xml configuration.
    </example>
    """
    def run(self, param, args):

        (name, boot, flag, b, src, rolls, ver, release,
            arch, self.out, plat) = self.fillParams([
                ('name', ),
                ('boot', '0'),
                ('flag', ''),
                ('bin', '1'),
                ('src', '0'),
                ('rolls', '1'),
                ('version', sunhpc.version),
                ('release', sunhpc.version_micro),
                ('arch', self.arch),
                ('path', os.getcwd()),
                ('os', self.os)])

        boot  = self.str2bool(boot)
        isbin = self.str2bool(b)
        issrc = self.str2bool(src)
        rolls = self.str2bool(rolls)

        if not name:
            self.abort('must supply roll name')

        if not os.path.isdir(self.out):
            self.abort('The %s must is directory' % self.out)

        if not os.path.exists(self.out):
            os.makedirs(self.out)

        self.makeRollXML(name, arch, plat, flag, isbin, issrc, rolls, boot, ver, release)

    def makeRollXML(self, name, arch, plat, flag, isbin, issrc, rolls, boot, ver, release):

        # ver1 = '-b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table'
        # ver2 = '-b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table '
        #        '-eltorito-alt-boot -e images/efiboot.img -no-emul-boot -J -T '
        if boot:
            flags  = '-b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot '
            flags += '-boot-load-size 4 -boot-info-table -eltorito-alt-boot '
            flags += '-e images/efiboot.img %s -no-emul-boot -J -T ' % flag
        else:
            flags = ''

        filename = os.path.join(self.out, "roll-%s.xml" % name)
        file = open(filename, 'w')
        file.write('<roll name="%s" interface="%s">\n' % (name, sunhpc.version))

        rolltime = time.strftime('%X')
        rolldate = time.strftime('%b %d %Y')
        rollzone = time.strftime('%Z')
        file.write('\t<timestamp time="%s" date="%s" tz="%s"/>\n' %
            (rolltime, rolldate, rollzone))

        file.write('\t<info version="%s" release="%s" arch="%s" os="%s"/>\n' %
            (ver, release, arch, plat))

        # size 是制作iso最大大小,如果超过size,制作时就会自动分割.
        file.write('\t<iso maxsize="0" bootable="%s" mkisofs="%s"/>\n' % (boot, flags))
        file.write('\t<rpm rolls="%s" bin="%s" src="%s"/>\n' % (rolls, isbin, issrc))
        file.write('\t<deps name="%s" host="sunhpc-cluster.local"/>\n' % name)
        file.write('</roll>\n')
        file.close()
