def test_ast_to_used_files(file_name):
    ast = lines_to_ast((
        '# some comment',
        'run /some/script',
        'copy-in /copyed/dir:/dst/dir',
        'install sopme-pkg',
        '',
        'root-password file:/root/pwd/file',
    ), context_file=file_name)
    expected = (
        used_file('/some/script', context(file_name, 2)),
        used_file('/copyed/dir', context(file_name, 3)),
        used_file('/root/pwd/file', context(file_name, 6)),
    )
    result = tuple(ast_to_used_files(ast))
    assert expected == result
Example #2
0
def check_command_file(cmd_file):
    """Helper tool to writing complex virt-* command files

    Read the given command file, attempt to perform all file inclusions and
    emdebbed script execution and yeild the resulting composit command file

    :param str cmd_file: The command file to check
    """
    ast = tuple(perform_ast_includes(file_to_ast(expanduser(cmd_file))))
    for used_file in ast_to_used_files(ast):
        used_file_path = find_included_file(used_file.path, used_file.context)
        if not exists(used_file_path):
            abort('File "{0}" included from "{1}" line {2} not found!'.format(
                used_file.path, used_file.context.file, used_file.context.line
            ))
    for item in ast:
        puts(ast_item_to_string(item))