#!/usr/bin/python # # buildmisc.py -- miscellaneous functions needed for the build system # Copyright (C) 2004 NC State University # Written by Jack Neely # Jeremy Katz # # SDG # # 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; either version 2 of the License, or # (at your option) any later version. # # 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 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 # import os,sys import types # taken from anaconda/iutil.py def rmrf (path): # this is only the very simple case. files = os.listdir (path) for file in files: if os.path.isdir(path + '/' + file): rmrf (path + '/' + file) else: os.unlink (path + '/' + file) os.rmdir (path) # also taken from anaconda/iutil.py def getfd(filespec, readOnly = 0): if type(filespec) == types.IntType: return filespec if filespec == None: filespec = "/dev/null" flags = os.O_RDWR | os.O_CREAT if (readOnly): flags = os.O_RDONLY return os.open(filespec, flags) # more from anaconda/iutil.py (although modified) def execWithRedirect(command, argv, stdin = 0, stdout = 1, stderr = 2): stdin = getfd(stdin) if stdout == stderr: stdout = getfd(stdout) stderr = stdout else: stdout = getfd(stdout) stderr = getfd(stderr) if not os.access (command, os.X_OK): raise RuntimeError, command + " can not be run" childpid = os.fork() if (not childpid): if type(stdin) == type("a"): stdin == os.open(stdin, os.O_RDONLY) if type(stdout) == type("a"): stdout == os.open(stdout, os.O_RDWR) if type(stderr) == type("a"): stderr = os.open(stderr, os.O_RDWR) if stdin != 0: os.dup2(stdin, 0) os.close(stdin) if stdout != 1: os.dup2(stdout, 1) if stdout != stderr: os.close(stdout) if stderr != 2: os.dup2(stderr, 2) os.close(stderr) os.execv(command, argv) sys.exit(1) status = -1 (pid, status) = os.waitpid(childpid, 0) return status # return a random string 16 chars long def getRandomString(): fd = open("/dev/urandom", "r") str = '' for c in fd.read(16): i = "%x" % (ord(c)) str = str + i return str[:15]