sourcelist.cc 6.11 KB
Newer Older
1 2
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
3
// $Id: sourcelist.cc,v 1.2 2003/12/26 17:04:22 mdz Exp $
4 5
/* ######################################################################

6
   SourcesList - Wrapper for the SourcesList functions
7 8 9 10 11 12

   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include "generic.h"
#include "apt_pkgmodule.h"
13 14

#include <apt-pkg/sourcelist.h>
15 16 17 18

#include <Python.h>
									/*}}}*/

19

Ben Finney's avatar
Ben Finney committed
20

21 22 23
// PkgsourceList Class							/*{{{*/
// ---------------------------------------------------------------------

24 25 26 27
static char *doc_PkgSourceListFindIndex =
    "find_index(pkgfile: apt_pkg.PackageFile) -> apt_pkg.IndexFile\n\n"
    "Return the index file for the given package file, or None if none\n"
    "could be found.";
28
static PyObject *PkgSourceListFindIndex(PyObject *Self,PyObject *Args)
Ben Finney's avatar
Ben Finney committed
29
{
30
   pkgSourceList *list = GetCpp<pkgSourceList*>(Self);
31
   PyObject *pyPkgFileIter;
32
   CppPyObject<pkgIndexFile*> *pyPkgIndexFile;
33

34
   if (PyArg_ParseTuple(Args, "O!", &PyPackageFile_Type,&pyPkgFileIter) == 0)
35 36 37 38 39 40
      return 0;

   pkgCache::PkgFileIterator &i = GetCpp<pkgCache::PkgFileIterator>(pyPkgFileIter);
   pkgIndexFile *index;
   if(list->FindIndex(i, index))
   {
41
      pyPkgIndexFile = CppPyObject_NEW<pkgIndexFile*>(pyPkgFileIter,&PyIndexFile_Type,index);
42 43
      // Do not delete the pkgIndexFile*, it is managed by pkgSourceList.
      pyPkgIndexFile->NoDelete = true;
44 45 46
      return pyPkgIndexFile;
   }

47
   //&PyIndexFile_Type,&pyPkgIndexFile)
Ben Finney's avatar
Ben Finney committed
48

49 50
   Py_INCREF(Py_None);
   return Py_None;
51 52
}

53 54 55 56
static char *doc_PkgSourceListReadMainList =
    "read_main_list() -> bool\n\n"
    "Read /etc/apt/sources.list and similar files to populate the list\n"
    "of indexes.";
57 58
static PyObject *PkgSourceListReadMainList(PyObject *Self,PyObject *Args)
{
59 60
   pkgSourceList *list = GetCpp<pkgSourceList*>(Self);
   bool res = list->ReadMainList();
61

62
   return HandleErrors(PyBool_FromLong(res));
63 64
}

65 66 67 68 69
static char *doc_PkgSourceListGetIndexes =
    "get_indexes(acquire: apt_pkg.Acquire[, all: bool=False]) -> bool\n\n"
    "Add all indexes (i.e. stuff like Release files, Packages files)\n"
    "to the Acquire object 'acquire'. If 'all' is True, all indexes\n"
    "will be added, otherwise only changed indexes will be added.";
70 71 72 73 74
static PyObject *PkgSourceListGetIndexes(PyObject *Self,PyObject *Args)
{
   pkgSourceList *list = GetCpp<pkgSourceList*>(Self);

   PyObject *pyFetcher;
Michael Vogt's avatar
Michael Vogt committed
75
   char all = 0;
76
   if (PyArg_ParseTuple(Args, "O!|b",&PyAcquire_Type,&pyFetcher, &all) == 0)
77 78 79
      return 0;

   pkgAcquire *fetcher = GetCpp<pkgAcquire*>(pyFetcher);
Michael Vogt's avatar
Michael Vogt committed
80
   bool res = list->GetIndexes(fetcher, all);
81

82
   return HandleErrors(PyBool_FromLong(res));
83 84
}

