[CalendarServer-changes] [11432] CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/ python
source_changes at macosforge.org
source_changes at macosforge.org
Wed Jun 26 01:21:51 PDT 2013
Revision: 11432
http://trac.calendarserver.org//changeset/11432
Author: glyph at apple.com
Date: 2013-06-26 01:21:51 -0700 (Wed, 26 Jun 2013)
Log Message:
-----------
Various cleanups.
Modified Paths:
--------------
CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/launchd.py
CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/test/test_launchd.py
Modified: CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/launchd.py
===================================================================
--- CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/launchd.py 2013-06-26 08:21:18 UTC (rev 11431)
+++ CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/launchd.py 2013-06-26 08:21:51 UTC (rev 11432)
@@ -16,12 +16,21 @@
##
"""
-CFFI bindings for launchd check-in API.
+Bindings for launchd check-in API.
+
+ at see: U{SampleD.c
+ <http://developer.apple.com/library/mac/#samplecode/SampleD/>}
+
+ at var ffi: a L{cffi.FFI} instance wrapping the functions exposed by C{launch.h}.
+
+ at var lib: a L{cffi} "U{dynamic library object
+ <http://cffi.readthedocs.org/en/release-0.6/#the-verification-step>}"
+ wrapping the functions exposed by C{launch.h}.
"""
from __future__ import print_function
-from cffi import FFI
+from cffi import FFI, VerificationError
ffi = FFI()
@@ -54,7 +63,6 @@
launch_data_t launch_data_new_fd(int);
launch_data_t launch_data_new_bool(bool);
launch_data_t launch_data_new_real(double);
-
launch_data_t launch_msg(const launch_data_t);
launch_data_type_t launch_data_get_type(const launch_data_t);
@@ -63,8 +71,7 @@
size_t launch_data_dict_get_count(const launch_data_t);
long long launch_data_get_integer(const launch_data_t);
void launch_data_dict_iterate(
- const launch_data_t,
- void (*)(const launch_data_t, const char *, void *),
+ const launch_data_t, void (*)(const launch_data_t, const char *, void *),
void *);
int launch_data_get_fd(const launch_data_t);
@@ -79,13 +86,17 @@
void launch_data_free(launch_data_t);
""")
-lib = ffi.verify("""
-#include <launch.h>
-""")
+try:
+ lib = ffi.verify("""
+ #include <launch.h>
+ """,
+ tag=__name__.replace(".", "_"))
+except VerificationError as ve:
+ raise ImportError(ve)
-class LaunchArray(object):
+class _LaunchArray(object):
def __init__(self, launchdata):
self.launchdata = launchdata
@@ -103,7 +114,7 @@
-class LaunchDictionary(object):
+class _LaunchDictionary(object):
def __init__(self, launchdata):
self.launchdata = launchdata
@@ -162,12 +173,12 @@
Convert a launchd python-like data structure into regular Python
dictionaries and lists.
"""
- if isinstance(x, LaunchDictionary):
+ if isinstance(x, _LaunchDictionary):
result = {}
for k, v in x.items():
result[k] = plainPython(v)
return result
- elif isinstance(x, LaunchArray):
+ elif isinstance(x, _LaunchArray):
return map(plainPython, x)
else:
return x
@@ -185,17 +196,17 @@
def _launchify(launchvalue):
"""
Convert a ctypes value wrapping a C{_launch_data} structure into the
- relevant Python object (integer, bytes, L{LaunchDictionary},
- L{LaunchArray}).
+ relevant Python object (integer, bytes, L{_LaunchDictionary},
+ L{_LaunchArray}).
"""
if launchvalue == ffi.NULL:
return None
dtype = lib.launch_data_get_type(launchvalue)
if dtype == lib.LAUNCH_DATA_DICTIONARY:
- return LaunchDictionary(launchvalue)
+ return _LaunchDictionary(launchvalue)
elif dtype == lib.LAUNCH_DATA_ARRAY:
- return LaunchArray(launchvalue)
+ return _LaunchArray(launchvalue)
elif dtype == lib.LAUNCH_DATA_FD:
return lib.launch_data_get_fd(launchvalue)
elif dtype == lib.LAUNCH_DATA_INTEGER:
@@ -272,3 +283,9 @@
return result
+__all__ = [
+ 'checkin',
+ 'lib',
+ 'ffi',
+ 'plainPython',
+]
Modified: CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/test/test_launchd.py
===================================================================
--- CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/test/test_launchd.py 2013-06-26 08:21:18 UTC (rev 11431)
+++ CalendarServer/branches/users/glyph/launchd-wrapper-bis/twext/python/test/test_launchd.py 2013-06-26 08:21:51 UTC (rev 11432)
@@ -21,6 +21,8 @@
import sys, os, plistlib, socket, json
if __name__ == '__main__':
+ # This module is loaded as a launchd job by test-cases below; the following
+ # code looks up an appropriate function to run.
testID = sys.argv[1]
a, b = testID.rsplit(".", 1)
from twisted.python.reflect import namedAny
@@ -34,10 +36,16 @@
sys.exit(0)
-from twext.python.launchd import (lib, ffi, LaunchDictionary, LaunchArray,
- _managed, constants, plainPython, checkin,
- _launchify)
+try:
+ from twext.python.launchd import (lib, ffi, _LaunchDictionary, _LaunchArray,
+ _managed, constants, plainPython, checkin,
+ _launchify)
+except ImportError:
+ skip = "LaunchD not available."
+else:
+ skip = False
+
from twisted.trial.unittest import TestCase
from twisted.python.filepath import FilePath
@@ -77,7 +85,7 @@
class DictionaryTests(TestCase):
"""
- Tests for L{LaunchDictionary}
+ Tests for L{_LaunchDictionary}
"""
def setUp(self):
@@ -99,58 +107,58 @@
self.assertEquals(lib.launch_data_dict_get_count(self.testDict), 3)
- def test_launchDictionaryLength(self):
+ def test__LaunchDictionaryLength(self):
"""
- C{len(LaunchDictionary())} returns the number of keys in the
+ C{len(_LaunchDictionary())} returns the number of keys in the
dictionary.
"""
- self.assertEquals(len(LaunchDictionary(self.testDict)), 3)
+ self.assertEquals(len(_LaunchDictionary(self.testDict)), 3)
- def test_launchDictionaryKeys(self):
+ def test__LaunchDictionaryKeys(self):
"""
- L{LaunchDictionary.keys} returns keys present in a C{launch_data_dict}.
+ L{_LaunchDictionary.keys} returns keys present in a C{launch_data_dict}.
"""
- dictionary = LaunchDictionary(self.testDict)
+ dictionary = _LaunchDictionary(self.testDict)
self.assertEquals(set(dictionary.keys()),
set([b"alpha", b"beta", b"gamma"]))
- def test_launchDictionaryValues(self):
+ def test__LaunchDictionaryValues(self):
"""
- L{LaunchDictionary.values} returns keys present in a
+ L{_LaunchDictionary.values} returns keys present in a
C{launch_data_dict}.
"""
- dictionary = LaunchDictionary(self.testDict)
+ dictionary = _LaunchDictionary(self.testDict)
self.assertEquals(set(dictionary.values()),
set([b"alpha-value", b"beta-value", 3]))
- def test_launchDictionaryItems(self):
+ def test__LaunchDictionaryItems(self):
"""
- L{LaunchDictionary.items} returns all (key, value) tuples present in a
+ L{_LaunchDictionary.items} returns all (key, value) tuples present in a
C{launch_data_dict}.
"""
- dictionary = LaunchDictionary(self.testDict)
+ dictionary = _LaunchDictionary(self.testDict)
self.assertEquals(set(dictionary.items()),
set([(b"alpha", b"alpha-value"),
(b"beta", b"beta-value"), (b"gamma", 3)]))
- def test_launchDictionaryPlainPython(self):
+ def test__LaunchDictionaryPlainPython(self):
"""
- L{plainPython} will convert a L{LaunchDictionary} into a Python
+ L{plainPython} will convert a L{_LaunchDictionary} into a Python
dictionary.
"""
self.assertEquals({b"alpha": b"alpha-value", b"beta": b"beta-value",
b"gamma": 3},
- plainPython(LaunchDictionary(self.testDict)))
+ plainPython(_LaunchDictionary(self.testDict)))
- def test_nestedLaunchDictionaryPlainPython(self):
+ def test_nested_LaunchDictionaryPlainPython(self):
"""
- L{plainPython} will convert a L{LaunchDictionary} containing another
- L{LaunchDictionary} into a nested Python dictionary.
+ L{plainPython} will convert a L{_LaunchDictionary} containing another
+ L{_LaunchDictionary} into a nested Python dictionary.
"""
otherDict = lib.launch_data_alloc(lib.LAUNCH_DATA_DICTIONARY)
lib.launch_data_dict_insert(otherDict,
@@ -158,12 +166,12 @@
lib.launch_data_dict_insert(self.testDict, otherDict, "delta")
self.assertEquals({b"alpha": b"alpha-value", b"beta": b"beta-value",
b"gamma": 3, b"delta": {b"foo": b"bar"}},
- plainPython(LaunchDictionary(self.testDict)))
+ plainPython(_LaunchDictionary(self.testDict)))
class ArrayTests(TestCase):
"""
- Tests for L{LaunchArray}
+ Tests for L{_LaunchArray}
"""
def setUp(self):
@@ -188,16 +196,16 @@
def test_length(self):
"""
- C{len(LaunchArray(...))} returns the number of elements in the array.
+ C{len(_LaunchArray(...))} returns the number of elements in the array.
"""
- self.assertEquals(len(LaunchArray(self.testArray)), 3)
+ self.assertEquals(len(_LaunchArray(self.testArray)), 3)
def test_indexing(self):
"""
- C{LaunchArray(...)[n]} returns the n'th element in the array.
+ C{_LaunchArray(...)[n]} returns the n'th element in the array.
"""
- array = LaunchArray(self.testArray)
+ array = _LaunchArray(self.testArray)
self.assertEquals(array[0], b"test-string-1")
self.assertEquals(array[1], b"another string.")
self.assertEquals(array[2], 4321)
@@ -205,18 +213,18 @@
def test_indexTooBig(self):
"""
- C{LaunchArray(...)[n]}, where C{n} is greater than the length of the
+ C{_LaunchArray(...)[n]}, where C{n} is greater than the length of the
array, raises an L{IndexError}.
"""
- array = LaunchArray(self.testArray)
+ array = _LaunchArray(self.testArray)
self.assertRaises(IndexError, lambda: array[3])
def test_iterating(self):
"""
- Iterating over a C{LaunchArray} returns each item in sequence.
+ Iterating over a C{_LaunchArray} returns each item in sequence.
"""
- array = LaunchArray(self.testArray)
+ array = _LaunchArray(self.testArray)
i = iter(array)
self.assertEquals(i.next(), b"test-string-1")
self.assertEquals(i.next(), b"another string.")
@@ -226,22 +234,22 @@
def test_plainPython(self):
"""
- L{plainPython} converts a L{LaunchArray} into a Python list.
+ L{plainPython} converts a L{_LaunchArray} into a Python list.
"""
- array = LaunchArray(self.testArray)
+ array = _LaunchArray(self.testArray)
self.assertEquals(plainPython(array),
[b"test-string-1", b"another string.", 4321])
def test_plainPythonNested(self):
"""
- L{plainPython} converts a L{LaunchArray} containing another
- L{LaunchArray} into a Python list.
+ L{plainPython} converts a L{_LaunchArray} containing another
+ L{_LaunchArray} into a Python list.
"""
sub = lib.launch_data_alloc(lib.LAUNCH_DATA_ARRAY)
lib.launch_data_array_set_index(sub, lib.launch_data_new_integer(7), 0)
lib.launch_data_array_set_index(self.testArray, sub, 3)
- array = LaunchArray(self.testArray)
+ array = _LaunchArray(self.testArray)
self.assertEqual(plainPython(array), [b"test-string-1",
b"another string.", 4321, [7]])
@@ -291,8 +299,10 @@
env["TESTING_PORT"] = repr(port.getHost().port)
self.stdout = fp.child("stdout.txt")
self.stderr = fp.child("stderr.txt")
+ self.launchLabel = ("org.calendarserver.UNIT-TESTS." +
+ str(os.getpid()) + "." + self.id())
plist = {
- "Label": "org.calendarserver.UNIT-TESTS." + repr(os.getpid()),
+ "Label": self.launchLabel,
"ProgramArguments": [sys.executable, "-m", __name__, self.id()],
"EnvironmentVariables": env,
"KeepAlive": False,
@@ -340,6 +350,7 @@
structure.
"""
d = json.loads(self.stdout.getContent())
+ self.assertEqual(d[constants.LAUNCH_JOBKEY_LABEL], self.launchLabel)
self.assertIsInstance(d, dict)
sockets = d[constants.LAUNCH_JOBKEY_SOCKETS]
self.assertEquals(len(sockets), 1)
@@ -350,7 +361,11 @@
def tearDown(self):
- os.spawnlp(os.P_WAIT, "launchctl", "launchctl", "unload", self.job.path)
+ """
+ Un-load the launchd job and report any errors it encountered.
+ """
+ os.spawnlp(os.P_WAIT, "launchctl",
+ "launchctl", "unload", self.job.path)
err = self.stderr.getContent()
if 'Traceback' in err:
self.fail(err)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130626/ee979e16/attachment-0001.html>
More information about the calendarserver-changes
mailing list