<commit_before># Copyright (C) 2011  <NAME> <<EMAIL>>
# Copyright (C) 2012-2017  <NAME> <<EMAIL>>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

"""
Debian installation
"""

import gzip
import os
import re
import shutil

import oz.GuestFSManager
import oz.Linux
import oz.OzException
import oz.ozutil

class DebianConfiguration(object):
    def __init__(self, need_auto_direct, need_auto_iso, default_netdev,
                 default_diskbus):
        self._need_auto_direct = need_auto_direct
        self._need_auto_iso = need_auto_iso
        self._default_netdev = default_netdev
        self._default_diskbus = default_diskbus

    @property
    def need_auto_direct(self):
        return self._need_auto_direct

    @property
    def need_auto_iso(self):
        return self._need_auto_iso

    @property
    def default_netdev(self):
        return self._default_netdev

    @property
    def default_diskbus(self):
        return self._default_diskbus

version_to_config = {
    '8': DebianConfiguration(need_auto_direct=False, need_auto_iso=True,
                             default_netdev='virtio', default_diskbus='virtio'),
    '7': DebianConfiguration(need_auto_direct=True, need_auto_iso=True,
                             default_netdev='virtio', default_diskbus='virtio'),
    '6': DebianConfiguration(need_auto_direct=False, need_auto_iso=False,
                             default_netdev='virtio', default_diskbus='virtio'),
    '5': DebianConfiguration(need_auto_direct=False, need_auto_iso=False,
                             default_netdev='virtio', default_diskbus='virtio'),
}

class DebianGuest(oz.Linux.LinuxCDGuest):
    """
    Class for Debian 5, 6, 7 and 8 installation.
    """
    def __init__(self, tdl, config, auto, output_disk, netdev, diskbus,
                 macaddress):
        self.config = version_to_config[tdl.update]

        if netdev is None:
            netdev = self.config.default_netdev
        if diskbus is None:
            diskbus = self.config.default_diskbus
        oz.Linux.LinuxCDGuest.__init__(self, tdl, config, auto, output_disk,
                                       netdev, diskbus, True, True, macaddress)

        self.crond_was_active = False
        self.sshd_was_active = False
        self.sshd_config = \
"""SyslogFacility AUTHPRIV
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
X11Forwarding yes
Subsystem       sftp    /usr/libexec/openssh/sftp-server
"""

        self.tunnels = {}

        self.ssh_startuplink = None
        self.cron_startuplink = None

        self.debarch = self.tdl.arch
        if self.debarch == "x86_64":
            self.debarch = "amd64"

        self.kernelfname = os.path.join(self.output_dir,
                                        self.tdl.name + "-kernel")
        self.initrdfname = os.path.join(self.output_dir,
                                        self.tdl.name + "-ramdisk")
        self.kernelcache = os.path.join(self.data_dir, "kernels",
                                        self.tdl.distro + self.tdl.update + self.tdl.arch + "-kernel")
        self.initrdcache = os.path.join(self.data_dir, "kernels",
                                        self.tdl.distro + self.tdl.update + self.tdl.arch + "-ramdisk")

    def _copy_preseed(self, outname):
        """
        Method to copy and modify an Debian style preseed file.
        """
        self.log.debug("Putting the preseed file in place")

        if self.default_auto_file():
            def _preseed_sub(line):
                """
                Method that is called back from oz.ozutil.copy_modify_file() to
                modify preseed files as appropriate for Debian.
                """
                if re.match('d-i passwd/root-password password', line):
                    return 'd-i passwd/root-password password ' + self.rootpw + '\n'
                elif re.match('d-i passwd/root-password-again password', line):
                    return 'd-i passwd/root-password-again password ' + self.rootpw + '\n'
                else:
                    return line

            oz.ozutil.copy_modify_file(self.auto, outname, _preseed_sub)
        else:
            shutil.copy(self.auto, outname)

    def _modify_iso(self):
        """
        Method to make the boot ISO auto-boot with appropriate parameters.
        """
        self.log.debug("Modifying ISO")
        self.log.debug("Copying preseed file")
        outname = os.path.join(self.iso_contents, "preseed", "customiso.seed")
        outdir = os.path.dirname(outname)
        oz.ozutil.mkdir_p(outdir)
        self._copy_preseed(outname)

        # arch == i386
        installdir = "/install.386"
        if self.tdl.arch == "x86_64":
            installdir = "/install.amd"

        self.log.debug("Modifying isolinux.cfg")
        isolinuxcfg = os.path.join(self.iso_contents, "isolinux", "isolinux.cfg")
        extra = ""
        if self.config.need_auto_iso:
            extra = "auto=true "

        with open(isolinuxcfg, 'w') as f:
            f.write("""\
default customiso
timeout 1
prompt 0
label customiso
  menu label ^Customiso
  menu default
  kernel %s/vmlinuz
  append file=/cdrom/preseed/customiso.seed %sdebian-installer/locale=en_US console-setup/layoutcode=us netcfg/choose_interface=auto priority=critical initrd=%s/initrd.gz --
""" % (installdir, extra, installdir))

    def get_auto_path(self):
        autoname = self.tdl.distro + self.tdl.update + ".auto"
        sp = self.tdl.update.split('.')
        if len(sp) == 3:
            autoname = self.tdl.distro + sp[0] + "." + sp[1] + ".auto"
        return oz.ozutil.generate_full_auto_path(autoname)

    def _generate_new_iso(self):
        """
        Method to create a new ISO based on the modified CD/DVD.
        """
        self.log.info("Generating new ISO")
        oz.ozutil.subprocess_check_output(["genisoimage", "-r", "-V", "Custom",
                                           "-J", "-l", "-no-emul-boot",
                                           "-b", "isolinux/isolinux.bin",
                                           "-c", "isolinux/boot.cat",
                                           "-boot-load-size", "4",
                                           "-cache-inodes", "-boot-info-table",
                                           "-v", "-o", self.output_iso,
                                           self.iso_contents],
                                          printfn=self.log.debug)

    def install(self, timeout=None, force=False):
        """
        Method to run the operating system installation.
        """
        extra = ""
        if self.config.need_auto_direct:<commit_msg>Use textwrap.dedent to format a few long strings nicely<commit_after># Copyright (C) 2011  <NAME> <<EMAIL>>
