- load callback works...
[platform/upstream/libsolv.git] / examples / solv.i
1 #
2 ##if defined(SWIGRUBY)
3 #  %rename("to_s") string();
4 ##endif
5 ##if defined(SWIGPYTHON)
6 #  %rename("__str__") string();
7 ##endif
8 #
9
10 %module solv
11
12 %typemap(in) (Id *idarray, int idarraylen) {
13   /* Check if is a list */
14   if (PyList_Check($input)) {
15     int size = PyList_Size($input);
16     int i = 0;
17     $1 = (Id *)sat_malloc2(size, sizeof(Id));
18     for (i = 0; i < size; i++) {
19       PyObject *o = PyList_GetItem($input,i);
20       int v;
21       int e = SWIG_AsVal_int(o, &v);
22       if (!SWIG_IsOK(e)) {
23         SWIG_exception_fail(SWIG_ArgError(e), "list must contain integers");
24         sat_free($1);
25         return NULL;
26       }
27       $1[i] = v;
28     }
29     $2 = size;
30   } else {
31     PyErr_SetString(PyExc_TypeError,"not a list");
32     return NULL;
33   }
34 }
35 %typemap(freearg) (Id *idarray, int idarraylen) {
36   sat_free($1);
37 }
38
39 %typemap(in) Queue {
40   /* Check if is a list */
41   queue_init(&$1);
42   if (PyList_Check($input)) {
43     int size = PyList_Size($input);
44     int i = 0;
45     for (i = 0; i < size; i++) {
46       PyObject *o = PyList_GetItem($input,i);
47       int v;
48       int e = SWIG_AsVal_int(o, &v);
49       if (!SWIG_IsOK(e)) {
50         SWIG_exception_fail(SWIG_ArgError(e), "list must contain only integers");
51         queue_free(&$1);
52         return NULL;
53       }
54       queue_push(&$1, v);
55     }
56   } else {
57     PyErr_SetString(PyExc_TypeError,"not a list");
58     return NULL;
59   }
60 }
61 %typemap(out) Queue {
62   int i;
63   PyObject *o = PyList_New($1.count);
64   for (i = 0; i < $1.count; i++)
65     PyList_SetItem(o, i, SWIG_From_int($1.elements[i]));
66   queue_free(&$1);
67   $result = o;
68 }
69 %typemap(arginit) Queue {
70   queue_init(&$1);
71 }
72 %typemap(freearg) Queue {
73   queue_free(&$1);
74 }
75
76
77 %include "cdata.i"
78 %include "file.i"
79 %include "typemaps.i"
80
81 %{
82 #include "stdio.h"
83 #include "pool.h"
84 #include "solver.h"
85 #include "repo_solv.h"
86 #include "chksum.h"
87
88 #include "repo_rpmdb.h"
89 #include "repo_rpmmd.h"
90 #include "repo_write.h"
91 #include "repo_products.h"
92 #include "repo_susetags.h"
93 #include "repo_updateinfoxml.h"
94 #include "repo_deltainfoxml.h"
95 #include "repo_repomdxml.h"
96 #include "repo_content.h"
97 #include "sat_xfopen.h"
98
99 #define true 1
100 #define false 1
101
102 #define SOLVER_SOLUTION_DEINSTALL -100
103 #define SOLVER_SOLUTION_REPLACE -101
104 typedef struct chksum Chksum;
105 typedef int bool;
106
107 typedef struct {
108   Pool* pool;
109   Id id;
110 } XSolvable;
111
112 typedef struct {
113   Solver* solv;
114   Id id;
115 } XRule;
116
117 typedef struct {
118   Repo* repo;
119   Id id;
120 } XRepodata;
121
122 typedef struct {
123   Pool *pool;
124   Id id;
125 } Pool_solvable_iterator;
126
127 typedef struct {
128   Pool *pool;
129   Id id;
130 } Pool_repo_iterator;
131
132 typedef struct {
133   Repo *repo;
134   Id id;
135 } Repo_solvable_iterator;
136
137 typedef struct {
138   Pool *pool;
139   Id how;
140   Id what;
141 } Job;
142
143 typedef struct {
144   Solver *solv;
145   Id id;
146 } Problem;
147
148 typedef struct {
149   Solver *solv;
150   Id problemid;
151   Id id;
152 } Solution;
153
154 typedef struct {
155   Solver *solv;
156   Id problemid;
157   Id solutionid;
158   Id id;
159
160   Id type;
161   Id p;
162   Id rp;
163 } Solutionelement;
164
165 %}
166
167 typedef int Id;
168
169 %include "knownid.h"
170
171 # from repodata.h
172 %constant Id SOLVID_META;
173 %constant Id SOLVID_POS;
174
175 %constant int REL_EQ;
176 %constant int REL_GT;
177 %constant int REL_LT;
178 %constant int REL_ARCH;
179
180
181 typedef struct {
182   Pool* const pool;
183   Id const id;
184 } XSolvable;
185
186 typedef struct {
187   Solver* const solv;
188   Id const id;
189 } XRule;
190
191 typedef struct {
192   Repo* const repo;
193   Id const id;
194 } XRepodata;
195
196 # put before pool/repo so we can access the constructor
197 %nodefaultctor Dataiterator;
198 %nodefaultdtor Dataiterator;
199 typedef struct _Dataiterator {
200   Pool * const pool;
201   Repo * const repo;
202   const Id solvid;
203 } Dataiterator;
204
205 typedef struct {
206   Pool * const pool;
207   Id how;
208   Id what;
209 } Job;
210
211 %nodefaultctor Pool;
212 %nodefaultdtor Pool;
213 typedef struct {
214 } Pool;
215
216 %nodefaultctor Repo;
217 %nodefaultdtor Repo;
218 typedef struct _Repo {
219   const char * const name;
220   int priority;
221   int subpriority;
222   int const nsolvables;
223 #if defined(SWIGRUBY)
224   VALUE appdata;
225 #endif
226 #if defined(SWIGPERL)
227   SV *appdata;
228 #endif
229 } Repo;
230
231 %nodefaultctor Pool_solvable_iterator;
232 typedef struct {} Pool_solvable_iterator;
233
234 %nodefaultctor Pool_repo_iterator;
235 typedef struct {} Pool_repo_iterator;
236
237 %nodefaultctor Repo_solvable_iterator;
238 typedef struct {} Repo_solvable_iterator;
239
240 %nodefaultctor Solver;
241 %nodefaultdtor Solver;
242 typedef struct {
243   Pool * const pool;
244   bool ignorealreadyrecommended;
245   bool dosplitprovides;
246   bool fixsystem;
247   bool allowuninstall;
248   bool distupgrade;
249   bool allowdowngrade;
250   bool allowarchchange;
251   bool allowvendorchange;
252 } Solver;
253
254 typedef struct chksum {} Chksum;
255
256 %rename(xfopen) sat_xfopen;
257 %rename(xfopen_fd) sat_xfopen_fd;
258 %rename(xfclose) sat_xfclose;
259 %rename(xfileno) sat_xfileno;
260
261 FILE *sat_xfopen(const char *fn);
262 FILE *sat_xfopen_fd(const char *fn, int fd);
263 %inline {
264   int sat_xfclose(FILE *fp) {
265     return fclose(fp);
266   }
267   int sat_xfileno(FILE *fp) {
268     return fileno(fp);
269   }
270 }
271
272 typedef struct {
273   Solver * const solv;
274   Id const id;
275 } Problem;
276
277 typedef struct {
278   Solver * const solv;
279   Id const problemid;
280   Id const id;
281 } Solution;
282
283 typedef struct {
284   Solver *const solv;
285   Id const problemid;
286   Id const solutionid;
287   Id const id;
288   Id const type;
289 } Solutionelement;
290
291 %nodefaultctor Transaction;
292 %nodefaultdtor Transaction;
293 typedef struct {
294   Pool * const pool;
295 } Transaction;
296
297
298
299 %extend Job {
300   static const Id SOLVER_SOLVABLE = SOLVER_SOLVABLE;
301   static const Id SOLVER_SOLVABLE_NAME = SOLVER_SOLVABLE_NAME;
302   static const Id SOLVER_SOLVABLE_PROVIDES = SOLVER_SOLVABLE_PROVIDES;
303   static const Id SOLVER_SOLVABLE_ONE_OF = SOLVER_SOLVABLE_ONE_OF;
304   static const Id SOLVER_SOLVABLE_REPO = SOLVER_SOLVABLE_REPO;
305   static const Id SOLVER_SOLVABLE_ALL = SOLVER_SOLVABLE_ALL;
306   static const Id SOLVER_SELECTMASK = SOLVER_SELECTMASK;
307   static const Id SOLVER_NOOP = SOLVER_NOOP;
308   static const Id SOLVER_INSTALL = SOLVER_INSTALL;
309   static const Id SOLVER_ERASE = SOLVER_ERASE;
310   static const Id SOLVER_UPDATE = SOLVER_UPDATE;
311   static const Id SOLVER_WEAKENDEPS = SOLVER_WEAKENDEPS;
312   static const Id SOLVER_NOOBSOLETES = SOLVER_NOOBSOLETES;
313   static const Id SOLVER_LOCK = SOLVER_LOCK;
314   static const Id SOLVER_DISTUPGRADE = SOLVER_DISTUPGRADE;
315   static const Id SOLVER_VERIFY = SOLVER_VERIFY;
316   static const Id SOLVER_DROP_ORPHANED = SOLVER_DROP_ORPHANED;
317   static const Id SOLVER_USERINSTALLED = SOLVER_USERINSTALLED;
318   static const Id SOLVER_JOBMASK = SOLVER_JOBMASK;
319   static const Id SOLVER_WEAK = SOLVER_WEAK;
320   static const Id SOLVER_ESSENTIAL = SOLVER_ESSENTIAL;
321   static const Id SOLVER_CLEANDEPS = SOLVER_CLEANDEPS;
322   static const Id SOLVER_SETEV = SOLVER_SETEV;
323   static const Id SOLVER_SETEVR = SOLVER_SETEVR;
324   static const Id SOLVER_SETARCH = SOLVER_SETARCH;
325   static const Id SOLVER_SETVENDOR = SOLVER_SETVENDOR;
326   static const Id SOLVER_SETREPO = SOLVER_SETREPO;
327   static const Id SOLVER_NOAUTOSET = SOLVER_NOAUTOSET;
328   static const Id SOLVER_SETMASK = SOLVER_SETMASK;
329
330   Job(Pool *pool, Id how, Id what) {
331     Job *job = sat_calloc(1, sizeof(*job));
332     job->pool = pool;
333     job->how = how;
334     job->what = what;
335     return job;
336   }
337 }
338
339 %extend Chksum {
340   Chksum(Id type) {
341     return (Chksum *)sat_chksum_create(type);
342   }
343   ~Chksum() {
344     sat_chksum_free($self, 0);
345   }
346   void add(const char *str) {
347     sat_chksum_add($self, str, strlen((char *)str));
348   }
349   void addfp(FILE *fp) {
350     char buf[4096];
351     int l;
352     while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
353       sat_chksum_add($self, buf, l);
354     rewind(fp);         /* convenience */
355   }
356   void addfd(int fd) {
357     char buf[4096];
358     int l;
359     while ((l = read(fd, buf, sizeof(buf))) > 0)
360       sat_chksum_add($self, buf, l);
361     lseek(fd, 0, 0);    /* convenience */
362   }
363   bool matches(char *othersum) {
364     int l;
365     unsigned char *b;
366     b = sat_chksum_get($self, &l);
367     return memcmp(b, (void *)othersum, l) == 0;
368   }
369   SWIGCDATA raw() {
370     int l;
371     unsigned char *b;
372     b = sat_chksum_get($self, &l);
373     return cdata_void((void *)b, l);
374   }
375 }
376
377
378
379 %extend Pool {
380   Pool() {
381     Pool *pool = pool_create();
382     return pool;
383   }
384   ~Pool() {
385   }
386   void set_debuglevel(int level) {
387     pool_setdebuglevel($self, level);
388   }
389   %{
390   SWIGINTERN int loadcallback(Pool *pool, Repodata *data, void *d) {
391     XRepodata *xd = new_XRepodata(data->repo, data - data->repo->repodata);
392     PyObject *args = Py_BuildValue("(O)", SWIG_NewPointerObj(SWIG_as_voidptr(xd), SWIGTYPE_p_XRepodata, SWIG_POINTER_OWN | 0));
393     PyObject *result = PyEval_CallObject((PyObject *)d, args);
394     if (!result)
395       return 0; /* exception */
396     int ecode = 0;
397     int vresult = 0;
398     Py_DECREF(args);
399     ecode = SWIG_AsVal_int(result, &vresult);
400     Py_DECREF(result);
401     return SWIG_IsOK(ecode) ? vresult : 0;
402   }
403   %}
404   void set_loadcallback(PyObject *callable) {
405     if (!callable) {
406       if ($self->loadcallback == loadcallback) {
407         Py_DECREF($self->loadcallbackdata);
408         pool_setloadcallback($self, 0, 0);
409       }
410       return;
411     }
412     Py_INCREF(callable);
413     pool_setloadcallback($self, loadcallback, callable);
414   }
415   Id str2id(const char *str, int create=1) {
416     return str2id($self, str, create);
417   }
418   const char *id2str(Id id) {
419     return id2str($self, id);
420   }
421   const char *dep2str(Id id) {
422     return dep2str($self, id);
423   }
424   Id rel2id(Id name, Id evr, int flags, int create=1) {
425     return rel2id($self, name, evr, flags, create);
426   }
427   void setarch(const char *arch) {
428     pool_setarch($self, arch);
429   }
430   Repo *add_repo(const char *name) {
431     return repo_create($self, name);
432   }
433   const char *lookup_str(Id entry, Id keyname) {
434     return pool_lookup_str($self, entry, keyname);
435   }
436   Id lookup_id(Id entry, Id keyname) {
437     return pool_lookup_id($self, entry, keyname);
438   }
439   unsigned int lookup_num(Id entry, Id keyname, unsigned int notfound = 0) {
440     return pool_lookup_num($self, entry, keyname, notfound);
441   }
442   bool lookup_void(Id entry, Id keyname) {
443     return pool_lookup_void($self, entry, keyname);
444   }
445   SWIGCDATA lookup_bin_checksum(Id entry, Id keyname, Id *OUTPUT) {
446     int l;
447     const unsigned char *b;
448     *OUTPUT = 0;
449     b = pool_lookup_bin_checksum($self, entry, keyname, OUTPUT);
450     if (!b)
451       return cdata_void(0, 0);
452     return cdata_void((char *)b, sat_chksum_len(*OUTPUT));
453   }
454   const char *lookup_checksum(Id entry, Id keyname, Id *OUTPUT) {
455     Id type = 0;
456     *OUTPUT = 0;
457     const char *b = pool_lookup_checksum($self, entry, keyname, OUTPUT);
458     return b;
459   }
460   %newobject dataiterator_new;
461   Dataiterator *dataiterator_new(Id p, Id key,  const char *match, int flags) {
462     return new_Dataiterator($self, 0, p, key, match, flags);
463   }
464   const char *solvid2str(Id solvid) {
465     return solvid2str($self, solvid);
466   }
467   void addfileprovides() {
468     pool_addfileprovides($self);
469   }
470   void createwhatprovides() {
471     pool_createwhatprovides($self);
472   }
473   Pool_solvable_iterator * const solvables;
474   %{
475   SWIGINTERN Pool_solvable_iterator * Pool_solvables_get(Pool *pool) {
476     Pool_solvable_iterator *s;
477     s = sat_calloc(1, sizeof(*s));
478     s->pool = pool;
479     s->id = 0;
480     return s;
481   }
482   %}
483   Pool_repo_iterator * const repos;
484   %{
485   SWIGINTERN Pool_repo_iterator * Pool_repos_get(Pool *pool) {
486     Pool_repo_iterator *s;
487     s = sat_calloc(1, sizeof(*s));
488     s->pool = pool;
489     s->id = 0;
490     return s;
491   }
492   %}
493   Repo *installed;
494   %{
495   SWIGINTERN void Pool_installed_set(Pool *pool, Repo *installed) {
496     pool_set_installed(pool, installed);
497   }
498   Repo *Pool_installed_get(Pool *pool) {
499     return pool->installed;
500   }
501   %}
502
503   Queue providerids(Id dep) {
504     Pool *pool = $self;
505     Queue q;
506     Id p, pp;
507     queue_init(&q);
508     FOR_PROVIDES(p, pp, dep)
509       queue_push(&q, p);
510     return q;
511   }
512   Queue allprovidingids() {
513     Pool *pool = $self;
514     Queue q;
515     Id id;
516     queue_init(&q);
517     for (id = 1; id < pool->ss.nstrings; id++)
518       if (pool->whatprovides[id])
519         queue_push(&q, id);
520     return q;
521   }
522   # move to job?
523   Queue jobsolvids(Job *job) {
524     Pool *pool = $self;
525     Id p, pp, how;
526     Queue q;
527     queue_init(&q);
528     how = job->how & SOLVER_SELECTMASK;
529     FOR_JOB_SELECT(p, pp, how, job->what)
530       queue_push(&q, p);
531     return q;
532   }
533   Job *Job(Id how, Id what) {
534     return new_Job($self, how, what);
535   }
536
537 #if defined(SWIGPYTHON)
538   %pythoncode {
539     def jobsolvables (self, *args):
540       return [ self.solvables[id] for id in self.jobsolvids(*args) ]
541     def providers(self, *args):
542       return [ self.solvables[id] for id in self.providerids(*args) ]
543   }
544 #endif
545
546   Id towhatprovides(Queue q) {
547     return pool_queuetowhatprovides($self, &q);
548   }
549   bool isknownarch(Id id) {
550     Pool *pool = $self;
551     if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
552       return 1;
553     if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
554       return 0;
555     return 1;
556   }
557
558   %newobject create_solver;
559   Solver *create_solver() {
560     return solver_create($self);
561   }
562 }
563
564 %extend Repo {
565   static const int REPO_REUSE_REPODATA = REPO_REUSE_REPODATA;
566   static const int REPO_NO_INTERNALIZE = REPO_NO_INTERNALIZE;
567   static const int REPO_LOCALPOOL = REPO_LOCALPOOL;
568   static const int REPO_USE_LOADING = REPO_USE_LOADING;
569   static const int REPO_EXTEND_SOLVABLES = REPO_EXTEND_SOLVABLES;
570   static const int SOLV_ADD_NO_STUBS = SOLV_ADD_NO_STUBS;       /* repo_solv */
571   static const int SUSETAGS_RECORD_SHARES = SUSETAGS_RECORD_SHARES; /* repo_susetags */
572
573   void free(int reuseids = 0) {
574     repo_free($self, reuseids);
575   }
576   bool add_solv(const char *name, int flags = 0) {
577     FILE *fp = fopen(name, "r");
578     if (!fp)
579       return 0;
580     int r = repo_add_solv_flags($self, fp, flags);
581     fclose(fp);
582     return r == 0;
583   }
584   bool add_solv(FILE *fp, int flags = 0) {
585     return repo_add_solv_flags($self, fp, flags) == 0;
586   }
587   bool add_products(const char *proddir, int flags = 0) {
588     repo_add_products($self, proddir, 0, flags);
589     return 1;
590   }
591   bool add_rpmmd(FILE *fp, const char *language, int flags = 0) {
592     repo_add_rpmmd($self, fp, language, flags);
593     return 1;
594   }
595   bool add_rpmdb(Repo *ref, int flags = 0) {
596     repo_add_rpmdb($self, ref, 0, flags);
597     return 1;
598   }
599   bool add_susetags(FILE *fp, Id defvendor, const char *language, int flags = 0) {
600     repo_add_susetags($self, fp, defvendor, language, flags);
601     return 1;
602   }
603   bool add_repomdxml(FILE *fp, int flags = 0) {
604     repo_add_repomdxml($self, fp, flags);
605     return 1;
606   }
607   bool add_content(FILE *fp, int flags = 0) {
608     repo_add_content($self, fp, flags);
609     return 1;
610   }
611   bool add_updateinfoxml(FILE *fp, int flags = 0) {
612     repo_add_updateinfoxml($self, fp, flags);
613     return 1;
614   }
615   bool add_deltainfoxml(FILE *fp, int flags = 0) {
616     repo_add_deltainfoxml($self, fp, flags);
617     return 1;
618   }
619   void internalize() {
620     repo_internalize($self);
621   }
622   const char *lookup_str(Id entry, Id keyname) {
623     return repo_lookup_str($self, entry, keyname);
624   }
625   Id lookup_id(Id entry, Id keyname) {
626     return repo_lookup_id($self, entry, keyname);
627   }
628   unsigned int lookup_num(Id entry, Id keyname, unsigned int notfound = 0) {
629     return repo_lookup_num($self, entry, keyname, notfound);
630   }
631   void write(FILE *fp) {
632     repo_write($self, fp, repo_write_stdkeyfilter, 0, 0);
633   }
634   # HACK, remove if no longer needed!
635   bool write_first_repodata(FILE *fp, int flags = 0) {
636     int oldnrepodata = $self->nrepodata;
637     $self->nrepodata = 1;
638     repo_write($self, fp, repo_write_stdkeyfilter, 0, 0);
639     $self->nrepodata = oldnrepodata;
640     return 1;
641   }
642   %newobject dataiterator_new;
643   Dataiterator *dataiterator_new(Id p, Id key,  const char *match, int flags) {
644     return new_Dataiterator($self->pool, $self, p, key, match, flags);
645   }
646
647   Id const id;
648   %{
649   SWIGINTERN Id Repo_id_get(Repo *repo) {
650     return repo->repoid;
651   }
652   %}
653   Repo_solvable_iterator * const solvables;
654   %{
655   SWIGINTERN Repo_solvable_iterator * Repo_solvables_get(Repo *repo) {
656     Repo_solvable_iterator *s;
657     s = sat_calloc(1, sizeof(*s));
658     s->repo = repo;
659     s->id = 0;
660     return s;
661   }
662   %}
663
664   XRepodata *add_repodata(int flags = 0) {
665     Repodata *rd = repo_add_repodata($self, flags);
666     return new_XRepodata($self, rd - $self->repodata);
667   }
668
669   void create_stubs() {
670     Repodata *data;
671     if (!$self->nrepodata)
672       return;
673     data = $self->repodata  + ($self->nrepodata - 1);
674     if (data->state != REPODATA_STUB)
675       repodata_create_stubs(data);
676   }
677 #if defined(SWIGPYTHON)
678   PyObject *appdata;
679   %{
680   SWIGINTERN void Repo_appdata_set(Repo *repo, PyObject *o) {
681     repo->appdata = o;
682   }
683   SWIGINTERN PyObject *Repo_appdata_get(Repo *repo) {
684     PyObject *o = repo->appdata;
685     Py_INCREF(o);
686     return o;
687   }
688   %}
689 #endif
690 }
691
692 %extend Dataiterator {
693   static const int SEARCH_STRING = SEARCH_STRING;
694   static const int SEARCH_SUBSTRING = SEARCH_SUBSTRING;
695   static const int SEARCH_GLOB = SEARCH_GLOB;
696   static const int SEARCH_REGEX = SEARCH_REGEX;
697   static const int SEARCH_NOCASE = SEARCH_NOCASE;
698   static const int SEARCH_FILES = SEARCH_FILES;
699   static const int SEARCH_COMPLETE_FILELIST = SEARCH_COMPLETE_FILELIST;
700
701   Dataiterator(Pool *pool, Repo *repo, Id p, Id key, const char *match, int flags) {
702     Dataiterator *di = sat_calloc(1, sizeof(*di));
703     dataiterator_init(di, pool, repo, p, key, match, flags);
704     return di;
705   }
706   ~Dataiterator() {
707     dataiterator_free($self);
708   }
709   Dataiterator *__iter__() {
710     return $self;
711   }
712   %exception next {
713     $action
714     if (!result) {
715       PyErr_SetString(PyExc_StopIteration,"no more matches");
716       return NULL;
717     }
718   }
719   %newobject next;
720   Dataiterator *next() {
721     Dataiterator *ndi;
722     if (!dataiterator_step($self)) {
723       return 0;
724     }
725     ndi = sat_calloc(1, sizeof(*ndi));
726     dataiterator_init_clone(ndi, $self);
727     return ndi;
728   }
729   void setpos_parent() {
730     dataiterator_setpos_parent($self);
731   }
732   void prepend_keyname(Id key) {
733     dataiterator_prepend_keyname($self, key);
734   }
735   void skip_solvable() {
736     dataiterator_skip_solvable($self);
737   }
738
739   %newobject solvable;
740   XSolvable * const solvable;
741   %{
742   SWIGINTERN XSolvable *Dataiterator_solvable_get(Dataiterator *di) {
743     return new_XSolvable(di->pool, di->solvid);
744   }
745   %}
746   Id key_id() {
747     return $self->key->name;
748   }
749   const char *key_idstr() {
750     return id2str($self->pool, $self->key->name);
751   }
752   Id keytype_id() {
753     return $self->key->type;
754   }
755   const char *keytype_idstr() {
756     return id2str($self->pool, $self->key->type);
757   }
758   Id match_id() {
759      return $self->kv.id;
760   }
761   const char *match_idstr() {
762      return id2str($self->pool, $self->kv.id);
763   }
764   const char *match_str() {
765      return $self->kv.str;
766   }
767   int match_num() {
768      return $self->kv.num;
769   }
770   int match_num2() {
771      return $self->kv.num2;
772   }
773 }
774
775
776 %extend Pool_solvable_iterator {
777   Pool_solvable_iterator *__iter__() {
778     return $self;
779   }
780   %exception next {
781     $action
782     if (!result) {
783       PyErr_SetString(PyExc_StopIteration,"no more matches");
784       return NULL;
785     }
786   }
787   %newobject next;
788   XSolvable *next() {
789     Pool *pool = $self->pool;
790     XSolvable *s;
791     if ($self->id >= pool->nsolvables)
792       return 0;
793     while (++$self->id < pool->nsolvables)
794       if (pool->solvables[$self->id].repo)
795         return new_XSolvable(pool, $self->id);
796     return 0;
797   }
798   %newobject __getitem__;
799   XSolvable *__getitem__(Id key) {
800     Pool *pool = $self->pool;
801     if (key > 0 && key < pool->nsolvables && pool->solvables[key].repo)
802       return new_XSolvable(pool, key);
803     return 0;
804   }
805 }
806
807 %extend Pool_repo_iterator {
808   Pool_repo_iterator *__iter__() {
809     return $self;
810   }
811   %exception next {
812     $action
813     if (!result) {
814       PyErr_SetString(PyExc_StopIteration,"no more matches");
815       return NULL;
816     }
817   }
818   Repo *next() {
819     Pool *pool = $self->pool;
820     if ($self->id >= pool->nrepos + 1)
821       return 0;
822     while (++$self->id < pool->nrepos + 1) {
823       Repo *r = pool_id2repo(pool, $self->id);
824       if (r)
825         return r;
826     }
827     return 0;
828   }
829   Repo *__getitem__(Id key) {
830     Pool *pool = $self->pool;
831     if (key > 0 && key < pool->nrepos + 1)
832       return pool_id2repo(pool, key);
833     return 0;
834   }
835 }
836
837 %extend Repo_solvable_iterator {
838   Repo_solvable_iterator *__iter__() {
839     return $self;
840   }
841   %exception next {
842     $action
843     if (!result) {
844       PyErr_SetString(PyExc_StopIteration,"no more matches");
845       return NULL;
846     }
847   }
848   %newobject next;
849   XSolvable *next() {
850     Repo *repo = $self->repo;
851     Pool *pool = repo->pool;
852     XSolvable *s;
853     if (repo->start > 0 && $self->id < repo->start)
854       $self->id = repo->start - 1;
855     if ($self->id >= repo->end)
856       return 0;
857     while (++$self->id < repo->end)
858       if (pool->solvables[$self->id].repo == repo)
859         return new_XSolvable(pool, $self->id);
860     return 0;
861   }
862   %newobject __getitem__;
863   XSolvable *__getitem__(Id key) {
864     Repo *repo = $self->repo;
865     Pool *pool = repo->pool;
866     if (key > 0 && key < pool->nsolvables && pool->solvables[key].repo == repo)
867       return new_XSolvable(pool, key);
868     return 0;
869   }
870 }
871
872 %extend XSolvable {
873   XSolvable(Pool *pool, Id id) {
874     if (!id)
875       return 0;
876     XSolvable *s = sat_calloc(1, sizeof(*s));
877     s->pool = pool;
878     s->id = id;
879     return s;
880   }
881   const char *str() {
882     return solvid2str($self->pool, $self->id);
883   }
884   const char *lookup_str(Id keyname) {
885     return pool_lookup_str($self->pool, $self->id, keyname);
886   }
887   Id lookup_id(Id keyname) {
888     return pool_lookup_id($self->pool, $self->id, keyname);
889   }
890   unsigned int lookup_num(Id keyname, unsigned int notfound = 0) {
891     return pool_lookup_num($self->pool, $self->id, keyname, notfound);
892   }
893   bool lookup_void(Id keyname) {
894     return pool_lookup_void($self->pool, $self->id, keyname);
895   }
896   SWIGCDATA lookup_bin_checksum(Id keyname, Id *OUTPUT) {
897     int l;
898     const unsigned char *b;
899     *OUTPUT = 0;
900     b = pool_lookup_bin_checksum($self->pool, $self->id, keyname, OUTPUT);
901     if (!b)
902       return cdata_void(0, 0);
903     return cdata_void((char *)b, sat_chksum_len(*OUTPUT));
904   }
905   const char *lookup_checksum(Id keyname, Id *OUTPUT) {
906     Id type = 0;
907     *OUTPUT = 0;
908     const char *b = pool_lookup_checksum($self->pool, $self->id, keyname, OUTPUT);
909     return b;
910   }
911   const char *lookup_location(int *OUTPUT) {
912     return solvable_get_location($self->pool->solvables + $self->id, OUTPUT);
913   }
914   bool installable() {
915     return pool_installable($self->pool, pool_id2solvable($self->pool, $self->id));
916   }
917   bool isinstalled() {
918     Pool *pool = $self->pool;
919     return pool->installed && pool_id2solvable(pool, $self->id)->repo == pool->installed;
920   }
921
922   const char * const name;
923   %{
924     SWIGINTERN const char *XSolvable_name_get(XSolvable *xs) {
925       Pool *pool = xs->pool;
926       return id2str(pool, pool->solvables[xs->id].name);
927     }
928   %}
929   Id const nameid;
930   %{
931     SWIGINTERN Id XSolvable_nameid_get(XSolvable *xs) {
932       return xs->pool->solvables[xs->id].name;
933     }
934   %}
935   const char * const evr;
936   %{
937     SWIGINTERN const char *XSolvable_evr_get(XSolvable *xs) {
938       Pool *pool = xs->pool;
939       return id2str(pool, pool->solvables[xs->id].evr);
940     }
941   %}
942   Id const evrid;
943   %{
944     SWIGINTERN Id XSolvable_evrid_get(XSolvable *xs) {
945       return xs->pool->solvables[xs->id].evr;
946     }
947   %}
948   const char * const arch;
949   %{
950     SWIGINTERN const char *XSolvable_arch_get(XSolvable *xs) {
951       Pool *pool = xs->pool;
952       return id2str(pool, pool->solvables[xs->id].arch);
953     }
954   %}
955   Id const archid;
956   %{
957     SWIGINTERN Id XSolvable_archid_get(XSolvable *xs) {
958       return xs->pool->solvables[xs->id].arch;
959     }
960   %}
961   Repo * const repo;
962   %{
963     SWIGINTERN Repo *XSolvable_repo_get(XSolvable *xs) {
964       return xs->pool->solvables[xs->id].repo;
965     }
966   %}
967 }
968
969 %extend Problem {
970   Problem(Solver *solv, Id id) {
971     Problem *p;
972     p = sat_calloc(1, sizeof(*p));
973     p->solv = solv;
974     p->id = id;
975     return p;
976   }
977   Id findproblemrule_helper() {
978     return solver_findproblemrule($self->solv, $self->id);
979   }
980   Queue findallproblemrules_helper(int unfiltered=0) {
981     Solver *solv = $self->solv;
982     Id probr;
983     int i, j;
984     Queue q;
985     queue_init(&q);
986     solver_findallproblemrules(solv, $self->id, &q);
987     if (!unfiltered)
988       {
989         for (i = j = 0; i < q.count; i++)
990           {
991             probr = q.elements[i];
992             if ((probr >= solv->updaterules && probr < solv->updaterules_end) || (probr >= solv->jobrules && probr < solv->jobrules_end))
993               continue;
994             q.elements[j++] = probr;
995           }
996         if (j)
997           queue_truncate(&q, j);
998       }
999     return q;
1000   }
1001   int solution_count() {
1002     return solver_solution_count($self->solv, $self->id);
1003   }
1004 #if defined(SWIGPYTHON)
1005   %pythoncode {
1006     def findproblemrule(self):
1007       return XRule(self.solv, self.findproblemrule_helper())
1008     def findallproblemrules(self, unfiltered=0):
1009       return [ XRule(self.solv, i) for i in self.findallproblemrules_helper(unfiltered) ]
1010     def solutions(self):
1011       return [ Solution(self, i) for i in range(1, self.solution_count() + 1) ];
1012   }
1013 #endif
1014 }
1015
1016 %extend Solution {
1017   Solution(Problem *p, Id id) {
1018     Solution *s;
1019     s = sat_calloc(1, sizeof(*s));
1020     s->solv = p->solv;
1021     s->problemid = p->id;
1022     s->id = id;
1023     return s;
1024   }
1025   int element_count() {
1026     return solver_solutionelement_count($self->solv, $self->problemid, $self->id);
1027   }
1028 #if defined(SWIGPYTHON)
1029   %pythoncode {
1030     def elements(self):
1031       return [ Solutionelement(self, i) for i in range(1, self.element_count() + 1) ];
1032   }
1033 #endif
1034 }
1035
1036 %extend Solutionelement {
1037   Solutionelement(Solution *s, Id id) {
1038     Solutionelement *e;
1039     e = sat_calloc(1, sizeof(*e));
1040     e->solv = s->solv;
1041     e->problemid = s->problemid;
1042     e->solutionid = s->id;
1043     e->id = id;
1044     solver_next_solutionelement(e->solv, e->problemid, e->solutionid, e->id - 1, &e->p, &e->rp);
1045     if (e->p > 0) {
1046       e->type = e->rp ? SOLVER_SOLUTION_REPLACE : SOLVER_SOLUTION_DEINSTALL;
1047     } else {
1048       e->type = e->p;
1049       e->p = e->rp;
1050       e->rp = 0;
1051     }
1052     return e;
1053   }
1054   %newobject solvable;
1055   XSolvable * const solvable;
1056   %newobject replacement;
1057   XSolvable * const replacement;
1058   int const jobidx;
1059   %{
1060     SWIGINTERN XSolvable *Solutionelement_solvable_get(Solutionelement *e) {
1061       return new_XSolvable(e->solv->pool, e->p);
1062     }
1063     SWIGINTERN XSolvable *Solutionelement_replacement_get(Solutionelement *e) {
1064       return new_XSolvable(e->solv->pool, e->rp);
1065     }
1066     SWIGINTERN int Solutionelement_jobidx_get(Solutionelement *e) {
1067       return (e->p - 1) / 2;
1068     }
1069   %}
1070 }
1071
1072 %extend Solver {
1073   static const int SOLVER_RULE_UNKNOWN = SOLVER_RULE_UNKNOWN;
1074   static const int SOLVER_RULE_RPM = SOLVER_RULE_RPM;
1075   static const int SOLVER_RULE_RPM_NOT_INSTALLABLE = SOLVER_RULE_RPM_NOT_INSTALLABLE;
1076   static const int SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP = SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP;
1077   static const int SOLVER_RULE_RPM_PACKAGE_REQUIRES = SOLVER_RULE_RPM_PACKAGE_REQUIRES;
1078   static const int SOLVER_RULE_RPM_SELF_CONFLICT = SOLVER_RULE_RPM_SELF_CONFLICT;
1079   static const int SOLVER_RULE_RPM_PACKAGE_CONFLICT = SOLVER_RULE_RPM_PACKAGE_CONFLICT;
1080   static const int SOLVER_RULE_RPM_SAME_NAME = SOLVER_RULE_RPM_SAME_NAME;
1081   static const int SOLVER_RULE_RPM_PACKAGE_OBSOLETES = SOLVER_RULE_RPM_PACKAGE_OBSOLETES;
1082   static const int SOLVER_RULE_RPM_IMPLICIT_OBSOLETES = SOLVER_RULE_RPM_IMPLICIT_OBSOLETES;
1083   static const int SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES = SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES;
1084   static const int SOLVER_RULE_UPDATE = SOLVER_RULE_UPDATE;
1085   static const int SOLVER_RULE_FEATURE = SOLVER_RULE_FEATURE;
1086   static const int SOLVER_RULE_JOB = SOLVER_RULE_JOB;
1087   static const int SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP = SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
1088   static const int SOLVER_RULE_DISTUPGRADE = SOLVER_RULE_DISTUPGRADE;
1089   static const int SOLVER_RULE_INFARCH = SOLVER_RULE_INFARCH;
1090   static const int SOLVER_RULE_CHOICE = SOLVER_RULE_CHOICE;
1091   static const int SOLVER_RULE_LEARNT = SOLVER_RULE_LEARNT;
1092
1093   static const int SOLVER_SOLUTION_JOB = SOLVER_SOLUTION_JOB;
1094   static const int SOLVER_SOLUTION_INFARCH = SOLVER_SOLUTION_INFARCH;
1095   static const int SOLVER_SOLUTION_DISTUPGRADE = SOLVER_SOLUTION_DISTUPGRADE;
1096   static const int SOLVER_SOLUTION_DEINSTALL = SOLVER_SOLUTION_DEINSTALL;
1097   static const int SOLVER_SOLUTION_REPLACE = SOLVER_SOLUTION_REPLACE;
1098
1099   ~Solver() {
1100     solver_free($self);
1101   }
1102 #if defined(SWIGPYTHON)
1103   %pythoncode {
1104     def solve(self, jobs):
1105       j = []
1106       for job in jobs: j += [job.how, job.what]
1107       nprob = self.solve_helper(j)
1108       return [ Problem(self, pid) for pid in range(1, nprob + 1) ]
1109   }
1110 #endif
1111   int solve_helper(Queue jobs) {
1112     solver_solve($self, &jobs);
1113     return solver_problem_count($self);
1114   }
1115   %newobject transaction;
1116   Transaction *transaction() {
1117     Transaction *t;
1118     t = sat_calloc(1, sizeof(*t));
1119     transaction_init_clone(t, &$self->trans);
1120     return t;
1121   }
1122 }
1123
1124 %extend Transaction {
1125   static const int SOLVER_TRANSACTION_IGNORE = SOLVER_TRANSACTION_IGNORE;
1126   static const int SOLVER_TRANSACTION_ERASE = SOLVER_TRANSACTION_ERASE;
1127   static const int SOLVER_TRANSACTION_REINSTALLED = SOLVER_TRANSACTION_REINSTALLED;
1128   static const int SOLVER_TRANSACTION_DOWNGRADED = SOLVER_TRANSACTION_DOWNGRADED;
1129   static const int SOLVER_TRANSACTION_CHANGED = SOLVER_TRANSACTION_CHANGED;
1130   static const int SOLVER_TRANSACTION_UPGRADED = SOLVER_TRANSACTION_UPGRADED;
1131   static const int SOLVER_TRANSACTION_OBSOLETED = SOLVER_TRANSACTION_OBSOLETED;
1132   static const int SOLVER_TRANSACTION_INSTALL = SOLVER_TRANSACTION_INSTALL;
1133   static const int SOLVER_TRANSACTION_REINSTALL = SOLVER_TRANSACTION_REINSTALL;
1134   static const int SOLVER_TRANSACTION_DOWNGRADE = SOLVER_TRANSACTION_DOWNGRADE;
1135   static const int SOLVER_TRANSACTION_CHANGE = SOLVER_TRANSACTION_CHANGE;
1136   static const int SOLVER_TRANSACTION_UPGRADE = SOLVER_TRANSACTION_UPGRADE;
1137   static const int SOLVER_TRANSACTION_OBSOLETES = SOLVER_TRANSACTION_OBSOLETES;
1138   static const int SOLVER_TRANSACTION_MULTIINSTALL = SOLVER_TRANSACTION_MULTIINSTALL;
1139   static const int SOLVER_TRANSACTION_MULTIREINSTALL = SOLVER_TRANSACTION_MULTIREINSTALL;
1140   static const int SOLVER_TRANSACTION_MAXTYPE = SOLVER_TRANSACTION_MAXTYPE;
1141   static const int SOLVER_TRANSACTION_SHOW_ACTIVE = SOLVER_TRANSACTION_SHOW_ACTIVE;
1142   static const int SOLVER_TRANSACTION_SHOW_ALL = SOLVER_TRANSACTION_SHOW_ALL;
1143   static const int SOLVER_TRANSACTION_SHOW_OBSOLETES = SOLVER_TRANSACTION_SHOW_OBSOLETES;
1144   static const int SOLVER_TRANSACTION_SHOW_MULTIINSTALL = SOLVER_TRANSACTION_SHOW_MULTIINSTALL;
1145   static const int SOLVER_TRANSACTION_CHANGE_IS_REINSTALL = SOLVER_TRANSACTION_CHANGE_IS_REINSTALL;
1146   static const int SOLVER_TRANSACTION_MERGE_VENDORCHANGES = SOLVER_TRANSACTION_MERGE_VENDORCHANGES;
1147   static const int SOLVER_TRANSACTION_MERGE_ARCHCHANGES = SOLVER_TRANSACTION_MERGE_ARCHCHANGES;
1148   static const int SOLVER_TRANSACTION_RPM_ONLY = SOLVER_TRANSACTION_RPM_ONLY;
1149   static const int SOLVER_TRANSACTION_ARCHCHANGE = SOLVER_TRANSACTION_ARCHCHANGE;
1150   static const int SOLVER_TRANSACTION_VENDORCHANGE = SOLVER_TRANSACTION_VENDORCHANGE;
1151   static const int SOLVER_TRANSACTION_KEEP_ORDERDATA = SOLVER_TRANSACTION_KEEP_ORDERDATA;
1152   ~Transaction() {
1153     transaction_free($self);
1154     sat_free($self);
1155   }
1156   bool isempty() {
1157     return $self->steps.count == 0;
1158   }
1159   Queue classify_helper(int mode) {
1160     Queue q;
1161     queue_init(&q);
1162     transaction_classify($self, mode, &q);
1163     return q;
1164   }
1165   Queue classify_pkgs_helper(int mode, Id cl, Id from, Id to) {
1166     Queue q;
1167     queue_init(&q);
1168     transaction_classify_pkgs($self, mode, cl, from, to, &q);
1169     return q;
1170   }
1171   %newobject othersolvable;
1172   XSolvable *othersolvable(XSolvable *s) {
1173     Id op = transaction_obs_pkg($self, s->id);
1174     return new_XSolvable($self->pool, op);
1175   }
1176 #if defined(SWIGPYTHON)
1177   %pythoncode {
1178     def classify(self, mode = 0):
1179       r = []
1180       cr = self.classify_helper(mode)
1181       for type, cnt, fromid, toid in zip(*([iter(cr)] * 4)):
1182         if type != self.SOLVER_TRANSACTION_IGNORE:
1183           r.append([ type, [ self.pool.solvables[j] for j in self.classify_pkgs_helper(mode, type, fromid, toid) ], fromid, toid ])
1184       return r
1185     }
1186 #endif
1187   Queue installedresult_helper(int *OUTPUT) {
1188     Queue q;
1189     queue_init(&q);
1190     *OUTPUT = transaction_installedresult(self, &q);
1191     return q;
1192   }
1193 #if defined(SWIGPYTHON)
1194   %pythoncode {
1195     def installedresult(self):
1196       r = self.installedresult_helper()
1197       newpkgs = r.pop()
1198       rn = [ self.pool.solvables[r[i]] for i in range(0, newpkgs) ]
1199       rk = [ self.pool.solvables[r[i]] for i in range(newpkgs, len(r)) ]
1200       return rn, rk
1201   }
1202 #endif
1203   Queue steps_helper() {
1204     Queue q;
1205     queue_init_clone(&q, &$self->steps);
1206     return q;
1207   }
1208   int steptype(XSolvable *s, int mode) {
1209     return transaction_type($self, s->id, mode);
1210   }
1211 #if defined(SWIGPYTHON)
1212   %pythoncode {
1213     def steps(self):
1214       return [ self.pool.solvables[i] for i in self.steps_helper() ]
1215   }
1216 #endif
1217   int calc_installsizechange() {
1218     return transaction_calc_installsizechange($self);
1219   }
1220 }
1221
1222 %extend XRule {
1223   XRule(Solver *solv, Id id) {
1224     if (!id)
1225       return 0;
1226     XRule *xr = sat_calloc(1, sizeof(*xr));
1227     xr->solv = solv;
1228     xr->id = id;
1229     return xr;
1230   }
1231   %apply Id *OUTPUT { Id *source, Id *target, Id *dep };
1232   int info_helper(Id *source, Id *target, Id *dep) {
1233     return solver_ruleinfo($self->solv, $self->id, source, target, dep);
1234   }
1235 #if defined(SWIGPYTHON)
1236   %pythoncode {
1237     def info(self):
1238       type, source, target, dep = self.info_helper()
1239       if source:
1240           source = self.solv.pool.solvables[source]
1241       if target:
1242           target = self.solv.pool.solvables[target]
1243       return type, source, target, dep
1244   }
1245 #endif
1246 }
1247
1248
1249 %extend XRepodata {
1250   XRepodata(Repo *repo, Id id) {
1251     XRepodata *xr = sat_calloc(1, sizeof(*xr));
1252     xr->repo = repo;
1253     xr->id = id;
1254     return xr;
1255   }
1256   Id new_handle() {
1257     return repodata_new_handle($self->repo->repodata + $self->id);
1258   }
1259   void set_id(Id solvid, Id keyname, Id id) {
1260     repodata_set_id($self->repo->repodata + $self->id, solvid, keyname, id);
1261   }
1262   void set_str(Id solvid, Id keyname, const char *str) {
1263     repodata_set_str($self->repo->repodata + $self->id, solvid, keyname, str);
1264   }
1265   void set_poolstr(Id solvid, Id keyname, const char *str) {
1266     repodata_set_poolstr($self->repo->repodata + $self->id, solvid, keyname, str);
1267   }
1268   void add_idarray(Id solvid, Id keyname, Id id) {
1269     repodata_add_idarray($self->repo->repodata + $self->id, solvid, keyname, id);
1270   }
1271   void add_flexarray(Id solvid, Id keyname, Id handle) {
1272     repodata_add_flexarray($self->repo->repodata + $self->id, solvid, keyname, handle);
1273   }
1274   void set_bin_checksum(Id solvid, Id keyname, Id chksumtype, const char *chksum) {
1275     repodata_set_bin_checksum($self->repo->repodata + $self->id, solvid, keyname, chksumtype, (const unsigned char *)chksum);
1276   }
1277   const char *lookup_str(Id solvid, Id keyname) {
1278     return repodata_lookup_str($self->repo->repodata + $self->id, solvid, keyname);
1279   }
1280   SWIGCDATA lookup_bin_checksum(Id solvid, Id keyname, Id *OUTPUT) {
1281     const unsigned char *b;
1282     *OUTPUT = 0;
1283     b = repodata_lookup_bin_checksum($self->repo->repodata + $self->id, solvid, keyname, OUTPUT);
1284     return cdata_void((char *)b, sat_chksum_len(*OUTPUT));
1285   }
1286   void internalize() {
1287     repodata_internalize($self->repo->repodata + $self->id);
1288   }
1289   void create_stubs() {
1290     repodata_create_stubs($self->repo->repodata + $self->id);
1291   }
1292   void write(FILE *fp) {
1293     repodata_write($self->repo->repodata + $self->id, fp, repo_write_stdkeyfilter, 0);
1294   }
1295 }