# Path: elbepack/archivedir.py
# class ArchivedirError(Exception):
#     pass
# 
# def combinearchivedir(xml):
#     if xml.find("//archivedir") is None:
#         return xml
# 
#     _combinearchivedir(xml, "archivedir", False)
#     _combinearchivedir(xml, "src-cdrom/archivedir", True)
# 
#     return xml
# 
# Path: elbepack/config.py
# class Config(dict):
#     def __init__(self):
# 
# Path: elbepack/directories.py
# def init_directories(elbe_relpath):
# def get_cmdlist():
# 
# Path: elbepack/shellhelper.py
# def command_out_stderr(cmd, stdin=None, env_add=None):
#     """command_out_stderr() - Execute cmd in a shell.
# 
#     Returns a tuple of the exitcode, stdout and stderr of cmd.
# 
#     --
# 
#     >>> command_out_stderr("$TRUE && cat -", stdin=b"ELBE", env_add={"TRUE":"true"})
#     (0, 'ELBE', '')
# 
#     >>> command_out_stderr("1>&2 cat - && false", stdin=b"ELBE")
#     (1, '', 'ELBE')
# 
#     >>> command_out_stderr("1>&2 cat - && false", stdin="ELBE")
#     (1, '', 'ELBE')
# 
#     >>> command_out_stderr("true")
#     (0, '', '')
# 
#     """
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     if isinstance(stdin, str):
#         stdin = stdin.encode()
# 
#     if stdin is None:
#         p = Popen(cmd, shell=True,
#                   stdout=PIPE, stderr=PIPE, env=new_env)
#         output, stderr = p.communicate()
#     else:
#         p = Popen(cmd, shell=True,
#                   stdout=PIPE, stderr=PIPE, stdin=PIPE, env=new_env)
#         output, stderr = p.communicate(input=stdin)
# 
#     output = TextIOWrapper(BytesIO(output), encoding='utf-8', errors='replace').read()
#     stderr = TextIOWrapper(BytesIO(stderr), encoding='utf-8', errors='replace').read()
# 
#     return p.returncode, output, stderr
# 
# class CommandError(Exception):
# 
#     def __init__(self, cmd, returncode):
#         super(CommandError, self).__init__(cmd, returncode)
#         self.returncode = returncode
#         self.cmd = cmd
# 
#     def __str__(self):
#         return "Error: %d returned from Command %s" % (
#             self.returncode, self.cmd)
# 
# Path: elbepack/isooptions.py
# def iso_option_valid(opt_name, text):
#     if opt_name not in iso_options:
#         return False
#     str_type = encoding[iso_options[opt_name][3]]
#     if len(text) > iso_options[opt_name][1]:
#         return len(text) - iso_options[opt_name][1]
#     for c in text:
#         if c not in str_type:
#             return c
#     return True
# 
# Path: elbepack/validate.py
# def error_log_to_strings(error_log):
#     errors = []
#     uses_xinclude = False
#     uses_norecommend = False
# 
#     for err in error_log:
#         errors.append("%s:%d error %s" % (err.filename, err.line, err.message))
#         if "http://www.w3.org/2003/XInclude" in err.message:
#             uses_xinclude = True
#         if "norecommend" in err.message:
#             uses_norecommend = True
# 
#     if uses_xinclude:
#         errors.append("\nThere are XIncludes in the XML file. "
#                       "Run 'elbe preprocess' first!\n")
#     if uses_norecommend:
#         errors.append("\nThe XML file uses <norecommend />. "
#                       "This function was broken all the time and did the "
#                       "opposite. If you want to retain the original "
#                       "behaviour, please specify <install-recommends /> !\n")
#     return errors


import os
import re
import sys
from tempfile import NamedTemporaryFile
from optparse import OptionGroup
from itertools import islice
from urllib.error import HTTPError,URLError
from urllib.request import urlopen
from lxml import etree
from lxml.etree import XMLParser, parse, Element
from elbepack.archivedir import ArchivedirError, combinearchivedir
from elbepack.config import cfg
from elbepack.directories import elbe_exe
from elbepack.shellhelper import command_out_stderr, CommandError
from elbepack.isooptions import iso_option_valid
from elbepack.validate import error_log_to_strings
        from elbepack.commands.preprocess import add_pass_through_options
        if valid is True:
            continue

        tag = '<%s>%s</%s>' % (opt.tag, opt.text, opt.tag)

        if valid is False:
            violation = "Invalid ISO option %s" % tag
        elif isinstance(valid, int):
            violation = ("Option %s will be truncated by %d characters" %
                         (tag, valid))
        elif isinstance(valid, str):
            violation = ("Character '%c' (%d) in ISO option %s "
                         "violated ISO-9660" %
                         (valid, ord(valid[0]), tag))
        if strict:
            raise XMLPreprocessError(violation)
        print("[WARN] %s" % violation)


