}
static PyObject* Archive_get(ArchiveObject* self, PyObject* args) {
+ const char* namespace = NULL;
const char* key = NULL;
- if (!PyArg_ParseTuple(args, "|s", &key))
+ if (!PyArg_ParseTuple(args, "zs", &namespace, &key))
return NULL;
- char* value = pakfire_archive_get(self->archive, key);
+ char* value = pakfire_archive_get(self->archive, namespace, key);
if (!value)
Py_RETURN_NONE;
}
static PyObject* Parser_get(ParserObject* self, PyObject* args) {
+ const char* namespace = NULL;
const char* key = NULL;
- if (!PyArg_ParseTuple(args, "s", &key))
+ if (!PyArg_ParseTuple(args, "zs", &namespace, &key))
+ return NULL;
+
+ char* value = pakfire_parser_get(self->parser, namespace, key);
+ if (!value) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+
+ PyObject* ret = PyUnicode_FromString(value);
+ free(value);
+
+ return ret;
+}
+
+static PyObject* Parser_expand(ParserObject* self, PyObject* args) {
+ const char* namespace = NULL;
+ const char* s = NULL;
+
+ if (!PyArg_ParseTuple(args, "zs", &namespace, &s))
return NULL;
- char* value = pakfire_parser_get(self->parser, key);
+ char* value = pakfire_parser_expand(self->parser, namespace, s);
if (!value) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
METH_VARARGS,
NULL,
},
+ {
+ "expand",
+ (PyCFunction)Parser_expand,
+ METH_VARARGS,
+ NULL,
+ },
{
"get",
(PyCFunction)Parser_get,
return pakfire_archive_walk(archive, pakfire_archive_read_metadata_entry);
}
-PAKFIRE_EXPORT char* pakfire_archive_get(PakfireArchive archive, const char* key) {
- return pakfire_parser_get(archive->parser, key);
+PAKFIRE_EXPORT char* pakfire_archive_get(PakfireArchive archive, const char* namespace, const char* key) {
+ return pakfire_parser_get(archive->parser, namespace, key);
}
static int archive_copy_data(Pakfire pakfire, struct archive* in, struct archive* out) {
Copy all metadata from this archive to the package object
*/
PAKFIRE_EXPORT PakfirePackage pakfire_archive_make_package(PakfireArchive archive, PakfireRepo repo) {
- char* name = pakfire_archive_get(archive, "package.name");
- char* type = pakfire_archive_get(archive, "package.type");
+ char* name = pakfire_archive_get(archive, "package", "name");
+ char* type = pakfire_archive_get(archive, "package", "type");
char* arch = NULL;
// Get arch for source packages
if (strcmp(type, "binary") == 0) {
- arch = pakfire_archive_get(archive, "package.arch");
+ arch = pakfire_archive_get(archive, "package", "arch");
}
- char* e = pakfire_archive_get(archive, "package.epoch");
- char* v = pakfire_archive_get(archive, "package.version");
- char* r = pakfire_archive_get(archive, "package.release");
+ char* e = pakfire_archive_get(archive, "package", "epoch");
+ char* v = pakfire_archive_get(archive, "package", "version");
+ char* r = pakfire_archive_get(archive, "package", "release");
char* evr = pakfire_package_join_evr(e, v, r);
PakfirePackage pkg = pakfire_package_create(
}
// Set UUID
- char* uuid = pakfire_archive_get(archive, "package.uuid");
+ char* uuid = pakfire_archive_get(archive, "package", "uuid");
if (uuid) {
pakfire_package_set_uuid(pkg, uuid);
free(uuid);
}
// Set groups
- char* groups = pakfire_archive_get(archive, "package.groups");
+ char* groups = pakfire_archive_get(archive, "package", "groups");
if (groups) {
pakfire_package_set_groups(pkg, groups);
free(groups);
}
// Set maintainer
- char* maintainer = pakfire_archive_get(archive, "package.maintainer");
+ char* maintainer = pakfire_archive_get(archive, "package", "maintainer");
if (maintainer) {
pakfire_package_set_maintainer(pkg, maintainer);
free(maintainer);
}
// Set URL
- char* url = pakfire_archive_get(archive, "package.url");
+ char* url = pakfire_archive_get(archive, "package", "url");
if (url) {
pakfire_package_set_url(pkg, url);
free(url);
}
// Set license
- char* license = pakfire_archive_get(archive, "package.license");
+ char* license = pakfire_archive_get(archive, "package", "license");
if (license) {
pakfire_package_set_license(pkg, license);
free(license);
}
// Set summary
- char* summary = pakfire_archive_get(archive, "package.summary");
+ char* summary = pakfire_archive_get(archive, "package", "summary");
if (summary) {
pakfire_package_set_summary(pkg, summary);
free(summary);
}
// Set description
- char* description = pakfire_archive_get(archive, "package.description");
+ char* description = pakfire_archive_get(archive, "package", "description");
if (description) {
pakfire_package_set_description(pkg, description);
free(description);
pakfire_package_set_downloadsize(pkg, pakfire_archive_get_size(archive));
// Get install size
- char* size = pakfire_archive_get(archive, "package.size");
+ char* size = pakfire_archive_get(archive, "package", "size");
if (size) {
size_t s = pakfire_string_to_size(size);
free(size);
}
// Set vendor
- char* vendor = pakfire_archive_get(archive, "distribution.vendor");
+ char* vendor = pakfire_archive_get(archive, "distribution", "vendor");
if (vendor) {
pakfire_package_set_vendor(pkg, vendor);
free(vendor);
}
// Set build host
- char* build_host = pakfire_archive_get(archive, "build.host");
+ char* build_host = pakfire_archive_get(archive, "build", "host");
if (build_host) {
pakfire_package_set_build_host(pkg, build_host);
free(build_host);
}
// Set build time
- char* build_time = pakfire_archive_get(archive, "build.time");
+ char* build_time = pakfire_archive_get(archive, "build", "time");
if (build_time) {
time_t t = strtoull(build_time, NULL, 10);
free(build_time);
const char* type;
void (*func)(PakfirePackage, PakfireRelationList);
} relations[] = {
- { "dependencies.provides", pakfire_package_set_provides },
- { "dependencies.prerequires", pakfire_package_set_prerequires },
- { "dependencies.requires", pakfire_package_set_requires },
- { "dependencies.conflicts", pakfire_package_set_conflicts },
- { "dependencies.obsoletes", pakfire_package_set_obsoletes },
- { "dependencies.recommends", pakfire_package_set_recommends },
- { "dependencies.suggests", pakfire_package_set_suggests },
+ { "provides", pakfire_package_set_provides },
+ { "prerequires", pakfire_package_set_prerequires },
+ { "requires", pakfire_package_set_requires },
+ { "conflicts", pakfire_package_set_conflicts },
+ { "obsoletes", pakfire_package_set_obsoletes },
+ { "recommends", pakfire_package_set_recommends },
+ { "suggests", pakfire_package_set_suggests },
{ NULL, NULL },
};
for (const struct __relation* relation = relations; relation->type; relation++) {
- char* relations = pakfire_archive_get(archive, relation->type);
+ char* relations = pakfire_archive_get(archive, "dependencies", relation->type);
if (!relations)
continue;
PakfireArchive pakfire_archive_unref(PakfireArchive archive);
Pakfire pakfire_archive_get_pakfire(PakfireArchive archive);
-char* pakfire_archive_get(PakfireArchive archive, const char* key);
+char* pakfire_archive_get(PakfireArchive archive, const char* namespace, const char* key);
PakfireArchive pakfire_archive_open(Pakfire pakfire, const char* path);
PakfireParser pakfire_parser_get_parent(PakfireParser parser);
int pakfire_parser_set(PakfireParser parser,
- const char* name, const char* value);
+ const char* namespace, const char* name, const char* value);
int pakfire_parser_append(PakfireParser parser,
- const char* name, const char* value);
+ const char* namespace, const char* name, const char* value);
-char* pakfire_parser_expand(PakfireParser parser, const char* value);
-char* pakfire_parser_get(PakfireParser parser, const char* name);
+char* pakfire_parser_expand(PakfireParser parser, const char* namespace, const char* value);
+char* pakfire_parser_get(PakfireParser parser, const char* namespace, const char* name);
int pakfire_parser_merge(PakfireParser parser1, PakfireParser parser2);
Pakfire pakfire_parser_get_pakfire(PakfireParser parser);
struct pakfire_parser_declaration {
+ char* namespace;
char* name;
char* value;
enum flags {
size_t num_declarations;
};
-static char* pakfire_parser_make_canonical_name(PakfireParser parser, const char* name) {
+static char* pakfire_parser_make_canonical_name(const char* namespace, const char* name) {
char* buffer = NULL;
- if (parser->namespace) {
- int r = asprintf(&buffer, "%s.%s", parser->namespace, name);
+ if (namespace) {
+ int r = asprintf(&buffer, "%s.%s", namespace, name);
if (r < 0)
return NULL;
} else {
struct pakfire_parser_declaration* d = parser->declarations[i];
// Free everything
+ if (d->namespace)
+ free(d->namespace);
if (d->name)
free(d->name);
if (d->value)
}
static struct pakfire_parser_declaration* pakfire_parser_get_declaration(
- PakfireParser parser, const char* name) {
+ PakfireParser parser, const char* namespace, const char* name) {
struct pakfire_parser_declaration* d;
for (unsigned i = 0; i < parser->num_declarations; i++) {
if (!d)
break;
+ // Skip if d does not have a namespace set
+ if (namespace && !d->namespace)
+ continue;
+
+ // Skip if namespace does not match
+ if (namespace && strcmp(d->namespace, namespace) != 0)
+ continue;
+
// Compare the name
if (strcmp(d->name, name) == 0)
return d;
return NULL;
}
-static int pakfire_parser_set_declaration(PakfireParser parser,
- const char* name, const char* value) {
+PAKFIRE_EXPORT int pakfire_parser_set(PakfireParser parser,
+ const char* namespace, const char* name, const char* value) {
if (!name)
return -EINVAL;
// Handle when name already exists
- struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, name);
+ struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, namespace, name);
if (d) {
// Replace value
if (d->value)
else
d->value = NULL;
- DEBUG(parser->pakfire, "%p: Updated declaration: %s = %s\n",
- parser, d->name, d->value);
+ DEBUG(parser->pakfire, "%p: Updated declaration: %s.%s = %s\n",
+ parser, d->namespace, d->name, d->value);
// All done
return 0;
if (!d)
return -1;
+ // Copy namespace
+ if (namespace)
+ d->namespace = strdup(namespace);
+ else
+ d->namespace = NULL;
+
// Import name & value
d->name = strdup(name);
if (value)
d->value = strdup(value);
- DEBUG(parser->pakfire, "%p: New declaration: %s = %s\n", parser, d->name, d->value);
+ DEBUG(parser->pakfire, "%p: New declaration: %s.%s = %s\n",
+ parser, d->namespace, d->name, d->value);
// Assign new declaration to array
parser->declarations = reallocarray(parser->declarations,
return 0;
}
-PAKFIRE_EXPORT int pakfire_parser_set(PakfireParser parser, const char* name, const char* value) {
- return pakfire_parser_set_declaration(parser, name, value);
-}
-
int pakfire_parser_apply_declaration(PakfireParser parser,
struct pakfire_parser_declaration* declaration) {
if (declaration->flags & PAKFIRE_PARSER_DECLARATION_APPEND)
- return pakfire_parser_append(parser, declaration->name, declaration->value);
+ return pakfire_parser_append(parser, declaration->namespace, declaration->name, declaration->value);
- return pakfire_parser_set(parser, declaration->name, declaration->value);
+ return pakfire_parser_set(parser, declaration->namespace, declaration->name, declaration->value);
}
-static const char* pakfire_parser_get_raw(PakfireParser parser, const char* name) {
- struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, name);
+static const char* pakfire_parser_get_raw(PakfireParser parser, const char* namespace, const char* name) {
+ struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, namespace, name);
// Return a match when it actually contains a string
if (d) {
// Search in parent parser if available
if (parser->parent)
- return pakfire_parser_get_raw(parser->parent, name);
+ return pakfire_parser_get_raw(parser->parent, namespace, name);
return NULL;
}
PAKFIRE_EXPORT int pakfire_parser_append(PakfireParser parser,
- const char* name, const char* value) {
+ const char* namespace, const char* name, const char* value) {
char* buffer = NULL;
// Fetch the value of the current declaration
- const char* old_value = pakfire_parser_get_raw(parser, name);
+ const char* old_value = pakfire_parser_get_raw(parser, namespace, name);
// Set the new value when there is no old one
if (!old_value)
- return pakfire_parser_set_declaration(parser, name, value);
+ return pakfire_parser_set(parser, namespace, name, value);
// Concat value
int r = asprintf(&buffer, "%s %s", old_value, value);
return r;
// Set the new value
- r = pakfire_parser_set_declaration(parser, name, buffer);
+ r = pakfire_parser_set(parser, namespace, name, buffer);
free(buffer);
return r;
}
-static void pakfire_parser_strip_namespace(char* s) {
- char* pos = strrchr(s, '.');
+static void pakfire_parser_strip_namespace(char** s) {
+ char* pos = strrchr(*s, '.');
if (pos)
- s[pos - s] = '\0';
+ *s[pos - *s] = '\0';
else
- s[0] = '\0';
+ *s = NULL;
}
static struct pakfire_parser_declaration* pakfire_parser_find_declaration(
- PakfireParser parser, const char* name) {
+ PakfireParser parser, const char* namespace, const char* name) {
char* n = NULL;
- // Create a working copy of the namespace
- if (parser->namespace)
- n = strdup(parser->namespace);
-
- size_t length = ((n) ? strlen(n) : 0) + strlen(name) + 1;
- char* buffer = malloc(length + 1);
+ // Create a working copy of the namespace variable
+ if (namespace)
+ n = strdupa(namespace);
struct pakfire_parser_declaration* d = NULL;
-
while (1) {
- if (n && *n)
- snprintf(buffer, length + 1, "%s.%s", n, name);
- else
- snprintf(buffer, length + 1, "%s", name);
-
- DEBUG(parser->pakfire, "Looking up %s in parser %p\n", buffer, parser);
+ DEBUG(parser->pakfire, "Looking up %s.%s in parser %p\n", n, name, parser);
// Lookup declaration
- d = pakfire_parser_get_declaration(parser, buffer);
+ d = pakfire_parser_get_declaration(parser, n, name);
// End if we have found a match
if (d)
break;
// End if namespace is empty
- if (!n || !*n)
+ if (!n)
break;
/*
If we did not find a match, we will remove one level of the
namespace and try again...
*/
- pakfire_parser_strip_namespace(n);
+ pakfire_parser_strip_namespace(&n);
}
- free(buffer);
-
- if (n)
- free(n);
-
if (!d && parser->parent)
- d = pakfire_parser_find_declaration(parser->parent, name);
+ d = pakfire_parser_find_declaration(parser->parent, namespace, name);
return d;
}
-PAKFIRE_EXPORT char* pakfire_parser_expand(PakfireParser parser, const char* value) {
+PAKFIRE_EXPORT char* pakfire_parser_expand(PakfireParser parser,
+ const char* namespace, const char* value) {
// Return NULL when the value is NULL
if (!value)
return NULL;
// Search for a declaration of this variable
struct pakfire_parser_declaration* v =
- pakfire_parser_find_declaration(parser, variable);
+ pakfire_parser_find_declaration(parser, namespace, variable);
const char* value = NULL;
if (v && v->value) {
return buffer;
}
-PAKFIRE_EXPORT char* pakfire_parser_get(PakfireParser parser, const char* name) {
- const char* value = pakfire_parser_get_raw(parser, name);
+PAKFIRE_EXPORT char* pakfire_parser_get(PakfireParser parser, const char* namespace, const char* name) {
+ const char* value = pakfire_parser_get_raw(parser, namespace, name);
// Return NULL when nothing was found
if (!value)
return NULL;
// Otherwise return the expanded value
- return pakfire_parser_expand(parser, value);
+ return pakfire_parser_expand(parser, namespace, value);
}
PAKFIRE_EXPORT int pakfire_parser_merge(PakfireParser parser1, PakfireParser parser2) {
if (parser1 == parser2)
return EINVAL;
+ char* namespace = NULL;
+
for (unsigned int i = 0; i < parser2->num_declarations; i++) {
struct pakfire_parser_declaration* d = parser2->declarations[i];
if (!d)
break;
- char* canonical_name = pakfire_parser_make_canonical_name(parser2, d->name);
+ if (parser2->namespace && d->namespace)
+ asprintf(&namespace, "%s.%s", parser2->namespace, d->namespace);
+ else if (parser2->namespace)
+ asprintf(&namespace, "%s", parser2->namespace);
+ else if (d->namespace)
+ asprintf(&namespace, "%s", d->namespace);
+ else
+ namespace = NULL;
- int r = pakfire_parser_set_declaration(parser1, canonical_name, d->value);
+ int r = pakfire_parser_set(parser1, namespace, d->name, d->value);
- free(canonical_name);
+ if (namespace)
+ free(namespace);
if (r)
return r;
PAKFIRE_EXPORT char* pakfire_parser_dump(PakfireParser parser) {
char* s = NULL;
+ char buffer[1024];
+
for (unsigned int i = 0; i < parser->num_declarations; i++) {
struct pakfire_parser_declaration* d = parser->declarations[i];
if (d) {
- if (s)
- asprintf(&s, "%s%-24s = %s\n", s, d->name, d->value);
+ if (d->namespace)
+ snprintf(buffer, sizeof(buffer) - 1, "%s.%s", d->namespace, d->name);
else
- asprintf(&s, "%-24s = %s\n", d->name, d->value);
+ snprintf(buffer, sizeof(buffer) - 1, "%s", d->name);
+
+ asprintf(&s, "%s%-24s = %s\n", (s) ? s : "", buffer, d->value);
}
}
if (!d)
return ENOMEM;
+ // Set namespace
+ d->namespace = NULL;
+
// Copy name
d->name = strdup(name);
if (r)
ABORT;
- pakfire_parser_set($$, key, value);
+ pakfire_parser_set($$, NULL, key, value);
if (key)
free(key);
DEBUG(pakfire, " parser = %p, if = %p, else = %p\n", *parser, if_block, else_block);
// Expand values
- char* v1 = pakfire_parser_expand(*parser, val1);
- char* v2 = pakfire_parser_expand(*parser, val2);
+ char* v1 = pakfire_parser_expand(*parser, NULL, val1);
+ char* v2 = pakfire_parser_expand(*parser, NULL, val2);
PakfireParser result = NULL;
# Parse all metadata
package = archive.get_package()
- requires = archive.get("dependencies.requires")
+ requires = archive.get("dependencies", "requires")
if requires:
packages += requires.splitlines()
PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL);
// Retrieve a value that does not exist
- value = pakfire_parser_get(parser, "null");
+ value = pakfire_parser_get(parser, NULL, "null");
ASSERT(!value);
// Set a value
- int r = pakfire_parser_set(parser, "a", "a");
+ int r = pakfire_parser_set(parser, NULL, "a", "a");
ASSERT(r == 0);
// Retrieve the value again
- value = pakfire_parser_get(parser, "a");
+ value = pakfire_parser_get(parser, NULL, "a");
ASSERT_STRING_EQUALS(value, "a");
// Append something to the value
- r = pakfire_parser_append(parser, "a", "b");
+ r = pakfire_parser_append(parser, NULL, "a", "b");
ASSERT(r == 0);
// Retrieve the value again
- value = pakfire_parser_get(parser, "a");
+ value = pakfire_parser_get(parser, NULL, "a");
ASSERT_STRING_EQUALS(value, "a b");
// Make a child parser
ASSERT(subparser);
// Try to get a again
- value = pakfire_parser_get(subparser, "a");
+ value = pakfire_parser_get(subparser, NULL, "a");
ASSERT_STRING_EQUALS(value, "a b");
// Append something to the subparser
- r = pakfire_parser_append(subparser, "a", "c");
+ r = pakfire_parser_append(subparser, NULL, "a", "c");
ASSERT(r == 0);
// The subparser should return "a b c"
- value = pakfire_parser_get(subparser, "a");
+ value = pakfire_parser_get(subparser, NULL, "a");
ASSERT_STRING_EQUALS(value, "a b c");
// The original parser should remain unchanged
- value = pakfire_parser_get(parser, "a");
+ value = pakfire_parser_get(parser, NULL, "a");
ASSERT_STRING_EQUALS(value, "a b");
// Set another value
- r = pakfire_parser_append(subparser, "b", "1");
+ r = pakfire_parser_append(subparser, NULL, "b", "1");
ASSERT(r == 0);
// Merge the two parsers
pakfire_parser_merge(parser, subparser);
- // Now a should have changed to "a b c"
- value = pakfire_parser_get(parser, "child.a");
- ASSERT_STRING_EQUALS(value, "a b c");
-
// Set a variable
- r = pakfire_parser_set(parser, "c", "%{b}");
+ r = pakfire_parser_set(parser, NULL, "c", "%{b}");
ASSERT(r == 0);
// Get the value of c
- value = pakfire_parser_get(parser, "c");
- ASSERT_STRING_EQUALS(value, "");
+ value = pakfire_parser_get(parser, NULL, "c");
+ ASSERT_STRING_EQUALS(value, "1");
// Dump the parser
char* s = pakfire_parser_dump(parser);