# 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
# ELBE - Debian Based Embedded Rootfilesystem Builder
# Copyright (c) 2014-2017 Torben Hohn <torben.hohn@linutronix.de>
# Copyright (c) 2014-2015, 2017 Manuel Traut <manut@linutronix.de>
#
# SPDX-License-Identifier: GPL-3.0-or-later




def size_to_int(size):
    if size[-1] in digits:
        return int(size)

    if size.endswith("M"):
        unit = 1000 * 1000
        s = size[:-1]
    elif size.endswith("MiB"):
        unit = 1024 * 1024
        s = size[:-3]
    elif size.endswith("MB"):
        unit = 1000 * 1000

# 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

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




def size_to_int(size):
    if size[-1] in digits:
        return int(size)

    if size.endswith("M"):
        unit = 1000 * 1000
        s = size[:-1]
    elif size.endswith("MiB"):
        unit = 1024 * 1024
        s = size[:-3]
    elif size.endswith("MB"):
        unit = 1000 * 1000

        s = size[:-2]