lib_rpm.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2012 Intel Corporation <markus.lehtonen@linux.intel.com>
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, please see
  16. # <http://www.gnu.org/licenses/>
  17. """Wrapper module for librpm"""
  18. import tempfile
  19. import gbp.log
  20. from gbp.rpm.policy import RpmPkgPolicy
  21. try:
  22. # Try to load special RPM lib to be used for GBP (only)
  23. librpm = __import__(RpmPkgPolicy.python_rpmlib_module_name)
  24. except ImportError:
  25. gbp.log.warn("Failed to import '%s' as rpm python module, using host's "
  26. "default rpm library instead" %
  27. RpmPkgPolicy.python_rpmlib_module_name)
  28. import rpm as librpm
  29. # Module initialization
  30. _rpmlog = tempfile.NamedTemporaryFile(prefix='gbp_rpmlog')
  31. _rpmlogfd = _rpmlog.file
  32. librpm.setVerbosity(librpm.RPMLOG_INFO)
  33. librpm.setLogFile(_rpmlogfd)
  34. def get_librpm_log(truncate=True):
  35. """Get rpmlib log output"""
  36. _rpmlogfd.seek(0)
  37. log = [line.strip() for line in _rpmlogfd.readlines()]
  38. if truncate:
  39. _rpmlogfd.truncate(0)
  40. return log