# Copyright 2013 Daniel Narvaez
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import shutil
import textwrap
from subprocess import check_call


class FedoraBuilder:
    def __init__(self, root):
        self._root = root

    def _setup_yum(self):
        yum_etc_path = os.path.join(self._root.path, "etc", "yum")
        os.makedirs(yum_etc_path)

        yum_conf = """
                      [main]
                      cachedir=/var/cache/yum
                      keepcache=1
                      debuglevel=2
                      logfile=/var/log/yum.log
                      exactarch=1
                      obsoletes=1
                   """

        yum_conf_path = os.path.join(self._root.path, "etc", "yum", "yum.conf")

        with open(yum_conf_path, "w") as f:
            f.write(textwrap.dedent(yum_conf))

        base_url = "ftp://mirrors.kernel.org/fedora/releases/19/Fedora/" \
                   "x86_64/os"

        repo_config = """
            [fedora]
            name=Fedora 19 - x86_64
            failovermethod=priority
            baseurl=%s
            enabled=1
            gpgcheck=0
            ignore_os=1""" % base_url

        repos_d_path = os.path.join(self._root.path, "etc", "yum.repos.d")

        with open(os.path.join(repos_d_path, "fedora.repo"), "w") as f:
            f.write(textwrap.dedent(repo_config))

        for repo_name in "fedora-updates", "fedora-updates-testing":
            os.unlink(os.path.join(repos_d_path, "%s.repo" % repo_name))

    def create(self, mirror="ftp://mirrors.kernel.org/fedora"):
        root_path = self._root.path

        release_rpm = "%s/releases/19/Fedora/x86_64/os/Packages/f/" \
                      "fedora-release-19-2.noarch.rpm" % mirror
        try:
            check_call(["rpm", "--root", root_path, "--initdb"])
            check_call(["rpm", "--root", root_path, "--ignoreos", "-i",
                        release_rpm])

            self._setup_yum()
            check_call(["yum", "-y", "--installroot", root_path, "install",
                        "yum"])
        except (Exception, KeyboardInterrupt):
            shutil.rmtree(root_path)
            raise

    def install_packages(self, packages):
        self._root.run("yum -y update")
        self._root.run("yum -v -y install %s" % " ".join(packages))


class DebianBuilder:
    def __init__(self, root):
        self._root = root

    def create(self, mirror=None):
        root_path = self._root.path

        try:
            args = ["debootstrap", "jessie", root_path]
            if mirror is not None:
                args.append(mirror)

            check_call(args)
        except (Exception, KeyboardInterrupt):
            shutil.rmtree(root_path)
            raise

    def install_packages(self, packages):
        self._root.run("apt-get update", root=True)
        self._root.run("apt-get dist-upgrade", root=True)
        self._root.run("apt-get -y --no-install-recommends install %s" %
                       " ".join(packages), root=True)

@@ -584,40 +584,14 @@
 ort 
-os%0Aimport shutil%0Aimport textwrap
+shutil
 %0Afro
@@ -713,1381 +713,153 @@
 def 
-_setup_yum(self):%0A        yum_etc_path = os.path.join(self._root.path, %22etc%22, %22yum%22)%0A        os.makedirs(yum_etc_path)%0A%0A        yum_conf = %22%22%22%0A                      %5Bmain%5D%0A                      cachedir=/var/cache/yum%0A                      keepcache=1%0A                      debuglevel=2%0A                      logfile=/var/log/yum.log%0A                      exactarch=1%0A                      obsoletes=1%0A                   %22%22%22%0A%0A        yum_conf_path = os.path.join(self._root.path, %22etc%22, %22yum%22, %22yum.conf%22)%0A%0A        with open(yum_conf_path, %22w%22) as f:%0A            f.write(textwrap.dedent(yum_conf))%0A%0A        base_url = %22ftp://mirrors.kernel.org/fedora/releases/19/Fedora/%22 %5C%0A                   %22x86_64/os%22%0A%0A        repo_config = %22%22%22%0A            %5Bfedora%5D%0A            name=Fedora 19 - x86_64%0A            failovermethod=priority%0A            baseurl=%25s%0A            enabled=1%0A            gpgcheck=0%0A            ignore_os=1%22%22%22 %25 base_url%0A%0A        repos_d_path = os.path.join(self._root.path, %22etc%22, %22yum.repos.d%22)%0A%0A        with open(os.path.join(repos_d_path, %22fedora.repo%22), %22w%22) as f:%0A            f.write(textwrap.dedent(repo_config))%0A%0A        for repo_name in %22fedora-updates%22, %22fedora-updates-testing%22:%0A            os.unlink(os.path.join(repos_d_path, %22%25s.repo%22 %25 repo_name))%0A%0A    def create(self, mirror=%22ftp://mirrors.kernel.org/fedora%22):%0A        root_path = self._root.path
+create(self, mirror=None):%0A        root_path = self._root.path%0A%0A        if mirror is None:%0A            mirror = %22ftp://mirrors.kernel.org/fedora%22
 %0A%0A  
@@ -1183,38 +1183,8 @@
 %5D)%0A%0A
-            self._setup_yum()%0A
     

Remove yum setup