Friday, July 20, 2012

Yellow Squad Weekly Tricks: July 20

Summary:
  • bac: Don't use seteuid with bzr
  • frankban: Using deb package recipes with Distribute-based Python packages
  • frankban: os.path.expanduser uses $HOME to expand '~'
  • frankban: In tests, use a temporary home plus a bzr whoami before bzr commit
[introduction] [project report] [tricks] (no topics this week)

This week the meeting was very short because we had an interview with a job candidate overlapping our usual meeting time.

We did use the kanban board in a new way to collect tricks and topics throughout the week. It seemed to work well. I'll write it up soon.

bac: Don't use seteuid with bzr

Using seteuid and setegid is not sufficient to get bzr to act as a different user. su -c 'bzr ...' is a big hammer that will do the trick.

If you are using our python-shelltoolbox package (code available from our PPA), this means that the shelltoolbox's su contextmanager will not be sufficient. You need to use the get_su_command function in conjunction with subprocess instead.

frankban: Using deb package recipes with Distribute-based Python packages

If you are trying to build a Debian package for a Python package that uses Distribute, and you are doing it in a controlled environment like the Launchpad builders, you may get URL errors because distribute_setup.py will try to download the Distribute source code tarball from the network.

distribute_setup.py has a nice trick for this though: if the Distribute tarball is in the same directory, it will use that and not try to download anything.

For Launchpad deb package recipes, if you use the "merge" approach, you can put the tarball in the packaging branch that is merged in before the packaging commands are run. That's what frankban did for lpsetup, and it worked well.

frankban: os.path.expanduser uses $HOME to expand '~'

Imagine you have a command that is run as one user (say, root) but can accept a path and something like a -u option to specify a different user. If you want to accept tildes in the path to represent the user's home directory, and you already know the user's home directory, then here's a way to do it. os.path.expanduser uses $HOME to expand '~'. Therefore, if you want to expand the '~' construction using a custom home directory you can try this.

import os.path
from shelltoolbox import environ

with environ(HOME='/myhome'):
    print os.path.expanduser('~/my/dir/')

Another direction to go is to leverage the '~user' construction, of course...but only if the user already exists.

frankban: In tests, use a temporary home plus a bzr whoami before bzr commit

Some lpsetup tests create a fake branch to be used as a source repository. Running these tests, bzr commit worked well locally, but raised an error in tarmac. This was because the tarmac user did not have an associated bazaar id.

A reasonable solution is to create a temporary home and make a bzr user id there (and then shelltoolbox's environ can temporarily set $HOME safely).

If bzr grew a hg-like "commit -u" option to temporarily set the user that would be convenient.

1 comment:

Unknown said...

self.useFixture(TempHomeDir())

Needs fixtures 0.3.9 or better.