pkgrecords.cc 9.61 KB
Newer Older
Arch Librarian's avatar
Arch Librarian committed
1 2
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
3
// $Id: pkgrecords.cc,v 1.3 2002/02/26 01:36:15 mdz Exp $
Arch Librarian's avatar
Arch Librarian committed
4 5 6 7 8 9 10 11 12
/* ######################################################################

   Package Records - Wrapper for the package records functions

   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include "generic.h"
#include "apt_pkgmodule.h"
13
#include "pkgrecords.h"
Arch Librarian's avatar
Arch Librarian committed
14 15


Arch Librarian's avatar
Arch Librarian committed
16
#include <Python.h>
Arch Librarian's avatar
Arch Librarian committed
17 18
									/*}}}*/

Ben Finney's avatar
Ben Finney committed
19

Arch Librarian's avatar
Arch Librarian committed
20 21 22 23 24
// PkgRecords Class							/*{{{*/
// ---------------------------------------------------------------------


static PyObject *PkgRecordsLookup(PyObject *Self,PyObject *Args)
Ben Finney's avatar
Ben Finney committed
25
{
Arch Librarian's avatar
Arch Librarian committed
26
   PkgRecordsStruct &Struct = GetCpp<PkgRecordsStruct>(Self);
Ben Finney's avatar
Ben Finney committed
27

Arch Librarian's avatar
Arch Librarian committed
28 29
   PyObject *PkgFObj;
   long int Index;
30
   if (PyArg_ParseTuple(Args,"(O!l)",&PyPackageFile_Type,&PkgFObj,&Index) == 0)
Arch Librarian's avatar
Arch Librarian committed
31
      return 0;
Ben Finney's avatar
Ben Finney committed
32

Arch Librarian's avatar
Arch Librarian committed
33 34 35 36 37 38 39 40 41
   // Get the index and check to make sure it is reasonable
   pkgCache::PkgFileIterator &PkgF = GetCpp<pkgCache::PkgFileIterator>(PkgFObj);
   pkgCache *Cache = PkgF.Cache();
   if (Cache->DataEnd() <= Cache->VerFileP + Index + 1 ||
       Cache->VerFileP[Index].File != PkgF.Index())
   {
      PyErr_SetNone(PyExc_IndexError);
      return 0;
   }
Ben Finney's avatar
Ben Finney committed
42

Arch Librarian's avatar
Arch Librarian committed
43 44
   // Do the lookup
   Struct.Last = &Struct.Records.Lookup(pkgCache::VerFileIterator(*Cache,Cache->VerFileP+Index));
Michael Vogt's avatar
Michael Vogt committed
45 46

   // always return true (to make it consistent with the pkgsrcrecords object
47
   return PyBool_FromLong(1);
Arch Librarian's avatar
Arch Librarian committed
48
}
Ben Finney's avatar
Ben Finney committed
49 50

static PyMethodDef PkgRecordsMethods[] =
Arch Librarian's avatar
Arch Librarian committed
51
{
52 53 54
   {"lookup",PkgRecordsLookup,METH_VARARGS,
    "lookup((packagefile: apt_pkg.PackageFile, index: int)) -> bool\n\n"
    "Changes to a new package"},
Arch Librarian's avatar
Arch Librarian committed
55 56 57
   {}
};

58 59 60 61 62
/**
 * Get the PkgSrcRecordsStruct from a PyObject. If no package has been looked
 * up, set an AttributeError using the given name.
 */
static inline PkgRecordsStruct &GetStruct(PyObject *Self,char *name) {
63
   PkgRecordsStruct &Struct = GetCpp<PkgRecordsStruct>(Self);
64 65 66 67
   if (Struct.Last == 0)
      PyErr_SetString(PyExc_AttributeError,name);
   return Struct;
}
68

69 70
static PyObject *PkgRecordsGetFileName(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"FileName");
71
   return (Struct.Last != 0) ? CppPyPath(Struct.Last->FileName()) : 0;
