# Path: elbepack/xmlpreprocess.py
# class XMLPreprocessError(Exception):
#     pass
# 
# def xmlpreprocess(fname, output, variants=None, proxy=None):
# 
#     # pylint: disable=too-many-locals
#     # pylint: disable=too-many-branches
# 
#     # first convert variants to a set
#     if not variants:
#         variants = set([])
#     else:
#         variants = set(variants)
# 
#     schema_file = "https://www.linutronix.de/projects/Elbe/dbsfed.xsd"
#     parser = XMLParser(huge_tree=True)
#     schema_tree = etree.parse(schema_file)
#     schema = etree.XMLSchema(schema_tree)
# 
#     try:
#         xml = parse(fname, parser=parser)
#         xml.xinclude()
# 
#         # Variant management
#         # check all nodes for variant field, and act accordingly.
#         # The result will not contain any variant attributes anymore.
#         rmlist = []
#         for tag in xml.iter('*'):
#             if 'variant' in tag.attrib:
#                 tag_variants = set(tag.attrib['variant'].split(','))
# 
#                 # check if tag_variants intersects with
#                 # active variants.
#                 intersect = variants.intersection(tag_variants)
# 
#                 if intersect:
#                     # variant is wanted, keep it and remove the variant
#                     # attribute
#                     tag.attrib.pop('variant')
#                 else:
#                     # tag has a variant attribute but the variant was not
#                     # specified: remove the tag delayed
#                     rmlist.append(tag)
# 
#         for tag in rmlist:
#             tag.getparent().remove(tag)
# 
#         # if there are multiple sections because of sth like '<finetuning
#         # variant='A'> ...  and <finetuning variant='B'> and running preprocess
#         # with --variant=A,B the two sections need to be merged
#         #
#         # Use xpath expressions to identify mergeable sections.
#         for mergepath in mergepaths:
#             mergenodes = xml.xpath(mergepath)
# 
#             # if there is just one section of a type
#             # or no section, nothing needs to be done
#             if len(mergenodes) < 2:
#                 continue
# 
#             # append all childrens of section[1..n] to section[0] and delete
#             # section[1..n]
#             for section in mergenodes[1:]:
#                 for c in section.getchildren():
#                     mergenodes[0].append(c)
#                 section.getparent().remove(section)
# 
#         # handle archivedir elements
#         xml = combinearchivedir(xml)
# 
#         preprocess_mirror_replacement(xml)
# 
#         preprocess_proxy_add(xml, proxy)
# 
#         # Change public PGP url key to raw key
#         preprocess_pgp_key(xml)
# 
#         # 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)))


import sys
import os
from optparse import OptionParser
from elbepack.xmlpreprocess import XMLPreprocessError, xmlpreprocess
# ELBE - Debian Based Embedded Rootfilesystem Builder
# Copyright (c) 2017 Benedikt Spranger <b.spranger@linutronix.de>
# Copyright (c) 2017 Manuel Traut <manut@linutronix.de>
#
# SPDX-License-Identifier: GPL-3.0-or-later



def add_pass_through_options(oparser):
    oparser.add_option("-v", "--variants", dest="variant",
                       default=None,
                       help="enable only tags with empty or given variant")

    oparser.add_option("-p", "--proxy", dest="proxy",
                       default=None,
                       help="add proxy to mirrors")


def run_command(argv):
    oparser = OptionParser(usage="usage: %prog preprocess [options] <xmlfile>")
    oparser.add_option("-o", "--output", dest="output",
                       default="preprocess.xml",
                       help="preprocessed output file", metavar="<xmlfile>")
    add_pass_through_options(oparser)
    (opt, args) = oparser.parse_args(argv)

    if len(args) != 1:
        print("Wrong number of arguments", file=sys.stderr)
        oparser.print_help()

# Path: elbepack/xmlpreprocess.py
# class XMLPreprocessError(Exception):
#     pass
# 
# def xmlpreprocess(fname, output, variants=None, proxy=None):
# 
#     # pylint: disable=too-many-locals
#     # pylint: disable=too-many-branches
# 
#     # first convert variants to a set
#     if not variants:
#         variants = set([])
#     else:
#         variants = set(variants)
# 
#     schema_file = "https://www.linutronix.de/projects/Elbe/dbsfed.xsd"
#     parser = XMLParser(huge_tree=True)
#     schema_tree = etree.parse(schema_file)
#     schema = etree.XMLSchema(schema_tree)
# 
#     try:
#         xml = parse(fname, parser=parser)
#         xml.xinclude()
# 
#         # Variant management
#         # check all nodes for variant field, and act accordingly.
#         # The result will not contain any variant attributes anymore.
#         rmlist = []
#         for tag in xml.iter('*'):
#             if 'variant' in tag.attrib:
#                 tag_variants = set(tag.attrib['variant'].split(','))
# 
#                 # check if tag_variants intersects with
#                 # active variants.
#                 intersect = variants.intersection(tag_variants)
# 
#                 if intersect:
#                     # variant is wanted, keep it and remove the variant
#                     # attribute
#                     tag.attrib.pop('variant')
#                 else:
#                     # tag has a variant attribute but the variant was not
#                     # specified: remove the tag delayed
#                     rmlist.append(tag)
# 
#         for tag in rmlist:
#             tag.getparent().remove(tag)
# 
#         # if there are multiple sections because of sth like '<finetuning
#         # variant='A'> ...  and <finetuning variant='B'> and running preprocess
#         # with --variant=A,B the two sections need to be merged
#         #
#         # Use xpath expressions to identify mergeable sections.
#         for mergepath in mergepaths:
#             mergenodes = xml.xpath(mergepath)
# 
#             # if there is just one section of a type
#             # or no section, nothing needs to be done
#             if len(mergenodes) < 2:
#                 continue
# 
#             # append all childrens of section[1..n] to section[0] and delete
#             # section[1..n]
#             for section in mergenodes[1:]:
#                 for c in section.getchildren():
#                     mergenodes[0].append(c)
#                 section.getparent().remove(section)
# 
#         # handle archivedir elements
#         xml = combinearchivedir(xml)
# 
#         preprocess_mirror_replacement(xml)
# 
#         preprocess_proxy_add(xml, proxy)
# 
#         # Change public PGP url key to raw key
#         preprocess_pgp_key(xml)
# 
#         # 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)))


# Path: elbepack/commands/preprocess.py
import sys
import os
from optparse import OptionParser
from elbepack.xmlpreprocess import XMLPreprocessError, xmlpreprocess

# ELBE - Debian Based Embedded Rootfilesystem Builder
# Copyright (c) 2017 Benedikt Spranger <b.spranger@linutronix.de>
# Copyright (c) 2017 Manuel Traut <manut@linutronix.de>
#
# SPDX-License-Identifier: GPL-3.0-or-later



def add_pass_through_options(oparser):
    oparser.add_option("-v", "--variants", dest="variant",
                       default=None,
                       help="enable only tags with empty or given variant")

    oparser.add_option("-p", "--proxy", dest="proxy",
                       default=None,
                       help="add proxy to mirrors")


def run_command(argv):
    oparser = OptionParser(usage="usage: %prog preprocess [options] <xmlfile>")
    oparser.add_option("-o", "--output", dest="output",
                       default="preprocess.xml",
                       help="preprocessed output file", metavar="<xmlfile>")
    add_pass_through_options(oparser)
    (opt, args) = oparser.parse_args(argv)

    if len(args) != 1:
        print("Wrong number of arguments", file=sys.stderr)
        oparser.print_help()

        sys.exit(20)