b019997
diff --git a/.gitignore b/.gitignore
b019997
index 8fe6157..5a569ca 100644
b019997
--- a/.gitignore
b019997
+++ b/.gitignore
b019997
@@ -209,3 +209,4 @@ src/safefile/stamp-h1
b019997
 src/safefile/stamp-h2
b019997
 src/safefile/safe_id_range_list.h.in.tmp
b019997
 src/safefile/safe_id_range_list.h.tmp_out
b019997
+src/condor_contrib/python-bindings/tests_tmp
b019997
diff --git a/externals/bundles/boost/1.49.0/CMakeLists.txt b/externals/bundles/boost/1.49.0/CMakeLists.txt
b019997
index 8608ee6..dcba24b 100644
b019997
--- a/externals/bundles/boost/1.49.0/CMakeLists.txt
b019997
+++ b/externals/bundles/boost/1.49.0/CMakeLists.txt
b019997
@@ -28,6 +28,9 @@ if (NOT WINDOWS)
b019997
 	if (BUILD_TESTING) 
b019997
 	  set (BOOST_COMPONENTS unit_test_framework ${BOOST_COMPONENTS})
b019997
 	endif()
b019997
+        if (WITH_PYTHON_BINDINGS)
b019997
+          set (BOOST_COMPONENTS python ${BOOST_COMPONENTS})
b019997
+        endif()
b019997
 
b019997
     endif()
b019997
 
b019997
@@ -104,6 +107,9 @@ if (NOT PROPER) # AND (NOT Boost_FOUND OR SYSTEM_NOT_UP_TO_SNUFF) )
b019997
 	condor_pre_external( BOOST ${BOOST_FILENAME}-p2 "lib;${INCLUDE_LOC}" "done")
b019997
 
b019997
 	set(BOOST_MIN_BUILD_DEP --with-thread --with-test)
b019997
+        if (WITH_PYTHON_BINDINGS)
b019997
+          set(BOOST_MIN_BUILD_DEP --with-python)
b019997
+        endif()
b019997
 	set(BOOST_PATCH echo "nothing")
b019997
 	set(BOOST_INSTALL echo "nothing")
b019997
 	unset(BOOST_INCLUDE)
b019997
diff --git a/src/condor_contrib/CMakeLists.txt b/src/condor_contrib/CMakeLists.txt
b019997
index 52f14c0..41b9002 100644
b019997
--- a/src/condor_contrib/CMakeLists.txt
b019997
+++ b/src/condor_contrib/CMakeLists.txt
b019997
@@ -32,4 +32,5 @@ else(WANT_CONTRIB)
b019997
   add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/campus_factory")
b019997
   add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/bosco")
085a687
   add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/lark")
b019997
+  add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/python-bindings")
b019997
 endif(WANT_CONTRIB)
