diff options
Diffstat (limited to 'python/rpm')
-rw-r--r-- | python/rpm/__init__.py (renamed from python/rpm/__init__.py.in) | 68 | ||||
-rw-r--r-- | python/rpm/transaction.py (renamed from python/rpm/transaction.py.in) | 62 |
2 files changed, 97 insertions, 33 deletions
diff --git a/python/rpm/__init__.py.in b/python/rpm/__init__.py index df1e3a282..54728bbd4 100644 --- a/python/rpm/__init__.py.in +++ b/python/rpm/__init__.py @@ -2,13 +2,42 @@ r"""RPM Module This module enables you to manipulate rpms and the rpm database. +The rpm base module provides the main starting point for +accessing RPM from Python. For most usage, call +the TransactionSet method to get a transaction set (rpmts). + +For example: + import rpm + ts = rpm.TransactionSet() + +The transaction set will open the RPM database as needed, so +in most cases, you do not need to explicitly open the +database. The transaction set is the workhorse of RPM. + +You can open another RPM database, such as one that holds +all packages for a given Linux distribution, to provide +packages used to solve dependencies. To do this, use +the following code: + +rpm.addMacro('_dbpath', '/path/to/alternate/database') +solvets = rpm.TransactionSet() +solvets.openDB() +rpm.delMacro('_dbpath') + +# Open default database +ts = rpm.TransactionSet() + +This code gives you access to two RPM databases through +two transaction sets (rpmts): ts is a transaction set +associated with the default RPM database and solvets +is a transaction set tied to an alternate database, which +is very useful for resolving dependencies. """ import warnings -import os -from @PYTHON_MODULENAME@._rpm import * -from @PYTHON_MODULENAME@.transaction import * -import @PYTHON_MODULENAME@._rpm as _rpm +from rpm._rpm import * +from rpm.transaction import * +import rpm._rpm as _rpm _RPMVSF_NODIGESTS = _rpm._RPMVSF_NODIGESTS _RPMVSF_NOHEADER = _rpm._RPMVSF_NOHEADER _RPMVSF_NOPAYLOAD = _rpm._RPMVSF_NOPAYLOAD @@ -19,23 +48,26 @@ __version_info__ = tuple(__version__.split('.')) # try to import build bits but dont require it try: - from @PYTHON_MODULENAME@._rpmb import * + from rpm._rpmb import * except ImportError: pass # try to import signing bits but dont require it try: - from @PYTHON_MODULENAME@._rpms import * + from rpm._rpms import * except ImportError: pass # backwards compatibility + give the same class both ways ts = TransactionSet + def headerLoad(*args, **kwds): + """DEPRECATED! Use rpm.hdr() instead.""" warnings.warn("Use rpm.hdr() instead.", DeprecationWarning, stacklevel=2) return hdr(*args, **kwds) + def _doHeaderListFromFD(rpm_fd, retrofit): hlist = [] while 1: @@ -49,18 +81,22 @@ def _doHeaderListFromFD(rpm_fd, retrofit): return hlist -def readHeaderListFromFD(file_desc, retrofit = True): + +def readHeaderListFromFD(file_desc, retrofit=True): if not isinstance(file_desc, fd): file_desc = fd(file_desc) return _doHeaderListFromFD(file_desc, retrofit) - -def readHeaderListFromFile(path, retrofit = True): + + +def readHeaderListFromFile(path, retrofit=True): f = fd(path) hlist = _doHeaderListFromFD(f, retrofit) f.close() return hlist - + + def readHeaderFromFD(file_desc): + """Return (header, pos_before_hdr)""" if not isinstance(file_desc, fd): file_desc = fd(file_desc) try: @@ -72,7 +108,9 @@ def readHeaderFromFD(file_desc): return (h, offset) + def signalsCaught(siglist): + """Returns list of signals that were caught.""" caught = [] for sig in siglist: if signalCaught(sig): @@ -80,5 +118,11 @@ def signalsCaught(siglist): return caught -def dsSingle(TagN, N, EVR = "", Flags = RPMSENSE_ANY): - return ds((N, EVR, Flags), TagN) + +def dsSingle(TagN, N, EVR="", Flags=RPMSENSE_ANY): + """ + Creates a single entry dependency set (ds) + + dsSingle(RPMTAG_CONFLICTNAME, "rpm") corresponds to "Conflicts: rpm" + """ + return ds((N, Flags, EVR), TagN) diff --git a/python/rpm/transaction.py.in b/python/rpm/transaction.py index e73075923..675ecaf8e 100644 --- a/python/rpm/transaction.py.in +++ b/python/rpm/transaction.py @@ -1,7 +1,14 @@ -#!/usr/bin/python +from __future__ import with_statement + +import sys +import rpm +from rpm._rpm import ts as TransactionSetCore + +if sys.version_info[0] == 3: + _string_types = str, +else: + _string_types = basestring, -import @PYTHON_MODULENAME@ as rpm -from @PYTHON_MODULENAME@._rpm import ts as TransactionSetCore # TODO: migrate relevant documentation from C-side class TransactionSet(TransactionSetCore): @@ -11,7 +18,7 @@ class TransactionSet(TransactionSetCore): oval = getattr(self, attr) setattr(self, attr, val) return oval - + def setVSFlags(self, flags): return self._wrapSetGet('_vsflags', flags) @@ -31,8 +38,8 @@ class TransactionSet(TransactionSetCore): return self._wrapSetGet('_probFilter', ignoreSet) def parseSpec(self, specfile): - import _rpmb - return _rpmb.spec(specfile) + import rpm._rpmb + return rpm._rpmb.spec(specfile) def getKeys(self): keys = [] @@ -44,23 +51,32 @@ class TransactionSet(TransactionSetCore): else: return tuple(keys) - def addInstall(self, item, key, how="u"): - if isinstance(item, basestring): - f = file(item) - header = self.hdrFromFdno(f) - f.close() - elif isinstance(item, file): - header = self.hdrFromFdno(item) - else: + def _f2hdr(self, item): + if isinstance(item, _string_types): + with open(item) as f: + header = self.hdrFromFdno(f) + elif isinstance(item, rpm.hdr): header = item + else: + header = self.hdrFromFdno(item) + return header + + def addInstall(self, item, key, how="u"): + header = self._f2hdr(item) - if not how in ['u', 'i']: + if how not in ['u', 'i']: raise ValueError('how argument must be "u" or "i"') upgrade = (how == "u") if not TransactionSetCore.addInstall(self, header, key, upgrade): raise rpm.error("adding package to transaction failed") + def addReinstall(self, item, key): + header = self._f2hdr(item) + + if not TransactionSetCore.addReinstall(self, header, key): + raise rpm.error("adding package to transaction failed") + def addErase(self, item): hdrs = [] if isinstance(item, rpm.hdr): @@ -69,7 +85,7 @@ class TransactionSet(TransactionSetCore): hdrs = item elif isinstance(item, int): hdrs = self.dbMatch(rpm.RPMDBI_PACKAGES, item) - elif isinstance(item, basestring): + elif isinstance(item, _string_types): hdrs = self.dbMatch(rpm.RPMDBI_LABEL, item) else: raise TypeError("invalid type %s" % type(item)) @@ -86,7 +102,7 @@ class TransactionSet(TransactionSetCore): rc = TransactionSetCore.run(self, callback, data, self._probFilter) # crazy backwards compatibility goo: None for ok, list of problems - # if transaction didnt complete and empty list if it completed + # if transaction didn't complete and empty list if it completed # with errors if rc == 0: return None @@ -122,14 +138,18 @@ class TransactionSet(TransactionSetCore): needflags = rpm.RPMSENSE_ANY if len(needs) == 3: needop = needs[1] - if needop.find('<') >= 0: needflags |= rpm.RPMSENSE_LESS - if needop.find('=') >= 0: needflags |= rpm.RPMSENSE_EQUAL - if needop.find('>') >= 0: needflags |= rpm.RPMSENSE_GREATER + if '<' in needop: + needflags |= rpm.RPMSENSE_LESS + if '=' in needop: + needflags |= rpm.RPMSENSE_EQUAL + if '>' in needop: + needflags |= rpm.RPMSENSE_GREATER needver = needs[2] else: needver = "" - res.append(((n, v, r),(needname,needver),needflags,sense,p.key)) + res.append(((n, v, r), + (needname, needver), needflags, sense, p.key)) return res |