Date: Tue, 25 Jun 2013 01:12:25 GMT From: mattbw@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r253466 - soc2013/mattbw/backend Message-ID: <201306250112.r5P1CPQH014360@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: mattbw Date: Tue Jun 25 01:12:25 2013 New Revision: 253466 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=253466 Log: adds license mapping (unused and untested at the moment). Added: soc2013/mattbw/backend/licenses.c soc2013/mattbw/backend/licenses.h soc2013/mattbw/backend/mappings.h Modified: soc2013/mattbw/backend/Makefile soc2013/mattbw/backend/groups.c Modified: soc2013/mattbw/backend/Makefile ============================================================================== --- soc2013/mattbw/backend/Makefile Tue Jun 25 00:29:59 2013 (r253465) +++ soc2013/mattbw/backend/Makefile Tue Jun 25 01:12:25 2013 (r253466) @@ -2,7 +2,7 @@ LIB= pk_backend_pkgng SHLIB_MAJOR= 1 -SRCS= pk-backend-pkgng.c get-details.c groups.c db.c +SRCS= pk-backend-pkgng.c get-details.c groups.c db.c licenses.c LIBDIR= /usr/local/lib/packagekit-backend Modified: soc2013/mattbw/backend/groups.c ============================================================================== --- soc2013/mattbw/backend/groups.c Tue Jun 25 00:29:59 2013 (r253465) +++ soc2013/mattbw/backend/groups.c Tue Jun 25 01:12:25 2013 (r253466) @@ -25,11 +25,12 @@ #include "pk-backend.h" /* PkGroupEnum, PK_* */ #include "groups.h" /* prototypes */ +#include "mappings.h" /* mapping macros */ const char ORIGIN_SEPARATOR = '/'; struct group_mapping { - const char *dir; + const char *key; PkGroupEnum group; }; @@ -40,7 +41,7 @@ * Some of these mappings are a bit iffy, peer review would be greatly * appreciated. * - * These should ALWAYS be in alphabetical order of ports directories. This is + * These should ALWAYS be in ASCIIbetical order of ports directories. This is * in case anything relies on this property when searching this list (for * example binary searching) */ @@ -122,7 +123,7 @@ PkBitfield bits; bits = 0; - for (i = 0; group_mappings[i].dir != NULL; i++) + for (i = 0; group_mappings[i].key != NULL; i++) pk_bitfield_add(bits, group_mappings[i].group); return bits; @@ -132,13 +133,10 @@ PkGroupEnum group_from_port_dir(const char *port_dir) { - int i; - - i = 0; - while (group_mappings[i].dir != NULL && - g_strcmp0(group_mappings[i].dir, port_dir) != 0) - i++; - return group_mappings[i].group; + struct group_mapping *result; + + MAPPING_FIND(port_dir, &result, group_mappings); + return result->group; } /* Maps from package origins to PackageKit groups. */ Added: soc2013/mattbw/backend/licenses.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2013/mattbw/backend/licenses.c Tue Jun 25 01:12:25 2013 (r253466) @@ -0,0 +1,92 @@ +/*- + * Copyright (C) 2013 Matt Windsor <mattbw@FreeBSD.org> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "pkg.h" /* struct pkg, etc */ +#include "pk-backend.h" /* PkLicenseEnum, PK_* */ + +#include "licenses.h" /* prototypes */ +#include "mappings.h" /* mapping macros */ + +static const char * license_name_from_pkg(struct pkg *pkg); + +struct license_mapping { + const char *key; + PkLicenseEnum license; +}; + +/* + * Mappings between the output of "pkg_license_name" and the closest available + * PackageKit license. + * + * These should ALWAYS be in ASCIIbetical order of license names. This is in + * case anything relies on this property when searching the list (for example + * binary searching). + */ +static struct license_mapping license_mappings[] = { + {"BSD", PK_LICENSE_ENUM_BSD}, + {NULL, PK_LICENSE_ENUM_UNKNOWN} +}; + +/* Returns the closest approximation packagekit has to this package's license. + * + * PackageKit only has a simple concept of licencing, so this is inherently a + * very lossy mapping. + */ +PkLicenseEnum +license_from_pkg(struct pkg *pkg) +{ + const char *pkg_license_name; + struct license_mapping *map; + PkLicenseEnum result; + + pkg_license_name = license_name_from_pkg(pkg); + + /* Is it in our manual license mapping? + * If not, then try interpreting it as a PackageKit license name and + * convert it back (messy, but hopefully safe), but only if it isn't + * NULL. + */ + MAPPING_FIND(pkg_license_name, &map, license_mappings); + if (pkg_license_name != NULL && map->key == NULL) + result = pk_license_enum_from_string(pkg_license_name); + else + result = map->license; + + return result; +} + +/* Retrieves the name of the FIRST license attached to the package. */ +static const char * +license_name_from_pkg(struct pkg *pkg) +{ + int err; + const char *match; + struct pkg_license *lic; + + lic = NULL; + match = NULL; + do { + err = pkg_licenses(pkg, &lic); + if (err == EPKG_OK) + match = pkg_license_name(lic); + } while (err == EPKG_OK && match == NULL); + + return match; +} Added: soc2013/mattbw/backend/licenses.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2013/mattbw/backend/licenses.h Tue Jun 25 01:12:25 2013 (r253466) @@ -0,0 +1,28 @@ +/*- + * Copyright (C) 2013 Matt Windsor <mattbw@FreeBSD.org> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _PKGNG_BACKEND_LICENSES_H_ +#define _PKGNG_BACKEND_LICENSES_H_ + +#include "pk-backend.h" + +PkLicenseEnum license_from_pkg(struct pkg *pkg); + +#endif /* _PKGNG_BACKEND_LICENSES_H_ */ Added: soc2013/mattbw/backend/mappings.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2013/mattbw/backend/mappings.h Tue Jun 25 01:12:25 2013 (r253466) @@ -0,0 +1,39 @@ +/*- + * Copyright (C) 2013 Matt Windsor <mattbw@FreeBSD.org> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* Macros for dealing with string-key-to-enum-value mapping lists. */ + +#include "glib.h" /* g_strcmp0 */ + +/* + * Finds a mapping entry given its string key "sk", and sets the pointer + * pointed to by "vp" to reference it. Expects the last (fallthrough) entry + * to have a string value of NULL, will point to it if no other match was + * found. + * + * Currently a linear search, could be improved. Any way to make this not a + * horrible macro would also be welcomed (it's a macro to make it work across + * different types of mapping, at the moment). + */ +#define MAPPING_FIND(sk, vp, map) do { \ + for (*(vp) = map; \ + (*(vp))->key != NULL && g_strcmp0((*(vp))->key, (sk)) != 0;\ + (*(vp)) += 1); \ +} while(0)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201306250112.r5P1CPQH014360>