b019997
diff --git a/src/condor_contrib/python-bindings/CMakeLists.txt b/src/condor_contrib/python-bindings/CMakeLists.txt
b019997
new file mode 100644
b019997
index 0000000..50d8a29
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/CMakeLists.txt
b019997
@@ -0,0 +1,26 @@
b019997
+
b019997
+option(WITH_PYTHON_BINDINGS "Support for HTCondor python bindings" OFF)
b019997
+
b019997
+if ( WITH_PYTHON_BINDINGS )
b019997
+
b019997
+  set ( CMAKE_LIBRARY_PATH_ORIG ${CMAKE_LIBRARY_PATH} )
b019997
+  set ( CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /usr/lib64 )
b019997
+  find_package(PythonLibs REQUIRED)
b019997
+  set ( CMAKE_LIBRARY_PATH CMAKE_LIBRARY_PATH_ORIG)
b019997
+
b019997
+  include_directories(${PYTHON_INCLUDE_DIRS})
b019997
+
b019997
+  condor_shared_lib( pyclassad classad.cpp classad_wrapper.h exprtree_wrapper.h )
b019997
+  target_link_libraries( pyclassad classad ${PYTHON_LIBRARIES} -lboost_python )
b019997
+
b019997
+  condor_shared_lib( classad_module classad_module.cpp )
b019997
+  target_link_libraries( classad_module pyclassad -lboost_python ${PYTHON_LIBRARIES} )
b019997
+  set_target_properties(classad_module PROPERTIES PREFIX "" OUTPUT_NAME classad )
b019997
+
b019997
+  set_source_files_properties(dc_tool.cpp schedd.cpp PROPERTIES COMPILE_FLAGS -Wno-strict-aliasing)
b019997
+  condor_shared_lib( condor condor.cpp collector.cpp config.cpp daemon_and_ad_types.cpp dc_tool.cpp export_headers.h old_boost.h schedd.cpp secman.cpp )
b019997
+  target_link_libraries( condor pyclassad condor_utils -lboost_python ${PYTHON_LIBRARIES} )
b019997
+  set_target_properties( condor PROPERTIES PREFIX "" )
b019997
+
b019997
+endif ( WITH_PYTHON_BINDINGS )
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/classad.cpp b/src/condor_contrib/python-bindings/classad.cpp
b019997
new file mode 100644
b019997
index 0000000..4c2db18
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/classad.cpp
b019997
@@ -0,0 +1,341 @@
b019997
+
b019997
+#include <string>
b019997
+
b019997
+#include <classad/source.h>
b019997
+#include <classad/sink.h>
b019997
+
b019997
+#include "classad_wrapper.h"
b019997
+#include "exprtree_wrapper.h"
b019997
+
b019997
+
b019997
+ExprTreeHolder::ExprTreeHolder(const std::string &str)
b019997
+    : m_expr(NULL), m_owns(true)
b019997
+{
b019997
+    classad::ClassAdParser parser;
b019997
+    classad::ExprTree *expr = NULL;
b019997
+    if (!parser.ParseExpression(str, expr))
b019997
+    {
b019997
+        PyErr_SetString(PyExc_SyntaxError, "Unable to parse string into a ClassAd.");
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    m_expr = expr;
b019997
+}
b019997
+
b019997
+
b019997
+ExprTreeHolder::ExprTreeHolder(classad::ExprTree *expr)
b019997
+     : m_expr(expr), m_owns(false)
b019997
+{}
b019997
+
b019997
+
b019997
+ExprTreeHolder::~ExprTreeHolder()
b019997
+{
b019997
+    if (m_owns && m_expr) delete m_expr;
b019997
+}
b019997
+
b019997
+
b019997
+boost::python::object ExprTreeHolder::Evaluate() const
b019997
+{
b019997
+    if (!m_expr)
b019997
+    {
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Cannot operate on an invalid ExprTree");
b019997
+            boost::python::throw_error_already_set();
b019997
+    }
b019997
+    classad::Value value;
b019997
+    if (!m_expr->Evaluate(value)) {
b019997
+            PyErr_SetString(PyExc_SyntaxError, "Unable to evaluate expression");
b019997
+            boost::python::throw_error_already_set();
b019997
+    }
b019997
+    boost::python::object result;
b019997
+    std::string strvalue;
b019997
+    long long intvalue;
b019997
+    bool boolvalue;
b019997
+    double realvalue;
b019997
+    PyObject* obj;
b019997
+    switch (value.GetType())
b019997
+    {
b019997
+    case classad::Value::BOOLEAN_VALUE:
b019997
+        value.IsBooleanValue(boolvalue);
b019997
+        obj = boolvalue ? Py_True : Py_False;
b019997
+        result = boost::python::object(boost::python::handle<>(boost::python::borrowed(obj)));
b019997
+        break;
b019997
+    case classad::Value::STRING_VALUE:
b019997
+        value.IsStringValue(strvalue);
b019997
+        result = boost::python::str(strvalue);
b019997
+        break;
b019997
+    case classad::Value::ABSOLUTE_TIME_VALUE:
b019997
+    case classad::Value::INTEGER_VALUE:
b019997
+        value.IsIntegerValue(intvalue);
b019997
+        result = boost::python::long_(intvalue);
b019997
+        break;
b019997
+    case classad::Value::RELATIVE_TIME_VALUE:
b019997
+    case classad::Value::REAL_VALUE:
b019997
+        value.IsRealValue(realvalue);
b019997
+        result = boost::python::object(realvalue);
b019997
+        break;
b019997
+    case classad::Value::ERROR_VALUE:
b019997
+        result = boost::python::object(classad::Value::ERROR_VALUE);
b019997
+        break;
b019997
+    case classad::Value::UNDEFINED_VALUE:
b019997
+        result = boost::python::object(classad::Value::UNDEFINED_VALUE);
b019997
+        break;
b019997
+    default:
b019997
+        PyErr_SetString(PyExc_TypeError, "Unknown ClassAd value type.");
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    return result;
b019997
+}
b019997
+
b019997
+
b019997
+std::string ExprTreeHolder::toRepr()
b019997
+{
b019997
+    if (!m_expr)
b019997
+    {
b019997
+        PyErr_SetString(PyExc_RuntimeError, "Cannot operate on an invalid ExprTree");
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    classad::ClassAdUnParser up;
b019997
+    std::string ad_str;
b019997
+    up.Unparse(ad_str, m_expr);
b019997
+    return ad_str;
b019997
+}
b019997
+
b019997
+
b019997
+std::string ExprTreeHolder::toString()
b019997
+{
b019997
+    if (!m_expr)
b019997
+    {
b019997
+        PyErr_SetString(PyExc_RuntimeError, "Cannot operate on an invalid ExprTree");
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    classad::PrettyPrint pp;
b019997
+    std::string ad_str;
b019997
+    pp.Unparse(ad_str, m_expr);
b019997
+    return ad_str;
b019997
+}
b019997
+
b019997
+
b019997
+classad::ExprTree *ExprTreeHolder::get()
b019997
+{
b019997
+    if (!m_expr)
b019997
+    {
b019997
+        PyErr_SetString(PyExc_RuntimeError, "Cannot operate on an invalid ExprTree");
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    return m_expr->Copy();
b019997
+}
b019997
+
b019997
+AttrPairToSecond::result_type AttrPairToSecond::operator()(AttrPairToSecond::argument_type p) const
b019997
+{
b019997
+    ExprTreeHolder holder(p.second);
b019997
+    if (p.second->GetKind() == classad::ExprTree::LITERAL_NODE)
b019997
+    {
b019997
+        return holder.Evaluate();
b019997
+    }
b019997
+    boost::python::object result(holder);
b019997
+    return result;
b019997
+} 
b019997
+
b019997
+
b019997
+AttrPair::result_type AttrPair::operator()(AttrPair::argument_type p) const
b019997
+{
b019997
+    ExprTreeHolder holder(p.second);
b019997
+    boost::python::object result(holder);
b019997
+    if (p.second->GetKind() == classad::ExprTree::LITERAL_NODE)
b019997
+    {
b019997
+        result = holder.Evaluate();
b019997
+    }
b019997
+    return boost::python::make_tuple<std::string, boost::python::object>(p.first, result);
b019997
+}
b019997
+
b019997
+
b019997
+boost::python::object ClassAdWrapper::LookupWrap(const std::string &attr) const
b019997
+{
b019997
+    classad::ExprTree * expr = Lookup(attr);
b019997
+    if (!expr)
b019997
+    {
b019997
+        PyErr_SetString(PyExc_KeyError, attr.c_str());
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    if (expr->GetKind() == classad::ExprTree::LITERAL_NODE) return EvaluateAttrObject(attr);
b019997
+    ExprTreeHolder holder(expr);
b019997
+    boost::python::object result(holder);
b019997
+    return result;
b019997
+}
b019997
+
b019997
+boost::python::object ClassAdWrapper::LookupExpr(const std::string &attr) const
b019997
+{
b019997
+    classad::ExprTree * expr = Lookup(attr);
b019997
+    if (!expr)
b019997
+    {
b019997
+        PyErr_SetString(PyExc_KeyError, attr.c_str());
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    ExprTreeHolder holder(expr);
b019997
+    boost::python::object result(holder);
b019997
+    return result;
b019997
+}
b019997
+
b019997
+boost::python::object ClassAdWrapper::EvaluateAttrObject(const std::string &attr) const
b019997
+{
b019997
+    classad::ExprTree *expr;
b019997
+    if (!(expr = Lookup(attr))) {
b019997
+        PyErr_SetString(PyExc_KeyError, attr.c_str());
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    ExprTreeHolder holder(expr);
b019997
+    return holder.Evaluate();
b019997
+}
b019997
+
b019997
+
b019997
+void ClassAdWrapper::InsertAttrObject( const std::string &attr, boost::python::object value)
b019997
+{
b019997
+    boost::python::extract<ExprTreeHolder&> expr_obj(value);
b019997
+    if (expr_obj.check())
b019997
+    {
b019997
+        classad::ExprTree *expr = expr_obj().get();
b019997
+        Insert(attr, expr);
b019997
+        return;
b019997
+    }
b019997
+    boost::python::extract<classad::Value::ValueType> value_enum_obj(value);
b019997
+    if (value_enum_obj.check())
b019997
+    {
b019997
+        classad::Value::ValueType value_enum = value_enum_obj();
b019997
+        classad::Value classad_value;
b019997
+        if (value_enum == classad::Value::ERROR_VALUE)
b019997
+        {
b019997
+            classad_value.SetErrorValue();
b019997
+            classad::ExprTree *lit = classad::Literal::MakeLiteral(classad_value);
b019997
+            Insert(attr, lit);
b019997
+        }
b019997
+        else if (value_enum == classad::Value::UNDEFINED_VALUE)
b019997
+        {
b019997
+            classad_value.SetUndefinedValue();
b019997
+            classad::ExprTree *lit = classad::Literal::MakeLiteral(classad_value);
b019997
+            if (!Insert(attr, lit))
b019997
+            {
b019997
+                PyErr_SetString(PyExc_AttributeError, attr.c_str());
b019997
+                boost::python::throw_error_already_set();
b019997
+            }
b019997
+        }
b019997
+        return;
b019997
+    }
b019997
+    if (PyString_Check(value.ptr()))
b019997
+    {
b019997
+        std::string cppvalue = boost::python::extract<std::string>(value);
b019997
+        if (!InsertAttr(attr, cppvalue))
b019997
+        {
b019997
+            PyErr_SetString(PyExc_AttributeError, attr.c_str());
b019997
+            boost::python::throw_error_already_set();
b019997
+        }
b019997
+        return;
b019997
+    }
b019997
+    if (PyLong_Check(value.ptr()))
b019997
+    {
b019997
+        long long cppvalue = boost::python::extract<long long>(value);
b019997
+        if (!InsertAttr(attr, cppvalue))
b019997
+        {
b019997
+            PyErr_SetString(PyExc_AttributeError, attr.c_str());
b019997
+            boost::python::throw_error_already_set();
b019997
+        }
b019997
+        return;
b019997
+    }
b019997
+    if (PyInt_Check(value.ptr()))
b019997
+    {
b019997
+        long int cppvalue = boost::python::extract<long int>(value);
b019997
+        if (!InsertAttr(attr, cppvalue))
b019997
+        {
b019997
+            PyErr_SetString(PyExc_AttributeError, attr.c_str());
b019997
+            boost::python::throw_error_already_set();
b019997
+        }
b019997
+        return;
b019997
+    }
b019997
+    if (PyFloat_Check(value.ptr()))
b019997
+    {
b019997
+        double cppvalue = boost::python::extract<double>(value);
b019997
+        if (!InsertAttr(attr, cppvalue))
b019997
+        {
b019997
+            PyErr_SetString(PyExc_AttributeError, attr.c_str());
b019997
+            boost::python::throw_error_already_set();
b019997
+        }
b019997
+        return;
b019997
+    }
b019997
+    PyErr_SetString(PyExc_TypeError, "Unknown ClassAd value type.");
b019997
+    boost::python::throw_error_already_set();
b019997
+}
b019997
+
b019997
+
b019997
+std::string ClassAdWrapper::toRepr()
b019997
+{
b019997
+    classad::ClassAdUnParser up;
b019997
+    std::string ad_str;
b019997
+    up.Unparse(ad_str, this);
b019997
+    return ad_str;
b019997
+}
b019997
+
b019997
+
b019997
+std::string ClassAdWrapper::toString()
b019997
+{
b019997
+    classad::PrettyPrint pp;
b019997
+    std::string ad_str;
b019997
+    pp.Unparse(ad_str, this);
b019997
+    return ad_str;
b019997
+}
b019997
+
b019997
+std::string ClassAdWrapper::toOldString()
b019997
+{
b019997
+    classad::ClassAdUnParser pp;
b019997
+    std::string ad_str;
b019997
+    pp.SetOldClassAd(true);
b019997
+    pp.Unparse(ad_str, this);
b019997
+    return ad_str;
b019997
+}
b019997
+
b019997
+AttrKeyIter ClassAdWrapper::beginKeys()
b019997
+{
b019997
+    return AttrKeyIter(begin());
b019997
+}
b019997
+
b019997
+
b019997
+AttrKeyIter ClassAdWrapper::endKeys()
b019997
+{
b019997
+    return AttrKeyIter(end());
b019997
+}
b019997
+
b019997
+AttrValueIter ClassAdWrapper::beginValues()
b019997
+{
b019997
+    return AttrValueIter(begin());
b019997
+}
b019997
+
b019997
+AttrValueIter ClassAdWrapper::endValues()
b019997
+{
b019997
+    return AttrValueIter(end());
b019997
+}
b019997
+
b019997
+AttrItemIter ClassAdWrapper::beginItems()
b019997
+{
b019997
+    return AttrItemIter(begin());
b019997
+}
b019997
+
b019997
+
b019997
+AttrItemIter ClassAdWrapper::endItems()
b019997
+{
b019997
+    return AttrItemIter(end());
b019997
+}
b019997
+
b019997
+
b019997
+ClassAdWrapper::ClassAdWrapper() : classad::ClassAd() {}
b019997
+
b019997
+
b019997
+ClassAdWrapper::ClassAdWrapper(const std::string &str)
b019997
+{
b019997
+    classad::ClassAdParser parser;
b019997
+    classad::ClassAd *result = parser.ParseClassAd(str);
b019997
+    if (!result)
b019997
+    {
b019997
+        PyErr_SetString(PyExc_SyntaxError, "Unable to parse string into a ClassAd.");
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    CopyFrom(*result);
b019997
+    result;
b019997
+}
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/classad_module.cpp b/src/condor_contrib/python-bindings/classad_module.cpp
b019997
new file mode 100644
b019997
index 0000000..b3f1970
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/classad_module.cpp
b019997
@@ -0,0 +1,145 @@
b019997
+
b019997
+#include <boost/python.hpp>
b019997
+#include <classad/source.h>
b019997
+
b019997
+#include "classad_wrapper.h"
b019997
+#include "exprtree_wrapper.h"
b019997
+
b019997
+using namespace boost::python;
b019997
+
b019997
+
b019997
+Py_ssize_t py_len(boost::python::object const& obj)
b019997
+{
b019997
+    Py_ssize_t result = PyObject_Length(obj.ptr());
b019997
+    if (PyErr_Occurred()) boost::python::throw_error_already_set();
b019997
+    return result;
b019997
+}
b019997
+
b019997
+
b019997
+std::string ClassadLibraryVersion()
b019997
+{
b019997
+    std::string val;
b019997
+    classad::ClassAdLibraryVersion(val);
b019997
+    return val;
b019997
+}
b019997
+
b019997
+
b019997
+ClassAdWrapper *parseString(const std::string &str)
b019997
+{
b019997
+    classad::ClassAdParser parser;
b019997
+    classad::ClassAd *result = parser.ParseClassAd(str);
b019997
+    if (!result)
b019997
+    {
b019997
+        PyErr_SetString(PyExc_SyntaxError, "Unable to parse string into a ClassAd.");
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    ClassAdWrapper * wrapper_result = new ClassAdWrapper();
b019997
+    wrapper_result->CopyFrom(*result);
b019997
+    delete result;
b019997
+    return wrapper_result;
b019997
+}
b019997
+
b019997
+
b019997
+ClassAdWrapper *parseFile(FILE *stream)
b019997
+{
b019997
+    classad::ClassAdParser parser;
b019997
+    classad::ClassAd *result = parser.ParseClassAd(stream);
b019997
+    if (!result)
b019997
+    {
b019997
+        PyErr_SetString(PyExc_SyntaxError, "Unable to parse input stream into a ClassAd.");
b019997
+        boost::python::throw_error_already_set();
b019997
+    }
b019997
+    ClassAdWrapper * wrapper_result = new ClassAdWrapper();
b019997
+    wrapper_result->CopyFrom(*result);
b019997
+    delete result;
b019997
+    return wrapper_result;
b019997
+}
b019997
+
b019997
+ClassAdWrapper *parseOld(object input)
b019997
+{
b019997
+    ClassAdWrapper * wrapper = new ClassAdWrapper();
b019997
+    object input_list;
b019997
+    extract<std::string> input_extract(input);
b019997
+    if (input_extract.check())
b019997
+    {
b019997
+        input_list = input.attr("splitlines")();
b019997
+    }
b019997
+    else
b019997
+    {
b019997
+        input_list = input.attr("readlines")();
b019997
+    }
b019997
+    unsigned input_len = py_len(input_list);
b019997
+    for (unsigned idx=0; idx
b019997
+    {
b019997
+        object line = input_list[idx].attr("strip")();
b019997
+        if (line.attr("startswith")("#"))
b019997
+        {
b019997
+            continue;
b019997
+        }
b019997
+        std::string line_str = extract<std::string>(line);
b019997
+        if (!wrapper->Insert(line_str))
b019997
+        {
b019997
+            PyErr_SetString(PyExc_SyntaxError, line_str.c_str());
b019997
+            throw_error_already_set();
b019997
+        }
b019997
+    }
b019997
+    return wrapper;
b019997
+}
b019997
+
b019997
+void *convert_to_FILEptr(PyObject* obj) {
b019997
+    return PyFile_Check(obj) ? PyFile_AsFile(obj) : 0;
b019997
+}
b019997
+
b019997
+BOOST_PYTHON_MODULE(classad)
b019997
+{
b019997
+    using namespace boost::python;
b019997
+
b019997
+    def("version", ClassadLibraryVersion, "Return the version of the linked ClassAd library.");
b019997
+
b019997
+    def("parse", parseString, return_value_policy<manage_new_object>());
b019997
+    def("parse", parseFile, return_value_policy<manage_new_object>(),
b019997
+        "Parse input into a ClassAd.\n"
b019997
+        ":param input: A string or a file pointer.\n"
b019997
+        ":return: A ClassAd object.");
b019997
+    def("parseOld", parseOld, return_value_policy<manage_new_object>(),
b019997
+        "Parse old ClassAd format input into a ClassAd.\n"
b019997
+        ":param input: A string or a file pointer.\n"
b019997
+        ":return: A ClassAd object.");
b019997
+
b019997
+    class_<ClassAdWrapper, boost::noncopyable>("ClassAd", "A classified advertisement.")
b019997
+        .def(init<std::string>())
b019997
+        .def("__delitem__", &ClassAdWrapper::Delete)
b019997
+        .def("__getitem__", &ClassAdWrapper::LookupWrap)
b019997
+        .def("eval", &ClassAdWrapper::EvaluateAttrObject, "Evaluate the ClassAd attribute to a python object.")
b019997
+        .def("__setitem__", &ClassAdWrapper::InsertAttrObject)
b019997
+        .def("__str__", &ClassAdWrapper::toString)
b019997
+        .def("__repr__", &ClassAdWrapper::toRepr)
b019997
+        // I see no way to use the SetParentScope interface safely.
b019997
+        // Delay exposing it to python until we absolutely have to!
b019997
+        //.def("setParentScope", &ClassAdWrapper::SetParentScope)
b019997
+        .def("__iter__", boost::python::range(&ClassAdWrapper::beginKeys, &ClassAdWrapper::endKeys))
b019997
+        .def("keys", boost::python::range(&ClassAdWrapper::beginKeys, &ClassAdWrapper::endKeys))
b019997
+        .def("values", boost::python::range(&ClassAdWrapper::beginValues, &ClassAdWrapper::endValues))
b019997
+        .def("items", boost::python::range(&ClassAdWrapper::beginItems, &ClassAdWrapper::endItems))
b019997
+        .def("__len__", &ClassAdWrapper::size)
b019997
+        .def("lookup", &ClassAdWrapper::LookupExpr, "Lookup an attribute and return a ClassAd expression.  This method will not attempt to evaluate it to a python object.")
b019997
+        .def("printOld", &ClassAdWrapper::toOldString, "Represent this ClassAd as a string in the \"old ClassAd\" format.")
b019997
+        ;
b019997
+
b019997
+    class_<ExprTreeHolder>("ExprTree", "An expression in the ClassAd language", init<std::string>())
b019997
+        .def("__str__", &ExprTreeHolder::toString)
b019997
+        .def("__repr__", &ExprTreeHolder::toRepr)
b019997
+        .def("eval", &ExprTreeHolder::Evaluate)
b019997
+        ;
b019997
+
b019997
+    register_ptr_to_python< boost::shared_ptr<ClassAdWrapper> >();
b019997
+
b019997
+    boost::python::enum_<classad::Value::ValueType>("Value")
b019997
+        .value("Error", classad::Value::ERROR_VALUE)
b019997
+        .value("Undefined", classad::Value::UNDEFINED_VALUE)
b019997
+        ;
b019997
+
b019997
+    boost::python::converter::registry::insert(convert_to_FILEptr,
b019997
+        boost::python::type_id<FILE>());
b019997
+}
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/classad_wrapper.h b/src/condor_contrib/python-bindings/classad_wrapper.h
b019997
new file mode 100644
b019997
index 0000000..96600c3
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/classad_wrapper.h
b019997
@@ -0,0 +1,72 @@
b019997
+
b019997
+#ifndef __CLASSAD_WRAPPER_H_
b019997
+#define __CLASSAD_WRAPPER_H_
b019997
+
b019997
+#include <classad/classad.h>
b019997
+#include <boost/python.hpp>
b019997
+#include <boost/iterator/transform_iterator.hpp>
b019997
+
b019997
+struct AttrPairToFirst :
b019997
+  public std::unary_function<std::pair<std::string, classad::ExprTree*> const&, std::string>
b019997
+{
b019997
+  AttrPairToFirst::result_type operator()(AttrPairToFirst::argument_type p) const
b019997
+  {
b019997
+    return p.first;
b019997
+  }
b019997
+};
b019997
+
b019997
+typedef boost::transform_iterator<AttrPairToFirst, classad::AttrList::iterator> AttrKeyIter;
b019997
+
b019997
+class ExprTreeHolder;
b019997
+
b019997
+struct AttrPairToSecond :
b019997
+  public std::unary_function<std::pair<std::string, classad::ExprTree*> const&, boost::python::object>
b019997
+{   
b019997
+  AttrPairToSecond::result_type operator()(AttrPairToSecond::argument_type p) const;
b019997
+};
b019997
+
b019997
+typedef boost::transform_iterator<AttrPairToSecond, classad::AttrList::iterator> AttrValueIter;
b019997
+
b019997
+struct AttrPair :
b019997
+  public std::unary_function<std::pair<std::string, classad::ExprTree*> const&, boost::python::object>
b019997
+{
b019997
+  AttrPair::result_type operator()(AttrPair::argument_type p) const;
b019997
+};
b019997
+
b019997
+typedef boost::transform_iterator<AttrPair, classad::AttrList::iterator> AttrItemIter;
b019997
+
b019997
+struct ClassAdWrapper : classad::ClassAd, boost::python::wrapper<classad::ClassAd>
b019997
+{
b019997
+    boost::python::object LookupWrap( const std::string &attr) const;
b019997
+
b019997
+    boost::python::object EvaluateAttrObject(const std::string &attr) const;
b019997
+
b019997
+    void InsertAttrObject( const std::string &attr, boost::python::object value);
b019997
+
b019997
+    boost::python::object LookupExpr(const std::string &attr) const;
b019997
+
b019997
+    std::string toRepr();
b019997
+
b019997
+    std::string toString();
b019997
+
b019997
+    std::string toOldString();
b019997
+
b019997
+    AttrKeyIter beginKeys();
b019997
+
b019997
+    AttrKeyIter endKeys();
b019997
+
b019997
+    AttrValueIter beginValues();
b019997
+
b019997
+    AttrValueIter endValues();
b019997
+
b019997
+    AttrItemIter beginItems();
b019997
+
b019997
+    AttrItemIter endItems();
b019997
+
b019997
+    ClassAdWrapper();
b019997
+
b019997
+    ClassAdWrapper(const std::string &str);
b019997
+};
b019997
+
b019997
+#endif
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/collector.cpp b/src/condor_contrib/python-bindings/collector.cpp
b019997
new file mode 100644
b019997
index 0000000..3c4fa39
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/collector.cpp
b019997
@@ -0,0 +1,329 @@
b019997
+
b019997
+#include "condor_adtypes.h"
b019997
+#include "dc_collector.h"
b019997
+#include "condor_version.h"
b019997
+
b019997
+#include <memory>
b019997
+#include <boost/python.hpp>
b019997
+
b019997
+#include "old_boost.h"
b019997
+#include "classad_wrapper.h"
b019997
+
b019997
+using namespace boost::python;
b019997
+
b019997
+AdTypes convert_to_ad_type(daemon_t d_type)
b019997
+{
b019997
+    AdTypes ad_type = NO_AD;
b019997
+    switch (d_type)
b019997
+    {
b019997
+    case DT_MASTER:
b019997
+        ad_type = MASTER_AD;
b019997
+        break;
b019997
+    case DT_STARTD:
b019997
+        ad_type = STARTD_AD;
b019997
+        break;
b019997
+    case DT_SCHEDD:
b019997
+        ad_type = SCHEDD_AD;
b019997
+        break;
b019997
+    case DT_NEGOTIATOR:
b019997
+        ad_type = NEGOTIATOR_AD;
b019997
+        break;
b019997
+    case DT_COLLECTOR:
b019997
+        ad_type = COLLECTOR_AD;
b019997
+        break;
b019997
+    default:
b019997
+        PyErr_SetString(PyExc_ValueError, "Unknown daemon type.");
b019997
+        throw_error_already_set();
b019997
+    }
b019997
+    return ad_type;
b019997
+}
b019997
+
b019997
+struct Collector {
b019997
+
b019997
+    Collector(const std::string &pool="")
b019997
+      : m_collectors(NULL)
b019997
+    {
b019997
+        if (pool.size())
b019997
+            m_collectors = CollectorList::create(pool.c_str());
b019997
+        else
b019997
+            m_collectors = CollectorList::create();
b019997
+    }
b019997
+
b019997
+    ~Collector()
b019997
+    {
b019997
+        if (m_collectors) delete m_collectors;
b019997
+    }
b019997
+
b019997
+    object query(AdTypes ad_type, const std::string &constraint, list attrs)
b019997
+    {
b019997
+        CondorQuery query(ad_type);
b019997
+        if (constraint.length())
b019997
+        {
b019997
+            query.addANDConstraint(constraint.c_str());
b019997
+        }
b019997
+        std::vector<const char *> attrs_char;
b019997
+        std::vector<std::string> attrs_str;
b019997
+        int len_attrs = py_len(attrs);
b019997
+        if (len_attrs)
b019997
+        {
b019997
+            attrs_str.reserve(len_attrs);
b019997
+            attrs_char.reserve(len_attrs+1);
b019997
+            attrs_char[len_attrs] = NULL;
b019997
+            for (int i=0; i
b019997
+            {
b019997
+                std::string str = extract<std::string>(attrs[i]);
b019997
+                attrs_str.push_back(str);
b019997
+                attrs_char[i] = attrs_str[i].c_str();
b019997
+            }
b019997
+            query.setDesiredAttrs(&attrs_char[0]);
b019997
+        }
b019997
+        ClassAdList adList;
b019997
+
b019997
+        QueryResult result = m_collectors->query(query, adList, NULL);
b019997
+
b019997
+        switch (result)
b019997
+        {
b019997
+        case Q_OK:
b019997
+            break;
b019997
+        case Q_INVALID_CATEGORY:
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Category not supported by query type.");
b019997
+            boost::python::throw_error_already_set();
b019997
+        case Q_MEMORY_ERROR:
b019997
+            PyErr_SetString(PyExc_MemoryError, "Memory allocation error.");
b019997
+            boost::python::throw_error_already_set();
b019997
+        case Q_PARSE_ERROR:
b019997
+            PyErr_SetString(PyExc_SyntaxError, "Query constraints could not be parsed.");
b019997
+            boost::python::throw_error_already_set();
b019997
+        case Q_COMMUNICATION_ERROR:
b019997
+            PyErr_SetString(PyExc_IOError, "Failed communication with collector.");
b019997
+            boost::python::throw_error_already_set();
b019997
+        case Q_INVALID_QUERY:
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Invalid query.");
b019997
+            boost::python::throw_error_already_set();
b019997
+        case Q_NO_COLLECTOR_HOST:
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Unable to determine collector host.");
b019997
+            boost::python::throw_error_already_set();
b019997
+        default:
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Unknown error from collector query.");
b019997
+            boost::python::throw_error_already_set();
b019997
+        }
b019997
+
b019997
+        list retval;
b019997
+        ClassAd * ad;
b019997
+        adList.Open();
b019997
+        while ((ad = adList.Next()))
b019997
+        {
b019997
+            boost::shared_ptr<ClassAdWrapper> wrapper(new ClassAdWrapper());
b019997
+            wrapper->CopyFrom(*ad);
b019997
+            retval.append(wrapper);
b019997
+        }
b019997
+        return retval;
b019997
+    }
b019997
+
b019997
+    object locateAll(daemon_t d_type)
b019997
+    {
b019997
+        AdTypes ad_type = convert_to_ad_type(d_type);
b019997
+        return query(ad_type, "", list());
b019997
+    }
b019997
+
b019997
+    object locate(daemon_t d_type, const std::string &name)
b019997
+    {
b019997
+        std::string constraint = ATTR_NAME " =?= \"" + name + "\"";
b019997
+        object result = query(convert_to_ad_type(d_type), constraint, list());
b019997
+        if (py_len(result) >= 1) {
b019997
+            return result[0];
b019997
+        }
b019997
+        PyErr_SetString(PyExc_ValueError, "Unable to find daemon.");
b019997
+        throw_error_already_set();
b019997
+        return object();
b019997
+    }
b019997
+
b019997
+    ClassAdWrapper *locateLocal(daemon_t d_type)
b019997
+    {
b019997
+        Daemon my_daemon( d_type, 0, 0 );
b019997
+
b019997
+        ClassAdWrapper *wrapper = new ClassAdWrapper();
b019997
+        if (my_daemon.locate())
b019997
+        {
b019997
+            classad::ClassAd *daemonAd;
b019997
+            if ((daemonAd = my_daemon.daemonAd()))
b019997
+            {
b019997
+                wrapper->CopyFrom(*daemonAd);
b019997
+            }
b019997
+            else
b019997
+            {
b019997
+                std::string addr = my_daemon.addr();
b019997
+                if (!my_daemon.addr() || !wrapper->InsertAttr(ATTR_MY_ADDRESS, addr))
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_RuntimeError, "Unable to locate daemon address.");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+                std::string name = my_daemon.name() ? my_daemon.name() : "Unknown";
b019997
+                if (!wrapper->InsertAttr(ATTR_NAME, name))
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_RuntimeError, "Unable to insert daemon name.");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+                std::string hostname = my_daemon.fullHostname() ? my_daemon.fullHostname() : "Unknown";
b019997
+                if (!wrapper->InsertAttr(ATTR_MACHINE, hostname))
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_RuntimeError, "Unable to insert daemon hostname.");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+                std::string version = my_daemon.version() ? my_daemon.version() : "";
b019997
+                if (!wrapper->InsertAttr(ATTR_VERSION, version))
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_RuntimeError, "Unable to insert daemon version.");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+                const char * my_type = AdTypeToString(convert_to_ad_type(d_type));
b019997
+                if (!my_type)
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_ValueError, "Unable to determined daemon type.");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+                std::string my_type_str = my_type;
b019997
+                if (!wrapper->InsertAttr(ATTR_MY_TYPE, my_type_str))
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_RuntimeError, "Unable to insert daemon type.");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+                std::string cversion = CondorVersion(); std::string platform = CondorPlatform();
b019997
+                if (!wrapper->InsertAttr(ATTR_VERSION, cversion) || !wrapper->InsertAttr(ATTR_PLATFORM, platform))
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_RuntimeError, "Unable to insert HTCondor version.");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+            }
b019997
+        }
b019997
+        else
b019997
+        {
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Unable to locate local daemon");
b019997
+            boost::python::throw_error_already_set();
b019997
+        }
b019997
+        return wrapper;
b019997
+    }
b019997
+
b019997
+
b019997
+    // Overloads for the Collector; can't be done in boost.python and provide
b019997
+    // docstrings.
b019997
+    object query0()
b019997
+    {
b019997
+        return query(ANY_AD, "", list());
b019997
+    }
b019997
+    object query1(AdTypes ad_type)
b019997
+    {
b019997
+        return query(ad_type, "", list());
b019997
+    }
b019997
+    object query2(AdTypes ad_type, const std::string &constraint)
b019997
+    {
b019997
+        return query(ad_type, constraint, list());
b019997
+    }
b019997
+
b019997
+    // TODO: this has crappy error handling when there are multiple collectors.
b019997
+    void advertise(list ads, const std::string &command_str="UPDATE_AD_GENERIC", bool use_tcp=false)
b019997
+    {
b019997
+        m_collectors->rewind();
b019997
+        Daemon *collector;
b019997
+        std::auto_ptr<Sock> sock;
b019997
+
b019997
+        int command = getCollectorCommandNum(command_str.c_str());
b019997
+        if (command == -1)
b019997
+        {
b019997
+            PyErr_SetString(PyExc_ValueError, ("Invalid command " + command_str).c_str());
b019997
+            throw_error_already_set();
b019997
+        }
b019997
+
b019997
+        if (command == UPDATE_STARTD_AD_WITH_ACK)
b019997
+        {
b019997
+            PyErr_SetString(PyExc_NotImplementedError, "Startd-with-ack protocol is not implemented at this time.");
b019997
+        }
b019997
+
b019997
+        int list_len = py_len(ads);
b019997
+        if (!list_len)
b019997
+            return;
b019997
+
b019997
+        compat_classad::ClassAd ad;
b019997
+        while (m_collectors->next(collector))
b019997
+        {
b019997
+            if(!collector->locate()) {
b019997
+                PyErr_SetString(PyExc_ValueError, "Unable to locate collector.");
b019997
+                throw_error_already_set();
b019997
+            }
b019997
+            int list_len = py_len(ads);
b019997
+            sock.reset();
b019997
+            for (int i=0; i
b019997
+            {
b019997
+                ClassAdWrapper &wrapper = extract<ClassAdWrapper &>(ads[i]);
b019997
+                ad.CopyFrom(wrapper);
b019997
+                if (use_tcp)
b019997
+                {
b019997
+                    if (!sock.get())
b019997
+                        sock.reset(collector->startCommand(command,Stream::reli_sock,20));
b019997
+                    else
b019997
+                    {
b019997
+                        sock->encode();
b019997
+                        sock->put(command);
b019997
+                    }
b019997
+                }
b019997
+                else
b019997
+                {
b019997
+                    sock.reset(collector->startCommand(command,Stream::safe_sock,20));
b019997
+                }
b019997
+                int result = 0;
b019997
+                if (sock.get()) {
b019997
+                    result += ad.put(*sock);
b019997
+                    result += sock->end_of_message();
b019997
+                }
b019997
+                if (result != 2) {
b019997
+                    PyErr_SetString(PyExc_ValueError, "Failed to advertise to collector");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+            }
b019997
+            sock->encode();
b019997
+            sock->put(DC_NOP);
b019997
+            sock->end_of_message();
b019997
+        }
b019997
+    }
b019997
+
b019997
+private:
b019997
+
b019997
+    CollectorList *m_collectors;
b019997
+
b019997
+};
b019997
+
b019997
+BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(advertise_overloads, advertise, 1, 3);
b019997
+
b019997
+void export_collector()
b019997
+{
b019997
+    class_<Collector>("Collector", "Client-side operations for the HTCondor collector")
b019997
+        .def(init<std::string>(":param pool: Name of collector to query; if not specified, uses the local one."))
b019997
+        .def("query", &Collector::query0)
b019997
+        .def("query", &Collector::query1)
b019997
+        .def("query", &Collector::query2)
b019997
+        .def("query", &Collector::query,
b019997
+            "Query the contents of a collector.\n"
b019997
+            ":param ad_type: Type of ad to return from the AdTypes enum; if not specified, uses ANY_AD.\n"
b019997
+            ":param constraint: A constraint for the ad query; defaults to true.\n"
b019997
+            ":param attrs: A list of attributes; if specified, the returned ads will be "
b019997
+            "projected along these attributes.\n"
b019997
+            ":return: A list of ads in the collector matching the constraint.")
b019997
+        .def("locate", &Collector::locateLocal, return_value_policy<manage_new_object>())
b019997
+        .def("locate", &Collector::locate,
b019997
+            "Query the collector for a particular daemon.\n"
b019997
+            ":param daemon_type: Type of daemon; must be from the DaemonTypes enum.\n"
b019997
+            ":param name: Name of daemon to locate.  If not specified, it searches for the local daemon.\n"
b019997
+            ":return: The ad of the corresponding daemon.")
b019997
+        .def("locateAll", &Collector::locateAll,
b019997
+            "Query the collector for all ads of a particular type.\n"
b019997
+            ":param daemon_type: Type of daemon; must be from the DaemonTypes enum.\n"
b019997
+            ":return: A list of matching ads.")
b019997
+        .def("advertise", &Collector::advertise, advertise_overloads(
b019997
+            "Advertise a list of ClassAds into the collector.\n"
b019997
+            ":param ad_list: A list of ClassAds.\n"
b019997
+            ":param command: A command for the collector; defaults to UPDATE_AD_GENERIC;"
b019997
+            " other commands, such as UPDATE_STARTD_AD, may require reduced authorization levels.\n"
b019997
+            ":param use_tcp: When set to true, updates are sent via TCP."))
b019997
+        ;
b019997
+}
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/condor.cpp b/src/condor_contrib/python-bindings/condor.cpp
b019997
new file mode 100644
b019997
index 0000000..f4a4fd4
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/condor.cpp
b019997
@@ -0,0 +1,25 @@
b019997
+
b019997
+#include <boost/python.hpp>
b019997
+
b019997
+#include "old_boost.h"
b019997
+#include "export_headers.h"
b019997
+
b019997
+using namespace boost::python;
b019997
+
b019997
+
b019997
+BOOST_PYTHON_MODULE(condor)
b019997
+{
b019997
+    scope().attr("__doc__") = "Utilities for interacting with the HTCondor system.";
b019997
+
b019997
+    py_import("classad");
b019997
+
b019997
+    // TODO: old boost doesn't have this; conditionally compile only one newer systems.
b019997
+    //docstring_options local_docstring_options(true, false, false);
b019997
+
b019997
+    export_config();
b019997
+    export_daemon_and_ad_types();
b019997
+    export_collector();
b019997
+    export_schedd();
b019997
+    export_dc_tool();
b019997
+    export_secman();
b019997
+}
b019997
diff --git a/src/condor_contrib/python-bindings/config.cpp b/src/condor_contrib/python-bindings/config.cpp
b019997
new file mode 100644
b019997
index 0000000..0afdfc4
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/config.cpp
b019997
@@ -0,0 +1,60 @@
b019997
+
b019997
+#include "condor_common.h"
b019997
+#include "condor_config.h"
b019997
+#include "condor_version.h"
b019997
+
b019997
+#include <boost/python.hpp>
b019997
+
b019997
+using namespace boost::python;
b019997
+
b019997
+struct Param
b019997
+{
b019997
+    std::string getitem(const std::string &attr)
b019997
+    {
b019997
+        std::string result;
b019997
+        if (!param(result, attr.c_str()))
b019997
+        {
b019997
+            PyErr_SetString(PyExc_KeyError, attr.c_str());
b019997
+            throw_error_already_set();
b019997
+        }
b019997
+        return result;
b019997
+    }
b019997
+
b019997
+    void setitem(const std::string &attr, const std::string &val)
b019997
+    {
b019997
+        param_insert(attr.c_str(), val.c_str());
b019997
+    }
b019997
+
b019997
+    std::string setdefault(const std::string &attr, const std::string &def)
b019997
+    {
b019997
+        std::string result;
b019997
+        if (!param(result, attr.c_str()))
b019997
+        {
b019997
+           param_insert(attr.c_str(), def.c_str());
b019997
+           return def;
b019997
+        }
b019997
+        return result;
b019997
+    }
b019997
+};
b019997
+
b019997
+std::string CondorVersionWrapper() { return CondorVersion(); }
b019997
+
b019997
+std::string CondorPlatformWrapper() { return CondorPlatform(); }
b019997
+
b019997
+BOOST_PYTHON_FUNCTION_OVERLOADS(config_overloads, config, 0, 3);
b019997
+
b019997
+void export_config()
b019997
+{
b019997
+    config();
b019997
+    def("version", CondorVersionWrapper, "Returns the version of HTCondor this module is linked against.");
b019997
+    def("platform", CondorPlatformWrapper, "Returns the platform of HTCondor this module is running on.");
b019997
+    def("reload_config", config, config_overloads("Reload the HTCondor configuration from disk."));
b019997
+    class_<Param>("_Param")
b019997
+        .def("__getitem__", &Param::getitem)
b019997
+        .def("__setitem__", &Param::setitem)
b019997
+        .def("setdefault", &Param::setdefault)
b019997
+        ;
b019997
+    object param = object(Param());
b019997
+    param.attr("__doc__") = "A dictionary-like object containing the HTCondor configuration.";
b019997
+    scope().attr("param") = param;
b019997
+}
b019997
diff --git a/src/condor_contrib/python-bindings/daemon_and_ad_types.cpp b/src/condor_contrib/python-bindings/daemon_and_ad_types.cpp
b019997
new file mode 100644
b019997
index 0000000..f2b0bab
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/daemon_and_ad_types.cpp
b019997
@@ -0,0 +1,30 @@
b019997
+
b019997
+#include <condor_adtypes.h>
b019997
+#include <daemon_types.h>
b019997
+#include <boost/python.hpp>
b019997
+
b019997
+using namespace boost::python;
b019997
+
b019997
+void export_daemon_and_ad_types()
b019997
+{
b019997
+    enum_<daemon_t>("DaemonTypes")
b019997
+        .value("None", DT_NONE)
b019997
+        .value("Any", DT_ANY)
b019997
+        .value("Master", DT_MASTER)
b019997
+        .value("Schedd", DT_SCHEDD)
b019997
+        .value("Startd", DT_STARTD)
b019997
+        .value("Collector", DT_COLLECTOR)
b019997
+        .value("Negotiator", DT_NEGOTIATOR)
b019997
+        ;
b019997
+
b019997
+    enum_<AdTypes>("AdTypes")
b019997
+        .value("None", NO_AD)
b019997
+        .value("Any", ANY_AD)
b019997
+        .value("Generic", GENERIC_AD)
b019997
+        .value("Startd", STARTD_AD)
b019997
+        .value("Schedd", SCHEDD_AD)
b019997
+        .value("Master", MASTER_AD)
b019997
+        .value("Collector", COLLECTOR_AD)
b019997
+        .value("Negotiator", NEGOTIATOR_AD)
b019997
+        ;
b019997
+}
b019997
diff --git a/src/condor_contrib/python-bindings/dc_tool.cpp b/src/condor_contrib/python-bindings/dc_tool.cpp
b019997
new file mode 100644
b019997
index 0000000..973c1e3
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/dc_tool.cpp
b019997
@@ -0,0 +1,129 @@
b019997
+
b019997
+#include "condor_common.h"
b019997
+
b019997
+#include <boost/python.hpp>
b019997
+
b019997
+#include "daemon.h"
b019997
+#include "daemon_types.h"
b019997
+#include "condor_commands.h"
b019997
+#include "condor_attributes.h"
b019997
+#include "compat_classad.h"
b019997
+
b019997
+#include "classad_wrapper.h"
b019997
+
b019997
+using namespace boost::python;
b019997
+
b019997
+enum DaemonCommands {
b019997
+  DDAEMONS_OFF = DAEMONS_OFF,
b019997
+  DDAEMONS_OFF_FAST = DAEMONS_OFF_FAST,
b019997
+  DDAEMONS_OFF_PEACEFUL = DAEMONS_OFF_PEACEFUL,
b019997
+  DDAEMON_OFF = DAEMON_OFF,
b019997
+  DDAEMON_OFF_FAST = DAEMON_OFF_FAST,
b019997
+  DDAEMON_OFF_PEACEFUL = DAEMON_OFF_PEACEFUL,
b019997
+  DDC_OFF_FAST = DC_OFF_FAST,
b019997
+  DDC_OFF_PEACEFUL = DC_OFF_PEACEFUL,
b019997
+  DDC_OFF_GRACEFUL = DC_OFF_GRACEFUL,
b019997
+  DDC_SET_PEACEFUL_SHUTDOWN = DC_SET_PEACEFUL_SHUTDOWN,
b019997
+  DDC_RECONFIG_FULL = DC_RECONFIG_FULL,
b019997
+  DRESTART = RESTART,
b019997
+  DRESTART_PEACEFUL = RESTART_PEACEFUL
b019997
+};
b019997
+
b019997
+void send_command(const ClassAdWrapper & ad, DaemonCommands dc, const std::string &target="")
b019997
+{
b019997
+    std::string addr;
b019997
+    if (!ad.EvaluateAttrString(ATTR_MY_ADDRESS, addr))
b019997
+    {
b019997
+        PyErr_SetString(PyExc_ValueError, "Address not available in location ClassAd.");
b019997
+        throw_error_already_set();
b019997
+    }
b019997
+    std::string ad_type_str;
b019997
+    if (!ad.EvaluateAttrString(ATTR_MY_TYPE, ad_type_str))
b019997
+    {
b019997
+        PyErr_SetString(PyExc_ValueError, "Daemon type not available in location ClassAd.");
b019997
+        throw_error_already_set();
b019997
+    }
b019997
+    int ad_type = AdTypeFromString(ad_type_str.c_str());
b019997
+    if (ad_type == NO_AD)
b019997
+    {
b019997
+        printf("ad type %s.\n", ad_type_str.c_str());
b019997
+        PyErr_SetString(PyExc_ValueError, "Unknown ad type.");
b019997
+        throw_error_already_set();
b019997
+    }
b019997
+    daemon_t d_type;
b019997
+    switch (ad_type) {
b019997
+    case MASTER_AD: d_type = DT_MASTER; break;
b019997
+    case STARTD_AD: d_type = DT_STARTD; break;
b019997
+    case SCHEDD_AD: d_type = DT_SCHEDD; break;
b019997
+    case NEGOTIATOR_AD: d_type = DT_NEGOTIATOR; break;
b019997
+    case COLLECTOR_AD: d_type = DT_COLLECTOR; break;
b019997
+    default:
b019997
+        d_type = DT_NONE;
b019997
+        PyErr_SetString(PyExc_ValueError, "Unknown daemon type.");
b019997
+        throw_error_already_set();
b019997
+    }
b019997
+
b019997
+    ClassAd ad_copy; ad_copy.CopyFrom(ad);
b019997
+    Daemon d(&ad_copy, d_type, NULL);
b019997
+    if (!d.locate())
b019997
+    {
b019997
+        PyErr_SetString(PyExc_RuntimeError, "Unable to locate daemon.");
b019997
+        throw_error_already_set();
b019997
+    }
b019997
+    ReliSock sock;
b019997
+    if (!sock.connect(d.addr()))
b019997
+    {
b019997
+        PyErr_SetString(PyExc_RuntimeError, "Unable to connect to the remote daemon");
b019997
+        throw_error_already_set();
b019997
+    }
b019997
+    if (!d.startCommand(dc, &sock, 0, NULL))
b019997
+    {
b019997
+        PyErr_SetString(PyExc_RuntimeError, "Failed to start command.");
b019997
+        throw_error_already_set();
b019997
+    }
b019997
+    if (target.size())
b019997
+    {
b019997
+        std::vector<unsigned char> target_cstr; target_cstr.reserve(target.size()+1);
b019997
+        memcpy(&target_cstr[0], target.c_str(), target.size()+1);
b019997
+        if (!sock.code(&target_cstr[0]))
b019997
+        {
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Failed to send target.");
b019997
+            throw_error_already_set();
b019997
+        }
b019997
+        if (!sock.end_of_message())
b019997
+        {
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Failed to send end-of-message.");
b019997
+            throw_error_already_set();
b019997
+        }
b019997
+    }
b019997
+    sock.close();
b019997
+}
b019997
+
b019997
+BOOST_PYTHON_FUNCTION_OVERLOADS(send_command_overloads, send_command, 2, 3);
b019997
+
b019997
+void
b019997
+export_dc_tool()
b019997
+{
b019997
+    enum_<DaemonCommands>("DaemonCommands")
b019997
+        .value("DaemonsOff", DDAEMONS_OFF)
b019997
+        .value("DaemonsOffFast", DDAEMONS_OFF_FAST)
b019997
+        .value("DaemonsOffPeaceful", DDAEMONS_OFF_PEACEFUL)
b019997
+        .value("DaemonOff", DDAEMON_OFF)
b019997
+        .value("DaemonOffFast", DDAEMON_OFF_FAST)
b019997
+        .value("DaemonOffPeaceful", DDAEMON_OFF_PEACEFUL)
b019997
+        .value("OffGraceful", DDC_OFF_GRACEFUL)
b019997
+        .value("OffPeaceful", DDC_OFF_PEACEFUL)
b019997
+        .value("OffFast", DDC_OFF_FAST)
b019997
+        .value("SetPeacefulShutdown", DDC_SET_PEACEFUL_SHUTDOWN)
b019997
+        .value("Reconfig", DDC_RECONFIG_FULL)
b019997
+        .value("Restart", DRESTART)
b019997
+        .value("RestartPeacful", DRESTART_PEACEFUL)
b019997
+        ;
b019997
+
b019997
+    def("send_command", send_command, send_command_overloads("Send a command to a HTCondor daemon specified by a location ClassAd\n"
b019997
+        ":param ad: An ad specifying the location of the daemon; typically, found by using Collector.locate(...).\n"
b019997
+        ":param dc: A command type; must be a member of the enum DaemonCommands.\n"
b019997
+        ":param target: Some commands require additional arguments; for example, sending DaemonOff to a master requires one to specify which subsystem to turn off."
b019997
+        "  If this parameter is given, the daemon is sent an additional argument."))
b019997
+        ;
b019997
+}
b019997
diff --git a/src/condor_contrib/python-bindings/export_headers.h b/src/condor_contrib/python-bindings/export_headers.h
b019997
new file mode 100644
b019997
index 0000000..4480495
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/export_headers.h
b019997
@@ -0,0 +1,8 @@
b019997
+
b019997
+void export_collector();
b019997
+void export_schedd();
b019997
+void export_dc_tool();
b019997
+void export_daemon_and_ad_types();
b019997
+void export_config();
b019997
+void export_secman();
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/exprtree_wrapper.h b/src/condor_contrib/python-bindings/exprtree_wrapper.h
b019997
new file mode 100644
b019997
index 0000000..e3d2bc0
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/exprtree_wrapper.h
b019997
@@ -0,0 +1,30 @@
b019997
+
b019997
+#ifndef __EXPRTREE_WRAPPER_H_
b019997
+#define __EXPRTREE_WRAPPER_H_
b019997
+
b019997
+#include <classad/exprTree.h>
b019997
+#include <boost/python.hpp>
b019997
+
b019997
+struct ExprTreeHolder
b019997
+{
b019997
+    ExprTreeHolder(const std::string &str);
b019997
+
b019997
+    ExprTreeHolder(classad::ExprTree *expr);
b019997
+
b019997
+    ~ExprTreeHolder();
b019997
+
b019997
+    boost::python::object Evaluate() const;
b019997
+
b019997
+    std::string toRepr();
b019997
+
b019997
+    std::string toString();
b019997
+
b019997
+    classad::ExprTree *get();
b019997
+
b019997
+private:
b019997
+    classad::ExprTree *m_expr;
b019997
+    bool m_owns;
b019997
+};
b019997
+
b019997
+#endif
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/old_boost.h b/src/condor_contrib/python-bindings/old_boost.h
b019997
new file mode 100644
b019997
index 0000000..7d159bc
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/old_boost.h
b019997
@@ -0,0 +1,25 @@
b019997
+
b019997
+#include <boost/python.hpp>
b019997
+
b019997
+/*
b019997
+ * This header contains all boost.python constructs missing in
b019997
+ * older versions of boost.
b019997
+ *
b019997
+ * We'll eventually not compile these if the version of boost
b019997
+ * is sufficiently recent.
b019997
+ */
b019997
+
b019997
+inline ssize_t py_len(boost::python::object const& obj)
b019997
+{
b019997
+    ssize_t result = PyObject_Length(obj.ptr());
b019997
+    if (PyErr_Occurred()) boost::python::throw_error_already_set();
b019997
+    return result;
b019997
+}
b019997
+
b019997
+inline boost::python::object py_import(boost::python::str name)
b019997
+{
b019997
+  char * n = boost::python::extract<char *>(name);
b019997
+  boost::python::handle<> module(PyImport_ImportModule(n));
b019997
+  return boost::python::object(module);
b019997
+}
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/schedd.cpp b/src/condor_contrib/python-bindings/schedd.cpp
b019997
new file mode 100644
b019997
index 0000000..9bbc830
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/schedd.cpp
b019997
@@ -0,0 +1,402 @@
b019997
+
b019997
+#include "condor_attributes.h"
b019997
+#include "condor_q.h"
b019997
+#include "condor_qmgr.h"
b019997
+#include "daemon.h"
b019997
+#include "daemon_types.h"
b019997
+#include "enum_utils.h"
b019997
+#include "dc_schedd.h"
b019997
+
b019997
+#include <boost/python.hpp>
b019997
+
b019997
+#include "old_boost.h"
b019997
+#include "classad_wrapper.h"
b019997
+#include "exprtree_wrapper.h"
b019997
+
b019997
+using namespace boost::python;
b019997
+
b019997
+#define DO_ACTION(action_name) \
b019997
+    reason_str = extract<std::string>(reason); \
b019997
+    if (use_ids) \
b019997
+        result = schedd. action_name (&ids, reason_str.c_str(), NULL, AR_TOTALS); \
b019997
+    else \
b019997
+        result = schedd. action_name (constraint.c_str(), reason_str.c_str(), NULL, AR_TOTALS);
b019997
+
b019997
+struct Schedd {
b019997
+
b019997
+    Schedd()
b019997
+    {
b019997
+        Daemon schedd( DT_SCHEDD, 0, 0 );
b019997
+
b019997
+        if (schedd.locate())
b019997
+        {
b019997
+            if (schedd.addr())
b019997
+            {
b019997
+                m_addr = schedd.addr();
b019997
+            }
b019997
+            else
b019997
+            {
b019997
+                PyErr_SetString(PyExc_RuntimeError, "Unable to locate schedd address.");
b019997
+                throw_error_already_set();
b019997
+            }
b019997
+            m_name = schedd.name() ? schedd.name() : "Unknown";
b019997
+            m_version = schedd.version() ? schedd.version() : "";
b019997
+        }
b019997
+        else
b019997
+        {
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Unable to locate local daemon");
b019997
+            boost::python::throw_error_already_set();
b019997
+        }
b019997
+    }
b019997
+
b019997
+    Schedd(const ClassAdWrapper &ad)
b019997
+      : m_addr(), m_name("Unknown"), m_version("")
b019997
+    {
b019997
+        if (!ad.EvaluateAttrString(ATTR_SCHEDD_IP_ADDR, m_addr))
b019997
+        {
b019997
+            PyErr_SetString(PyExc_ValueError, "Schedd address not specified.");
b019997
+            throw_error_already_set();
b019997
+        }
b019997
+        ad.EvaluateAttrString(ATTR_NAME, m_name);
b019997
+        ad.EvaluateAttrString(ATTR_VERSION, m_version);
b019997
+    }
b019997
+
b019997
+    object query(const std::string &constraint="", list attrs=list())
b019997
+    {
b019997
+        CondorQ q;
b019997
+
b019997
+        if (constraint.size())
b019997
+            q.addAND(constraint.c_str());
b019997
+
b019997
+        StringList attrs_list(NULL, "\n");
b019997
+        // Must keep strings alive; StringList does not create an internal copy.
b019997
+        int len_attrs = py_len(attrs);
b019997
+        std::vector<std::string> attrs_str; attrs_str.reserve(len_attrs);
b019997
+        for (int i=0; i
b019997
+        {
b019997
+            std::string attrName = extract<std::string>(attrs[i]);
b019997
+            attrs_str.push_back(attrName);
b019997
+            attrs_list.append(attrs_str[i].c_str());
b019997
+        }
b019997
+
b019997
+        ClassAdList jobs;
b019997
+
b019997
+        int fetchResult = q.fetchQueueFromHost(jobs, attrs_list, m_addr.c_str(), m_version.c_str(), NULL);
b019997
+        switch (fetchResult)
b019997
+        {
b019997
+        case Q_OK:
b019997
+            break;
b019997
+        case Q_PARSE_ERROR:
b019997
+        case Q_INVALID_CATEGORY:
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Parse error in constraint.");
b019997
+            throw_error_already_set();
b019997
+            break;
b019997
+        default:
b019997
+            PyErr_SetString(PyExc_IOError, "Failed to fetch ads from schedd.");
b019997
+            throw_error_already_set();
b019997
+            break;
b019997
+        }
b019997
+
b019997
+        list retval;
b019997
+        ClassAd *job;
b019997
+        jobs.Open();
b019997
+        while ((job = jobs.Next()))
b019997
+        {
b019997
+            boost::shared_ptr<ClassAdWrapper> wrapper(new ClassAdWrapper());
b019997
+            wrapper->CopyFrom(*job);
b019997
+            retval.append(wrapper);
b019997
+        }
b019997
+        return retval;
b019997
+    }
b019997
+
b019997
+    object actOnJobs(JobAction action, object job_spec, object reason=object())
b019997
+    {
b019997
+        if (reason == object())
b019997
+        {
b019997
+            reason = object("Python-initiated action");
b019997
+        }
b019997
+        StringList ids;
b019997
+        std::vector<std::string> ids_list;
b019997
+        std::string constraint, reason_str, reason_code;
b019997
+        bool use_ids = false;
b019997
+        extract<std::string> constraint_extract(job_spec);
b019997
+        if (constraint_extract.check())
b019997
+        {
b019997
+            constraint = constraint_extract();
b019997
+        }
b019997
+        else
b019997
+        {
b019997
+            int id_len = py_len(job_spec);
b019997
+            ids_list.reserve(id_len);
b019997
+            for (int i=0; i
b019997
+            {
b019997
+                std::string str = extract<std::string>(job_spec[i]);
b019997
+                ids_list.push_back(str);
b019997
+                ids.append(ids_list[i].c_str());
b019997
+            }
b019997
+            use_ids = true;
b019997
+        }
b019997
+        DCSchedd schedd(m_addr.c_str());
b019997
+        ClassAd *result = NULL;
b019997
+        VacateType vacate_type;
b019997
+        tuple reason_tuple;
b019997
+        const char *reason_char, *reason_code_char = NULL;
b019997
+        extract<tuple> try_extract_tuple(reason);
b019997
+        switch (action)
b019997
+        {
b019997
+        case JA_HOLD_JOBS:
b019997
+            if (try_extract_tuple.check())
b019997
+            {
b019997
+                reason_tuple = extract<tuple>(reason);
b019997
+                if (py_len(reason_tuple) != 2)
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_ValueError, "Hold action requires (hold string, hold code) tuple as the reason.");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+                reason_str = extract<std::string>(reason_tuple[0]); reason_char = reason_str.c_str();
b019997
+                reason_code = extract<std::string>(reason_tuple[1]); reason_code_char = reason_code.c_str();
b019997
+            }
b019997
+            else
b019997
+            {
b019997
+                reason_str = extract<std::string>(reason);
b019997
+                reason_char = reason_str.c_str();
b019997
+            }
b019997
+            if (use_ids)
b019997
+                result = schedd.holdJobs(&ids, reason_char, reason_code_char, NULL, AR_TOTALS);
b019997
+            else
b019997
+                result = schedd.holdJobs(constraint.c_str(), reason_char, reason_code_char, NULL, AR_TOTALS);
b019997
+            break;
b019997
+        case JA_RELEASE_JOBS:
b019997
+            DO_ACTION(releaseJobs)
b019997
+            break;
b019997
+        case JA_REMOVE_JOBS:
b019997
+            DO_ACTION(removeJobs)
b019997
+            break;
b019997
+        case JA_REMOVE_X_JOBS:
b019997
+            DO_ACTION(removeXJobs)
b019997
+            break;
b019997
+        case JA_VACATE_JOBS:
b019997
+        case JA_VACATE_FAST_JOBS:
b019997
+            vacate_type = action == JA_VACATE_JOBS ? VACATE_GRACEFUL : VACATE_FAST;
b019997
+            if (use_ids)
b019997
+                result = schedd.vacateJobs(&ids, vacate_type, NULL, AR_TOTALS);
b019997
+            else
b019997
+                result = schedd.vacateJobs(constraint.c_str(), vacate_type, NULL, AR_TOTALS);
b019997
+            break;
b019997
+        case JA_SUSPEND_JOBS:
b019997
+            DO_ACTION(suspendJobs)
b019997
+            break;
b019997
+        case JA_CONTINUE_JOBS:
b019997
+            DO_ACTION(continueJobs)
b019997
+            break;
b019997
+        default:
b019997
+            PyErr_SetString(PyExc_NotImplementedError, "Job action not implemented.");
b019997
+            throw_error_already_set();
b019997
+        }
b019997
+        if (!result)
b019997
+        {
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Error when querying the schedd.");
b019997
+            throw_error_already_set();
b019997
+        }
b019997
+
b019997
+        boost::shared_ptr<ClassAdWrapper> wrapper(new ClassAdWrapper());
b019997
+        wrapper->CopyFrom(*result);
b019997
+        object wrapper_obj(wrapper);
b019997
+
b019997
+        boost::shared_ptr<ClassAdWrapper> result_ptr(new ClassAdWrapper());
b019997
+        object result_obj(result_ptr);
b019997
+
b019997
+        result_obj["TotalError"] = wrapper_obj["result_total_0"];
b019997
+        result_obj["TotalSuccess"] = wrapper_obj["result_total_1"];
b019997
+        result_obj["TotalNotFound"] = wrapper_obj["result_total_2"];
b019997
+        result_obj["TotalBadStatus"] = wrapper_obj["result_total_3"];
b019997
+        result_obj["TotalAlreadyDone"] = wrapper_obj["result_total_4"];
b019997
+        result_obj["TotalPermissionDenied"] = wrapper_obj["result_total_5"];
b019997
+        result_obj["TotalJobAds"] = wrapper_obj["TotalJobAds"];
b019997
+        result_obj["TotalChangedAds"] = wrapper_obj["ActionResult"];
b019997
+        return result_obj;
b019997
+    }
b019997
+
b019997
+    object actOnJobs2(JobAction action, object job_spec)
b019997
+    {
b019997
+        return actOnJobs(action, job_spec, object("Python-initiated action."));
b019997
+    }
b019997
+
b019997
+    int submit(ClassAdWrapper &wrapper, int count=1)
b019997
+    {
b019997
+        ConnectionSentry sentry(*this); // Automatically connects / disconnects.
b019997
+
b019997
+        int cluster = NewCluster();
b019997
+        if (cluster < 0)
b019997
+        {
b019997
+            PyErr_SetString(PyExc_RuntimeError, "Failed to create new cluster.");
b019997
+            throw_error_already_set();
b019997
+        }
b019997
+        ClassAd ad; ad.CopyFrom(wrapper);
b019997
+        for (int idx=0; idx
b019997
+        {
b019997
+            int procid = NewProc (cluster);
b019997
+            if (procid < 0)
b019997
+            {
b019997
+                PyErr_SetString(PyExc_RuntimeError, "Failed to create new proc id.");
b019997
+                throw_error_already_set();
b019997
+            }
b019997
+            ad.InsertAttr(ATTR_CLUSTER_ID, cluster);
b019997
+            ad.InsertAttr(ATTR_PROC_ID, procid);
b019997
+
b019997
+            classad::ClassAdUnParser unparser;
b019997
+            unparser.SetOldClassAd( true );
b019997
+            for (classad::ClassAd::const_iterator it = ad.begin(); it != ad.end(); it++)
b019997
+            {
b019997
+                std::string rhs;
b019997
+                unparser.Unparse(rhs, it->second);
b019997
+                if (-1 == SetAttribute(cluster, procid, it->first.c_str(), rhs.c_str(), SetAttribute_NoAck))
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_ValueError, it->first.c_str());
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+            }
b019997
+        }
b019997
+
b019997
+        return cluster;
b019997
+    }
b019997
+
b019997
+    void edit(object job_spec, std::string attr, object val)
b019997
+    {
b019997
+        std::vector<int> clusters;
b019997
+        std::vector<int> procs;
b019997
+        std::string constraint;
b019997
+        bool use_ids = false;
b019997
+        extract<std::string> constraint_extract(job_spec);
b019997
+        if (constraint_extract.check())
b019997
+        {
b019997
+            constraint = constraint_extract();
b019997
+        }
b019997
+        else
b019997
+        {
b019997
+            int id_len = py_len(job_spec);
b019997
+            clusters.reserve(id_len);
b019997
+            procs.reserve(id_len);
b019997
+            for (int i=0; i
b019997
+            {
b019997
+                object id_list = job_spec[i].attr("split")(".");
b019997
+                if (py_len(id_list) != 2)
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_ValueError, "Invalid ID");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+                clusters.push_back(extract<int>(long_(id_list[0])));
b019997
+                procs.push_back(extract<int>(long_(id_list[1])));
b019997
+            }
b019997
+            use_ids = true;
b019997
+        }
b019997
+
b019997
+        std::string val_str;
b019997
+        extract<ExprTreeHolder &> exprtree_extract(val);
b019997
+        if (exprtree_extract.check())
b019997
+        {
b019997
+            classad::ClassAdUnParser unparser;
b019997
+            unparser.Unparse(val_str, exprtree_extract().get());
b019997
+        }
b019997
+        else
b019997
+        {
b019997
+            val_str = extract<std::string>(val);
b019997
+        }
b019997
+
b019997
+        ConnectionSentry sentry(*this);
b019997
+
b019997
+        if (use_ids)
b019997
+        {
b019997
+            for (unsigned idx=0; idx
b019997
+            {
b019997
+                if (-1 == SetAttribute(clusters[idx], procs[idx], attr.c_str(), val_str.c_str()))
b019997
+                {
b019997
+                    PyErr_SetString(PyExc_RuntimeError, "Unable to edit job");
b019997
+                    throw_error_already_set();
b019997
+                }
b019997
+            }
b019997
+        }
b019997
+        else
b019997
+        {
b019997
+            if (-1 == SetAttributeByConstraint(constraint.c_str(), attr.c_str(), val_str.c_str()))
b019997
+            {
b019997
+                PyErr_SetString(PyExc_RuntimeError, "Unable to edit jobs matching constraint");
b019997
+                throw_error_already_set();
b019997
+            }
b019997
+        }
b019997
+    }
b019997
+
b019997
+private:
b019997
+    struct ConnectionSentry
b019997
+    {
b019997
+    public:
b019997
+        ConnectionSentry(Schedd &schedd) : m_connected(false)
b019997
+        {
b019997
+            if (ConnectQ(schedd.m_addr.c_str(), 0, false, NULL, NULL, schedd.m_version.c_str()) == 0)
b019997
+            {
b019997
+                PyErr_SetString(PyExc_RuntimeError, "Failed to connect to schedd.");
b019997
+                throw_error_already_set();
b019997
+            }
b019997
+            m_connected = true;
b019997
+        }
b019997
+
b019997
+        void disconnect()
b019997
+        {
b019997
+            if (m_connected && !DisconnectQ(NULL))
b019997
+            {
b019997
+                m_connected = false;
b019997
+                PyErr_SetString(PyExc_RuntimeError, "Failed to commmit and disconnect from queue.");
b019997
+                throw_error_already_set();
b019997
+            }
b019997
+            m_connected = false;
b019997
+        }
b019997
+
b019997
+        ~ConnectionSentry()
b019997
+        {
b019997
+            disconnect();
b019997
+        }
b019997
+    private:
b019997
+        bool m_connected;
b019997
+    };
b019997
+
b019997
+    std::string m_addr, m_name, m_version;
b019997
+};
b019997
+
b019997
+BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(query_overloads, query, 0, 2);
b019997
+BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(submit_overloads, submit, 1, 2);
b019997
+
b019997
+void export_schedd()
b019997
+{
b019997
+    enum_<JobAction>("JobAction")
b019997
+        .value("Hold", JA_HOLD_JOBS)
b019997
+        .value("Release", JA_RELEASE_JOBS)
b019997
+        .value("Remove", JA_REMOVE_JOBS)
b019997
+        .value("RemoveX", JA_REMOVE_X_JOBS)
b019997
+        .value("Vacate", JA_VACATE_JOBS)
b019997
+        .value("VacateFast", JA_VACATE_FAST_JOBS)
b019997
+        .value("Suspend", JA_SUSPEND_JOBS)
b019997
+        .value("Continue", JA_CONTINUE_JOBS)
b019997
+        ;
b019997
+
b019997
+    class_<Schedd>("Schedd", "A client class for the HTCondor schedd")
b019997
+        .def(init<const ClassAdWrapper &>(":param ad: An ad containing the location of the schedd"))
b019997
+        .def("query", &Schedd::query, query_overloads("Query the HTCondor schedd for jobs.\n"
b019997
+            ":param constraint: An optional constraint for filtering out jobs; defaults to 'true'\n"
b019997
+            ":param attr_list: A list of attributes for the schedd to project along.  Defaults to having the schedd return all attributes.\n"
b019997
+            ":return: A list of matching jobs, containing the requested attributes."))
b019997
+        .def("act", &Schedd::actOnJobs2)
b019997
+        .def("act", &Schedd::actOnJobs, "Change status of job(s) in the schedd.\n"
b019997
+            ":param action: Action to perform; must be from enum JobAction.\n"
b019997
+            ":param job_spec: Job specification; can either be a list of job IDs or a string specifying a constraint to match jobs.\n"
b019997
+            ":return: Number of jobs changed.")
b019997
+        .def("submit", &Schedd::submit, submit_overloads("Submit one or more jobs to the HTCondor schedd.\n"
b019997
+            ":param ad: ClassAd describing job cluster.\n"
b019997
+            ":param count: Number of jobs to submit to cluster.\n"
b019997
+            ":return: Newly created cluster ID."))
b019997
+        .def("edit", &Schedd::edit, "Edit one or more jobs in the queue.\n"
b019997
+            ":param job_spec: Either a list of jobs (CLUSTER.PROC) or a string containing a constraint to match jobs against.\n"
b019997
+            ":param attr: Attribute name to edit.\n"
b019997
+            ":param value: The new value of the job attribute; should be a string (which will be converted to a ClassAds expression) or a ClassAds expression.");
b019997
+        ;
b019997
+}
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/secman.cpp b/src/condor_contrib/python-bindings/secman.cpp
b019997
new file mode 100644
b019997
index 0000000..343fba8
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/secman.cpp
b019997
@@ -0,0 +1,35 @@
b019997
+
b019997
+#include "condor_common.h"
b019997
+
b019997
+#include <boost/python.hpp>
b019997
+
b019997
+// Note - condor_secman.h can't be included directly.  The following headers must
b019997
+// be loaded first.  Sigh.
b019997
+#include "condor_ipverify.h"
b019997
+#include "sock.h"
b019997
+
b019997
+#include "condor_secman.h"
b019997
+
b019997
+using namespace boost::python;
b019997
+
b019997
+struct SecManWrapper
b019997
+{
b019997
+public:
b019997
+    SecManWrapper() : m_secman() {}
b019997
+
b019997
+    void
b019997
+    invalidateAllCache()
b019997
+    {
b019997
+        m_secman.invalidateAllCache();
b019997
+    }
b019997
+
b019997
+private:
b019997
+    SecMan m_secman;
b019997
+};
b019997
+
b019997
+void
b019997
+export_secman()
b019997
+{
b019997
+    class_<SecManWrapper>("SecMan", "Access to the internal security state information.")
b019997
+        .def("invalidateAllSessions", &SecManWrapper::invalidateAllCache, "Invalidate all security sessions.");
b019997
+}
b019997
diff --git a/src/condor_contrib/python-bindings/tests/classad_tests.py b/src/condor_contrib/python-bindings/tests/classad_tests.py
b019997
new file mode 100644
b019997
index 0000000..7641190
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/tests/classad_tests.py
b019997
@@ -0,0 +1,79 @@
b019997
+#!/usr/bin/python
b019997
+
b019997
+import re
b019997
+import classad
b019997
+import unittest
b019997
+
b019997
+class TestClassad(unittest.TestCase):
b019997
+
b019997
+    def test_load_classad_from_file(self):
b019997
+        ad = classad.parse(open("tests/test.ad"))
b019997
+        self.assertEqual(ad["foo"], "bar")
b019997
+        self.assertEqual(ad["baz"], classad.Value.Undefined)
b019997
+        self.assertRaises(KeyError, ad.__getitem__, "bar")
b019997
+
b019997
+    def test_old_classad(self):
b019997
+        ad = classad.parseOld(open("tests/test.old.ad"))
b019997
+        contents = open("tests/test.old.ad").read()
b019997
+        self.assertEqual(ad.printOld(), contents)
b019997
+
b019997
+    def test_exprtree(self):
b019997
+        ad = classad.ClassAd()
b019997
+        ad["foo"] = classad.ExprTree("2+2")
b019997
+        expr = ad["foo"]
b019997
+        self.assertEqual(expr.__repr__(), "2 + 2")
b019997
+        self.assertEqual(expr.eval(), 4)
b019997
+
b019997
+    def test_exprtree_func(self):
b019997
+        ad = classad.ClassAd()
b019997
+        ad["foo"] = classad.ExprTree('regexps("foo (bar)", "foo bar", "\\\\1")')
b019997
+        self.assertEqual(ad.eval("foo"), "bar")
b019997
+
b019997
+    def test_ad_assignment(self):
b019997
+        ad = classad.ClassAd()
b019997
+        ad["foo"] = 2.1
b019997
+        self.assertEqual(ad["foo"], 2.1)
b019997
+        ad["foo"] = 2
b019997
+        self.assertEqual(ad["foo"], 2)
b019997
+        ad["foo"] = "bar"
b019997
+        self.assertEqual(ad["foo"], "bar")
b019997
+        self.assertRaises(TypeError, ad.__setitem__, {})
b019997
+
b019997
+    def test_ad_refs(self):
b019997
+        ad = classad.ClassAd()
b019997
+        ad["foo"] = classad.ExprTree("bar + baz")
b019997
+        ad["bar"] = 2.1
b019997
+        ad["baz"] = 4
b019997
+        self.assertEqual(ad["foo"].__repr__(), "bar + baz")
b019997
+        self.assertEqual(ad.eval("foo"), 6.1)
b019997
+
b019997
+    def test_ad_special_values(self):
b019997
+        ad = classad.ClassAd()
b019997
+        ad["foo"] = classad.ExprTree('regexp(12, 34)')
b019997
+        ad["bar"] = classad.Value.Undefined
b019997
+        self.assertEqual(ad["foo"].eval(), classad.Value.Error)
b019997
+        self.assertNotEqual(ad["foo"].eval(), ad["bar"])
b019997
+        self.assertEqual(classad.Value.Undefined, ad["bar"])
b019997
+
b019997
+    def test_ad_iterator(self):
b019997
+        ad = classad.ClassAd()
b019997
+        ad["foo"] = 1
b019997
+        ad["bar"] = 2
b019997
+        self.assertEqual(len(ad), 2)
b019997
+        self.assertEqual(len(list(ad)), 2)
b019997
+        self.assertEqual(list(ad)[1], "foo")
b019997
+        self.assertEqual(list(ad)[0], "bar")
b019997
+        self.assertEqual(list(ad.items())[1][1], 1)
b019997
+        self.assertEqual(list(ad.items())[0][1], 2)
b019997
+        self.assertEqual(list(ad.values())[1], 1)
b019997
+        self.assertEqual(list(ad.values())[0], 2)
b019997
+
b019997
+    def test_ad_lookup(self):
b019997
+        ad = classad.ClassAd()
b019997
+        ad["foo"] = classad.Value.Error
b019997
+        self.assertTrue(isinstance(ad.lookup("foo"), classad.ExprTree))
b019997
+        self.assertEquals(ad.lookup("foo").eval(), classad.Value.Error)
b019997
+
b019997
+if __name__ == '__main__':
b019997
+    unittest.main()
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/tests/condor_tests.py b/src/condor_contrib/python-bindings/tests/condor_tests.py
b019997
new file mode 100644
b019997
index 0000000..2293fc2
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/tests/condor_tests.py
b019997
@@ -0,0 +1,173 @@
b019997
+#!/usr/bin/python
b019997
+
b019997
+import os
b019997
+import re
b019997
+import time
b019997
+import condor
b019997
+import errno
b019997
+import signal
b019997
+import classad
b019997
+import unittest
b019997
+
b019997
+class TestConfig(unittest.TestCase):
b019997
+
b019997
+    def setUp(self):
b019997
+        os.environ["_condor_FOO"] = "BAR"
b019997
+        condor.reload_config()
b019997
+
b019997
+    def test_config(self):
b019997
+        self.assertEquals(condor.param["FOO"], "BAR")
b019997
+
b019997
+    def test_reconfig(self):
b019997
+        condor.param["FOO"] = "BAZ"
b019997
+        self.assertEquals(condor.param["FOO"], "BAZ")
b019997
+        os.environ["_condor_FOO"] = "1"
b019997
+        condor.reload_config()
b019997
+        self.assertEquals(condor.param["FOO"], "1")
b019997
+
b019997
+class TestVersion(unittest.TestCase):
b019997
+
b019997
+    def setUp(self):
b019997
+        fd = os.popen("condor_version")
b019997
+        self.lines = []
b019997
+        for line in fd.readlines():
b019997
+            self.lines.append(line.strip())
b019997
+        if fd.close():
b019997
+            raise RuntimeError("Unable to invoke condor_version")
b019997
+
b019997
+    def test_version(self):
b019997
+        self.assertEquals(condor.version(), self.lines[0])
b019997
+
b019997
+    def test_platform(self):
b019997
+        self.assertEquals(condor.platform(), self.lines[1])
b019997
+
b019997
+def makedirs_ignore_exist(directory):
b019997
+    try:
b019997
+        os.makedirs(directory)
b019997
+    except OSError, oe:
b019997
+        if oe.errno != errno.EEXIST:
b019997
+            raise
b019997
+
b019997
+def remove_ignore_missing(file):
b019997
+    try:
b019997
+        os.unlink(file)
b019997
+    except OSError, oe:
b019997
+        if oe.errno != errno.ENOENT:
b019997
+            raise
b019997
+
b019997
+class TestWithDaemons(unittest.TestCase):
b019997
+
b019997
+    def setUp(self):
b019997
+        self.pid = -1
b019997
+        testdir = os.path.join(os.getcwd(), "tests_tmp")
b019997
+        makedirs_ignore_exist(testdir)
b019997
+        os.environ["_condor_LOCAL_DIR"] = testdir
b019997
+        os.environ["_condor_LOG"] =  '$(LOCAL_DIR)/log'
b019997
+        os.environ["_condor_LOCK"] = '$(LOCAL_DIR)/lock'
b019997
+        os.environ["_condor_RUN"] = '$(LOCAL_DIR)/run'
b019997
+        os.environ["_condor_COLLECTOR_NAME"] = "python_classad_tests"
b019997
+        os.environ["_condor_SCHEDD_NAME"] = "python_classad_tests"
b019997
+        condor.reload_config()
b019997
+        condor.SecMan().invalidateAllSessions()
b019997
+
b019997
+    def launch_daemons(self, daemons=["MASTER", "COLLECTOR"]):
b019997
+        makedirs_ignore_exist(condor.param["LOG"])
b019997
+        makedirs_ignore_exist(condor.param["LOCK"])
b019997
+        makedirs_ignore_exist(condor.param["EXECUTE"])
b019997
+        makedirs_ignore_exist(condor.param["SPOOL"])
b019997
+        makedirs_ignore_exist(condor.param["RUN"])
b019997
+        remove_ignore_missing(condor.param["MASTER_ADDRESS_FILE"])
b019997
+        remove_ignore_missing(condor.param["COLLECTOR_ADDRESS_FILE"])
b019997
+        remove_ignore_missing(condor.param["SCHEDD_ADDRESS_FILE"])
b019997
+        if "COLLECTOR" in daemons:
b019997
+            os.environ["_condor_PORT"] = "9622"
b019997
+            os.environ["_condor_COLLECTOR_ARGS"] = "-port $(PORT)"
b019997
+            os.environ["_condor_COLLECTOR_HOST"] = "$(CONDOR_HOST):$(PORT)"
b019997
+        if 'MASTER' not in daemons:
b019997
+            daemons.append('MASTER')
b019997
+        os.environ["_condor_DAEMON_LIST"] = ", ".join(daemons)
b019997
+        condor.reload_config()
b019997
+        self.pid = os.fork()
b019997
+        if not self.pid:
b019997
+            try:
b019997
+                try:
b019997
+                    os.execvp("condor_master", ["condor_master", "-f"])
b019997
+                except Exception, e:
b019997
+                    print str(e)
b019997
+            finally:
b019997
+                os._exit(1)
b019997
+        for daemon in daemons:
b019997
+            self.waitLocalDaemon(daemon)
b019997
+
b019997
+    def tearDown(self):
b019997
+        if self.pid > 1:
b019997
+            os.kill(self.pid, signal.SIGQUIT)
b019997
+            pid, exit_status = os.waitpid(self.pid, 0)
b019997
+            self.assertTrue(os.WIFEXITED(exit_status))
b019997
+            code = os.WEXITSTATUS(exit_status)
b019997
+            self.assertEquals(code, 0)
b019997
+
b019997
+    def waitLocalDaemon(self, daemon, timeout=5):
b019997
+        address_file = condor.param[daemon + "_ADDRESS_FILE"]
b019997
+        for i in range(timeout):
b019997
+            if os.path.exists(address_file):
b019997
+                return
b019997
+            time.sleep(1)
b019997
+        if not os.path.exists(address_file):
b019997
+            raise RuntimeError("Waiting for daemon %s timed out." % daemon)
b019997
+
b019997
+    def waitRemoteDaemon(self, dtype, dname, pool=None, timeout=5):
b019997
+        if pool:
b019997
+            coll = condor.Collector(pool)
b019997
+        else:
b019997
+            coll = condor.Collector()
b019997
+        for i in range(timeout):
b019997
+            try:
b019997
+                return coll.locate(dtype, dname)
b019997
+            except Exception:
b019997
+                pass
b019997
+            time.sleep(1)
b019997
+        return coll.locate(dtype, dname)
b019997
+
b019997
+    def testDaemon(self):
b019997
+        self.launch_daemons(["COLLECTOR"])
b019997
+
b019997
+    def testLocate(self):
b019997
+        self.launch_daemons(["COLLECTOR"])
b019997
+        coll = condor.Collector()
b019997
+        coll_ad = coll.locate(condor.DaemonTypes.Collector)
b019997
+        self.assertTrue("MyAddress" in coll_ad)
b019997
+        self.assertEquals(coll_ad["Name"].split(":")[-1], os.environ["_condor_PORT"])
b019997
+
b019997
+    def testRemoteLocate(self):
b019997
+        self.launch_daemons(["COLLECTOR"])
b019997
+        coll = condor.Collector()
b019997
+        coll_ad = coll.locate(condor.DaemonTypes.Collector)
b019997
+        remote_ad = self.waitRemoteDaemon(condor.DaemonTypes.Collector, "%s@%s" % (condor.param["COLLECTOR_NAME"], condor.param["CONDOR_HOST"]))
b019997
+        self.assertEquals(remote_ad["MyAddress"], coll_ad["MyAddress"])
b019997
+
b019997
+    def testScheddLocate(self):
b019997
+        self.launch_daemons(["SCHEDD", "COLLECTOR"])
b019997
+        coll = condor.Collector()
b019997
+        name = "%s@%s" % (condor.param["SCHEDD_NAME"], condor.param["CONDOR_HOST"])
b019997
+        schedd_ad = self.waitRemoteDaemon(condor.DaemonTypes.Schedd, name, timeout=10)
b019997
+        self.assertEquals(schedd_ad["Name"], name)
b019997
+
b019997
+    def testCollectorAdvertise(self):
b019997
+        self.launch_daemons(["COLLECTOR"])
b019997
+        print condor.param["COLLECTOR_HOST"]
b019997
+        coll = condor.Collector()
b019997
+        now = time.time()
b019997
+        ad = classad.ClassAd('[MyType="GenericAd"; Name="Foo"; Foo=1; Bar=%f; Baz="foo"]' % now) 
b019997
+        coll.advertise([ad])
b019997
+        for i in range(5):
b019997
+            ads = coll.query(condor.AdTypes.Any, 'Name =?= "Foo"', ["Bar"])
b019997
+            if ads: break
b019997
+            time.sleep
b019997
+        self.assertEquals(len(ads), 1)
b019997
+        self.assertEquals(ads[0]["Bar"], now)
b019997
+        self.assertTrue("Foo" not in ads[0])
b019997
+
b019997
+if __name__ == '__main__':
b019997
+    unittest.main()
b019997
+
b019997
diff --git a/src/condor_contrib/python-bindings/tests/test.ad b/src/condor_contrib/python-bindings/tests/test.ad
b019997
new file mode 100644
b019997
index 0000000..06eeeb5
b019997
--- /dev/null
b019997
+++ b/src/condor_contrib/python-bindings/tests/test.ad
b019997
@@ -0,0 +1,4 @@
b019997
+[
b019997
+foo = "bar";
b019997
+baz = undefined;
b019997
+]