54728bbd4be660b0ee25b9fe517434f2c6337185
[tools/librpm-tizen.git] / python / rpm / __init__.py
1 r"""RPM Module
2
3 This module enables you to manipulate rpms and the rpm database.
4
5 The rpm base module provides the main starting point for
6 accessing RPM from Python. For most usage, call
7 the TransactionSet method to get a transaction set (rpmts).
8
9 For example:
10         import rpm
11         ts = rpm.TransactionSet()
12
13 The transaction set will open the RPM database as needed, so
14 in most cases, you do not need to explicitly open the
15 database. The transaction set is the workhorse of RPM.
16
17 You can open another RPM database, such as one that holds
18 all packages for a given Linux distribution, to provide
19 packages used to solve dependencies. To do this, use
20 the following code:
21
22 rpm.addMacro('_dbpath', '/path/to/alternate/database')
23 solvets = rpm.TransactionSet()
24 solvets.openDB()
25 rpm.delMacro('_dbpath')
26
27 # Open default database
28 ts = rpm.TransactionSet()
29
30 This code gives you access to two RPM databases through
31 two transaction sets (rpmts): ts is a transaction set
32 associated with the default RPM database and solvets
33 is a transaction set tied to an alternate database, which
34 is very useful for resolving dependencies.
35 """
36
37 import warnings
38 from rpm._rpm import *
39 from rpm.transaction import *
40 import rpm._rpm as _rpm
41 _RPMVSF_NODIGESTS = _rpm._RPMVSF_NODIGESTS
42 _RPMVSF_NOHEADER = _rpm._RPMVSF_NOHEADER
43 _RPMVSF_NOPAYLOAD = _rpm._RPMVSF_NOPAYLOAD
44 _RPMVSF_NOSIGNATURES = _rpm._RPMVSF_NOSIGNATURES
45
46 __version__ = _rpm.__version__
47 __version_info__ = tuple(__version__.split('.'))
48
49 # try to import build bits but dont require it
50 try:
51     from rpm._rpmb import *
52 except ImportError:
53     pass
54
55 # try to import signing bits but dont require it
56 try:
57     from rpm._rpms import *
58 except ImportError:
59     pass
60
61 # backwards compatibility + give the same class both ways
62 ts = TransactionSet
63
64
65 def headerLoad(*args, **kwds):
66     """DEPRECATED! Use rpm.hdr() instead."""
67     warnings.warn("Use rpm.hdr() instead.", DeprecationWarning, stacklevel=2)
68     return hdr(*args, **kwds)
69
70
71 def _doHeaderListFromFD(rpm_fd, retrofit):
72     hlist = []
73     while 1:
74         try:
75             h = hdr(rpm_fd)
76             if retrofit:
77                 h.convert(HEADERCONV_RETROFIT_V3)
78             hlist.append(h)
79         except _rpm.error:
80             break
81
82     return hlist
83
84
85 def readHeaderListFromFD(file_desc, retrofit=True):
86     if not isinstance(file_desc, fd):
87         file_desc = fd(file_desc)
88     return _doHeaderListFromFD(file_desc, retrofit)
89
90
91 def readHeaderListFromFile(path, retrofit=True):
92     f = fd(path)
93     hlist = _doHeaderListFromFD(f, retrofit)
94     f.close()
95     return hlist
96
97
98 def readHeaderFromFD(file_desc):
99     """Return (header, pos_before_hdr)"""
100     if not isinstance(file_desc, fd):
101         file_desc = fd(file_desc)
102     try:
103         offset = file_desc.tell()
104         h = hdr(file_desc)
105     except (_rpm.error, IOError):
106         offset = None
107         h = None
108
109     return (h, offset)
110
111
112 def signalsCaught(siglist):
113     """Returns list of signals that were caught."""
114     caught = []
115     for sig in siglist:
116         if signalCaught(sig):
117             caught.append(sig)
118
119     return caught
120
121
122 def dsSingle(TagN, N, EVR="", Flags=RPMSENSE_ANY):
123     """
124     Creates a single entry dependency set (ds)
125
126     dsSingle(RPMTAG_CONFLICTNAME, "rpm") corresponds to "Conflicts: rpm"
127     """
128     return ds((N, Flags, EVR), TagN)