# 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/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


import bz2
import os
import re
import sys
from urllib.parse import urljoin,urlparse
from base64 import encodebytes, standard_b64decode
from subprocess import CalledProcessError
from tempfile import NamedTemporaryFile
from elbepack.treeutils import etree
from elbepack.shellhelper import system
from elbepack.filesystem import TmpdirFilesystem
    fp = NamedTemporaryFile()
    fp.write(standard_b64decode(arch_elem))
    fp.file.flush()
    return fp


def prepare_path(url):
    url = urlparse(url)
    path = url.geturl().replace("%s://"%url.scheme, '', 1)
    return re.sub(r'/$', "", path)

def get_and_append_local(url, tararchive, keep):
    if urlparse(url).netloc:
        msg = "Reject suspicious file:// URI \"{}\". ".format(url)
        msg += "Please use an absolute URI (file:///a/b/c) or a "
        msg += "relative URI (a/b/c) instead."
        raise ArchivedirError(msg)
    collect(tararchive, prepare_path(url), keep)

def get_and_append_unknown(url, _archive):
    msg = "unhandled scheme \"{}://\"".format(urlparse(url).scheme)
    raise NotImplementedError(msg)

def get_and_append_method(url):
    return {
        '': get_and_append_local,
        'file': get_and_append_local,
    }.get(urlparse(url).scheme, get_and_append_unknown)

def _combinearchivedir(xml, xpath, use_volume):

# 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/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/archivedir.py
import bz2
import os
import re
import sys
from urllib.parse import urljoin,urlparse
from base64 import encodebytes, standard_b64decode
from subprocess import CalledProcessError
from tempfile import NamedTemporaryFile
from elbepack.treeutils import etree
from elbepack.shellhelper import system
from elbepack.filesystem import TmpdirFilesystem

    fp = NamedTemporaryFile()
    fp.write(standard_b64decode(arch_elem))
    fp.file.flush()
    return fp


def prepare_path(url):
    url = urlparse(url)
    path = url.geturl().replace("%s://"%url.scheme, '', 1)
    return re.sub(r'/$', "", path)

def get_and_append_local(url, tararchive, keep):
    if urlparse(url).netloc:
        msg = "Reject suspicious file:// URI \"{}\". ".format(url)
        msg += "Please use an absolute URI (file:///a/b/c) or a "
        msg += "relative URI (a/b/c) instead."
        raise ArchivedirError(msg)
    collect(tararchive, prepare_path(url), keep)

def get_and_append_unknown(url, _archive):
    msg = "unhandled scheme \"{}://\"".format(urlparse(url).scheme)
    raise NotImplementedError(msg)

def get_and_append_method(url):
    return {
        '': get_and_append_local,
        'file': get_and_append_local,
    }.get(urlparse(url).scheme, get_and_append_unknown)

def _combinearchivedir(xml, xpath, use_volume):

    elbexml = etree(None)