Posts Tagged: python


28
Apr 09

Python-Debian Packaging for Maemo

Packaging a software component made using python to a Maemo device could be easier, if CDBS cared about hand-held devices and their limitations.

Knowing that using setup.py was the Right Way of Doing It™ for python applications I tried to push myself into making CDBS work together, but not without a little harassment.

First of all, such scope-limited distributions tend to gather components in customized places as to promote integration between them or just in sake of a plain different organization. This difficulty can be overcome by using pycentral and including the following line at debian/rules file.

export DH_PYCENTRAL=nomove

Just as the manpage says, this will prevent the build-system from moving the files from the selected install prefix to a central place (like /usr/share/pycentral).

Secondly, because of limited storage capacity and speed-up necessities, usually python components install just their .pyo files. This requirement made me struggle trough CDBS’ python-distutils.mk source code in hope for a simple fix. The answer I’ve found was to overrule the python-install target with the following commands (look here for the diff).

define FIXUP_DIST
    -find $(DEB_DESTDIR) -name '*.py' -exec rm -f {} \;
    -find $(DEB_DESTDIR) -name '*.pyc' -exec rm -f {} \;
    -find $(DEB_DESTDIR) -name '*.egg-info' -type d -exec rm -rf {} \;
endef

ifeq (all, $(cdbs_python_module_arch))
common-install-arch common-install-indep:: python-install-py
python-install-py:
    cd $(DEB_SRCDIR) && $(call cdbs_python_binary,python$(cdbs_python_compile_version)) $(DEB_PYTHON_SETUP_CMD) install --root=$(DEB_DESTDIR) $(DEB_PYTHON_INSTALL_ARGS_ALL)
    $(call FIXUP_DIST)
else
common-install-arch common-install-indep:: $(addprefix python-install-, $(cdbs_python_build_versions))
python-install-%:
    cd $(DEB_SRCDIR) && $(call cdbs_python_binary,python$*) $(DEB_PYTHON_SETUP_CMD) install --root=$(DEB_DESTDIR) $(DEB_PYTHON_INSTALL_ARGS_ALL)
    $(call FIXUP_DIST)
endif # archall detection

Let’s say it’s inside a file named debian/fixup.mk, then my complete debian/rules file would be like this.

#!/usr/bin/make -f

DEB_PYTHON_SYSTEM=pycentral

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/python-distutils.mk
include debian/fixup.mk

export DH_PYCENTRAL=nomove

And this is the beauty of CDBS, a file which would be several lines long gets resumed to a few lines.

I’m still trying to find a way to make this code available for all my components without installing it to a globally reachable path, but did not find a thing such as a MAKEFILEPATH variable untill now. I guess a package like cdbs-maemo-dev.deb would be an appropriate place for stuff like this, but pushing it there is for another post.


9
Apr 09

Vim Scripting Using Python

Trying to solve a colleague’s problem when editing Edje files, I pushed myself into learning how to do scripting inside Vim.

Scripting wasn’t ever my choice for reasonably complex editing tasks since I refused to learn Yet Another Scripting Language just because my favorite editor wanted me to. But, for the sake of all lazy guys like me, Vim started to add python support, and python was a must learn bullet in my language listing.

However, not everything are flowers and the entry point must be configured using common vim script. Well, at least until vim supports python from its core, which – I believe – is not far from possible, since python integration has been voted as top priority for a long time.

To successfully import your python script inside vim context, one can wrap it into a vim function in an external file. Let’s call it extras.vim.

" Title-ize sentences using python str methods
function! PyMakeTitle() " the ! erases previous definitions
python << END " here-document (bash-style), read 'till given word
import vim
w = vim.current.window
b = vim.current.buffer
line, col = w.cursor
line -= 1
b[line] = b[line].title() # str.title() method
END
endfunction

After using :source extras.vim command to load it, one can call this function by typing :call PyMakeTitle(). Remember repeating the :source step every time the script gets updated.

The net effect of this function is to turn all initial word letters in the current line into capitals. It proved me to be useful when editing a large LaTeX document where all section titles were small letters only.

If it comes to be a very useful function, you may map it to a key-stroke by using :nmap \t :call PyMakeTitle()<CR> inside your vimrc script.

A more complex example accessing internal vim properties. Ok, it’s a bit useless, but it demonstrates well such features.

" Auto Documentation (example code)
function! PyCreateDoc()
python << END
import vim
name = vim.eval("expand(\"<cword>\")") # expand word under cursor
ts = int(vim.eval("&ts")) # tab space property
il = int(vim.eval("indent(\".\")")) # indentation on current line
w = vim.current.window
l, c = w.cursor
docstr = '%s# @brief %s - write description' % (' ' * il, name)
b = vim.current.buffer
b[l-1:l-1] = [ docstr ]
print 'Indent set is %d, cursor at (%d, %d)' % (ts, l, c)
END
endfunction

With the cursor over a function name, type :call PyCreateDoc() and watch it insert a line above containing doxygen-like formatting.

For more examples, there is an excellent material at vim’s online help (see :h python). Furthermore, there are some plugins being made using only python, take a look at Omni Completion for Python (pythoncomplete.vim) for a good reference on the power of using python inside vim.

Happy Easter Vim’ing!