Exemplo n.º 1
0
def test_file_to_ast(fake_open, file_name):
    lines = (
        '# comment\n',
        'command arg1:arg2\n',
    )
    expected = tuple(lines_to_ast(lines, file_name))
    with fake_open({file_name: ''.join(lines)}):
        result = tuple(file_to_ast(file_name))
    assert expected == result
Exemplo n.º 2
0
def customize(**kwargs):
    """Run virt-customize on the remote host

    All keyword arguments to this command are converted to command line
    arguments for virt-customize by converting underscores (_) to hyphens (-)
    and prepending double hyphens (--). This means that single-character flags
    will most probably not work.
    Two of the arguments maintain their original meaning but get special
    treatment:

    :param str add:                Specifying this argument is mandatory as the
                                   command is pretty useless without it
    :param str commands_from_file: This argument is meant to refer to a local
                                   file. The file is parsed, and its content is
                                   converted into arguments for the remote
                                   virt-customize command.

    Any files referred to by the local command file are either uploaded to the
    remote host (With the command arguments adjusted to point to the right
    remote file locations) or parsed locally and converted to arguments in case
    they are referred to by a 'commands-from-file' command. The
    'commands-from-shell' command can also be used in the command files to
    embed some locally-executed shell logic to generate more complex commands.
    See the fabric_ci.lib.virt.command_file.perform_ast_includes for full
    details of inclusion logic.
    """
    remote_files = []

    def file_map(local_file):
        """Upload local file to remote location and calculate remote name"""
        tmpfile = RemoteTempfile(directory=True)
        local_path = find_included_file(local_file.path, local_file.context)
        put(local_path=local_path, remote_path=tmpfile.name)
        remote_files.append(tmpfile)
        return join(tmpfile.name, basename(local_path))

    if 'add' not in kwargs:
        abort("The 'add' parameter must be specified")
    if 'commands_from_file' in kwargs:
        commands_from_file = expanduser(kwargs.pop('commands_from_file'))
        args_from_file = ast_to_command_params(swap_ast_used_files(
            perform_ast_includes(file_to_ast(commands_from_file)),
            file_map
        ))
    else:
        args_from_file = ()
    run(quote_and_join('virt-customize', *tuple(chain(
        dict_to_opts(kwargs), args_from_file
    ))))
Exemplo n.º 3
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))