# 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

        # Replace old debootstrapvariant with debootstrap
        preprocess_bootstrap(xml)

        preprocess_iso_option(xml)

        preprocess_initvm_ports(xml)

        preprocess_mirrors(xml)

        if schema.validate(xml):
            # if validation succedes write xml file
            xml.write(
                output,
                encoding="UTF-8",
                pretty_print=True,
                compression=9)
            # the rest of the code is exception and error handling
            return

    except etree.XMLSyntaxError:
        raise XMLPreprocessError("XML Parse error\n" + str(sys.exc_info()[1]))
    except ArchivedirError:
        raise XMLPreprocessError("<archivedir> handling failed\n" +
                                 str(sys.exc_info()[1]))
    except BaseException:
        raise XMLPreprocessError(
            "Unknown Exception during validation\n" + str(sys.exc_info()[1]))

    # We have errors, return them in string form...

# 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


        # Replace old debootstrapvariant with debootstrap
        preprocess_bootstrap(xml)

        preprocess_iso_option(xml)

        preprocess_initvm_ports(xml)

        preprocess_mirrors(xml)

        if schema.validate(xml):
            # if validation succedes write xml file
            xml.write(
                output,
                encoding="UTF-8",
                pretty_print=True,
                compression=9)
            # the rest of the code is exception and error handling
            return

    except etree.XMLSyntaxError:
        raise XMLPreprocessError("XML Parse error\n" + str(sys.exc_info()[1]))
    except ArchivedirError:
        raise XMLPreprocessError("<archivedir> handling failed\n" +
                                 str(sys.exc_info()[1]))
    except BaseException:
        raise XMLPreprocessError(
            "Unknown Exception during validation\n" + str(sys.exc_info()[1]))

    # We have errors, return them in string form...

    raise XMLPreprocessError("\n".join(error_log_to_strings(schema.error_log)))