From 2f8090559ea77a7b58ff3b51e0c8bc6c0673ea7d Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Wed, 15 Jun 2011 13:55:59 -0700 Subject: [PATCH] add Python ptql re op impl --- bindings/python/_sigar.c | 40 ++++++++++++++++++++++++++++++++++++++-- bindings/python/sigar.py | 6 ++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/bindings/python/_sigar.c b/bindings/python/_sigar.c index 1aa6fc55..ba04aaeb 100644 --- a/bindings/python/_sigar.c +++ b/bindings/python/_sigar.c @@ -69,6 +69,42 @@ static void pysigar_free(PyObject *self) self->ob_type->tp_free((PyObject *)self); } +static int pysigar_ptql_re_impl(void *data, + char *haystack, char *needle) +{ + PyObject *name = PyString_FromString("sigar"); + PyObject *module = PyImport_Import(name); + PyObject *match, *args, *source, *regex, *result; + int matches = 0; + + match = PyObject_GetAttrString(module, "string_matches"); + + source = PyString_FromString(haystack); + regex = PyString_FromString(needle); + args = PyTuple_New(2); + PyTuple_SetItem(args, 0, source); /* steals source reference */ + PyTuple_SetItem(args, 1, regex); /* steals regex reference */ + + result = PyObject_CallObject(match, args); + + Py_DECREF(name); + Py_DECREF(module); + Py_DECREF(args); + Py_DECREF(match); + + if (result == NULL) { + if (PyErr_Occurred()) { + PyErr_Print(); + } + } + else { + matches = (result == Py_True); + Py_DECREF(result); + } + + return matches; +} + #define sigar_isdigit(c) \ (isdigit(((unsigned char)(c)))) @@ -103,9 +139,9 @@ static int pysigar_parse_pid(sigar_t *sigar, PyObject *args, long *pid) status = sigar_ptql_query_create(&query, ptql, &error); if (status == SIGAR_OK) { - /*sigar_ptql_re_impl_set(sigar, NULL, pysigar_ptql_re_impl);*/ + sigar_ptql_re_impl_set(sigar, NULL, pysigar_ptql_re_impl); status = sigar_ptql_query_find_process(sigar, query, (sigar_pid_t *)pid); - /*sigar_ptql_re_impl_set(sigar, NULL, NULL);*/ + sigar_ptql_re_impl_set(sigar, NULL, NULL); sigar_ptql_query_destroy(query); if (status == SIGAR_OK) { diff --git a/bindings/python/sigar.py b/bindings/python/sigar.py index 7982bdd6..f50b76fe 100644 --- a/bindings/python/sigar.py +++ b/bindings/python/sigar.py @@ -1,5 +1,6 @@ # # Copyright (c) 2007 Hyperic, Inc. +# Copyright (c) 2011 VMware, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,3 +16,8 @@ # from _sigar import * + +import re +#wrapper for pysigar_ptql_re_impl +def string_matches(source, regex): + return re.compile(regex).match(source) != None