summaryrefslogtreecommitdiffstats
path: root/base/util.py
blob: 87da8af28747fafdbb58c35481741f3d36f48300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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']