Import summary and description into the repo files.
Also add a 'info' command for displaying them.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 /* Import a yum filelist as a razor package set. */
38 YUM_STATE_PACKAGE_NAME,
39 YUM_STATE_PACKAGE_ARCH,
41 YUM_STATE_DESCRIPTION,
51 XML_Parser primary_parser;
52 XML_Parser filelists_parser;
53 XML_Parser current_parser;
55 struct razor_importer *importer;
56 struct import_property_context *current_property_context;
57 char name[256], arch[64], summary[512], description[4096], buffer[512], *p;
62 static enum razor_version_relation
63 yum_to_razor_flags (const char *flags)
67 return RAZOR_VERSION_EQUAL;
69 if (flags[0] == 'L') {
71 return RAZOR_VERSION_LESS;
73 return RAZOR_VERSION_LESS_OR_EQUAL;
74 } else if (flags[0] == 'G') {
76 return RAZOR_VERSION_GREATER;
78 return RAZOR_VERSION_GREATER_OR_EQUAL;
80 return RAZOR_VERSION_EQUAL;
84 yum_primary_start_element(void *data, const char *name, const char **atts)
86 struct yum_context *ctx = data;
87 const char *n, *epoch, *version, *release, *flags;
91 if (strcmp(name, "name") == 0) {
92 ctx->state = YUM_STATE_PACKAGE_NAME;
94 } else if (strcmp(name, "arch") == 0) {
95 ctx->state = YUM_STATE_PACKAGE_ARCH;
97 } else if (strcmp(name, "version") == 0) {
101 for (i = 0; atts[i]; i += 2) {
102 if (strcmp(atts[i], "epoch") == 0)
104 else if (strcmp(atts[i], "ver") == 0)
105 version = atts[i + 1];
106 else if (strcmp(atts[i], "rel") == 0)
107 release = atts[i + 1];
109 if (version == NULL || release == NULL) {
110 fprintf(stderr, "invalid version tag, "
111 "missing version or release attribute\n");
115 razor_build_evr(buffer, sizeof buffer, epoch, version, release);
116 razor_importer_begin_package(ctx->importer,
117 ctx->name, buffer, ctx->arch);
118 } else if (strcmp(name, "summary") == 0) {
119 ctx->p = ctx->summary;
120 ctx->state = YUM_STATE_SUMMARY;
121 } else if (strcmp(name, "description") == 0) {
122 ctx->p = ctx->description;
123 ctx->state = YUM_STATE_DESCRIPTION;
124 } else if (strcmp(name, "checksum") == 0) {
126 ctx->state = YUM_STATE_CHECKSUM;
127 } else if (strcmp(name, "rpm:requires") == 0) {
128 ctx->state = YUM_STATE_REQUIRES;
129 } else if (strcmp(name, "rpm:provides") == 0) {
130 ctx->state = YUM_STATE_PROVIDES;
131 } else if (strcmp(name, "rpm:obsoletes") == 0) {
132 ctx->state = YUM_STATE_OBSOLETES;
133 } else if (strcmp(name, "rpm:conflicts") == 0) {
134 ctx->state = YUM_STATE_CONFLICTS;
135 } else if (strcmp(name, "rpm:entry") == 0 &&
136 ctx->state != YUM_STATE_BEGIN) {
142 for (i = 0; atts[i]; i += 2) {
143 if (strcmp(atts[i], "name") == 0)
145 else if (strcmp(atts[i], "epoch") == 0)
147 else if (strcmp(atts[i], "ver") == 0)
148 version = atts[i + 1];
149 else if (strcmp(atts[i], "rel") == 0)
150 release = atts[i + 1];
151 else if (strcmp(atts[i], "flags") == 0)
156 fprintf(stderr, "invalid rpm:entry, "
157 "missing name or version attributes\n");
161 razor_build_evr(buffer, sizeof buffer, epoch, version, release);
162 switch (ctx->state) {
163 case YUM_STATE_REQUIRES:
164 razor_importer_add_property(ctx->importer, n,
165 yum_to_razor_flags (flags),
167 RAZOR_PROPERTY_REQUIRES);
169 case YUM_STATE_PROVIDES:
170 razor_importer_add_property(ctx->importer, n,
171 yum_to_razor_flags (flags),
173 RAZOR_PROPERTY_PROVIDES);
175 case YUM_STATE_OBSOLETES:
176 razor_importer_add_property(ctx->importer, n,
177 yum_to_razor_flags (flags),
179 RAZOR_PROPERTY_OBSOLETES);
181 case YUM_STATE_CONFLICTS:
182 razor_importer_add_property(ctx->importer, n,
183 yum_to_razor_flags (flags),
185 RAZOR_PROPERTY_CONFLICTS);
192 yum_primary_end_element (void *data, const char *name)
194 struct yum_context *ctx = data;
196 switch (ctx->state) {
197 case YUM_STATE_PACKAGE_NAME:
198 case YUM_STATE_PACKAGE_ARCH:
199 case YUM_STATE_SUMMARY:
200 case YUM_STATE_DESCRIPTION:
201 case YUM_STATE_CHECKSUM:
203 ctx->state = YUM_STATE_BEGIN;
207 if (strcmp(name, "package") == 0) {
208 razor_importer_add_details(ctx->importer, ctx->summary, ctx->description);
210 XML_StopParser(ctx->current_parser, XML_TRUE);
211 ctx->current_parser = ctx->filelists_parser;
216 yum_character_data (void *data, const XML_Char *s, int len)
218 struct yum_context *ctx = data;
220 switch (ctx->state) {
221 case YUM_STATE_PACKAGE_NAME:
222 case YUM_STATE_PACKAGE_ARCH:
223 case YUM_STATE_SUMMARY:
224 case YUM_STATE_DESCRIPTION:
225 case YUM_STATE_CHECKSUM:
227 memcpy(ctx->p, s, len);
235 yum_filelists_start_element(void *data, const char *name, const char **atts)
237 struct yum_context *ctx = data;
238 const char *pkg, *pkgid;
241 if (strcmp(name, "package") == 0) {
244 for (i = 0; atts[i]; i += 2) {
245 if (strcmp(atts[i], "name") == 0)
247 else if (strcmp(atts[i], "pkgid") == 0)
250 if (strcmp(pkgid, ctx->pkgid) != 0)
251 fprintf(stderr, "primary.xml and filelists.xml "
252 "mismatch for %s: %s vs %s",
253 pkg, pkgid, ctx->pkgid);
254 } else if (strcmp(name, "file") == 0) {
255 ctx->state = YUM_STATE_FILE;
256 ctx->p = ctx->buffer;
262 yum_filelists_end_element (void *data, const char *name)
264 struct yum_context *ctx = data;
266 ctx->state = YUM_STATE_BEGIN;
267 if (strcmp(name, "package") == 0) {
268 XML_StopParser(ctx->current_parser, XML_TRUE);
269 ctx->current_parser = ctx->primary_parser;
270 razor_importer_finish_package(ctx->importer);
271 } else if (strcmp(name, "file") == 0)
272 razor_importer_add_file(ctx->importer, ctx->buffer);
276 #define XML_BUFFER_SIZE 4096
279 razor_set_create_from_yum(void)
281 struct yum_context ctx;
284 gzFile primary, filelists;
285 XML_ParsingStatus status;
287 ctx.importer = razor_importer_new();
288 ctx.state = YUM_STATE_BEGIN;
290 ctx.primary_parser = XML_ParserCreate(NULL);
291 XML_SetUserData(ctx.primary_parser, &ctx);
292 XML_SetElementHandler(ctx.primary_parser,
293 yum_primary_start_element,
294 yum_primary_end_element);
295 XML_SetCharacterDataHandler(ctx.primary_parser,
298 ctx.filelists_parser = XML_ParserCreate(NULL);
299 XML_SetUserData(ctx.filelists_parser, &ctx);
300 XML_SetElementHandler(ctx.filelists_parser,
301 yum_filelists_start_element,
302 yum_filelists_end_element);
303 XML_SetCharacterDataHandler(ctx.filelists_parser,
306 primary = gzopen("primary.xml.gz", "rb");
309 filelists = gzopen("filelists.xml.gz", "rb");
310 if (filelists == NULL)
313 ctx.current_parser = ctx.primary_parser;
316 XML_GetParsingStatus(ctx.current_parser, &status);
317 switch (status.parsing) {
319 ret = XML_ResumeParser(ctx.current_parser);
322 case XML_INITIALIZED:
323 buf = XML_GetBuffer(ctx.current_parser,
325 if (ctx.current_parser == ctx.primary_parser)
326 len = gzread(primary, buf, XML_BUFFER_SIZE);
328 len = gzread(filelists, buf, XML_BUFFER_SIZE);
331 "couldn't read input: %s\n",
336 XML_ParseBuffer(ctx.current_parser, len, len == 0);
341 } while (status.parsing != XML_FINISHED);
344 XML_ParserFree(ctx.primary_parser);
345 XML_ParserFree(ctx.filelists_parser);
350 return razor_importer_finish(ctx.importer);