From: Christian Seiler Date: Sun, 18 Aug 2013 22:52:44 +0000 (+0200) Subject: python/attach: Add function that returns personality for architecture X-Git-Tag: lxc-1.0.0.alpha1~1^2~49 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c9ec905567952830d58a14d1e3a3ea4e1f8b0041;p=thirdparty%2Flxc.git python/attach: Add function that returns personality for architecture Adds the arch_to_personality function that looks up an architecture and returns the corresponding personality. This may be used in conjunction with the attach/attach_wait keyword argument. Signed-off-by: Christian Seiler Signed-off-by: Serge Hallyn --- diff --git a/src/python-lxc/lxc.c b/src/python-lxc/lxc.c index e0123c097..8d9805062 100644 --- a/src/python-lxc/lxc.c +++ b/src/python-lxc/lxc.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -886,6 +887,35 @@ LXC_attach_run_shell(PyObject *self, PyObject *arg) return PyLong_FromLong(rv); } +static PyObject * +LXC_arch_to_personality(PyObject *self, PyObject *arg) +{ + long rv = -1; + PyObject *pystr; + char *str; + + if (!PyUnicode_Check(arg)) { + PyErr_SetString(PyExc_ValueError, "Expected a string"); + return NULL; + } + + pystr = PyUnicode_AsUTF8String(arg); + if (!pystr) + return NULL; + + str = PyBytes_AsString(pystr); + if (!str) + goto out; + + rv = lxc_config_parse_arch(str); + if (rv == -1) + PyErr_SetString(PyExc_KeyError, "Failed to lookup architecture."); + +out: + Py_DECREF(pystr); + return rv == -1 ? NULL : PyLong_FromLong(rv); +} + static PyObject * LXC_attach_run_command(PyObject *self, PyObject *arg) { @@ -1140,6 +1170,8 @@ static PyMethodDef LXC_methods[] = { "Starts up a shell when attaching, to use as the run parameter for attach or attach_wait"}, {"attach_run_command", (PyCFunction)LXC_attach_run_command, METH_O, "Runs a command when attaching, to use as the run parameter for attach or attach_wait"}, + {"arch_to_personality", (PyCFunction)LXC_arch_to_personality, METH_O, + "Returns the process personality of the corresponding architecture"}, {"get_default_config_path", (PyCFunction)LXC_get_default_config_path, METH_NOARGS, "Returns the current LXC config path"}, {"get_version", (PyCFunction)LXC_get_version, METH_NOARGS, diff --git a/src/python-lxc/lxc/__init__.py b/src/python-lxc/lxc/__init__.py index aecbf8cb0..2dca4401c 100644 --- a/src/python-lxc/lxc/__init__.py +++ b/src/python-lxc/lxc/__init__.py @@ -450,6 +450,14 @@ def attach_run_shell(): """ return _lxc.attach_run_shell(None) +def arch_to_personality(arch): + """ + Determine the process personality corresponding to the architecture + """ + if isinstance(arch, bytes): + arch = str(arch, 'utf-8') + return _lxc.arch_to_personality(arch) + # Some constants for attach LXC_ATTACH_KEEP_ENV = _lxc.LXC_ATTACH_KEEP_ENV LXC_ATTACH_CLEAR_ENV = _lxc.LXC_ATTACH_CLEAR_ENV