Пример #1
0
    def test_cleanup_items_removes_and_yields_items_as_necessary(
            self, monkeypatch, tmp_folder):
        """Test ``cleanup_items()`` if it successfully removes every item.
        
        :param monkeypatch: pytest monkey patch fixture
        :type monkeypatch: _pytest.monkeypatch.MonkeyPatch
        :param tmp_folder: Test params and dummy test folder factory fixture pair.
        :type tmp_folder: (dict, dirtools.tests.factory.DummyFolderFactory)
        """
        params, _factory = tmp_folder
        if params['total_items'] == 0:
            return

        # Use another factory folder that is not shared with other tests
        with DummyFolderFactory(params['total_items'],
                                params['total_size'],
                                level=params['level']) as factory:
            scan = Folder(factory.path,
                          params['sort_by'],
                          level=params['level'])
            _humanise_item = Mock(side_effect=scan._humanise_item)
            monkeypatch.setattr(scan, '_humanise_item', _humanise_item)

            # cleanup must return a generator even the same (or greater) total size given
            result = scan.cleanup_items(
                bytes2human(scan.total_size, precision=11))
            assert isinstance(result, types.GeneratorType)
            with pytest.raises(StopIteration):
                next(result)
            _humanise_item.assert_not_called()

            # Get a copy of items and remove one by one
            items = tuple(scan.items(humanise=False))
            _humanise_item.assert_not_called()
            for item in items:
                last_total = scan.total_size
                last_items_len = scan._items_len
                mines_one_byte = bytes2human(scan.total_size - 1, precision=11)
                full_path = os.path.abspath(
                    os.path.join(factory.path, item['name']))
                assert os.path.exists(full_path)

                # attempt to remove single item
                deleted = next(
                    scan.cleanup_items(mines_one_byte, humanise=False))

                _humanise_item.assert_not_called()
                assert deleted == item
                assert scan.total_size == (last_total - deleted['size'])
                assert scan._items_len == last_items_len - 1
                assert not os.path.exists(full_path)
Пример #2
0
    def test_public_methods_should_block_before_processing(
            self, monkeypatch, tmp_folder):
        """Make sure public methods and properties blocks until scanning
        finishes.

        :param monkeypatch: pytest monkey patch fixture
        :type monkeypatch: _pytest.monkeypatch.MonkeyPatch
        :param tmp_folder: Test params and dummy test folder factory fixture pair.
        :type tmp_folder: (dict, dirtools.tests.factory.DummyFolderFactory)
        """
        params, factory = tmp_folder
        # Mock the _await internal function first
        scan = Folder(factory.path, params['sort_by'], params['level'])
        _ = scan.items()

        blocker = Mock()
        monkeypatch.setattr(scan, '_await', blocker)

        # len(scan)
        assert len(scan) == factory.total_items
        blocker.assert_called_once()
        blocker.reset_mock()

        # .total_size
        assert scan.total_size == factory.total_size
        blocker.assert_called_once()
        blocker.reset_mock()

        # .items
        _ = scan.items()
        blocker.assert_called_once()
        blocker.reset_mock()

        # .cleanup_items() calls human2bytes once
        _human2bytes = Mock(side_effect=human2bytes)
        monkeypatch.setattr('dirtools.utils.human2bytes', _human2bytes)
        factory_size_human = bytes2human(factory.total_size, precision=11)
        with pytest.raises(StopIteration):
            next(scan.cleanup_items(factory_size_human))
        _human2bytes.assert_called_once_with(factory_size_human)
        blocker.assert_called_once()
        blocker.reset_mock()

        # So give -1 to make it block actually, run only for >0 sizes
        if factory.total_size > 0:
            sh_rmtree = Mock()
            os_remove = Mock()
            monkeypatch.setattr('shutil.rmtree', sh_rmtree)
            monkeypatch.setattr(os, 'remove', os_remove)
            # Attempt to remove single item
            next(
                scan.cleanup_items(
                    bytes2human(factory.total_size - 1, precision=11)))
            blocker.assert_called_once()
            blocker.reset_mock()
            assert sh_rmtree.called or os_remove.called

        # .resort() called at the end of async loop, so it shouldn't _await
        scan.resort(params['sort_by'])
        blocker.assert_not_called()
        blocker.reset_mock()
Пример #3
0
def invoke_dirtools3(args):
    """Command line interface to the dirtools package."""
    # Get SortBy enum val
    try:
        sortby = next(s for s in SortBy if args.sortby.upper() == str(s))
    except StopIteration:
        sys.stderr.write("Invalid sort by option: {0}".format(sortby))
        return
    path = args.path
    precision = args.precision
    depth = args.depth
    nohuman = args.nohuman
    trim_down = args.trim_down
    output = args.output
    # Create folder object and start its scanning process
    scan = Folder(path, sortby, level=depth)
    old_size = scan.total_size
    old_items_len = len(scan)

    # Only folder scanning and listing etc
    if trim_down is None:
        items = scan.items(humanise=not nohuman, precision=precision)
    # Do not allow to pass only digit value because it will be interpreted as
    # byte value and probably this was an accident.
    elif trim_down.isdigit():
        sys.stderr.write(
            "--trim-down value cannot be only numeric to prevent accident, {0} given.".format(
                trim_down
            )
        )
        return
    # Folder trimming
    else:
        items = scan.cleanup_items(trim_down, humanise=not nohuman, precision=precision)

    # CSV - custom
    if output.lower() == "csv":
        with io.StringIO() as csv_io:
            writer = csv.writer(csv_io, quoting=csv.QUOTE_NONNUMERIC)
            writer.writerow(TABLE_HEADERS.values())
            writer.writerows(i.values() for i in items)
            rows = csv_io.getvalue().rstrip()
    # Display it with tabulate
    else:
        rows = tabulate(items, TABLE_HEADERS, tablefmt=output, stralign="right")

    sys.stdout.write(rows)

    # Give summary info regarding to its listing
    lit = lambda n, sing, plur: str(n) + (f" {sing}" if n == 1 else f" {plur}")
    if trim_down is None:
        sys.stdout.write(
            "\n{ct} with total of {size} data; took {exec}.".format(
                exec=lit(scan.exec_took, "second", "seconds"),
                size=bytes2human(scan.total_size, precision=precision),
                ct=lit(len(scan), "item", "items"),
            )
        )
    # or cleaning operation
    else:
        del_len = len(scan) - old_items_len
        del_size = bytes2human(old_size - scan.total_size, precision=precision)
        sys.stderr.write(
            "{del_len:d} items with total of {del_size} data has been deleted.".format(
                del_len=del_len, del_size=del_size
            )
        )
        sys.stderr.write(
            "Currently {len} items left with {size} of data; took {exec} second(s).".format(
                len=len(scan),
                size=bytes2human(scan.total_size, precision=precision),
                exec=scan.exec_took,
            )
        )