# Path: elbepack/shellhelper.py
# def do(cmd, allow_fail=False, stdin=None, env_add=None):
#     """do() - Execute cmd in a shell and redirect outputs to logging.
# 
#     Throws a CommandError if cmd failed with allow_Fail=False.
# 
#     --
# 
#     Let's redirect the loggers to current stdout
#     >>> import sys
#     >>> from elbepack.log import open_logging
#     >>> open_logging({"streams":sys.stdout})
# 
#     >>> do("true")
#     [CMD] true
# 
#     >>> do("false", allow_fail=True)
#     [CMD] false
# 
#     >>> do("cat -", stdin=b"ELBE")
#     [CMD] cat -
# 
#     >>> do("cat - && false", stdin=b"ELBE") # doctest: +ELLIPSIS
#     Traceback (most recent call last):
#     ...
#     elbepack.shellhelper.CommandError: ...
# 
#     >>> do("false") # doctest: +ELLIPSIS
#     Traceback (most recent call last):
#     ...
#     elbepack.shellhelper.CommandError: ...
#     """
# 
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     if isinstance(stdin, str):
#         stdin = stdin.encode()
# 
#     logging.info(cmd, extra={"context":"[CMD] "})
# 
#     r, w = os.pipe()
# 
#     if stdin is None:
#         p = Popen(cmd, shell=True, stdout=w, stderr=STDOUT, env=new_env)
#     else:
#         p = Popen(cmd, shell=True, stdin=PIPE, stdout=w, stderr=STDOUT, env=new_env)
# 
#     async_logging(r, w, soap, log)
#     p.communicate(input=stdin)
# 
#     if p.returncode and not allow_fail:
#         raise CommandError(cmd, p.returncode)


import os
import shutil
import errno
import gzip
from glob import glob
from tempfile import mkdtemp
from string import digits
from elbepack.shellhelper import do
        # 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


class ImgMountFilesystem(Filesystem):
    def __init__(self, mntpoint, dev):
        Filesystem.__init__(self, mntpoint)

        self.dev = dev

    def __enter__(self):
        cmd = 'mount "%s" "%s"' % (self.dev, self.path)
        do(cmd)

        return self

    def __exit__(self, typ, value, traceback):

# Path: elbepack/shellhelper.py
# def do(cmd, allow_fail=False, stdin=None, env_add=None):
#     """do() - Execute cmd in a shell and redirect outputs to logging.
# 
#     Throws a CommandError if cmd failed with allow_Fail=False.
# 
#     --
# 
#     Let's redirect the loggers to current stdout
#     >>> import sys
#     >>> from elbepack.log import open_logging
#     >>> open_logging({"streams":sys.stdout})
# 
#     >>> do("true")
#     [CMD] true
# 
#     >>> do("false", allow_fail=True)
#     [CMD] false
# 
#     >>> do("cat -", stdin=b"ELBE")
#     [CMD] cat -
# 
#     >>> do("cat - && false", stdin=b"ELBE") # doctest: +ELLIPSIS
#     Traceback (most recent call last):
#     ...
#     elbepack.shellhelper.CommandError: ...
# 
#     >>> do("false") # doctest: +ELLIPSIS
#     Traceback (most recent call last):
#     ...
#     elbepack.shellhelper.CommandError: ...
#     """
# 
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     if isinstance(stdin, str):
#         stdin = stdin.encode()
# 
#     logging.info(cmd, extra={"context":"[CMD] "})
# 
#     r, w = os.pipe()
# 
#     if stdin is None:
#         p = Popen(cmd, shell=True, stdout=w, stderr=STDOUT, env=new_env)
#     else:
#         p = Popen(cmd, shell=True, stdin=PIPE, stdout=w, stderr=STDOUT, env=new_env)
# 
#     async_logging(r, w, soap, log)
#     p.communicate(input=stdin)
# 
#     if p.returncode and not allow_fail:
#         raise CommandError(cmd, p.returncode)


# Path: elbepack/filesystem.py
import os
import shutil
import errno
import gzip
from glob import glob
from tempfile import mkdtemp
from string import digits
from elbepack.shellhelper import do

        # 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


class ImgMountFilesystem(Filesystem):
    def __init__(self, mntpoint, dev):
        Filesystem.__init__(self, mntpoint)

        self.dev = dev

    def __enter__(self):
        cmd = 'mount "%s" "%s"' % (self.dev, self.path)
        do(cmd)

        return self

    def __exit__(self, typ, value, traceback):

        do('umount "%s"' % self.path)