Exemplo n.º 1
0
 def test_hash_files_filenotfound(self):
     """
     Test hashing a list of non-existing files.
     """
     patch_log = patch('bear.hashing.LOG.warning')
     patch_ignore = patch('bear.hashing.ignore_append')
     with patch_log as warning, patch_ignore as ignore:
         inp = ['does_not_exist_123', 'does_not_exist_456']
         hash_files(inp, Hasher.MD5)
         self.assertEqual(ignore.mock_calls, [call(inp[0]), call(inp[1])])
         self.assertEqual(len(warning.mock_calls), len(inp))
Exemplo n.º 2
0
    def test_hash_files_oserror(self):
        """
        Test hashing a list of files returning OSError on file.read().
        """
        def raise_os_error(*_):
            raise OSError()

        patch_log = patch('bear.hashing.LOG.critical')
        patch_hash = patch('builtins.open', raise_os_error)
        patch_ignore = patch('bear.hashing.ignore_append')
        with patch_hash, patch_log as critical, patch_ignore as ignore:
            inp = ['lalala', 'somefile']
            hash_files(inp, Hasher.MD5)
            self.assertEqual(ignore.mock_calls, [call(inp[0]), call(inp[1])])
            self.assertEqual(len(critical.mock_calls), len(inp))
Exemplo n.º 3
0
    def test_hash_files(self):
        """
        Test hashing a list of files returning hashes + paths.
        """

        file_hash = {
            'first': '123', 'second': '456',
            'original': '789', 'duplicate': '789'
        }

        # pylint: disable=unused-argument
        # is actually used for kwargs comparison which is more important
        @ensure_annotations
        def _side_effect(path: str, hasher: Hasher):
            return file_hash[path]

        with patch('bear.hashing.hash_file', side_effect=_side_effect):
            inp = ['first', 'second', 'original', 'duplicate']
            out = hash_files(files=inp, hasher=Hasher.MD5)

        expected = {}
        for key, val in file_hash.items():
            if val not in expected:
                expected[val] = [key]
            else:
                expected[val].extend([key])
        self.assertEqual(out, expected)
Exemplo n.º 4
0
    def test_hash_files(self):
        """
        Test hashing a list of files returning hashes + paths.
        """

        file_hash = {
            'first': '123',
            'second': '456',
            'original': '789',
            'duplicate': '789'
        }

        def _side_effect(fname):
            return file_hash[fname]

        with patch('bear.hashing.hash_file', side_effect=_side_effect):
            inp = ['first', 'second', 'original', 'duplicate']
            out = hash_files(inp)

        expected = {}
        for key, val in file_hash.items():
            if val not in expected:
                expected[val] = [key]
            else:
                expected[val].extend([key])
        self.assertEqual(out, expected)
Exemplo n.º 5
0
def main(args: Namespace):
    """
    Main function for calling the API from the package depending on
    the CLI options.
    """
    # pylint: disable=too-many-branches
    ctx = Context(args)

    print_logo(ctx)

    if ctx.quiet:
        set_log_levels(logging.NOTSET)
    elif 0 < ctx.verbose <= 1:
        set_log_levels(logging.WARNING)
    elif 1 < ctx.verbose <= 2:
        set_log_levels(logging.INFO)
    elif ctx.verbose > 2:
        set_log_levels(logging.DEBUG)

    LOG.info('Setting up default logging level to %s', LOG.level)
    LOG.debug('CLI args: %s', args)
    LOG.debug('Context: %s', vars(ctx))

    hasher = ctx.hasher

    # actions
    if ctx.files:
        for file in ctx.files:
            print(hash_file(path=file, hasher=hasher))
    elif ctx.traverse:
        for folder in ctx.traverse:
            print(find_files(ctx=ctx, folder=folder))
    elif ctx.hash:
        found_lists = [
            find_files(ctx=ctx, folder=folder)
            for folder in ctx.hash
        ]
        print(filter_files(hash_files(files=[
            file
            for file_list in found_lists
            for file in file_list
        ], hasher=hasher)))
    elif ctx.duplicates:
        handle_duplicates(ctx=ctx, hasher=hasher)
    elif ctx.version:
        print(VERSION)
    elif ctx.community:
        open_browser(COMMUNITY_URL)
    elif ctx.load_hashes:
        if not ctx.hashfiles:
            ctx.hashfiles = ctx.load_hashes
        load_hashes(ctx=ctx)