sebastien/cuisine

@sudo fails for pip, with mode_sudo(): works

Closed this issue · 4 comments

# My fabfile.py
from fabric.api import *
from cuisine import *

@sudo
def upgrade_pip():
    python_package_upgrade_pip("pip")

@sudo
def install_virtual_envs():
    python_package_ensure_pip("virtualenvwrapper")

@task
def provision():
    execute(upgrade_pip)
    execute(install_virtual_envs)

Running

$ fab provision
No hosts found. Please specify (single) host string for connection: someone@localhost:2022
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 658, in main
    docstring, callables, default = load_fabfile(fabfile)
  File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 165, in load_fabfile
    imported = importer(os.path.splitext(fabfile)[0])
  File "/home/someone/path/to/my/fabfile.py", line 4, in <module>
    @sudo
  File "/usr/local/lib/python2.7/dist-packages/cuisine.py", line 141, in wrapper
    return function(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/cuisine.py", line 397, in sudo
    return run(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/cuisine.py", line 375, in run
    return fabric.api.sudo(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/fabric/network.py", line 647, in host_prompting_wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/fabric/operations.py", line 1107, in sudo
    stderr=stderr, timeout=timeout, shell_escape=shell_escape,
  File "/usr/local/lib/python2.7/dist-packages/fabric/operations.py", line 907, in _run_command
    _prefix_commands(_prefix_env_vars(command), 'remote'),
  File "/usr/local/lib/python2.7/dist-packages/fabric/operations.py", line 716, in _prefix_env_vars
    return shell_env_str + command
TypeError: cannot concatenate 'str' and 'function' objects

But this works perfectly

from fabric.api import *
from cuisine import *

def upgrade_pip():
    with mode_sudo():
        python_package_upgrade_pip("pip")

def install_virtual_envs():
    with mode_sudo():
        python_package_ensure_pip("virtualenvwrapper")

@task
def provision():
    execute(upgrade_pip)
    execute(install_virtual_envs)
$ pip --version
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)

$ pip show cuisine
---
Name: cuisine
Version: 0.7.4
Location: /usr/local/lib/python2.7/dist-packages
Requires: fabric

$ pip show fabric
---
Name: Fabric
Version: 1.10.1
Location: /usr/local/lib/python2.7/dist-packages
Requires: paramiko

Indeed, by doing from cuisine import * you are importing cuisine.sudo into the current namespace, overshadowing fabric's own. Simply import fabric after cuisine or do import cuisine to prvent importing all the symbols in your module's namespace. Let me know if this solves the problem.

@sebastien
Nope, same error for this

from fabric.api import *
import cuisine

@sudo
def upgrade_pip():
    cuisine.python_package_upgrade_pip("pip")

@task
def stuff():
    execute(upgrade_pip)

However, shouldn't cuisine overshadow fabric code and act as a wrapper for them?

I get exactly the same error with the following code (no cuisine involved). It's Fabric-related.

from fabric.api import *

@sudo
def upgrade_pip():
    print "hello"