fexpect breaks fabric scripts
Question: I hot upon an requirement where I needed to automatically answer the prompt on
remote machine and then I found fexpect after reading different stackoverflow
questions. But the moment I include fexpect in my script it breaks the whole
script!

    
    
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 743, in main
        *args, **kwargs
      File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 368, in execute
        multiprocessing
      File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 264, in _execute
        return task.run(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 171, in run
        return self.wrapped(*args, **kwargs)
      File "/etc/puppet/fabfile.py", line 165, in edit_sudoers
        run('echo "Current Permission of the file /etc/sudoers - "`stat -c "%a %n" /etc/sudoers`')
      File "/usr/local/lib/python2.7/dist-packages/ilogue/fexpect/api.py", line 15, in run
        wrappedCmd = wrapExpectations(cmd)
      File "/usr/local/lib/python2.7/dist-packages/ilogue/fexpect/internals.py", line 15, in wrapExpectations
        script = createScript(cmd)
      File "/usr/local/lib/python2.7/dist-packages/ilogue/fexpect/internals.py", line 39, in createScript
        for e in fabric.state.env.expectations:
      File "/usr/local/lib/python2.7/dist-packages/fabric/utils.py", line 184, in __getattr__
        raise AttributeError(key)
    AttributeError: expectations
    

The moment i write `from ilogue.fexpect import expect, expecting, run` fabric
stops working with the above error message.

I asked in fabric irc as well but I got to know that this might be because of
some version related issues. Has anyone else encountered this error before?

fexpect==0.2.post7 Fabric==1.8.0


Answer: Just import fexpect's `run` as `erun` and its `sudo` as `esudo`.

When you use the fexpect `run` or `sudo` functions, you _must_ wrap those
calls in a `with expecting(prompts):` context. This is a [known
issue](https://github.com/ilogue/fexpect/issues/8) in fexpect, although there
is a pull request, so it might be fixed by the time posterity reads this.

One solution is to import fexpect's `run` function with a different name, e.g.
`erun`, and use it only when you need the automatic prompt handling
functionality:

    
    
    from fabric.api import run 
    from ilogue.fexpect import expect, expecting, run as erun
    run(a_cmd)   # Native Fabric run - should work fine
    prompts = [...]
    with expecting(prompts):
        erun(a_prompting_cmd)   # fexpect run - should with fine inside expecting context
    

Another thing that isn't explicitly stated in the fexpect documentation is
that the `pexpect` package needs to be installed on the target system.

Yet another fexpect gotcha is that the prompt strings are _regular
expressions_ \-- the fexpect sample code is misleading about this.

