from __future__ import with_statement from contextlib import contextmanager from os import chdir, getcwd from os import symlink as sym from os.path import abspath, lexists from shutil import copyfileobj, rmtree from shutil import move as mv from base import log @contextmanager def pwd(dir): old_dir = getcwd() log.debug('changing dir to %s' % dir) chdir(dir) yield log.debug('changing dir to %s' % old_dir) chdir(old_dir) def copy(src, dst): # we're using copyfileobj so later we can do this from a URL src = file(src, 'rb') dst = file(dst, 'wb') copyfileobj(src, dst) src.close() dst.close() def symlink(src, dst): if lexists(dst): remove(dst) sym(abspath(src), abspath(dst)) def move(src, dst): if lexists(dst): rmtree(dst) mv(src, dst) __all__ = ['pwd', 'copy']