summaryrefslogtreecommitdiffstats
path: root/base/util.py
blob: e192b945f4202629819291948dedb6dd2be7dffe (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Fedora Developer Shell
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors: Yaakov M. Nemoy <ynemoy@redhat.com>
#

from __future__ import with_statement

from contextlib import contextmanager
from datetime import datetime
from os import chdir, getcwd, remove
from os import symlink as sym
from os.path import abspath, lexists, isdir, islink, isfile
from shutil import copyfileobj, rmtree
from shutil import move as mv
from shutil import copytree as cpthree
from urllib import urlopen

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)

@contextmanager
def log_file(fname):
    with file(fname, 'a') as fout:
        fout.write("-- Beginning log of %s at %s --\n" % (fname, datetime.now().isoformat(' ')))
        fout.flush()
        yield fout
        fout.write("-- Ending log of %s at %s --\n\n" % (fname, datetime.now().isoformat(' ')))

def rm(tgt):
    if isdir(tgt):
        rmtree(tgt)
    else:
        remove(tgt)
        
def copy(src, dst):
    # we're using copyfileobj so we can do this from a URL
    if isfile(src):
        src_f = file(src, 'rb')
    else:
        src_f = urlopen(src)
    dst_f = file(dst, 'wb')
    copyfileobj(src_f, dst_f)
    src_f.close()
    dst_f.close()
    # return the dst path as a matter of utility
    return dst

def symlink(src, dst):
    if lexists(dst):
        rm(dst)
    sym(abspath(src), abspath(dst))
    return dst
    
def move(src, dst):
    if lexists(dst):
        rm(dst)
    mv(src, dst)
    return dst

def copytree(src, dst):
    if lexists(dst):
        rm(dst)
    # cee-pee-tree in jamaica, mon!
    cpthree(src, dst)
    return dst

def one(l, f):
    for x in l:
        if f(x):
            return x

def isiter(i):
    return hasattr(i, '__iter__')

def flatten(l):
    acc = []
    def _flatten(acc, i):
        if isiter(i):
            for x in i:
                _flatten(acc, x)
        else:
            acc.append(i)
    _flatten(acc, l)
    return acc

__all__ = ['pwd', 'copy', 'with_sudo', 'with_su', 'symlink', 'move',
           'log_file', 'one']