def test_build():
    tree = fs.mkdir(
        '/', [fs.mkdir('etc'),
              fs.mkdir('usr'),
              fs.mkfile('robots.txt')])

    expected = {
        'children': [
            {
                'children': [],
                'meta': {},
                'name': 'etc',
                'type': 'directory',
            },
            {
                'children': [],
                'meta': {},
                'name': 'usr',
                'type': 'directory',
            },
            {
                'meta': {},
                'name': 'robots.txt',
                'type': 'file',
            },
        ],
        'meta': {},
        'name':
        '/',
        'type':
        'directory',
    }
    assert tree == expected
def test_get_meta():
    file = fs.mkfile('robots.txt')
    dir = fs.mkdir('/')
    tree = fs.mkdir(
        '/', [fs.mkdir('etc'),
              fs.mkdir('usr'),
              fs.mkfile('robots.txt')])
    expected = [
        {
            'children': [],
            'meta': {},
            'name': 'etc',
            'type': 'directory',
        },
        {
            'children': [],
            'meta': {},
            'name': 'usr',
            'type': 'directory',
        },
        {
            'meta': {},
            'name': 'robots.txt',
            'type': 'file',
        },
    ]
    assert not fs.get_children(file)
    assert fs.get_children(dir) == []
    assert fs.get_children(tree) == expected
示例#3
0
def downcase_file_names(tree):
    name = get_name(tree)
    meta = copy.deepcopy(get_meta(tree))
    if is_file(tree):
        return mkfile(name.lower(), meta)
    children = get_children(tree)
    new_children = list(map(downcase_file_names, children))
    return mkdir(name, new_children, meta)
示例#4
0
def change_owner(node, owner):
    name = fs.get_name(node)
    new_meta = copy.deepcopy(fs.get_meta(node))
    new_meta['owner'] = owner
    if fs.is_file(node):
        # Возвращаем обновлённый файл
        return fs.mkfile(name, new_meta)
    children = fs.get_children(node)
    # Ключевая строчка
    # Вызываем рекурсивное обновление каждого ребёнка
    new_children = list(
        map(lambda child: change_owner(child, owner), children))
    new_tree = fs.mkdir(name, new_children, new_meta)
    # Возвращаем обновлённую директорию
    return new_tree
示例#5
0
# example
import copy
from pprint import pprint
from hexlet import fs

tree = fs.mkdir('/', [
    fs.mkdir('etc', [
        fs.mkfile('bashrc'),
        fs.mkfile('consul.cfg'),
    ]),
    fs.mkfile('hexletrc'),
    fs.mkdir('bin', [
        fs.mkfile('ls'),
        fs.mkfile('cat'),
    ]),
])


def change_owner(node, owner):
    name = fs.get_name(node)
    new_meta = copy.deepcopy(fs.get_meta(node))
    new_meta['owner'] = owner
    if fs.is_file(node):
        # Возвращаем обновлённый файл
        return fs.mkfile(name, new_meta)
    children = fs.get_children(node)
    # Ключевая строчка
    # Вызываем рекурсивное обновление каждого ребёнка
    new_children = list(
        map(lambda child: change_owner(child, owner), children))
    new_tree = fs.mkdir(name, new_children, new_meta)
def test_get_meta():
    file = fs.mkfile('robots.txt', {'owner': 'root'})
    dir = fs.mkdir('etc')
    assert fs.get_meta(dir) == {}
    assert fs.get_meta(file).get('owner') == 'root'
def test_is_directory():
    node = fs.mkdir('/')
    assert fs.is_directory(node)
    assert not fs.is_file(node)
def test_get_name():
    file = fs.mkfile('robots.txt')
    dir = fs.mkdir('etc')
    assert fs.get_name(file) == 'robots.txt'
    assert fs.get_name(dir) == 'etc'
示例#9
0
    result = list(
        map(
            lambda child: (get_name(child), get_size_node(child)),
            children,
        ))
    result.sort(key=lambda size: size[1], reverse=True)
    return result


# END

if __name__ == '__main__':
    tree = mkdir('/', [
        mkdir('etc', [
            mkdir('apache'),
            mkdir('nginx', [
                mkfile('nginx.conf', {'size': 800}),
            ]),
            mkdir('consul', [
                mkfile('.config.json', {'size': 1200}),
                mkfile('data', {'size': 8200}),
                mkfile('raft', {'size': 80}),
            ]),
        ]),
        mkfile('hosts', {'size': 3500}),
        mkfile('resolve', {'size': 1000}),
    ])
    assert du(tree) == [('etc', 10280), ('hosts', 3500), ('resolve', 1000)]
    expected = [('consul', 9480), ('nginx', 800), ('apache', 0)]
    assert du(get_children(tree)[0]) == expected
示例#10
0
    meta = copy.deepcopy(get_meta(tree))
    if is_file(tree):
        return mkfile(name.lower(), meta)
    children = get_children(tree)
    new_children = list(map(downcase_file_names, children))
    return mkdir(name, new_children, meta)


# END

if __name__ == "__main__":
    tree = mkdir('/', [
        mkdir('eTc', [
            mkdir('NgiNx', [], {'size': 4000}),
            mkdir(
                'CONSUL',
                [mkfile('config.JSON', {'uid': 0})],
            ),
        ]),
        mkfile('hOsts'),
    ])
    original = copy.deepcopy(tree)
    downcase_file_names(tree)
    assert tree == original
    tree = mkdir('/', [
        mkdir('eTc', [
            mkdir('NgiNx', [], {'size': 4000}),
            mkdir(
                'CONSUL',
                [mkfile('config.JSON', {'uid': 0})],
            ),
        ]),
示例#11
0
    children = get_children(node)
    count_hidden_children = list(map(get_hidden_files_count, children))
    return sum(count_hidden_children)


# END

if __name__ == '__main__':
    tree = mkdir('/', [
        mkdir('etc', [
            mkdir('apache'),
            mkdir('nginx', [
                mkfile('.nginx.conf'),
            ]),
            mkdir('consul', [
                mkfile('.config.json'),
                mkfile('data'),
                mkfile('raft'),
            ]),
        ]),
        mkfile('.hosts', {'size': 3500}),
        mkfile('resolve'),
    ])
    assert get_hidden_files_count(tree) == 3
    tree = mkdir('/', [
        mkdir('.etc', [
            mkdir('.apache'),
            mkdir('nginx', [
                mkfile('nginx.conf'),
            ]),
            mkdir('.consul', [