# Path: elbepack/archivedir.py
# def chg_archive(xml, path, keep):
#     if os.path.isdir(path):
#         archive = '.archive.tar'
#         if os.path.exists(archive):
#             os.remove(archive)
# 
#         collect(archive, path, keep)
#         compress = True
#     else:
#         archive = path
#         compress = False
# 
#     arch = xml.ensure_child("archive")
#     arch.set_text(enbase(archive, compress))
# 
#     if os.path.isdir(path):
#         os.remove(archive)
# 
#     return xml
# 
# Path: elbepack/treeutils.py
# class etree(ebase):
#     def __init__(self, fname):
#         if fname is not None:
#             parser = XMLParser(huge_tree=True, remove_comments=False)
#             et = parse(fname, parser=parser)
#         else:
#             et = ElementTree(file=None)
# 
#         ebase.__init__(self, et)
# 
#     def write(self, fname, encoding=None):
#         # Make sure, that we end with a newline
#         self.et.getroot().tail = '\n'
#         self.et.write(fname, encoding=encoding)
# 
#     def tostring(self):
#         return self.et.tostring()
# 
#     def ensure_child(self, tag):
#         retval = self.et.find("./" + tag)
#         if retval is not None:
#             return elem(retval)
#         return elem(SubElement(self.et.getroot(), tag))
# 
#     def set_child_position(self, child, pos):
#         root = self.et.getroot()
#         root.remove(child.et)
#         root.insert(pos, child.et)
# 
#     def setroot(self, tag):
#         retval = elem(Element(tag))
#         # pylint: disable=protected-access
#         self.et._setroot(retval.et)
#         return retval
# 
#     @property
#     def root(self):
#         return elem(self.et.getroot())


import sys
from optparse import OptionParser
from elbepack.archivedir import chg_archive
from elbepack.treeutils import etree
# ELBE - Debian Based Embedded Rootfilesystem Builder
# Copyright (c) 2013, 2015, 2017 Manuel Traut <manut@linutronix.de>
#
# SPDX-License-Identifier: GPL-3.0-or-later




def run_command(argv):

    oparser = OptionParser(
        usage="usage: %prog chg_archive [options] <xmlfile> "
              "[<archive>|<directory>]")
    oparser.add_option(
        "--keep-attributes",
        action="store_true",
        help="keep file owners and groups, if not specified all files will "
             "belong to root:root",
        dest="keep_attributes",
        default=False)

    (opt, args) = oparser.parse_args(argv)

    if len(args) != 2:
        print("Wrong number of arguments")
        oparser.print_help()
        sys.exit(20)

    try:

# Path: elbepack/archivedir.py
# def chg_archive(xml, path, keep):
#     if os.path.isdir(path):
#         archive = '.archive.tar'
#         if os.path.exists(archive):
#             os.remove(archive)
# 
#         collect(archive, path, keep)
#         compress = True
#     else:
#         archive = path
#         compress = False
# 
#     arch = xml.ensure_child("archive")
#     arch.set_text(enbase(archive, compress))
# 
#     if os.path.isdir(path):
#         os.remove(archive)
# 
#     return xml
# 
# Path: elbepack/treeutils.py
# class etree(ebase):
#     def __init__(self, fname):
#         if fname is not None:
#             parser = XMLParser(huge_tree=True, remove_comments=False)
#             et = parse(fname, parser=parser)
#         else:
#             et = ElementTree(file=None)
# 
#         ebase.__init__(self, et)
# 
#     def write(self, fname, encoding=None):
#         # Make sure, that we end with a newline
#         self.et.getroot().tail = '\n'
#         self.et.write(fname, encoding=encoding)
# 
#     def tostring(self):
#         return self.et.tostring()
# 
#     def ensure_child(self, tag):
#         retval = self.et.find("./" + tag)
#         if retval is not None:
#             return elem(retval)
#         return elem(SubElement(self.et.getroot(), tag))
# 
#     def set_child_position(self, child, pos):
#         root = self.et.getroot()
#         root.remove(child.et)
#         root.insert(pos, child.et)
# 
#     def setroot(self, tag):
#         retval = elem(Element(tag))
#         # pylint: disable=protected-access
#         self.et._setroot(retval.et)
#         return retval
# 
#     @property
#     def root(self):
#         return elem(self.et.getroot())


# Path: elbepack/commands/chg_archive.py
import sys
from optparse import OptionParser
from elbepack.archivedir import chg_archive
from elbepack.treeutils import etree

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




def run_command(argv):

    oparser = OptionParser(
        usage="usage: %prog chg_archive [options] <xmlfile> "
              "[<archive>|<directory>]")
    oparser.add_option(
        "--keep-attributes",
        action="store_true",
        help="keep file owners and groups, if not specified all files will "
             "belong to root:root",
        dest="keep_attributes",
        default=False)

    (opt, args) = oparser.parse_args(argv)

    if len(args) != 2:
        print("Wrong number of arguments")
        oparser.print_help()
        sys.exit(20)

    try:

        xml = etree(args[0])