72
}
73 74 75 76 77 78 79 80 81
static PyObject *PkgRecordsGetHashes(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"Hashes");
   if (Struct.Last == 0)
      return 0;

   auto py = CppPyObject_NEW<HashStringList> (nullptr, &PyHashStringList_Type);
   py->Object = Struct.Last->Hashes();
   return py;
}
82 83
static PyObject *PkgRecordsGetMD5Hash(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"MD5Hash");
84 85
   if (Struct.Last == NULL)
      return 0;
86 87
   auto hashes = Struct.Last->Hashes();
   auto hash = hashes.find("md5sum");
88 89 90
   if (hash == NULL)
      return 0;
   return CppPyString(hash->HashValue());
91 92 93
}
static PyObject *PkgRecordsGetSHA1Hash(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"SHA1Hash");
94 95
   if (Struct.Last == NULL)
      return 0;
96 97
   auto hashes = Struct.Last->Hashes();
   auto hash = hashes.find("sha1");
98 99 100
   if (hash == NULL)
      return 0;
   return CppPyString(hash->HashValue());
101 102 103
}
static PyObject *PkgRecordsGetSHA256Hash(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"SHA256Hash");
104 105
   if (Struct.Last == NULL)
      return 0;
106 107
   auto hashes = Struct.Last->Hashes();
   auto hash = hashes.find("sha256");
108 109 110
   if (hash == NULL)
      return 0;
   return CppPyString(hash->HashValue());
111 112 113 114 115 116 117 118
}
static PyObject *PkgRecordsGetSourcePkg(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"SourcePkg");
   return (Struct.Last != 0) ? CppPyString(Struct.Last->SourcePkg()) : 0;
}
static PyObject *PkgRecordsGetSourceVer(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"SourceVer");
   return (Struct.Last != 0) ? CppPyString(Struct.Last->SourceVer()) : 0;
119
}
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
static PyObject *PkgRecordsGetMaintainer(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"Maintainer");
   return (Struct.Last != 0) ? CppPyString(Struct.Last->Maintainer()) : 0;
}
static PyObject *PkgRecordsGetShortDesc(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"ShortDesc");
   return (Struct.Last != 0) ? CppPyString(Struct.Last->ShortDesc()) : 0;
}
static PyObject *PkgRecordsGetLongDesc(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"LongDesc");
   return (Struct.Last != 0) ? CppPyString(Struct.Last->LongDesc()) : 0;
}
static PyObject *PkgRecordsGetName(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"Name");
   return (Struct.Last != 0) ? CppPyString(Struct.Last->Name()) : 0;
}
static PyObject *PkgRecordsGetHomepage(PyObject *Self,void*) {
   PkgRecordsStruct &Struct = GetStruct(Self,"Homepage");
   return (Struct.Last != 0) ? CppPyString(Struct.Last->Homepage()) : 0;
}
static PyObject *PkgRecordsGetRecord(PyObject *Self,void*) {
   const char *start, *stop;
   PkgRecordsStruct &Struct = GetStruct(Self,"Record");
   if (Struct.Last == 0)
      return 0;
   Struct.Last->GetRec(start, stop);
   return PyString_FromStringAndSize(start,stop-start);
}
static PyGetSetDef PkgRecordsGetSet[] = {
149 150 151 152 153 154 155
   {"filename",PkgRecordsGetFileName,0,
    "The filename of the package, as stored in the 'Filename' field."},
   {"homepage",PkgRecordsGetHomepage,0,
    "The homepage of the package, as stored in the 'Homepage' field."},
   {"long_desc",PkgRecordsGetLongDesc,0,
    "The long description of the packages; i.e. all lines in the\n"
    "'Description' field except for the first one."},
156 157
   {"hashes",PkgRecordsGetHashes,0,
    "The hashes of the packages, as a HashStringList"},
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
   {"md5_hash",PkgRecordsGetMD5Hash,0,
    "The MD5 hash value of the package, as stored in the 'MD5Sum' field."},
   {"maintainer",PkgRecordsGetMaintainer,0,
    "The maintainer of the package, as stored in the 'Maintainer' field."},
   {"name",PkgRecordsGetName,0,
    "The name of the package, as stored in the 'Package' field."},
   {"record",PkgRecordsGetRecord,0,
    "The raw record, suitable for parsing by apt_pkg.TagSection."},
   {"sha1_hash",PkgRecordsGetSHA1Hash,0,
    "The SHA1 hash value, as stored in the 'SHA1' field."},
   {"sha256_hash",PkgRecordsGetSHA256Hash,0,
    "The SHA256 hash value, as stored in the 'SHA256' field."},
   {"short_desc",PkgRecordsGetShortDesc,0,
    "The short description of the package, i.e. the first line of the\n"
    "'Description' field."},
   {"source_pkg",PkgRecordsGetSourcePkg,0,
    "The name of the source package, if different from the name of the\n"
    "binary package. This information is retrieved from the 'Source' field."},
   {"source_ver",PkgRecordsGetSourceVer,0,
    "The version of the source package, if it differs from the version\n"
    "of the binary package. Just like 'source_pkg', this information\n"
    "is retrieved from the 'Source' field."},
180 181 182
   {}
};

