Add bunch of missing includes to fix build with gcc 4.7
[apt.git] / python / apt.i
1 %module apt
2 %{
3 /* XXX Hack around SWIG brokenness on gcc 4.6 */
4 #include <cstddef>
5 %}
6
7 %include std_string.i
8 %include std_vector.i
9
10 %{
11 #include <apt-pkg/init.h>
12 #include <apt-pkg/pkgcache.h>
13 #include <apt-pkg/depcache.h>
14 #include <apt-pkg/cachefile.h>
15 #include <apt-pkg/mmap.h>
16 #include <apt-pkg/cacheiterators.h>
17 #include <apt-pkg/algorithms.h>
18 #include <apt-pkg/pkgsystem.h>
19 #include <apt-pkg/contrib/configuration.h>
20 #include <apt-pkg/contrib/progress.h>
21 #include <apt-pkg/version.h>
22 #include <apt-pkg/pkgrecords.h>
23 #include <apt-pkg/acquire.h>
24 #include <apt-pkg/acquire-item.h>
25 #include <apt-pkg/packagemanager.h>
26 #include <apt-pkg/sourcelist.h>
27 #include <apt-pkg/contrib/error.h>
28 #include <apt-pkg/luaiface.h>
29
30 #include <string>
31 #include <time.h>
32 %}
33
34 %inline %{
35 typedef pkgCache::VerIterator VerIterator;
36 typedef pkgCache::PkgIterator PkgIterator;
37 typedef pkgCache::DepIterator DepIterator;
38 typedef pkgCache::PrvIterator PrvIterator;
39 typedef pkgCache::PkgFileIterator PkgFileIterator;
40 typedef pkgCache::VerFileIterator VerFileIterator;
41 typedef pkgCache::Header Header;
42 typedef pkgCache::Package Package;
43 typedef pkgCache::PackageFile PackageFile;
44 typedef pkgCache::VerFile VerFile;
45 typedef pkgCache::Version Version;
46 typedef pkgCache::Dependency Dependency;
47 typedef pkgCache::Provides Provides;
48 typedef pkgCache::StringItem StringItem;
49 typedef pkgCache::Dep Dep;
50 typedef pkgCache::State State;
51 typedef pkgCache::Flag Flag;
52 typedef pkgDepCache::Policy Policy;
53 typedef pkgDepCache::StateCache StateCache;
54 typedef Configuration::Item Item;
55 typedef pkgRecords::Parser Parser;
56 %}
57
58 /* Fix some operators. */
59 %rename(next) operator++;
60 %rename(assign) operator=;
61 %rename(pkgCache) operator pkgCache *;
62 %rename(pkgDepCache) operator pkgDepCache *;
63 %rename(Package) operator Package *;
64 %rename(Version) operator Version *;
65 %rename(Dependency) operator Dependency *;
66 %rename(Provides) operator Provides *;
67 %rename(PackageFile) operator PackageFile *;
68 %rename(VerFile) operator VerFile *;
69 %rename(__getitem__) operator[];
70 %ignore operator pkgCache &;
71 %ignore operator pkgDepCache &;
72
73 /* Set some data as immutable. */
74 %immutable pkgVersion;
75 %immutable pkgLibVersion;
76 %immutable pkgOS;
77 %immutable pkgCPU;
78 %immutable pkgSystem::Label;
79 %immutable pkgVersioningSystem::Label;
80 %immutable pkgDepCache::StateCache::CandVersion;
81 %immutable pkgDepCache::StateCache::CurVersion;
82
83 /* One-shot initialization function. */
84 %inline %{
85 inline bool pkgInit() 
86 {
87    return pkgInitConfig(*_config) && pkgInitSystem(*_config,_system);
88 }
89 %}
90
91 /* No suport for nested classes yet. */
92 %rename(pkgCacheHeader) pkgCache::Header;
93 %rename(pkgCachePackage) pkgCache::Package;
94 %rename(pkgCachePackageFile) pkgCache::PackageFile;
95 %rename(pkgCacheVerFile) pkgCache::VerFile;
96 %rename(pkgCacheVersion) pkgCache::Version;
97 %rename(pkgCacheDependency) pkgCache::Dependency;
98 %rename(pkgCacheProvides) pkgCache::Provides;
99 %rename(pkgCacheStringItem) pkgCache::StringItem;
100 %rename(pkgCacheDep) pkgCache::Dep;
101 %rename(pkgCacheState) pkgCache::State;
102 %rename(pkgCacheFlag) pkgCache::Flag;
103 %rename(pkgCacheVerIterator) pkgCache::VerIterator;
104 %rename(pkgCachePkgIterator) pkgCache::PkgIterator;
105 %rename(pkgCacheDepIterator) pkgCache::DepIterator;
106 %rename(pkgCachePrvIterator) pkgCache::PrvIterator;
107 %rename(pkgCachePkgFileIterator) pkgCache::PkgFileIterator;
108 %rename(pkgCacheVerFileIterator) pkgCache::VerFileIterator;
109 %rename(pkgDepCacheStateCache) pkgDepCache::StateCache;
110 %rename(pkgRecordsParser) pkgRecords::Parser;
111 %rename(pkgAcquireItem) pkgAcquire::Item;
112 %rename(ConfigurationItem) Configuration::Item;
113
114 /* Wonderful SWIG magic to turn APT iterators into Python iterators. */
115 %exception next {
116         $action
117         /* Pass ahead the StopIteration exception. */
118         if (!result) return NULL;
119 }
120 #define EXTEND_ITERATOR(IterClass) \
121 %newobject IterClass::next; \
122 %newobject IterClass::__iter__; \
123 %ignore IterClass::operator++; \
124 %extend IterClass { \
125         inline bool __nonzero__() { return self->end() == false; }; \
126         inline IterClass *next() { \
127                 if (self->end() == true) { \
128                         PyErr_SetObject(PyExc_StopIteration, Py_None); \
129                         return NULL; \
130                 } \
131                 IterClass *ret = new IterClass(*self); \
132                 (*self)++; \
133                 return ret; \
134         }; \
135         /* We must return a copy here, otherwise the original object
136          * might be deallocated and the returned pointer would become
137          * invalid */ \
138         inline IterClass *__iter__() { return new IterClass(*self); }; \
139 }
140 EXTEND_ITERATOR(pkgCache::PkgIterator);
141 EXTEND_ITERATOR(pkgCache::VerIterator);
142 EXTEND_ITERATOR(pkgCache::DepIterator);
143 EXTEND_ITERATOR(pkgCache::PrvIterator);
144 EXTEND_ITERATOR(pkgCache::PkgFileIterator);
145 EXTEND_ITERATOR(pkgCache::VerFileIterator);
146
147 /* Now transform the functions returning iterators into ones with more
148  * meaningful names for that purpose. */
149 %rename(PkgIter) pkgDepCache::PkgBegin;
150 %rename(PkgIter) pkgCache::PkgBegin;
151 %rename(FileIter) pkgCache::FileBegin;
152 %ignore pkgCache::PkgEnd;
153 %ignore pkgCache::FileEnd;
154
155 /* That's the best way I found to access ItemsBegin/ItemsEnd, for now.
156  * It may be changed to behave like the iterators above in the future,
157  * so don't expect a list to be returned. */
158 %ignore pkgAcquire::ItemsBegin;
159 %ignore pkgAcquire::ItemsEnd;
160 %extend pkgAcquire {
161 PyObject *
162 ItemsIter()
163 {
164         static swig_type_info *ItemDescr = NULL;
165         PyObject *list, *o;
166         pkgAcquire::ItemIterator I;
167         if (!ItemDescr) {
168                 ItemDescr = SWIG_TypeQuery("pkgAcquire::Item *");
169                 assert(ItemDescr);
170         }
171         list = PyList_New(0);
172         if (list == NULL)
173                 return NULL;
174         for (I = self->ItemsBegin(); I != self->ItemsEnd(); I++) {
175                 o = SWIG_NewPointerObj((void *)(*I), ItemDescr, 0);
176                 if (!o || PyList_Append(list, o) == -1) {
177                         Py_XDECREF(o);
178                         Py_DECREF(list);
179                         return NULL;
180                 }
181                 Py_DECREF(o);
182         }
183         return list;
184 }
185 }
186
187 /* SWIG 2.0.2 chokes on these, ignore for now... */
188 %ignore pkgAcquire::UriBegin;
189 %ignore pkgAcquire::UriEnd;
190
191 /* Wrap string members. */
192 %immutable pkgAcquire::Item::DestFile;
193 %immutable pkgAcquire::Item::ErrorText;
194 %extend pkgAcquire::Item {
195         const char *DestFile;
196         const char *ErrorText;
197 }
198 %ignore pkgAcquire::Item::DestFile;
199 %ignore pkgAcquire::Item::ErrorText;
200 %{
201 #define pkgAcquire_Item_DestFile_get(x) ((x)->DestFile.c_str())
202 #define pkgAcquire_Item_ErrorText_get(x) ((x)->ErrorText.c_str())
203 %}
204
205 /* Also from Configuration::Item. */
206 %extend Configuration::Item {
207         const char *Tag;
208         const char *Value;
209 }
210 %ignore Configuration::Item::Tag;
211 %ignore Configuration::Item::Value;
212 %{
213 #define Configuration_Item_Tag_get(x) ((x)->Tag.c_str())
214 #define Configuration_Item_Value_get(x) ((x)->Value.c_str())
215 #define Configuration_Item_Tag_set(x,y) ((x)->Tag = (y))
216 #define Configuration_Item_Value_set(x,y) ((x)->Value = (y))
217 %}
218
219 /* Typemap to present map_ptrloc in a better way */
220 %apply int { map_ptrloc };
221
222 /* That should be enough for our usage, but _error is indeed an alias
223  * for a function which returns an statically alocated GlobalError object. */
224 %immutable _error;
225 GlobalError *_error;
226
227 %immutable _lua;
228 Lua *_lua;
229
230 /* Undefined reference!? */
231 %ignore pkgCache::PkgIterator::TargetVer;
232
233 /* There's a struct and a function with the same name. */
234 %ignore SubstVar;
235
236 /* Allow threads to run while doing DoInstall() */
237 %exception pkgPackageManager::DoInstall {
238 Py_BEGIN_ALLOW_THREADS
239 $function
240 Py_END_ALLOW_THREADS
241 }
242
243 /* Preprocess string macros (note that we're %import'ing). */
244 %import <apt-pkg/contrib/strutl.h>
245
246 %include <apt-pkg/init.h>
247 %include <apt-pkg/pkgcache.h>
248 %include <apt-pkg/depcache.h>
249 %include <apt-pkg/cacheiterators.h>
250 %include <apt-pkg/cachefile.h>
251 %include <apt-pkg/algorithms.h>
252 %include <apt-pkg/pkgsystem.h>
253 %include <apt-pkg/contrib/configuration.h>
254 %include <apt-pkg/contrib/progress.h>
255 %include <apt-pkg/version.h>
256 %include <apt-pkg/pkgrecords.h>
257 %include <apt-pkg/acquire.h>
258 %include <apt-pkg/acquire-item.h>
259 %include <apt-pkg/packagemanager.h>
260 %include <apt-pkg/sourcelist.h>
261 %include <apt-pkg/contrib/error.h>
262 %include <apt-pkg/luaiface.h>
263
264 /* Create a dumb status class which can be instantiated. pkgAcquireStatus
265  * has fully abstract methods. */
266 %inline %{
267 class pkgAcquireStatusDumb : public pkgAcquireStatus
268 {
269    virtual bool MediaChange(string Media,string Drive) {};
270 };
271 %}
272
273 /* That's the real class we use for Python inheritance. */
274 %{
275 class ROpPyProgress : public OpProgress {
276         PyObject *PyObj;
277
278         public:
279         OpProgress::Op;
280         OpProgress::SubOp;
281         OpProgress::Percent;
282         OpProgress::MajorChange;
283         OpProgress::CheckChange;
284
285         virtual void Update()
286         {
287                 if (PyObject_HasAttrString(PyObj, "Update")) {
288                         PyObject *Ret;
289                         Ret = PyObject_CallMethod(PyObj, "Update", NULL);
290                         Py_XDECREF(Ret);
291                 }
292         };
293
294         virtual void Done()
295         {
296                 if (PyObject_HasAttrString(PyObj, "Done")) {
297                         PyObject *Ret;
298                         Ret = PyObject_CallMethod(PyObj, "Done", NULL);
299                         Py_XDECREF(Ret);
300                 }
301         };
302         
303         ROpPyProgress(PyObject *PyObj) : PyObj(PyObj) {Py_INCREF(PyObj);};
304         ~ROpPyProgress() {Py_DECREF(PyObj);};
305 };
306 %}
307
308 /* That's how we want SWIG to see our class. */
309 %extend ROpPyProgress {
310         const char *Op;
311         const char *SubOp;
312 }
313 %ignore ROpPyProgress::Op;
314 %ignore ROpPyProgress::SubOp;
315 %{
316 #define ROpPyProgress_Op_get(x) ((x)->Op.c_str())
317 #define ROpPyProgress_Op_set(x,y) ((x)->Op = (y))
318 #define ROpPyProgress_SubOp_get(x) ((x)->SubOp.c_str())
319 #define ROpPyProgress_SubOp_set(x,y) ((x)->SubOp = (y))
320 %}
321 class ROpPyProgress : public OpProgress {
322         public:
323         float Percent;
324         bool MajorChange;
325         bool CheckChange(float Interval=0.7);               
326         ROpPyProgress(PyObject *PyObj);
327 };
328
329 /* That's the proxy class the user sees. This exists only to pass
330  * "self" as the first parameter of ROpPyProgress. */
331 %pythoncode %{
332 class OpPyProgress(ROpPyProgress):
333         def __init__(self):
334                 ROpPyProgress.__init__(self, self)
335         def __repr__(self):
336                 return "<C OpPyProgress instance at %s>" % self.this
337 %}
338
339 #if 0
340 /* The same scheme as above for pkgAcquireStatus. */
341 %{
342 class pkgRPyAcquireStatus : public pkgAcquireStatus
343 {
344         PyObject *PyObj;
345
346         void ItemDescMethod(const char *Name, pkgAcquire::ItemDesc &Itm) {
347                 static swig_type_info *SwigType = 0;
348                 if (!SwigType) {
349                         SwigType = SWIG_TypeQuery("pkgAcquire::ItemDesc *");
350                         assert(SwigType);
351                 }
352                 PyObject *attr = PyObject_GetAttrString(PyObj, (char*)Name);
353                 if (attr != NULL) {
354                         PyObject *Arg;
355                         Arg = SWIG_NewPointerObj(&Itm, SwigType, 0);
356                         if (!Arg) return;
357                         PyObject *Ret;
358                         Ret = PyObject_CallFunction(attr, "(O)", Arg);
359                         Py_XDECREF(Arg);
360                         Py_XDECREF(Ret);
361                 } else {
362                         PyErr_Clear();
363                 }
364         };
365
366         public:
367         /* No wrapping yet. */
368         //struct timeval Time;
369         //struct timeval StartTime;
370         pkgAcquireStatus::LastBytes;
371         pkgAcquireStatus::CurrentCPS;
372         pkgAcquireStatus::CurrentBytes;
373         pkgAcquireStatus::TotalBytes;
374         pkgAcquireStatus::FetchedBytes;
375         pkgAcquireStatus::ElapsedTime;
376         pkgAcquireStatus::TotalItems;
377         pkgAcquireStatus::CurrentItems;
378    
379         /* Call only Python method, if existent, or parent method. */
380         void Fetched(unsigned long Size,unsigned long ResumePoint)
381         {
382                 PyObject *attr = PyObject_GetAttrString(PyObj, "Fetched");
383                 if (attr != NULL) {
384                         PyObject *Ret;
385                         Ret = PyObject_CallFunction(attr, "(ii)",
386                                                     Size, ResumePoint);
387                         Py_XDECREF(Ret);
388                 } else {
389                         PyErr_Clear();
390                         pkgAcquireStatus::Fetched(Size, ResumePoint);
391                 }
392         };
393
394         bool MediaChange(string Media,string Drive)
395         {
396                 PyObject *attr = PyObject_GetAttrString(PyObj, "MediaChange");
397                 if (attr != NULL) {
398                         PyObject *Ret;
399                         bool RealRet = false;
400                         Ret = PyObject_CallFunction(attr, "(ss)",
401                                                     Media.c_str(),
402                                                     Drive.c_str());
403                         if (Ret) {
404                                 RealRet = PyObject_IsTrue(Ret);
405                                 Py_DECREF(Ret);
406                         }
407                         return RealRet;
408                 } else {
409                         PyErr_Clear();
410                 }
411         };
412    
413         void IMSHit(pkgAcquire::ItemDesc &Itm)
414                 { ItemDescMethod("IMSHit", Itm); };
415         void Fetch(pkgAcquire::ItemDesc &Itm)
416                 { ItemDescMethod("Fetch", Itm); };
417         void Done(pkgAcquire::ItemDesc &Itm)
418                 { ItemDescMethod("Done", Itm); };
419         void Fail(pkgAcquire::ItemDesc &Itm)
420                 { ItemDescMethod("Fail", Itm); };
421
422         bool Pulse(pkgAcquire *Owner) {
423                 pkgAcquireStatus::Pulse(Owner);
424                 static swig_type_info *SwigType = 0;
425                 if (!SwigType) {
426                         SwigType = SWIG_TypeQuery("pkgAcquire *");
427                         assert(SwigType);
428                 }
429                 PyObject *attr = PyObject_GetAttrString(PyObj, "Pulse");
430                 if (attr != NULL) {
431                         PyObject *Arg;
432                         Arg = SWIG_NewPointerObj(Owner, SwigType, 0);
433                         if (!Arg) return false;
434                         PyObject *Ret;
435                         Ret = PyObject_CallFunction(attr, "(O)", Arg);
436                         Py_XDECREF(Arg);
437                         bool RealRet = false;
438                         if (Ret != NULL) {
439                                 RealRet = PyObject_IsTrue(Ret);
440                                 Py_DECREF(Ret);
441                         }
442                         return RealRet;
443                 } else {
444                         PyErr_Clear();
445                 }
446         };
447
448         void Start() {
449                 pkgAcquireStatus::Start();
450                 PyObject *attr = PyObject_GetAttrString(PyObj, "Start");
451                 if (attr != NULL) {
452                         PyObject *Ret;
453                         Ret = PyObject_CallFunction(attr, NULL);
454                         Py_XDECREF(Ret);
455                 } else {
456                         PyErr_Clear();
457                 }
458         };
459         void Stop() {
460                 pkgAcquireStatus::Stop();
461                 PyObject *attr = PyObject_GetAttrString(PyObj, "Stop");
462                 if (attr != NULL) {
463                         PyObject *Ret;
464                         Ret = PyObject_CallFunction(attr, NULL);
465                         Py_XDECREF(Ret);
466                 } else {
467                         PyErr_Clear();
468                 }
469         };
470    
471         pkgRPyAcquireStatus(PyObject *PyObj) : PyObj(PyObj)
472                 { Py_INCREF(PyObj); };
473         ~pkgRPyAcquireStatus()
474                 { Py_DECREF(PyObj); };
475 };
476 %}
477 class pkgRPyAcquireStatus : public pkgAcquireStatus {
478         public:
479         pkgRPyAcquireStatus(PyObject *PyObj);
480         double LastBytes;
481         double CurrentCPS;
482         double CurrentBytes;
483         double TotalBytes;
484         double FetchedBytes;
485         unsigned long ElapsedTime;
486         unsigned long TotalItems;
487         unsigned long CurrentItems;
488 };
489 %pythoncode %{
490 class pkgPyAcquireStatus(pkgRPyAcquireStatus):
491         def __init__(self):
492                 pkgRPyAcquireStatus.__init__(self, self)
493         def __repr__(self):
494                 return "<C pkgPyAcquireStatus instance at %s>" % self.this
495 %}
496 #endif
497
498 // vim:ft=swig