Re: [mezzanine-users] "fab python" usage tips

158 views
Skip to first unread message

Ken Bolton

unread,
Sep 27, 2012, 11:17:37 AM9/27/12
to mezzani...@googlegroups.com
Hi Dominique,

I'm going to start by pointing you to the Fabric documentation here:
http://docs.fabfile.org/en/1.4.3/. There is nothing magical about how
Mezzanine uses Fabric.

The `python` function can be found here:
https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/project_template/fabfile.py#L291-302

Line 294 contains the docstring for the `python` function - Runs
Python code in the project's virtual environment, with Django loaded.

So your "unimpressive" example is exactly how it works. More elaborate
examples, including running blocks of code, can be seen in the source,
e.g. https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/project_template/fabfile.py#L291-302.

Hope that helps.

best,
ken




On Thu, Sep 27, 2012 at 10:36 AM, Dominique <dbis...@gmail.com> wrote:
> Hello,
>
> Can anyone provide some tips on effective ways to use the fab python
> command?
>
> I can do the following, but it's not impressive:
>
> fab python:"print 'hello world'"
>
>
> For instance, how would one run a block of code?

Dominique

unread,
Nov 1, 2012, 3:56:23 PM11/1/12
to mezzani...@googlegroups.com
Two Fabric related items that tripped me up (in case other Mezzanine users have the same issue):

1. For most complex code, you have to prepend your code string with code=, as in fab python:code="[Your code]". Not doing this will result in the following error:
TypeError: python() got an unexpected keyword argument '[the first few chars of your code...]'

2. If you have an = in your code, it must be escaped. Not escaping them will result in the following error: 
ValueError: too many values to unpack

Here's a short sample:

fab python:code="from datetime import datetime
from example.foo.models import Foo
foos \= Foo.objects.filter(time__exact\='').all()
for foo in foos: foo.time \= datetime.now(); foo.save(); time.sleep(10)"

Stephen McDonald

unread,
Nov 1, 2012, 4:03:03 PM11/1/12
to mezzani...@googlegroups.com
For more complex code you might want to consider a Django management command.
--
Stephen McDonald
http://jupo.org
Reply all
Reply to author
Forward
0 new messages