# Copyright (C) 2012-2017  <NAME> <<EMAIL>>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

"""
Debian installation
"""

import gzip
import os
import re
import shutil
import textwrap

import oz.GuestFSManager
import oz.Linux
import oz.OzException
import oz.ozutil

class DebianConfiguration(object):
    def __init__(self, need_auto_direct, need_auto_iso, default_netdev,
                 default_diskbus):
        self._need_auto_direct = need_auto_direct
        self._need_auto_iso = need_auto_iso
        self._default_netdev = default_netdev
        self._default_diskbus = default_diskbus

    @property
    def need_auto_direct(self):
        return self._need_auto_direct

    @property
    def need_auto_iso(self):
        return self._need_auto_iso

    @property
    def default_netdev(self):
        return self._default_netdev

    @property
    def default_diskbus(self):
        return self._default_diskbus

version_to_config = {
    '8': DebianConfiguration(need_auto_direct=False, need_auto_iso=True,
                             default_netdev='virtio', default_diskbus='virtio'),
    '7': DebianConfiguration(need_auto_direct=True, need_auto_iso=True,
                             default_netdev='virtio', default_diskbus='virtio'),
    '6': DebianConfiguration(need_auto_direct=False, need_auto_iso=False,
                             default_netdev='virtio', default_diskbus='virtio'),
    '5': DebianConfiguration(need_auto_direct=False, need_auto_iso=False,
                             default_netdev='virtio', default_diskbus='virtio'),
}

