# 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 # 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 remove_all(i, l): while i in l: l.remove(i) 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 def base_dir(tarball): ti = tarball.next() return ti.name.split('/')[0] __all__ = ['pwd', 'copy', 'with_sudo', 'with_su', 'symlink', 'move', 'log_file', 'one', 'remove_all', 'flatten', 'base_dir']