# Path: elbepack/shellhelper.py
# def system(cmd, allow_fail=False, env_add=None):
#     """system() - Execute cmd in a shell.
# 
#     Throws a CommandError if cmd returns none-zero and allow_fail=False
# 
#     --
# 
#     >>> system("true")
# 
#     >>> system("false", allow_fail=True)
# 
#     >>> system("$FALSE", env_add={"FALSE":"false"}) # doctest: +ELLIPSIS
#     Traceback (most recent call last):
#     ...
#     elbepack.shellhelper.CommandError: ...
# 
#     """
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     ret = call(cmd, shell=True, env=new_env)
# 
#     if ret != 0:
#         if not allow_fail:
#             raise CommandError(cmd, ret)
# 
# Path: elbepack/filesystem.py
# class TmpdirFilesystem (Filesystem):
#     def __init__(self, debug=False):
#         tmpdir = mkdtemp()
#         Filesystem.__init__(self, tmpdir)
#         self.debug = debug
# 
#     def __del__(self):
#         # dont delete files in debug mode
#         if self.debug:
#             print('leaving TmpdirFilesystem in "%s"' % self.path)
#         else:
#             self.delete()
# 
#     def delete(self):
#         shutil.rmtree(self.path, True)
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exec_type, exec_value, tb):
#         shutil.rmtree(self.path)
#         return False
# 
# Path: elbepack/rfs.py
# def create_apt_prefs(xml, rfs):
# 
#     filename =  "etc/apt/preferences"
# 
#     if rfs.lexists(filename):
#         rfs.remove(filename)
# 
#     rfs.mkdir_p("/etc/apt")
# 
#     pinned_origins = []
#     if xml.has('project/mirror/url-list'):
#         for url in xml.node('project/mirror/url-list'):
#             if not url.has('binary'):
#                 continue
# 
#             repo = url.node('binary')
#             if 'pin' not in repo.et.attrib:
#                 continue
# 
#             origin = urlsplit(repo.et.text.strip()).hostname
#             pin = repo.et.attrib['pin']
#             if 'package' in repo.et.attrib:
#                 package = repo.et.attrib['package']
#             else:
#                 package = '*'
#             pinning = {'pin': pin,
#                        'origin': origin,
#                        'package': package}
#             pinned_origins.append(pinning)
# 
#     d = {"xml": xml,
#          "prj": xml.node("/project"),
#          "pkgs": xml.node("/target/pkg-list"),
#          "porgs": pinned_origins}
# 
#     write_pack_template(rfs.fname(filename), "preferences.mako", d)


import os
import sys
import apt  # pylint: disable=unused-import
import apt_pkg
from elbepack.shellhelper import system
from elbepack.filesystem import TmpdirFilesystem
from elbepack.rfs import create_apt_prefs

    def start(self):
        pass

    def stop(self):
        pass

    @staticmethod
    def pulse(_obj):
        return True

    def initialize_dirs(self):
        self.basefs.mkdir_p("cache/archives/partial")
        self.basefs.mkdir_p("etc/apt/preferences.d")
        self.basefs.mkdir_p("etc/apt/trusted.gpg.d")
        self.basefs.mkdir_p("db")
        self.basefs.mkdir_p("log")
        self.basefs.mkdir_p("state/lists/partial")
        self.basefs.mkdir_p("tmp")
        self.basefs.touch_file("state/status")

    def setup_gpg(self):
        ring_path = self.basefs.fname("etc/apt/trusted.gpg")
        if not os.path.isdir("/etc/apt/trusted.gpg.d"):
            print("/etc/apt/trusted.gpg.d doesn't exist")
            print("apt-get install debian-archive-keyring may "
                  "fix this problem")
            sys.exit(20)

        if os.path.exists("/etc/apt/trusted.gpg"):

# Path: elbepack/shellhelper.py
# def system(cmd, allow_fail=False, env_add=None):
#     """system() - Execute cmd in a shell.
# 
#     Throws a CommandError if cmd returns none-zero and allow_fail=False
# 
#     --
# 
#     >>> system("true")
# 
#     >>> system("false", allow_fail=True)
# 
#     >>> system("$FALSE", env_add={"FALSE":"false"}) # doctest: +ELLIPSIS
#     Traceback (most recent call last):
#     ...
#     elbepack.shellhelper.CommandError: ...
# 
#     """
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     ret = call(cmd, shell=True, env=new_env)
# 
#     if ret != 0:
#         if not allow_fail:
#             raise CommandError(cmd, ret)
# 
# Path: elbepack/filesystem.py
# class TmpdirFilesystem (Filesystem):
#     def __init__(self, debug=False):
#         tmpdir = mkdtemp()
#         Filesystem.__init__(self, tmpdir)
#         self.debug = debug
# 
#     def __del__(self):
#         # dont delete files in debug mode
#         if self.debug:
#             print('leaving TmpdirFilesystem in "%s"' % self.path)
#         else:
#             self.delete()
# 
#     def delete(self):
#         shutil.rmtree(self.path, True)
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exec_type, exec_value, tb):
#         shutil.rmtree(self.path)
#         return False
# 
# Path: elbepack/rfs.py
# def create_apt_prefs(xml, rfs):
# 
#     filename =  "etc/apt/preferences"
# 
#     if rfs.lexists(filename):
#         rfs.remove(filename)
# 
#     rfs.mkdir_p("/etc/apt")
# 
#     pinned_origins = []
#     if xml.has('project/mirror/url-list'):
#         for url in xml.node('project/mirror/url-list'):
#             if not url.has('binary'):
#                 continue
# 
#             repo = url.node('binary')
#             if 'pin' not in repo.et.attrib:
#                 continue
# 
#             origin = urlsplit(repo.et.text.strip()).hostname
#             pin = repo.et.attrib['pin']
#             if 'package' in repo.et.attrib:
#                 package = repo.et.attrib['package']
#             else:
#                 package = '*'
#             pinning = {'pin': pin,
#                        'origin': origin,
#                        'package': package}
#             pinned_origins.append(pinning)
# 
#     d = {"xml": xml,
#          "prj": xml.node("/project"),
#          "pkgs": xml.node("/target/pkg-list"),
#          "porgs": pinned_origins}
# 
#     write_pack_template(rfs.fname(filename), "preferences.mako", d)


# Path: elbepack/virtapt.py
import os
import sys
import apt  # pylint: disable=unused-import
import apt_pkg
from elbepack.shellhelper import system
from elbepack.filesystem import TmpdirFilesystem
from elbepack.rfs import create_apt_prefs


    def start(self):
        pass

    def stop(self):
        pass

    @staticmethod
    def pulse(_obj):
        return True

    def initialize_dirs(self):
        self.basefs.mkdir_p("cache/archives/partial")
        self.basefs.mkdir_p("etc/apt/preferences.d")
        self.basefs.mkdir_p("etc/apt/trusted.gpg.d")
        self.basefs.mkdir_p("db")
        self.basefs.mkdir_p("log")
        self.basefs.mkdir_p("state/lists/partial")
        self.basefs.mkdir_p("tmp")
        self.basefs.touch_file("state/status")

    def setup_gpg(self):
        ring_path = self.basefs.fname("etc/apt/trusted.gpg")
        if not os.path.isdir("/etc/apt/trusted.gpg.d"):
            print("/etc/apt/trusted.gpg.d doesn't exist")
            print("apt-get install debian-archive-keyring may "
                  "fix this problem")
            sys.exit(20)

        if os.path.exists("/etc/apt/trusted.gpg"):

            system('cp /etc/apt/trusted.gpg "%s"' % ring_path)