[virt-tools-list] [PATCH libosinfo 1/2] Add support for filtering products based on release/eol dates
Daniel P. Berrange
berrange at redhat.com
Tue Feb 21 18:53:38 UTC 2012
From: "Daniel P. Berrange" <berrange at redhat.com>
---
osinfo/libosinfo.syms | 1 +
osinfo/osinfo_loader.c | 2 +
osinfo/osinfo_product.c | 48 +++++++++++++++++++++++++
osinfo/osinfo_product.h | 16 ++++++--
osinfo/osinfo_productfilter.c | 32 +++++++++++++++++
osinfo/osinfo_productfilter.h | 2 +
test/test-product.c | 77 +++++++++++++++++++++++++++++++++++++++++
7 files changed, 174 insertions(+), 4 deletions(-)
diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index f4f6337..2f801af 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -93,6 +93,7 @@ LIBOSINFO_0.0.1 {
osinfo_productfilter_clear_product_constraint;
osinfo_productfilter_clear_product_constraints;
osinfo_productfilter_get_product_constraint_values;
+ osinfo_productfilter_add_support_date_constraint;
osinfo_product_get_type;
osinfo_product_get_related;
osinfo_product_add_related;
diff --git a/osinfo/osinfo_loader.c b/osinfo/osinfo_loader.c
index c44095b..6c55e29 100644
--- a/osinfo/osinfo_loader.c
+++ b/osinfo/osinfo_loader.c
@@ -370,6 +370,8 @@ static void osinfo_loader_product(OsinfoLoader *loader,
OSINFO_PRODUCT_PROP_VENDOR,
OSINFO_PRODUCT_PROP_VERSION,
OSINFO_PRODUCT_PROP_SHORT_ID,
+ OSINFO_PRODUCT_PROP_RELEASE_DATE,
+ OSINFO_PRODUCT_PROP_EOL_DATE,
NULL,
};
diff --git a/osinfo/osinfo_product.c b/osinfo/osinfo_product.c
index 0b57c90..4978655 100644
--- a/osinfo/osinfo_product.c
+++ b/osinfo/osinfo_product.c
@@ -24,6 +24,9 @@
#include <osinfo/osinfo.h>
+#include <stdlib.h>
+#include <string.h>
+
G_DEFINE_ABSTRACT_TYPE (OsinfoProduct, osinfo_product, OSINFO_TYPE_ENTITY);
#define OSINFO_PRODUCT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), OSINFO_TYPE_PRODUCT, OsinfoProductPrivate))
@@ -280,6 +283,51 @@ const gchar *osinfo_product_get_name(OsinfoProduct *prod)
return osinfo_entity_get_param_value(OSINFO_ENTITY(prod), OSINFO_PRODUCT_PROP_NAME);
}
+const gchar *osinfo_product_get_release_date_string(OsinfoProduct *prod)
+{
+ return osinfo_entity_get_param_value(OSINFO_ENTITY(prod), OSINFO_PRODUCT_PROP_RELEASE_DATE);
+}
+
+const gchar *osinfo_product_get_eol_date_string(OsinfoProduct *prod)
+{
+ return osinfo_entity_get_param_value(OSINFO_ENTITY(prod), OSINFO_PRODUCT_PROP_EOL_DATE);
+}
+
+
+static GDate *date_from_string(const gchar *str)
+{
+ int y, m, d;
+ const gchar *tmp;
+
+ y = strtoll(str, NULL, 10);
+ tmp = strchr(str, '-');
+ m = strtoll(tmp+1, NULL, 10);
+ tmp = strchr(tmp+1, '-');
+ d = strtoll(tmp+1, NULL, 10);
+ return g_date_new_dmy(d,m,y);
+}
+
+GDate *osinfo_product_get_release_date(OsinfoProduct *prod)
+{
+ const gchar *str = osinfo_product_get_release_date_string(prod);
+ if (!str)
+ return NULL;
+
+ return date_from_string(str);
+}
+
+
+GDate *osinfo_product_get_eol_date(OsinfoProduct *prod)
+{
+ const gchar *str = osinfo_product_get_eol_date_string(prod);
+ if (!str)
+ return NULL;
+
+ return date_from_string(str);
+}
+
+
+
/*
* Local variables:
* indent-tabs-mode: nil
diff --git a/osinfo/osinfo_product.h b/osinfo/osinfo_product.h
index b06ffe0..4db6df9 100644
--- a/osinfo/osinfo_product.h
+++ b/osinfo/osinfo_product.h
@@ -44,10 +44,12 @@ typedef struct _OsinfoProductClass OsinfoProductClass;
typedef struct _OsinfoProductPrivate OsinfoProductPrivate;
-#define OSINFO_PRODUCT_PROP_VENDOR "vendor"
-#define OSINFO_PRODUCT_PROP_VERSION "version"
-#define OSINFO_PRODUCT_PROP_SHORT_ID "short-id"
-#define OSINFO_PRODUCT_PROP_NAME "name"
+#define OSINFO_PRODUCT_PROP_VENDOR "vendor"
+#define OSINFO_PRODUCT_PROP_VERSION "version"
+#define OSINFO_PRODUCT_PROP_SHORT_ID "short-id"
+#define OSINFO_PRODUCT_PROP_NAME "name"
+#define OSINFO_PRODUCT_PROP_RELEASE_DATE "release-date"
+#define OSINFO_PRODUCT_PROP_EOL_DATE "eol-date"
/* object */
struct _OsinfoProduct
@@ -86,6 +88,12 @@ const gchar *osinfo_product_get_version(OsinfoProduct *prod);
const gchar *osinfo_product_get_short_id(OsinfoProduct *prod);
const gchar *osinfo_product_get_name(OsinfoProduct *prod);
+const gchar *osinfo_product_get_release_date_string(OsinfoProduct *prod);
+const gchar *osinfo_product_get_eol_date_string(OsinfoProduct *prod);
+GDate *osinfo_product_get_release_date(OsinfoProduct *prod);
+GDate *osinfo_product_get_eol_date(OsinfoProduct *prod);
+
+
#endif /* __OSINFO_PRODUCT_H__ */
/*
* Local variables:
diff --git a/osinfo/osinfo_productfilter.c b/osinfo/osinfo_productfilter.c
index c1f9983..eba8cc1 100644
--- a/osinfo/osinfo_productfilter.c
+++ b/osinfo/osinfo_productfilter.c
@@ -45,6 +45,8 @@ struct _OsinfoProductFilterPrivate
// Value: GList of OsinfoProduct *
// Note: Only used when productfiltering OsinfoProduct objects
GHashTable *productConstraints;
+
+ GDate *supportDate;
};
static void osinfo_productfilter_finalize (GObject *object);
@@ -197,6 +199,22 @@ GList *osinfo_productfilter_get_product_constraint_values(OsinfoProductFilter *p
}
+void osinfo_productfilter_add_support_date_constraint(OsinfoProductFilter *productfilter, GDate *when)
+{
+ g_return_if_fail(OSINFO_IS_PRODUCTFILTER(productfilter));
+
+ if (productfilter->priv->supportDate)
+ g_date_free(productfilter->priv->supportDate);
+ productfilter->priv->supportDate = NULL;
+ if (when) {
+ productfilter->priv->supportDate = g_date_new_dmy(g_date_get_day(when),
+ g_date_get_month(when),
+ g_date_get_year(when));
+ }
+}
+
+
+
struct osinfo_productfilter_match_args {
OsinfoProductFilter *productfilter;
OsinfoEntity *entity;
@@ -256,6 +274,20 @@ static gboolean osinfo_productfilter_matches_default(OsinfoFilter *filter, Osinf
osinfo_productfilter_match_product_iterator,
&args);
+ if (productfilter->priv->supportDate) {
+ GDate *when = productfilter->priv->supportDate;
+ GDate *release = osinfo_product_get_release_date(OSINFO_PRODUCT(entity));
+ GDate *eol = osinfo_product_get_eol_date(OSINFO_PRODUCT(entity));
+
+ if (release &&
+ (g_date_compare(release, when) > 0))
+ return FALSE;
+
+ if (eol &&
+ (g_date_compare(eol, when) < 0))
+ return FALSE;
+ }
+
return args.matched;
}
diff --git a/osinfo/osinfo_productfilter.h b/osinfo/osinfo_productfilter.h
index 574c1fc..a0adf22 100644
--- a/osinfo/osinfo_productfilter.h
+++ b/osinfo/osinfo_productfilter.h
@@ -70,6 +70,8 @@ void osinfo_productfilter_clear_product_constraints(OsinfoProductFilter *product
GList *osinfo_productfilter_get_product_constraint_values(OsinfoProductFilter *productfilter, OsinfoProductRelationship relshp);
+void osinfo_productfilter_add_support_date_constraint(OsinfoProductFilter *productfilter, GDate *when);
+
#endif /* __OSINFO_PRODUCTFILTER_H__ */
/*
* Local variables:
diff --git a/test/test-product.c b/test/test-product.c
index 31796c2..1f49227 100644
--- a/test/test-product.c
+++ b/test/test-product.c
@@ -88,6 +88,82 @@ END_TEST
+START_TEST(test_supportdate)
+{
+ OsinfoProductList *products = osinfo_productlist_new();
+ OsinfoProduct *product1 = osinfo_dummy_new("pony");
+ OsinfoProduct *product2 = osinfo_dummy_new("donkey");
+ OsinfoProduct *product3 = osinfo_dummy_new("wathog");
+ OsinfoProduct *product4 = osinfo_dummy_new("aardvark");
+ OsinfoProductFilter *filter = osinfo_productfilter_new();
+ OsinfoProductList *tmp;
+ GDate *date;
+
+ osinfo_list_add(OSINFO_LIST(products), OSINFO_ENTITY(product1));
+ osinfo_list_add(OSINFO_LIST(products), OSINFO_ENTITY(product2));
+ osinfo_list_add(OSINFO_LIST(products), OSINFO_ENTITY(product3));
+ osinfo_list_add(OSINFO_LIST(products), OSINFO_ENTITY(product4));
+
+ osinfo_entity_set_param(OSINFO_ENTITY(product2), OSINFO_PRODUCT_PROP_RELEASE_DATE, "2000-01-01");
+
+ osinfo_entity_set_param(OSINFO_ENTITY(product3), OSINFO_PRODUCT_PROP_EOL_DATE, "2010-01-01");
+
+ osinfo_entity_set_param(OSINFO_ENTITY(product4), OSINFO_PRODUCT_PROP_RELEASE_DATE, "2005-01-01");
+ osinfo_entity_set_param(OSINFO_ENTITY(product4), OSINFO_PRODUCT_PROP_EOL_DATE, "2006-01-01");
+
+ /* Product 1 & 3 */
+ date = g_date_new_dmy(31, 12, 1999);
+ osinfo_productfilter_add_support_date_constraint(filter, date);
+ tmp = osinfo_productlist_new_filtered(products, OSINFO_FILTER(filter));
+ fail_unless(osinfo_list_get_length(OSINFO_LIST(tmp)) == 2, "2 products");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 0) == (OsinfoEntity*)product1, "Got product 1");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 1) == (OsinfoEntity*)product3, "Got product 3");
+ g_object_unref(tmp);
+ g_date_free(date);
+
+ /* Product 1, 2 & 3 */
+ date = g_date_new_dmy(01, 01, 2000);
+ osinfo_productfilter_add_support_date_constraint(filter, date);
+ tmp = osinfo_productlist_new_filtered(products, OSINFO_FILTER(filter));
+ fail_unless(osinfo_list_get_length(OSINFO_LIST(tmp)) == 3, "3 products");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 0) == (OsinfoEntity*)product1, "Got product 1");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 1) == (OsinfoEntity*)product2, "Got product 2");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 2) == (OsinfoEntity*)product3, "Got product 3");
+ g_object_unref(tmp);
+ g_date_free(date);
+
+ /* Product 1, 2 & 3 */
+ date = g_date_new_dmy(01, 01, 2010);
+ osinfo_productfilter_add_support_date_constraint(filter, date);
+ tmp = osinfo_productlist_new_filtered(products, OSINFO_FILTER(filter));
+ fail_unless(osinfo_list_get_length(OSINFO_LIST(tmp)) == 3, "3 products");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 0) == (OsinfoEntity*)product1, "Got product 1");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 1) == (OsinfoEntity*)product2, "Got product 2");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 2) == (OsinfoEntity*)product3, "Got product 3");
+ g_object_unref(tmp);
+ g_date_free(date);
+
+ /* Product 1, 2 & 3 */
+ date = g_date_new_dmy(01, 05, 2005);
+ osinfo_productfilter_add_support_date_constraint(filter, date);
+ tmp = osinfo_productlist_new_filtered(products, OSINFO_FILTER(filter));
+ fail_unless(osinfo_list_get_length(OSINFO_LIST(tmp)) == 4, "4 products");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 0) == (OsinfoEntity*)product1, "Got product 1");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 1) == (OsinfoEntity*)product2, "Got product 2");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 2) == (OsinfoEntity*)product3, "Got product 3");
+ fail_unless(osinfo_list_get_nth(OSINFO_LIST(tmp), 3) == (OsinfoEntity*)product4, "Got product 4");
+ g_object_unref(tmp);
+ g_date_free(date);
+
+ g_object_unref(product1);
+ g_object_unref(product2);
+ g_object_unref(product3);
+ g_object_unref(product4);
+}
+END_TEST
+
+
+
static Suite *
product_suite(void)
{
@@ -95,6 +171,7 @@ product_suite(void)
TCase *tc = tcase_create("Core");
tcase_add_test(tc, test_basic);
tcase_add_test(tc, test_relproduct);
+ tcase_add_test(tc, test_supportdate);
suite_add_tcase(s, tc);
return s;
}
--
1.7.7.6
More information about the virt-tools-list
mailing list