Here is a tip of something I've been using on quite a lot of my pavements: a
pushd context manager that runs a block of statements into another directory:
##
from __future__ import with_statement
from contextlib import contextmanager
@contextmanager
def pushd(dir):
''' A context manager for pushing into a dir and automatically coming back
to the previous one '''
old_dir = os.getcwd()
runtime.info('cd %s' % dir)
os.chdir(dir)
yield
runtime.info('cd %s' % old_dir)
os.chdir(old_dir)
# Usage:
import os
print "Running from dir", os.getcwd()
with pushd(os.pardir):
print "Now running from parent dir:", os.getcwd()
print "Back to the previous dir:", os.getcwd()
##
It can also be easily adapted to be a path object attribute. Feel free to use
the code anywhere you like.
The only regard is that it will not work with older Python versions due to the
context managers dependency.
--
Best Regards,
Steve Howe
This will likely belong in paver/path.py (though not on the path
object itself, of course). For 2.4, there would likely just be a
dirstack object or something of that sort that lets you push and pop.
The context manager is an elegant way to handle this, though.
I'd monkey with this myself, but I am completely swamped right now.
Kevin
--
Kevin Dangoor
Product Manager
SitePen, Inc.
Web development experts:
development, support, training
> And btw, your implementation needs a try/finally.
No it doesn't. I wanted it to raise an error if something went wrong.
FYI, this is finally in Paver 1.0a3. I had to go out of my way a bit
to ensure that Paver will still work with Python 2.4, but it should
(you don't get pushd, then).
Kevin
--
Kevin Dangoor
work: http://labs.mozilla.com/
email: k...@blazingthings.com
blog: http://www.BlueSkyOnMars.com