def preprocess_initvm_ports(xml):
    "Filters out the default port forwardings to prevent qemu conflict"

    for forward in xml.iterfind('initvm/portforwarding/forward'):
        prot = forward.find('proto')
        benv = forward.find('buildenv')
        host = forward.find('host')
        if prot is None or benv is None or host is None:
            continue
        if prot.text == 'tcp' and (
                host.text == cfg['sshport'] and benv.text == '22' or

# Path: elbepack/archivedir.py
# class ArchivedirError(Exception):
#     pass
# 
# def combinearchivedir(xml):
#     if xml.find("//archivedir") is None:
#         return xml
# 
#     _combinearchivedir(xml, "archivedir", False)
#     _combinearchivedir(xml, "src-cdrom/archivedir", True)
# 
#     return xml
# 
# Path: elbepack/config.py
# class Config(dict):
#     def __init__(self):
# 
# Path: elbepack/directories.py
# def init_directories(elbe_relpath):
# def get_cmdlist():
# 
# Path: elbepack/shellhelper.py
# def command_out_stderr(cmd, stdin=None, env_add=None):
#     """command_out_stderr() - Execute cmd in a shell.
# 
#     Returns a tuple of the exitcode, stdout and stderr of cmd.
# 
#     --
# 
#     >>> command_out_stderr("$TRUE && cat -", stdin=b"ELBE", env_add={"TRUE":"true"})
#     (0, 'ELBE', '')
# 
#     >>> command_out_stderr("1>&2 cat - && false", stdin=b"ELBE")
#     (1, '', 'ELBE')
# 
#     >>> command_out_stderr("1>&2 cat - && false", stdin="ELBE")
#     (1, '', 'ELBE')
# 
#     >>> command_out_stderr("true")
#     (0, '', '')
# 
#     """
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     if isinstance(stdin, str):
#         stdin = stdin.encode()
# 
#     if stdin is None:
#         p = Popen(cmd, shell=True,
#                   stdout=PIPE, stderr=PIPE, env=new_env)
#         output, stderr = p.communicate()
#     else:
#         p = Popen(cmd, shell=True,
#                   stdout=PIPE, stderr=PIPE, stdin=PIPE, env=new_env)
#         output, stderr = p.communicate(input=stdin)
# 
#     output = TextIOWrapper(BytesIO(output), encoding='utf-8', errors='replace').read()
#     stderr = TextIOWrapper(BytesIO(stderr), encoding='utf-8', errors='replace').read()
# 
#     return p.returncode, output, stderr
# 
# class CommandError(Exception):
# 
#     def __init__(self, cmd, returncode):
#         super(CommandError, self).__init__(cmd, returncode)
#         self.returncode = returncode
#         self.cmd = cmd
# 
#     def __str__(self):
#         return "Error: %d returned from Command %s" % (
#             self.returncode, self.cmd)
# 
# Path: elbepack/isooptions.py
# def iso_option_valid(opt_name, text):
#     if opt_name not in iso_options:
#         return False
#     str_type = encoding[iso_options[opt_name][3]]
#     if len(text) > iso_options[opt_name][1]:
#         return len(text) - iso_options[opt_name][1]
#     for c in text:
#         if c not in str_type:
#             return c
#     return True
# 
# Path: elbepack/validate.py
# def error_log_to_strings(error_log):
#     errors = []
#     uses_xinclude = False
#     uses_norecommend = False
# 
#     for err in error_log:
#         errors.append("%s:%d error %s" % (err.filename, err.line, err.message))
#         if "http://www.w3.org/2003/XInclude" in err.message:
#             uses_xinclude = True
#         if "norecommend" in err.message:
#             uses_norecommend = True
# 
#     if uses_xinclude:
#         errors.append("\nThere are XIncludes in the XML file. "
#                       "Run 'elbe preprocess' first!\n")
#     if uses_norecommend:
#         errors.append("\nThe XML file uses <norecommend />. "
#                       "This function was broken all the time and did the "
#                       "opposite. If you want to retain the original "
#                       "behaviour, please specify <install-recommends /> !\n")
#     return errors


# Path: elbepack/xmlpreprocess.py
import os
import re
import sys
from tempfile import NamedTemporaryFile
from optparse import OptionGroup
from itertools import islice
from urllib.error import HTTPError,URLError
from urllib.request import urlopen
from lxml import etree
from lxml.etree import XMLParser, parse, Element
from elbepack.archivedir import ArchivedirError, combinearchivedir
from elbepack.config import cfg
from elbepack.directories import elbe_exe
from elbepack.shellhelper import command_out_stderr, CommandError
from elbepack.isooptions import iso_option_valid
from elbepack.validate import error_log_to_strings
        from elbepack.commands.preprocess import add_pass_through_options

        if valid is True:
            continue

        tag = '<%s>%s</%s>' % (opt.tag, opt.text, opt.tag)

        if valid is False:
            violation = "Invalid ISO option %s" % tag
        elif isinstance(valid, int):
            violation = ("Option %s will be truncated by %d characters" %
                         (tag, valid))
        elif isinstance(valid, str):
            violation = ("Character '%c' (%d) in ISO option %s "
                         "violated ISO-9660" %
                         (valid, ord(valid[0]), tag))
        if strict:
            raise XMLPreprocessError(violation)
        print("[WARN] %s" % violation)


def preprocess_initvm_ports(xml):
    "Filters out the default port forwardings to prevent qemu conflict"

    for forward in xml.iterfind('initvm/portforwarding/forward'):
        prot = forward.find('proto')
        benv = forward.find('buildenv')
        host = forward.find('host')
        if prot is None or benv is None or host is None:
            continue
        if prot.text == 'tcp' and (
                host.text == cfg['sshport'] and benv.text == '22' or

                host.text == cfg['soapport'] and benv.text == '7588'):