Ben Finney's avatar
Ben Finney committed
85
static PyMethodDef PkgSourceListMethods[] =
86
{
87 88 89
   {"find_index",PkgSourceListFindIndex,METH_VARARGS,doc_PkgSourceListFindIndex},
   {"read_main_list",PkgSourceListReadMainList,METH_VARARGS,doc_PkgSourceListReadMainList},
   {"get_indexes",PkgSourceListGetIndexes,METH_VARARGS,doc_PkgSourceListGetIndexes},
90 91 92
   {}
};

93
static PyObject *PkgSourceListGetList(PyObject *Self,void*)
94
{
Michael Vogt's avatar
Michael Vogt committed
95
   pkgSourceList *list = GetCpp<pkgSourceList*>(Self);
96
   PyObject *List = PyList_New(0);
97
   for (std::vector<metaIndex *>::const_iterator I = list->begin();
98
        I != list->end(); I++)
Michael Vogt's avatar
Michael Vogt committed
99
   {
100 101
      CppPyObject<metaIndex*> *Obj;
      Obj = CppPyObject_NEW<metaIndex*>(Self, &PyMetaIndex_Type,*I);
102 103
      // Never delete metaIndex*, they are managed by the pkgSourceList.
      Obj->NoDelete = true;
104
      PyList_Append(List,Obj);
105
      Py_DECREF(Obj);
Michael Vogt's avatar
Michael Vogt committed
106
   }
107
   return List;
108
}
109 110

static PyGetSetDef PkgSourceListGetSet[] = {
111
    {"list",PkgSourceListGetList,0,"A list of MetaIndex() objects.",0},
112 113 114
    {}
};

115 116 117 118 119
static PyObject *PkgSourceListNew(PyTypeObject *type,PyObject *args,PyObject *kwds)
{
   char *kwlist[] = {0};
   if (PyArg_ParseTupleAndKeywords(args,kwds,"",kwlist) == 0)
      return 0;
120
   return CppPyObject_NEW<pkgSourceList*>(NULL, type,new pkgSourceList());
121 122
}

123 124 125 126
static const char *sourcelist_doc =
    "SourceList()\n\n"
    "Represent the list of sources stored in /etc/apt/sources.list and\n"
    "similar files.";
127
PyTypeObject PySourceList_Type =
128
{
129
   PyVarObject_HEAD_INIT(&PyType_Type, 0)
130
   "apt_pkg.SourceList",                          // tp_name
131
   sizeof(CppPyObject<pkgSourceList*>),   // tp_basicsize
132 133
   0,                                   // tp_itemsize
   // Methods
134
   CppDeallocPtr<pkgSourceList*>,       // tp_dealloc
135
   0,                                   // tp_print
136
   0,                                   // tp_getattr
137 138 139 140 141 142 143
   0,                                   // tp_setattr
   0,                                   // tp_compare
   0,                                   // tp_repr
   0,                                   // tp_as_number
   0,                                   // tp_as_sequence
   0,                                   // tp_as_mapping
   0,                                   // tp_hash
144 145
   0,                                   // tp_call
   0,                                   // tp_str
Julian Andres Klode's avatar
Julian Andres Klode committed
146
   _PyAptObject_getattro,               // tp_getattro
147 148
   0,                                   // tp_setattro
   0,                                   // tp_as_buffer
149 150
   (Py_TPFLAGS_DEFAULT |                // tp_flags
    Py_TPFLAGS_BASETYPE),
151
   sourcelist_doc,                      // tp_doc
152 153 154 155 156 157 158 159 160
   0,                                   // tp_traverse
   0,                                   // tp_clear
   0,                                   // tp_richcompare
   0,                                   // tp_weaklistoffset
   0,                                   // tp_iter
   0,                                   // tp_iternext
   PkgSourceListMethods,                // tp_methods
   0,                                   // tp_members
   PkgSourceListGetSet,                 // tp_getset
161 162 163 164 165 166 167 168
   0,                                   // tp_base
   0,                                   // tp_dict
   0,                                   // tp_descr_get
   0,                                   // tp_descr_set
   0,                                   // tp_dictoffset
   0,                                   // tp_init
   0,                                   // tp_alloc
   PkgSourceListNew,                    // tp_new
169 170
};