# 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)
# 
# class CommandError(Exception):
# 
#     def __init__(self, cmd, returncode):
#         super(CommandError, self).__init__(cmd, returncode)
#         self.returncode = returncode
#         self.cmd = cmd
# 
#     def __str__(self):
#         return "Error: %d returned from Command %s" % (
#             self.returncode, self.cmd)


import hashlib
from elbepack.shellhelper import system, CommandError
# ELBE - Debian Based Embedded Rootfilesystem Builder
# Copyright (c) 2018 Torben Hohn <torben.hohn@linutronix.de>
#
# SPDX-License-Identifier: GPL-3.0-or-later



class HashValidationFailed(Exception):
    pass


def validate_sha256(fname, expected_hash):
    m = hashlib.sha256()
    with open(fname, "rb") as f:
        buf = f.read(65536)
        while buf:
            m.update(buf)
            buf = f.read(65536)

# 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)
# 
# class CommandError(Exception):
# 
#     def __init__(self, cmd, returncode):
#         super(CommandError, self).__init__(cmd, returncode)
#         self.returncode = returncode
#         self.cmd = cmd
# 
#     def __str__(self):
#         return "Error: %d returned from Command %s" % (
#             self.returncode, self.cmd)


# Path: elbepack/hashes.py
import hashlib
from elbepack.shellhelper import system, CommandError

# ELBE - Debian Based Embedded Rootfilesystem Builder
# Copyright (c) 2018 Torben Hohn <torben.hohn@linutronix.de>
#
# SPDX-License-Identifier: GPL-3.0-or-later



class HashValidationFailed(Exception):
    pass


def validate_sha256(fname, expected_hash):
    m = hashlib.sha256()
    with open(fname, "rb") as f:
        buf = f.read(65536)
        while buf:
            m.update(buf)
            buf = f.read(65536)

    if m.hexdigest() != expected_hash: