diff --git a/src/pypungi/util.py b/src/pypungi/util.py new file mode 100644 index 00000000..93895775 --- /dev/null +++ b/src/pypungi/util.py @@ -0,0 +1,87 @@ +#!/usr/bin/python -tt +# 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. + +import subprocess +import os +import shutil + +def _doRunCommand(command, logger, rundir='/tmp', output=subprocess.PIPE, error=subprocess.PIPE, env=None): + """Run a command and log the output. Error out if we get something on stderr""" + + + logger.info("Running %s" % subprocess.list2cmdline(command)) + + p1 = subprocess.Popen(command, cwd=rundir, stdout=output, stderr=error, universal_newlines=True, env=env) + (out, err) = p1.communicate() + + if out: + logger.debug(out) + + if p1.returncode != 0: + logger.error("Got an error from %s" % command[0]) + logger.error(err) + raise OSError, "Got an error from %s: %s" % (command[0], err) + +def _link(local, target, logger, force=False): + """Simple function to link or copy a package, removing target optionally.""" + + if os.path.exists(target) and force: + os.remove(target) + + try: + os.link(local, target) + except OSError, e: + if e.errno != 18: # EXDEV + logger.error('Got an error linking from cache: %s' % e) + raise OSError, e + + # Can't hardlink cross file systems + shutil.copy2(local, target) + +def _ensuredir(target, logger, force=False, clean=False): + """Ensure that a directory exists, if it already exists, only continue + if force is set.""" + + # We have to check existance of a logger, as setting the logger could + # itself cause an issue. + def whoops(func, path, exc_info): + message = 'Could not remove %s' % path + if logger: + logger.error(message) + else: + sys.stderr(message) + sys.exit(1) + + if os.path.exists(target) and not os.path.isdir(target): + message = '%s exists but is not a directory.' % target + if logger: + logger.error(message) + else: + sys.stderr(message) + sys.exit(1) + + if not os.path.isdir(target): + os.makedirs(target) + elif force and clean: + shutil.rmtree(target, onerror=whoops) + os.makedirs(target) + elif force: + return + else: + message = 'Directory %s already exists. Use --force to overwrite.' % target + if logger: + logger.error(message) + else: + sys.stderr(message) + sys.exit(1)