/usr/share/pyshared/drmaa/helpers.py is in python-drmaa 0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | # -----------------------------------------------------------
# Copyright (C) 2009 StatPro Italia s.r.l.
#
# StatPro Italia
# Via G. B. Vico 4
# I-20123 Milano
# ITALY
#
# phone: +39 02 96875 1
# fax: +39 02 96875 605
#
# email: info@riskmap.net
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the license for more details.
# -----------------------------------------------------------
#
# Author: Enrico Sirola <enrico.sirola@statpro.com>
"""internal helpers"""
import ctypes as _ct
from drmaa.wrappers import *
from drmaa.errors import error_buffer
import drmaa.const as const
_BUFLEN=const.ATTR_BUFFER
try:
import namedtuple as _nt
except ImportError: # pre 2.6 behaviour
import nt as _nt
class BoolConverter(object):
"""Helper class to convert to/from bool attributes."""
def __init__(self, true='y', false='n'):
self.true=true
self.false=false
def to_drmaa(self, value):
if value: return self.true
else: return self.false
def from_drmaa(self, value):
if value == self.true:
return True
else:
return False
class IntConverter(object):
"""Helper class to convert to/from float attributes."""
@staticmethod
def to_drmaa(value):
return str(value)
@staticmethod
def from_drmaa(value):
return int(value)
class SessionStringAttribute(object):
def __init__(self, drmaa_function):
self._f = drmaa_function
def __get__(self, *args):
buf = _ct.create_string_buffer(_BUFLEN)
c(self._f, buf, _ct.sizeof(buf))
return buf.value
Version = _nt.namedtuple("Version", "major minor")
Version.__str__ = lambda x: "%s.%s" % (x.major, x.minor)
#Version.__doc__ = """\
#An object representing the DRMAA version.
#
#major and minor attributes are int. For DRMAA 1.0, major == 1 and minor == 0.
#"""
class SessionVersionAttribute(object):
"""A Version attribute."""
def __get__(self, *args):
major = _ct.c_uint(10)
minor = _ct.c_uint(10)
c(drmaa_version, _ct.byref(major), _ct.byref(minor))
return Version(major.value, minor.value)
class Attribute(object):
"""A DRMAA attribute, to be managed with scalar C DRMAA attribute management functions."""
def __init__(self, name, type_converter=None):
"""\
Attribute constructor.
:Parameters:
`name` : string
name of the attribute to be managed, as seen by the underlying C DRMAA
`type_converter`
a converter to translate attribute values to/from the underlying
implementation. See BoolConverter for an example.
"""
self.name = name
self.converter = type_converter
def __set__(self, instance, value):
if self.converter:
v = self.converter.to_drmaa(value)
else:
v = value
c(drmaa_set_attribute, instance, self.name, v)
def __get__(self, instance, _):
attr_buffer = create_string_buffer(const.ATTR_BUFFER)
c(drmaa_get_attribute, instance, self.name,
attr_buffer, sizeof(attr_buffer))
if self.converter:
return self.converter.from_drmaa(attr_buffer.value)
else:
return attr_buffer.value
class VectorAttribute(object):
"""\
A DRMAA attribute representing a list.
To be managed with vector C DRMAA attribute management functions."""
def __init__(self, name):
self.name = name
def __set__(self, instance, value):
c(drmaa_set_vector_attribute, instance, self.name, string_vector(value))
def __get__(self, instance, _):
return list(vector_attribute_iterator(
instance, self.name))
class DictAttribute(object):
"""\
A DRMAA attribute representing a python dict.
To be managed with vector C DRMAA attribute management functions."""
def __init__(self, name):
self.name = name
def __set__(self, instance, value):
v = [ "%s=%s" % (k, v) for (k, v) in value.iteritems() ]
c(drmaa_set_vector_attribute, instance, self.name, string_vector(v))
def __get__(self, instance, _):
x = [ i.split('=', 1) for i in list(vector_attribute_iterator(
instance, self.name)) ]
return dict(x)
def attributes_iterator(attributes):
try:
buf = create_string_buffer(const.ATTR_BUFFER)
while drmaa_get_next_attr_value(attributes, buf, sizeof(buf))\
!= const.NO_MORE_ELEMENTS:
yield buf.value
except:
drmaa_release_attr_values(attributes)
raise
else:
drmaa_release_attr_values(attributes)
def adapt_rusage(rusage):
"transform a rusage data structure into a dict"
rv = dict()
for attr in attributes_iterator(rusage.contents):
k, v = attr.split('=')
rv[k] = v
return rv
def vector_attribute_iterator(jt, attr_name):
avalues = pointer(POINTER(drmaa_attr_values_t)())
c(drmaa_get_vector_attribute, jt, attr_name, avalues)
return attributes_iterator(avalues.contents)
def attribute_names_iterator():
attrn_p = pointer(POINTER(drmaa_attr_names_t)())
c(drmaa_get_attribute_names, attrn_p)
try:
name = create_string_buffer(_BUFLEN)
while drmaa_get_next_attr_name(attrn_p.contents, name, _BUFLEN)\
!= const.NO_MORE_ELEMENTS:
yield name.value
except:
drmaa_release_attr_names(attrn_p.contents)
raise
else:
drmaa_release_attr_names(attrn_p.contents)
def vector_attribute_names_iterator():
attrn_p = pointer(POINTER(drmaa_attr_names_t)())
c(drmaa_get_vector_attribute_names, attrn_p)
try:
name = create_string_buffer(_BUFLEN)
while drmaa_get_next_attr_name(attrn_p.contents, name, _BUFLEN)\
!= const.NO_MORE_ELEMENTS:
yield name.value
except:
drmaa_release_attr_names(attrn_p.contents)
raise
else:
drmaa_release_attr_names(attrn_p.contents)
def run_bulk_job(jt, start, end, incr=1):
jids = pointer(POINTER(drmaa_job_ids_t)())
try:
c(drmaa_run_bulk_jobs, jids, jt, start, end, incr)
jid = create_string_buffer(_BUFLEN)
while drmaa_get_next_job_id(jids.contents, jid, _BUFLEN)\
!= const.NO_MORE_ELEMENTS:
yield jid.value
except:
drmaa_release_job_ids(jids.contents)
raise
else:
drmaa_release_job_ids(jids.contents)
def c(f, *args):
"""An helper function wrapping calls to the C DRMAA functions with error managing code."""
return f(*(args + (error_buffer, sizeof(error_buffer))))
def string_vector(v):
vlen = len(v)
values = (STRING * (vlen + 1))()
for i, el in enumerate(v):
values[i] = STRING(el)
values[vlen] = STRING()
return values
def attribute_setter(obj, attribute_name):
"returns a drmaa attribute setter"
def f(value):
"setter for %s" % attribute_name
c(drmaa_set_attribute, obj, attribute_name, value)
f.__name__ = 'set_'+attribute_name
return f
def attribute_getter(obj, attribute_name):
"returns a drmaa attribute setter"
def f():
"getter for %s" % attribute_name
attr_buffer = create_string_buffer(1024)
c(drmaa_get_attribute, obj, attribute_name, attr_buffer, sizeof(attr_buffer))
return attr_buffer.value
f.__name__ = 'get_'+attribute_name
return f
|