class DebianGuest(oz.Linux.LinuxCDGuest):
    """
    Class for Debian 5, 6, 7 and 8 installation.
    """
    def __init__(self, tdl, config, auto, output_disk, netdev, diskbus,
                 macaddress):
        self.config = version_to_config[tdl.update]

        if netdev is None:
            netdev = self.config.default_netdev
        if diskbus is None:
            diskbus = self.config.default_diskbus
        oz.Linux.LinuxCDGuest.__init__(self, tdl, config, auto, output_disk,
                                       netdev, diskbus, True, True, macaddress)

        self.crond_was_active = False
        self.sshd_was_active = False
        self.sshd_config = textwrap.dedent(
            """\
            SyslogFacility AUTHPRIV
            PasswordAuthentication yes
            ChallengeResponseAuthentication no
            GSSAPIAuthentication yes
            GSSAPICleanupCredentials yes
            UsePAM yes
            AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
            AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
            AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
            AcceptEnv XMODIFIERS
            X11Forwarding yes
            Subsystem       sftp    /usr/libexec/openssh/sftp-server
            """
        )

        self.tunnels = {}

        self.ssh_startuplink = None
        self.cron_startuplink = None

        self.debarch = self.tdl.arch
        if self.debarch == "x86_64":
            self.debarch = "amd64"

        self.kernelfname = os.path.join(self.output_dir,
                                        self.tdl.name + "-kernel")
        self.initrdfname = os.path.join(self.output_dir,
                                        self.tdl.name + "-ramdisk")
        self.kernelcache = os.path.join(self.data_dir, "kernels",
                                        self.tdl.distro + self.tdl.update + self.tdl.arch + "-kernel")
        self.initrdcache = os.path.join(self.data_dir, "kernels",
                                        self.tdl.distro + self.tdl.update + self.tdl.arch + "-ramdisk")

    def _copy_preseed(self, outname):
        """
        Method to copy and modify an Debian style preseed file.
        """
        self.log.debug("Putting the preseed file in place")

        if self.default_auto_file():
            def _preseed_sub(line):
                """
                Method that is called back from oz.ozutil.copy_modify_file() to
                modify preseed files as appropriate for Debian.
                """
                if re.match('d-i passwd/root-password password', line):
                    return 'd-i passwd/root-password password ' + self.rootpw + '\n'
                elif re.match('d-i passwd/root-password-again password', line):
                    return 'd-i passwd/root-password-again password ' + self.rootpw + '\n'
                else:
                    return line

            oz.ozutil.copy_modify_file(self.auto, outname, _preseed_sub)
        else:
            shutil.copy(self.auto, outname)

    def _modify_iso(self):
        """
        Method to make the boot ISO auto-boot with appropriate parameters.
        """
        self.log.debug("Modifying ISO")
        self.log.debug("Copying preseed file")
        outname = os.path.join(self.iso_contents, "preseed", "customiso.seed")
        outdir = os.path.dirname(outname)
        oz.ozutil.mkdir_p(outdir)
        self._copy_preseed(outname)

        # arch == i386
        installdir = "/install.386"
        if self.tdl.arch == "x86_64":
            installdir = "/install.amd"

        self.log.debug("Modifying isolinux.cfg")
        isolinuxcfg = os.path.join(self.iso_contents, "isolinux", "isolinux.cfg")
        extra = ""
        if self.config.need_auto_iso:
            extra = "auto=true "

        with open(isolinuxcfg, 'w') as f:
            f.write(textwrap.dedent(
                """\
                default customiso
                timeout 1
                prompt 0
                label customiso
                  menu label ^Customiso
                  menu default
                  kernel %s/vmlinuz
                  append file=/cdrom/preseed/customiso.seed %sdebian-installer/\
locale=en_US console-setup/layoutcode=us netcfg/choose_interface=auto priority=\
critical initrd=%s/initrd.gz --
                """ % (installdir, extra, installdir)))

    def get_auto_path(self):
        autoname = self.tdl.distro + self.tdl.update + ".auto"
        sp = self.tdl.update.split('.')
        if len(sp) == 3:
            autoname = self.tdl.distro + sp[0] + "." + sp[1] + ".auto"
        return oz.ozutil.generate_full_auto_path(autoname)

    def _generate_new_iso(self):
        """
        Method to create a new ISO based on the modified CD/DVD.
        """
        self.log.info("Generating new ISO")
        oz.ozutil.subprocess_check_output(["genisoimage", "-r", "-V", "Custom",
                                           "-J", "-l", "-no-emul-boot",
                                           "-b", "isolinux/isolinux.bin",
                                           "-c", "isolinux/boot.cat",
                                           "-boot-load-size", "4",
                                           "-cache-inodes", "-boot-info-table",
                                           "-v", "-o", self.output_iso,
                                           self.iso_contents],
                                          printfn=self.log.debug)

    def install(self, timeout=None, force=False):
        """
        Method to run the operating system installation.
        """
        extra = ""
        if self.config.need_auto_direct:
{'fraction_non_alphanumeric': 0.09591326105087573, 'fraction_numerical': 0.007313787130300892, 'mean_word_length': 1.9708404802744426, 'pattern_counts': {'":': 4, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1941892', 'n_tokens_mistral': 4607, 'n_tokens_neox': 4377, 'n_words': 1066}