# Path: zabby/core/utils.py
# def sh(command, timeout=1.0, wait_step=0.01, raise_on_empty_out=True,
#        raise_on_nonempty_err=False):
#     """
#     Creates and returns a function that when called will run command with shell
#     and return it's output.
# 
#     Command can contain replacement fields as described in python documentation
#     http://docs.python.org/library/string.html?highlight=formatter#format-string-syntax
# 
#     sh('command {0}')('argument') will call 'command argument'
# 
#     :param timeout: if command does not terminate in it will be killed and
#         OperatingSystemError will be raised
#     :param wait_step: poll interval for process running command
#     :param raise_on_empty_out: whether exception should be raised if command
#         does not write anything to stdout
#     :param raise_on_nonempty_err: whether exception should be raised if command
#         writes to stderr
# 
#     :raises: WrongArgumentError if command contains replacement fields and
#         resulting function is called without arguments
#     :raises: OperatingSystemError if command does not terminate until timeout
#     """
# 
#     def call_command(*args):
#         try:
#             formatted_command = command.format(*args)
#         except IndexError:
#             raise WrongArgumentError(
#                 "'{0}' not enough arguments. Called with {1}".format(command,
#                                                                      args))
#         process = Popen(formatted_command, stdout=PIPE, stderr=PIPE,
#                         shell=True, close_fds=True, universal_newlines=True)
#         try:
#             if timeout:
#                 wait_time_remaining = timeout
#                 while process.poll() is None and wait_time_remaining > 0:
#                     time.sleep(wait_step)
#                     wait_time_remaining -= wait_step
# 
#                 if wait_time_remaining <= 0:
#                     process.kill()
#                     raise OperatingSystemError(
#                         "{0} have not completed in {1} seconds".format(
#                             formatted_command, timeout))
#         finally:
#             (out, err) = process.communicate()
# 
#         (out, err) = (out.rstrip(), err.rstrip())
# 
#         if out == '' and raise_on_empty_out:
#             raise OperatingSystemError(
#                 "'{0}' has not written to stdout".format(formatted_command))
# 
#         if err != '':
#             message = "'{0}' has written to stderr: {1}".format(
#                 formatted_command, err)
# 
#             if raise_on_nonempty_err:
#                 raise OperatingSystemError(message)
#             else:
#                 log = logging.getLogger('sh')
#                 log.warn(message)
# 
#         return out
# 
#     return call_command
# 
# Path: zabby/items/vfs/file.py
# def md5sum(file_path, block_size=8192):


import os
from nose.plugins.attrib import attr
from nose.tools import assert_equal
from zabby.core.utils import sh
from zabby.items.vfs import file



@attr(os='linux')
class TestMd5Sum():
    FILE_PATH = '/tmp/zabby_md5_test_file'
    FILE_CONTENT = '''\
    line 1
    '''

    def setup(self):
        with open(self.FILE_PATH, 'w') as f:
            f.write(self.FILE_CONTENT)

    def teardown(self):
        os.remove(self.FILE_PATH)

    def test_result_is_equal_to_coreutils_md5sum(self):
        coreutils_md5sum = sh('md5sum {0}')(self.FILE_PATH).split()[0]

# Path: zabby/core/utils.py
# def sh(command, timeout=1.0, wait_step=0.01, raise_on_empty_out=True,
#        raise_on_nonempty_err=False):
#     """
#     Creates and returns a function that when called will run command with shell
#     and return it's output.
# 
#     Command can contain replacement fields as described in python documentation
#     http://docs.python.org/library/string.html?highlight=formatter#format-string-syntax
# 
#     sh('command {0}')('argument') will call 'command argument'
# 
#     :param timeout: if command does not terminate in it will be killed and
#         OperatingSystemError will be raised
#     :param wait_step: poll interval for process running command
#     :param raise_on_empty_out: whether exception should be raised if command
#         does not write anything to stdout
#     :param raise_on_nonempty_err: whether exception should be raised if command
#         writes to stderr
# 
#     :raises: WrongArgumentError if command contains replacement fields and
#         resulting function is called without arguments
#     :raises: OperatingSystemError if command does not terminate until timeout
#     """
# 
#     def call_command(*args):
#         try:
#             formatted_command = command.format(*args)
#         except IndexError:
#             raise WrongArgumentError(
#                 "'{0}' not enough arguments. Called with {1}".format(command,
#                                                                      args))
#         process = Popen(formatted_command, stdout=PIPE, stderr=PIPE,
#                         shell=True, close_fds=True, universal_newlines=True)
#         try:
#             if timeout:
#                 wait_time_remaining = timeout
#                 while process.poll() is None and wait_time_remaining > 0:
#                     time.sleep(wait_step)
#                     wait_time_remaining -= wait_step
# 
#                 if wait_time_remaining <= 0:
#                     process.kill()
#                     raise OperatingSystemError(
#                         "{0} have not completed in {1} seconds".format(
#                             formatted_command, timeout))
#         finally:
#             (out, err) = process.communicate()
# 
#         (out, err) = (out.rstrip(), err.rstrip())
# 
#         if out == '' and raise_on_empty_out:
#             raise OperatingSystemError(
#                 "'{0}' has not written to stdout".format(formatted_command))
# 
#         if err != '':
#             message = "'{0}' has written to stderr: {1}".format(
#                 formatted_command, err)
# 
#             if raise_on_nonempty_err:
#                 raise OperatingSystemError(message)
#             else:
#                 log = logging.getLogger('sh')
#                 log.warn(message)
# 
#         return out
# 
#     return call_command
# 
# Path: zabby/items/vfs/file.py
# def md5sum(file_path, block_size=8192):


# Path: zabby/tests/items/vfs/test_file.py
import os
from nose.plugins.attrib import attr
from nose.tools import assert_equal
from zabby.core.utils import sh
from zabby.items.vfs import file




@attr(os='linux')
class TestMd5Sum():
    FILE_PATH = '/tmp/zabby_md5_test_file'
    FILE_CONTENT = '''\
    line 1
    '''

    def setup(self):
        with open(self.FILE_PATH, 'w') as f:
            f.write(self.FILE_CONTENT)

    def teardown(self):
        os.remove(self.FILE_PATH)

    def test_result_is_equal_to_coreutils_md5sum(self):
        coreutils_md5sum = sh('md5sum {0}')(self.FILE_PATH).split()[0]

        zabby_md5sum = file.md5sum(self.FILE_PATH)