fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Fab-user] exec python code on remote host


From: Jeff Forcier
Subject: Re: [Fab-user] exec python code on remote host
Date: Thu, 3 Sep 2009 08:35:15 -0400

Hi Aljosa,

On Thu, Sep 3, 2009 at 7:19 AM, Aljosa
Mohorovic<address@hidden> wrote:
> what do you do when you need to exec 4 lines of python code on remote host?
>
> i usually do write a script and put it somewhere on remote host but
> this just seems stupid for 4 lines of python code or not?
> any tips appreciated.

Other than copying a script as you mention, which is probably the
safest option, you could use your remote Python's -c argument. For
example::

   from fabric.api import run

   def run_remotely():
       myscript = """
def double(x):
   return x * 2
for i in range(3):
   print(double(i))
""".strip()

       run('python -c "%s"' % myscript)

This works in the above simple case; if your Python code to be
executed contains special shell characters such as double-quotes,
you'd naturally need to escape them. Partly because of that annoyance,
however, using a script file is still probably the best approach for
anything nontrivial.

A while ago, the idea of having some sort of special "remote Python
execution" feature was raised -- either an encapsulated version of the
above approaches, or something more custom involving RPC of some form
-- but that's not currently on the table for core Fabric as it's not
the primary use-case. (Though I don't dispute its potential usefulness
:))

Best,
Jeff




reply via email to

[Prev in Thread] Current Thread [Next in Thread]