183 184 185 186
static PyObject *PkgRecordsNew(PyTypeObject *type,PyObject *Args,PyObject *kwds)
{
   PyObject *Owner;
   char *kwlist[] = {"cache",0};
187
   if (PyArg_ParseTupleAndKeywords(Args,kwds,"O!",kwlist,&PyCache_Type,
188 189 190
                                   &Owner) == 0)
      return 0;

191
   return HandleErrors(CppPyObject_NEW<PkgRecordsStruct>(Owner,type,
192 193 194
							      GetCpp<pkgCache *>(Owner)));
}

195 196 197 198 199 200 201
static const char *packagerecords_doc =
    "PackageRecords(cache: apt_pkg.Cache)\n\n"
    "Package Records contain information about packages. Those objects\n"
    "can be used to retrieve information such as maintainer or filename\n"
    "of a package. They can also be used to retrieve the raw records\n"
    "of the packages (i.e. those stanzas stored in Packages files).";

202
PyTypeObject PyPackageRecords_Type =
Arch Librarian's avatar
Arch Librarian committed
203
{
204
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
205
   "apt_pkg.PackageRecords",                          // tp_name
206
   sizeof(CppPyObject<PkgRecordsStruct>),   // tp_basicsize
Arch Librarian's avatar
Arch Librarian committed
207 208
   0,                                   // tp_itemsize
   // Methods
209
   CppDealloc<PkgRecordsStruct>,   // tp_dealloc
Arch Librarian's avatar
Arch Librarian committed
210
   0,                                   // tp_print
211
   0,                                    // tp_getattr
Arch Librarian's avatar
Arch Librarian committed
212 213 214 215 216 217 218
   0,                                   // tp_setattr
   0,                                   // tp_compare
   0,                                   // tp_repr
   0,                                   // tp_as_number
   0,                                   // tp_as_sequence
   0,                                   // tp_as_mapping
   0,                                   // tp_hash
219 220
   0,                                   // tp_call
   0,                                   // tp_str
Julian Andres Klode's avatar
Julian Andres Klode committed
221
   _PyAptObject_getattro,               // tp_getattro
222 223
   0,                                   // tp_setattro
   0,                                   // tp_as_buffer
224
   (Py_TPFLAGS_DEFAULT |                // tp_flags
225 226
    Py_TPFLAGS_BASETYPE |
    Py_TPFLAGS_HAVE_GC),
227 228 229
   packagerecords_doc,                  // tp_doc
   CppTraverse<PkgRecordsStruct>,       // tp_traverse
   CppClear<PkgRecordsStruct>,          // tp_clear
230 231 232 233 234 235 236
   0,                                   // tp_richcompare
   0,                                   // tp_weaklistoffset
   0,                                   // tp_iter
   0,                                   // tp_iternext
   PkgRecordsMethods,                   // tp_methods
   0,                                   // tp_members
   PkgRecordsGetSet,                    // tp_getset
237 238 239 240 241 242 243 244
   0,                                   // tp_base
   0,                                   // tp_dict
   0,                                   // tp_descr_get
   0,                                   // tp_descr_set
   0,                                   // tp_dictoffset
   0,                                   // tp_init
   0,                                   // tp_alloc
   PkgRecordsNew,                       // tp_new
Arch Librarian's avatar
Arch Librarian committed
245 246 247 248
};

									/*}}}*/

249