Eclipse/pydev and buildout

3

25/05/2008 by Gilles Lenfant

Pydev has a nice code completion feature when editing Python code. You start typing a name, and Eclipse/Pydev offers context specific completions with call tips that saves a lot of time when programming for a hugh framework like Plone.

This requires to stuff somehow the PYTHONPATH for your project. This is fairly easy with a Plone bundle install tarball, where you just need to provide the paths to the Products directory, the lib/python of your instance and the global Zope $SOFTWARE_HOME.

With an instance created with buildout, things are not as straightforward. Such an instance is made of tons of eggs that are not in the standard “site-packages”, in addition, Zope 2 style products may be located in various places. Adding all this manually in the Pydev project PYTHONPATH is a real nightmare.

Hopefully we have two recipe companions who can help us a lot configuring Eclipse + Pydev on your buildout instance:

iw.recipe.cmd that builds some symlinks tree, with the help of some Python lines, for…
pb.recipe.pydev that makes most of the job

From now we assume you already have a ready Plone 3.x instance.

With Eclipse, create a new Pydev project, say at the root of your buildout. Do not add anything to the Pydev – PYTHONPATH of that project. Now quit Eclipse. Your instance root should have a “.pydevproject” file.

Open your buildout.cfg and add that stuff:

[buildout]
...
zope-directory = /path/to/your/Zope-2.10.5
...
parts =
    ...
    make_pydev_init_files
    pydev
    ...
# Do not add parts that add eggs or products after the above parts
...
[make_pydev_init_files]
# we need this (a Products directory with symlinks to all plone products)
# to have completion of code in the Products namespace
recipe = iw.recipe.cmd:py
on_install = true
cmds =
      >>> import os
      >>> dirs = """${instance:products}""".split("\n")
      >>> prodlinks = os.path.join("""${buildout:directory}""".strip() , 'pydevlinks')
      >>> Products = os.path.join(prodlinks,'Products')
      >>> if not os.path.isdir(prodlinks): os.mkdir(prodlinks)
      >>> if not os.path.isdir(Products): os.mkdir(Products)
      >>> file(os.path.join(Products , '__init__.py'),'w').write('#')
      >>> for dir in dirs:
      >>>     if not dir: continue
      >>>     for product in [os.path.join(dir,a) for a in os.listdir(dir) if os.path.isdir(os.path.join(dir,a))]:
      >>>         linkname = os.path.join(Products, os.path.basename(product))
      >>>         if not os.path.islink(linkname): os.symlink(product,linkname)

[pydev]
recipe = pb.recipes.pydev
eggs = ${instance:eggs}

# See [make_pydev_init_files] below
extra_paths =
    ${buildout:directory}/pydevlinks
    ${buildout:zope-directory}/lib/python
pydevproject_path = ${buildout:directory}/.pydevproject

Re-run your buildout. That’s OK? Now open Eclipse and view your project properties. Et voilà…

As you can see we use “os.symlink” to fake a products hierarchy. Windows (NTFS) users should install a symlink tool like “junction” and tweak the script of the “make_pydev_init_files” part.

Caveat: do not change manually anything that’s in the /prodlinks directory of your buildout unless…

Many thanks to Tim Knapp and Sylvio for the hints in the products developers mailing list. Now this is in a blog.

Advertisements

3 thoughts on “Eclipse/pydev and buildout

  1. Gilles Lenfant says:

    Found in Martin Aspeli’s blog another solution (based on collective.recipe.omelette) and a discussion comparing both recipes.

    http://martinaspeli.net/articles/eclipse-pydev-and-buildout

  2. woeller says:

    It seems the Products of the zope lib/python and the Products of pydevlinks can not coexists, only the lib/python/Products can code complete.

    Removing the zope/lib/python stuff gives access to the pydevlinks/Products.

  3. Gilles Lenfant says:

    @woeller
    Since this post, collective.recipe.omelette has been largely improved, I use it from some weeks and you just need to add 3 lines in buildout.cfg in place of a Python script. But Omelette just can’t play with zipped eggs.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives

%d bloggers like this: