- export repo_add_cudf, use SOLVER_ORUPDATE for cudf update jobs
[platform/upstream/libsolv.git] / tools / cudftest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 #include "pool.h"
6 #include "evr.h"
7 #include "solver.h"
8 #include "solverdebug.h"
9 #include "repo_cudf.h"
10 #include "repo_write.h"
11 #include "solv_xfopen.h"
12
13 static void
14 dump_repo(Repo *repo, char *name)
15 {
16   FILE *fp;
17   if ((fp = fopen(name, "w")) == 0)
18     {
19       perror(name);
20       exit(1);
21     }
22   repo_write(repo, fp);
23   fclose(fp);
24 }
25
26 static int
27 sortfunc(const void *ap, const void *bp, void *dp)
28 {
29   Pool *pool = dp;
30   Solvable *sa, *sb;
31   sa = pool->solvables + *(Id *)ap;
32   sb = pool->solvables + *(Id *)bp;
33   if (sa->name != sb->name)
34     {
35       int r = strcmp(pool_id2str(pool, sa->name), pool_id2str(pool, sb->name));
36       if (r)
37         return r;
38     }
39   if (sa->evr != sb->evr)
40     {
41       int r = pool_evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
42       if (r)
43         return r;
44     }
45   return *(Id *)ap - *(Id *)bp;
46 }
47
48 int
49 main(int argc, char **argv)
50 {
51   char *cudfin;
52   char *cudfout = 0;
53   Pool *pool;
54   Repo *installed, *repo;
55   FILE *fp, *ofp;
56   Solver *solv;
57   Transaction *trans;
58   Queue job;
59   Queue dq;
60   int i;
61   int debug = 0;
62
63   while (argc > 1 && !strcmp(argv[1], "-d"))
64     {
65       debug++;
66       argc--;
67       argv++;
68     }
69   if (argc < 2)
70     {
71       fprintf(stderr, "Usage: cudftest <cudfin> [cudfout]\n");
72       exit(1);
73     }
74   cudfin = argv[1];
75   cudfout = argc > 2 ? argv[2] : 0;
76
77   if ((fp = solv_xfopen(cudfin, 0)) == 0)
78     {
79       perror(cudfin);
80       exit(1);
81     }
82   pool = pool_create();
83   if (debug > 1)
84     pool_setdebuglevel(pool, debug - 1);
85   installed = repo_create(pool, "installed");
86   pool_set_installed(pool, installed);
87   repo = repo_create(pool, "repo");
88   queue_init(&job);
89   repo_add_cudf(repo, installed, fp, &job, 0);
90   pool_createwhatprovides(pool);
91
92   /* debug */
93   if (debug)
94     {
95       dump_repo(installed, "cudf_installed.solv");
96       dump_repo(repo, "cudf_repo.solv");
97     }
98
99   solv = solver_create(pool);
100   solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1);
101   /* solver_set_flag(solv, SOLVER_FLAG_IGNORE_RECOMMENDED, 1); */
102
103   queue_push2(&job, SOLVER_VERIFY | SOLVER_SOLVABLE_ALL, 0);
104   if (solver_solve(solv, &job) != 0)
105     {
106       int problem;
107       int pcnt = solver_problem_count(solv);
108       printf("Found %d problems:\n", pcnt);
109       for (problem = 1; problem <= pcnt; problem++)
110         {
111           printf("Problem %d:\n", problem);
112           solver_printprobleminfo(solv, problem);
113           printf("\n");
114         }
115     }
116   trans = solver_create_transaction(solv);
117   solver_free(solv);
118
119   if (debug)
120     transaction_print(trans);
121
122   queue_init(&dq);
123   transaction_installedresult(trans, &dq);
124   solv_sort(dq.elements, dq.count, sizeof(Id), sortfunc, pool);
125
126   ofp = stdout;
127   if (cudfout && ((ofp = fopen(cudfout, "w")) == 0))
128     {
129       perror(cudfout);
130       exit(1);
131     }
132   for (i = 0; i < dq.count; i++)
133     {
134       Solvable *s = pool_id2solvable(pool, dq.elements[i]);
135       fprintf(ofp, "package: %s\n", pool_id2str(pool, s->name));
136       fprintf(ofp, "version: %s\n", pool_id2str(pool, s->evr));
137       fprintf(ofp, "installed: true\n");
138       if (s->repo == pool->installed)
139         fprintf(ofp, "was-installed: true\n");
140       fprintf(ofp, "\n");
141     }
142   queue_free(&dq);
143   transaction_free(trans);
144   queue_free(&job);
145   pool_free(pool);
146   if (ofp != stdout)
147     {
148       if (fclose(ofp))
149         {
150           perror("fclose");
151           exit(1);
152         }
153     }
154   exit(0);
155 }
156