File: pkgsrcrecords.cc

package info (click to toggle)
python-apt 0.7.7.1%2Bnmu1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,276 kB
  • ctags: 930
  • sloc: cpp: 4,126; python: 3,108; makefile: 52
file content (180 lines) | stat: -rw-r--r-- 5,351 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: pkgsrcrecords.cc,v 1.2 2003/12/26 17:04:22 mdz Exp $
/* ######################################################################

   Package Records - Wrapper for the package records functions

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

#include <apt-pkg/sourcelist.h>

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

struct PkgSrcRecordsStruct
{
   pkgSourceList List;
   pkgSrcRecords *Records;
   pkgSrcRecords::Parser *Last;
   
   PkgSrcRecordsStruct() : Last(0) {
      List.ReadMainList();
      Records = new pkgSrcRecords(List);
   };
   ~PkgSrcRecordsStruct() {
      delete Records;
   };
};
    
// PkgSrcRecords Class							/*{{{*/
// ---------------------------------------------------------------------

static char *doc_PkgSrcRecordsLookup = "xxx";
static PyObject *PkgSrcRecordsLookup(PyObject *Self,PyObject *Args)
{   
   PkgSrcRecordsStruct &Struct = GetCpp<PkgSrcRecordsStruct>(Self);
   
   char *Name = 0;
   if (PyArg_ParseTuple(Args,"s",&Name) == 0)
      return 0;
   
   Struct.Last = Struct.Records->Find(Name, false);
   if (Struct.Last == 0) {
      Struct.Records->Restart();
      Py_INCREF(Py_None);
      return HandleErrors(Py_None);	
   }

   return Py_BuildValue("i", 1);
}

static char *doc_PkgSrcRecordsRestart = "Start Lookup from the begining";
static PyObject *PkgSrcRecordsRestart(PyObject *Self,PyObject *Args)
{   
   PkgSrcRecordsStruct &Struct = GetCpp<PkgSrcRecordsStruct>(Self);
   
   char *Name = 0;
   if (PyArg_ParseTuple(Args,"") == 0)
      return 0;
   
   Struct.Records->Restart();

   Py_INCREF(Py_None);
   return HandleErrors(Py_None);	
}

static PyMethodDef PkgSrcRecordsMethods[] = 
{
   {"Lookup",PkgSrcRecordsLookup,METH_VARARGS,doc_PkgSrcRecordsLookup},
   {"Restart",PkgSrcRecordsRestart,METH_VARARGS,doc_PkgSrcRecordsRestart},
   {}
};

static PyObject *PkgSrcRecordsAttr(PyObject *Self,char *Name)
{
   PkgSrcRecordsStruct &Struct = GetCpp<PkgSrcRecordsStruct>(Self);

   if (Struct.Last != 0)
   {
      if (strcmp("Package",Name) == 0)
	 return CppPyString(Struct.Last->Package());
      else if (strcmp("Version",Name) == 0)
	 return CppPyString(Struct.Last->Version());
      else if (strcmp("Maintainer",Name) == 0)
	 return CppPyString(Struct.Last->Maintainer());
      else if (strcmp("Section",Name) == 0)
	 return CppPyString(Struct.Last->Section());
      else if (strcmp("Record",Name) == 0)
	 return CppPyString(Struct.Last->AsStr());
      else if (strcmp("Binaries",Name) == 0) {
         PyObject *List = PyList_New(0);

         for(const char **b = Struct.Last->Binaries();
             *b != 0;
             ++b)
            PyList_Append(List, CppPyString(*b));
         return List; // todo
      } else if (strcmp("Index",Name) == 0) {
	 const pkgIndexFile &tmp = Struct.Last->Index();
	 return CppOwnedPyObject_NEW<pkgIndexFile*>(Self,&PackageIndexFileType, (pkgIndexFile*)&tmp);
      } else if (strcmp("Files",Name) == 0) {
         PyObject *List = PyList_New(0);

         vector<pkgSrcRecords::File> f;
	 if(!Struct.Last->Files(f))
	    return NULL; // error

	 PyObject *v;
	 for(unsigned int i=0;i<f.size();i++) {
	    v = Py_BuildValue("(siss)", 
			      f[i].MD5Hash.c_str(), 
			      f[i].Size, 
			      f[i].Path.c_str(), 
			      f[i].Type.c_str());
	    PyList_Append(List, v);
	    Py_DECREF(v);
	 }
         return List;
      } else if (strcmp("BuildDepends",Name) == 0) {
         PyObject *List = PyList_New(0);

         vector<pkgSrcRecords::Parser::BuildDepRec> bd;
	 if(!Struct.Last->BuildDepends(bd, false /* arch-only*/))
	    return NULL; // error

	 PyObject *v;
	 for(unsigned int i=0;i<bd.size();i++) {
	    v = Py_BuildValue("(ssii)", bd[i].Package.c_str(), 
			      bd[i].Version.c_str(), bd[i].Op, bd[i].Type);
	    PyList_Append(List, v);
	    Py_DECREF(v);
	 }
         return List;
      }
   }
   
   return Py_FindMethod(PkgSrcRecordsMethods,Self,Name);
}
PyTypeObject PkgSrcRecordsType =
{
   PyObject_HEAD_INIT(&PyType_Type)
   0,			                // ob_size
   "pkgSrcRecords",                          // tp_name
   sizeof(CppPyObject<PkgSrcRecordsStruct>),   // tp_basicsize
   0,                                   // tp_itemsize
   // Methods
   CppDealloc<PkgSrcRecordsStruct>,   // tp_dealloc
   0,                                   // tp_print
   PkgSrcRecordsAttr,                      // tp_getattr
   0,                                   // tp_setattr
   0,                                   // tp_compare
   0,                                   // tp_repr
   0,                                   // tp_as_number
   0,                                   // tp_as_sequence
   0,                                   // tp_as_mapping
   0,                                   // tp_hash
};

									/*}}}*/

PyObject *GetPkgSrcRecords(PyObject *Self,PyObject *Args)
{
#if 0
   PyObject *Owner;
   if (PyArg_ParseTuple(Args,"O!",&PkgCacheType,&Owner) == 0)
      return 0;

   return HandleErrors(CppOwnedPyObject_NEW<PkgSrcRecordsStruct>(Owner,
			   &PkgSrcRecordsType));
#endif
   if (PyArg_ParseTuple(Args,"") == 0)
      return 0;
   
   return HandleErrors(CppPyObject_NEW<PkgSrcRecordsStruct>(&PkgSrcRecordsType));
}