3 /* XXX Hack around SWIG brokenness on gcc 4.6 */
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>
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;
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 &;
73 /* Set some data as immutable. */
74 %immutable pkgVersion;
75 %immutable pkgLibVersion;
78 %immutable pkgSystem::Label;
79 %immutable pkgVersioningSystem::Label;
80 %immutable pkgDepCache::StateCache::CandVersion;
81 %immutable pkgDepCache::StateCache::CurVersion;
83 /* One-shot initialization function. */
87 return pkgInitConfig(*_config) && pkgInitSystem(*_config,_system);
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;
114 /* Wonderful SWIG magic to turn APT iterators into Python iterators. */
117 /* Pass ahead the StopIteration exception. */
118 if (!result) return NULL;
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); \
131 IterClass *ret = new IterClass(*self); \
135 /* We must return a copy here, otherwise the original object
136 * might be deallocated and the returned pointer would become
138 inline IterClass *__iter__() { return new IterClass(*self); }; \
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);
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;
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;
164 static swig_type_info *ItemDescr = NULL;
166 pkgAcquire::ItemIterator I;
168 ItemDescr = SWIG_TypeQuery("pkgAcquire::Item *");
171 list = PyList_New(0);
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) {
187 /* SWIG 2.0.2 chokes on these, ignore for now... */
188 %ignore pkgAcquire::UriBegin;
189 %ignore pkgAcquire::UriEnd;
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;
198 %ignore pkgAcquire::Item::DestFile;
199 %ignore pkgAcquire::Item::ErrorText;
201 #define pkgAcquire_Item_DestFile_get(x) ((x)->DestFile.c_str())
202 #define pkgAcquire_Item_ErrorText_get(x) ((x)->ErrorText.c_str())
205 /* Also from Configuration::Item. */
206 %extend Configuration::Item {
210 %ignore Configuration::Item::Tag;
211 %ignore Configuration::Item::Value;
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))
219 /* Typemap to present map_ptrloc in a better way */
220 %apply int { map_ptrloc };
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. */
230 /* Undefined reference!? */
231 %ignore pkgCache::PkgIterator::TargetVer;
233 /* There's a struct and a function with the same name. */
236 /* Allow threads to run while doing DoInstall() */
237 %exception pkgPackageManager::DoInstall {
238 Py_BEGIN_ALLOW_THREADS
243 /* Preprocess string macros (note that we're %import'ing). */
244 %import <apt-pkg/contrib/strutl.h>
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>
264 /* Create a dumb status class which can be instantiated. pkgAcquireStatus
265 * has fully abstract methods. */
267 class pkgAcquireStatusDumb : public pkgAcquireStatus
269 virtual bool MediaChange(string Media,string Drive) {};
273 /* That's the real class we use for Python inheritance. */
275 class ROpPyProgress : public OpProgress {
282 OpProgress::MajorChange;
283 OpProgress::CheckChange;
285 virtual void Update()
287 if (PyObject_HasAttrString(PyObj, "Update")) {
289 Ret = PyObject_CallMethod(PyObj, "Update", NULL);
296 if (PyObject_HasAttrString(PyObj, "Done")) {
298 Ret = PyObject_CallMethod(PyObj, "Done", NULL);
303 ROpPyProgress(PyObject *PyObj) : PyObj(PyObj) {Py_INCREF(PyObj);};
304 ~ROpPyProgress() {Py_DECREF(PyObj);};
308 /* That's how we want SWIG to see our class. */
309 %extend ROpPyProgress {
313 %ignore ROpPyProgress::Op;
314 %ignore ROpPyProgress::SubOp;
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))
321 class ROpPyProgress : public OpProgress {
325 bool CheckChange(float Interval=0.7);
326 ROpPyProgress(PyObject *PyObj);
329 /* That's the proxy class the user sees. This exists only to pass
330 * "self" as the first parameter of ROpPyProgress. */
332 class OpPyProgress(ROpPyProgress):
334 ROpPyProgress.__init__(self, self)
336 return "<C OpPyProgress instance at %s>" % self.this
340 /* The same scheme as above for pkgAcquireStatus. */
342 class pkgRPyAcquireStatus : public pkgAcquireStatus
346 void ItemDescMethod(const char *Name, pkgAcquire::ItemDesc &Itm) {
347 static swig_type_info *SwigType = 0;
349 SwigType = SWIG_TypeQuery("pkgAcquire::ItemDesc *");
352 PyObject *attr = PyObject_GetAttrString(PyObj, (char*)Name);
355 Arg = SWIG_NewPointerObj(&Itm, SwigType, 0);
358 Ret = PyObject_CallFunction(attr, "(O)", Arg);
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;
379 /* Call only Python method, if existent, or parent method. */
380 void Fetched(unsigned long Size,unsigned long ResumePoint)
382 PyObject *attr = PyObject_GetAttrString(PyObj, "Fetched");
385 Ret = PyObject_CallFunction(attr, "(ii)",
390 pkgAcquireStatus::Fetched(Size, ResumePoint);
394 bool MediaChange(string Media,string Drive)
396 PyObject *attr = PyObject_GetAttrString(PyObj, "MediaChange");
399 bool RealRet = false;
400 Ret = PyObject_CallFunction(attr, "(ss)",
404 RealRet = PyObject_IsTrue(Ret);
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); };
422 bool Pulse(pkgAcquire *Owner) {
423 pkgAcquireStatus::Pulse(Owner);
424 static swig_type_info *SwigType = 0;
426 SwigType = SWIG_TypeQuery("pkgAcquire *");
429 PyObject *attr = PyObject_GetAttrString(PyObj, "Pulse");
432 Arg = SWIG_NewPointerObj(Owner, SwigType, 0);
433 if (!Arg) return false;
435 Ret = PyObject_CallFunction(attr, "(O)", Arg);
437 bool RealRet = false;
439 RealRet = PyObject_IsTrue(Ret);
449 pkgAcquireStatus::Start();
450 PyObject *attr = PyObject_GetAttrString(PyObj, "Start");
453 Ret = PyObject_CallFunction(attr, NULL);
460 pkgAcquireStatus::Stop();
461 PyObject *attr = PyObject_GetAttrString(PyObj, "Stop");
464 Ret = PyObject_CallFunction(attr, NULL);
471 pkgRPyAcquireStatus(PyObject *PyObj) : PyObj(PyObj)
472 { Py_INCREF(PyObj); };
473 ~pkgRPyAcquireStatus()
474 { Py_DECREF(PyObj); };
477 class pkgRPyAcquireStatus : public pkgAcquireStatus {
479 pkgRPyAcquireStatus(PyObject *PyObj);
485 unsigned long ElapsedTime;
486 unsigned long TotalItems;
487 unsigned long CurrentItems;
490 class pkgPyAcquireStatus(pkgRPyAcquireStatus):
492 pkgRPyAcquireStatus.__init__(self, self)
494 return "<C pkgPyAcquireStatus instance at %s>" % self.this