From 310f605630dcdd63fe234409fa41d92816aa49c5 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Sat, 15 Aug 2009 04:31:45 +0200 Subject: Add module to setup.py --- setup.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'setup.py') diff --git a/setup.py b/setup.py index 01107f1..8628cbb 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,8 @@ import os import sys from distutils.core import setup +from distutils.extension import Extension +from Cython.Distutils import build_ext from portato.constants import VERSION, ICON_DIR, PLUGIN_DIR, TEMPLATE_DIR, APP @@ -38,17 +40,12 @@ data_files = [ (PLUGIN_DIR, plugin_list("gpytage", "notify", "etc_proposals", "reload_portage", "package_details"))] # extension stuff -ext_modules = [] -cmdclass={'build_manpage': build_manpage} +ext_modules = [Extension("portato.mq", ["portato/mq.pyx"])] if "--disable-eix" in sys.argv: sys.argv.remove("--disable-eix") else: - from Cython.Distutils import build_ext - from distutils.extension import Extension - ext_modules.append(Extension("portato.eix.parser", ["portato/eix/parser.pyx"])) - cmdclass['build_ext'] = build_ext packages.append("portato.eix") if "--enable-eix" in sys.argv: @@ -67,5 +64,5 @@ setup(name=APP, packages = packages, data_files = data_files, ext_modules = ext_modules, - cmdclass = cmdclass + cmdclass={'build_manpage': build_manpage, 'build_ext' : build_ext} ) -- cgit v1.2.3-70-g09d2 From 088452702812614cbbbaa64b116f29971920fac2 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Sat, 15 Aug 2009 11:51:15 +0200 Subject: Renamed 'mq' to 'ipc' --- portato/__init__.py | 10 ++-- portato/ipc.pxd | 52 +++++++++++++++++++ portato/ipc.pyx | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++ portato/listener.py | 26 +++++----- portato/mq.pxd | 52 ------------------- portato/mq.pyx | 144 ---------------------------------------------------- setup.py | 2 +- 7 files changed, 215 insertions(+), 215 deletions(-) create mode 100644 portato/ipc.pxd create mode 100644 portato/ipc.pyx delete mode 100644 portato/mq.pxd delete mode 100644 portato/mq.pyx (limited to 'setup.py') diff --git a/portato/__init__.py b/portato/__init__.py index bc1783b..433c89a 100644 --- a/portato/__init__.py +++ b/portato/__init__.py @@ -89,12 +89,12 @@ def start(): else: # start us again in root modus and launch listener - from . import mq + from . import ipc - _mq = mq.MessageQueue(None, mq.MessageQueue.CREAT | mq.MessageQueue.EXCL) + mq = ipc.MessageQueue(None, ipc.MessageQueue.CREAT | ipc.MessageQueue.EXCL) # start listener - lt = threading.Thread(target=get_listener().set_recv, args = (_mq,)) + lt = threading.Thread(target=get_listener().set_recv, args = (mq,)) lt.setDaemon(False) lt.start() @@ -106,7 +106,7 @@ def start(): su = detect_su_command() if su: debug("Using '%s' as su command.", su.bin) - cmd = su.cmd("%s --no-fork --mq %ld" % (sys.argv[0], _mq.key)) + cmd = su.cmd("%s --no-fork --mq %ld" % (sys.argv[0], mq.key)) sp = subprocess.Popen(cmd, env = env) @@ -126,4 +126,4 @@ def start(): get_listener().close() lt.join() - _mq.remove() + mq.remove() diff --git a/portato/ipc.pxd b/portato/ipc.pxd new file mode 100644 index 0000000..64ca05d --- /dev/null +++ b/portato/ipc.pxd @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# +# File: portato/ipc.pxd +# This file is part of the Portato-Project, a graphical portage-frontend. +# +# Copyright (C) 2006-2009 René 'Necoro' Neumann +# This is free software. You may redistribute copies of it under the terms of +# the GNU General Public License version 2. +# There is NO WARRANTY, to the extent permitted by law. +# +# Written by René 'Necoro' Neumann + +from python_string cimport * +from python_mem cimport * + +cdef extern from "errno.h": + int errno + cdef enum: + EACCES, EEXIST, ENOENT, ENOMEM, ENOSPC, + EINVAL, EPERM, EIDRM, EINTR + +cdef extern from *: + int INT_MAX + int RAND_MAX + ctypedef size_t int + int rand() + +cdef extern from "string.h": + char* strerror(int errno) + void* memcpy (void* dst, void* src, size_t len) + +cdef extern from "sys/msg.h" nogil: + cdef enum: + IPC_CREAT, IPC_EXCL, IPC_NOWAIT, + IPC_RMID + + ctypedef int key_t + + struct msqid_ds: + pass + + int msgget(key_t key, int msgflg) + int msgctl(int msqid, int cmd, msqid_ds* buf) + int msgsnd(int msgid, void* msgp, size_t msgsz, int msgflg) + int msgrcv(int msgid, void* msgp, size_t msgsz, long msgtype, int msgflg) + +cdef struct msg_data: + long mtype + char mtext[1] + +cdef enum: + MAX_MESSAGE_SIZE = 2048 diff --git a/portato/ipc.pyx b/portato/ipc.pyx new file mode 100644 index 0000000..4a06102 --- /dev/null +++ b/portato/ipc.pyx @@ -0,0 +1,144 @@ +# -*- coding: utf-8 -*- +# +# File: portato/ipc.pyx +# This file is part of the Portato-Project, a graphical portage-frontend. +# +# Copyright (C) 2006-2009 René 'Necoro' Neumann +# This is free software. You may redistribute copies of it under the terms of +# the GNU General Public License version 2. +# There is NO WARRANTY, to the extent permitted by law. +# +# Written by René 'Necoro' Neumann + +class MessageQueueError(Exception): + pass + +class MessageQueueRemovedError (MessageQueueError): + pass + +cdef class MessageQueue (object): + + CREAT = IPC_CREAT + EXCL = IPC_EXCL + + cdef int msgid + cdef readonly key_t key + + def __init__ (self, key = None, int flags = 0): + + if (flags & IPC_EXCL) and not (flags & IPC_CREAT): + raise MessageQueueError("EXCL must be combined with CREAT.") + + if key is None and not (flags & IPC_EXCL): + raise MessageQueueError("The key can only be None if EXCL is set.") + + # make sure there is nothing ... obscure + flags &= (IPC_CREAT | IPC_EXCL) + + flags |= 0600 # mode + + if key is None: + check = True + while check: + self.key = self.random_key() + self.msgid = msgget(self.key, flags) + check = (self.msgid == -1 and errno == EEXIST) + else: + self.key = key + self.msgid = msgget(key, flags) + + if self.msgid == -1: + if errno == EACCES: + raise MessageQueueError("Permission denied.") + elif errno == EEXIST: + raise MessageQueueError("Queue already exists.") + elif errno == ENOENT: + raise MessageQueueError("Queue does not exist and CREAT is not set.") + elif errno == ENOMEM or errno == ENOSPC: + raise MessageQueueError("Insufficient ressources.") + else: + raise OSError(errno, strerror(errno)) + + def remove (self): + cdef msqid_ds info + cdef int ret + + ret = msgctl(self.msgid, IPC_RMID, &info) + + if ret == -1: + if errno == EIDRM or errno == EINVAL: + raise MessageQueueRemovedError("Queue already removed.") + elif errno == EPERM: + raise MessageQueueError("Permission denied.") + else: + raise OSError(errno, strerror(errno)) + + def send (self, message, int type = 1): + cdef msg_data * msg + cdef int ret + cdef long size = len(message) + + if type <= 0: + raise ValueError("type must be > 0") + + if size >= MAX_MESSAGE_SIZE: + raise ValueError("Message must be smaller than %d", MAX_MESSAGE_SIZE) + + msg = PyMem_Malloc(sizeof(msg_data) + size) + + if msg is NULL: + raise MemoryError("Out of memory") + + memcpy(msg.mtext, message, size) + msg.mtype = type + + with nogil: + ret = msgsnd(self.msgid, msg, size, 0) + + try: + if ret == -1: + if errno == EIDRM or errno == EINVAL: + raise MessageQueueRemovedError("Queue was removed.") + elif errno == EINTR: + raise MessageQueueError("Signaled while waiting.") + elif errno == EACCES: + raise MessageQueueError("Permission denied.") + else: + raise OSError(errno, strerror(errno)) + finally: + PyMem_Free(msg) + + def receive (self): + cdef msg_data * msg + cdef int ret + cdef object retTuple + + msg = PyMem_Malloc(sizeof(msg_data) + MAX_MESSAGE_SIZE) + + if msg is NULL: + raise MemoryError("Out of memory") + + msg.mtype = 0 + + with nogil: + ret = msgrcv(self.msgid, msg, MAX_MESSAGE_SIZE, 0, 0) + + try: + if ret == -1: + if errno == EIDRM or errno == EINVAL: + raise MessageQueueRemovedError("Queue was removed.") + elif errno == EINTR: + raise MessageQueueError("Signaled while waiting.") + elif errno == EACCES: + raise MessageQueueError("Permission denied.") + else: + raise OSError(errno, strerror(errno)) + + retTuple = (PyString_FromStringAndSize(msg.mtext, ret), msg.mtype) + finally: + PyMem_Free(msg) + + return retTuple + + cdef key_t random_key (self): + return (rand() / (RAND_MAX + 1) * INT_MAX) diff --git a/portato/listener.py b/portato/listener.py index 4666269..aa9e31d 100644 --- a/portato/listener.py +++ b/portato/listener.py @@ -32,13 +32,13 @@ class Listener (object): @ivar _send: sender socket @type _send: int""" - def set_recv (self, _mq): + def set_recv (self, mq): - self._mq = _mq + self.mq = mq while True: try: - msg, type = self._mq.receive() + msg, type = self.mq.receive() data = msg.split("\0") debug("Listener received: %s", data) @@ -53,7 +53,7 @@ class Listener (object): debug("Got KeyboardInterrupt. Aborting.") break - self._mq = None + self.mq = None def do_cmd (self, cmdlist): """Starts a command as the user. @@ -76,20 +76,20 @@ class Listener (object): n.set_urgency(int(urgency)) n.show() - def set_send (self, _mq = None): - if _mq is None: + def set_send (self, mq = None): + if mq is None: warning(_("Listener has not been started.")) - self._mq = None + self.mq = None else: - from . import mq + from . import ipc - self._mq = mq.MessageQueue(_mq) + self.mq = ipc.MessageQueue(mq) def __send (self, string): - self._mq.send(string) + self.mq.send(string) def send_notify (self, base = "", descr = "", icon = "", urgency = None): - if self._mq is None: + if self.mq is None: self.do_notify(base, descr, icon, urgency) else: string = "\0".join(["notify", base, descr, icon]) @@ -102,11 +102,11 @@ class Listener (object): self.__send(string) def send_cmd (self, cmdlist): - if self._mq is None: + if self.mq is None: self.do_cmd(cmdlist) else: self.__send("\0".join(["cmd"] +cmdlist)) def close (self): - if self._mq is not None: + if self.mq is not None: self.__send("close") diff --git a/portato/mq.pxd b/portato/mq.pxd deleted file mode 100644 index be7631b..0000000 --- a/portato/mq.pxd +++ /dev/null @@ -1,52 +0,0 @@ -# -*- coding: utf-8 -*- -# -# File: portato/mq.pxd -# This file is part of the Portato-Project, a graphical portage-frontend. -# -# Copyright (C) 2006-2009 René 'Necoro' Neumann -# This is free software. You may redistribute copies of it under the terms of -# the GNU General Public License version 2. -# There is NO WARRANTY, to the extent permitted by law. -# -# Written by René 'Necoro' Neumann - -from python_string cimport * -from python_mem cimport * - -cdef extern from "errno.h": - int errno - cdef enum: - EACCES, EEXIST, ENOENT, ENOMEM, ENOSPC, - EINVAL, EPERM, EIDRM, EINTR - -cdef extern from *: - int INT_MAX - int RAND_MAX - ctypedef size_t int - int rand() - -cdef extern from "string.h": - char* strerror(int errno) - void* memcpy (void* dst, void* src, size_t len) - -cdef extern from "sys/msg.h" nogil: - cdef enum: - IPC_CREAT, IPC_EXCL, IPC_NOWAIT, - IPC_RMID - - ctypedef int key_t - - struct msqid_ds: - pass - - int msgget(key_t key, int msgflg) - int msgctl(int msqid, int cmd, msqid_ds* buf) - int msgsnd(int msgid, void* msgp, size_t msgsz, int msgflg) - int msgrcv(int msgid, void* msgp, size_t msgsz, long msgtype, int msgflg) - -cdef struct msg_data: - long mtype - char mtext[1] - -cdef enum: - MAX_MESSAGE_SIZE = 2048 diff --git a/portato/mq.pyx b/portato/mq.pyx deleted file mode 100644 index e9ce464..0000000 --- a/portato/mq.pyx +++ /dev/null @@ -1,144 +0,0 @@ -# -*- coding: utf-8 -*- -# -# File: portato/mq.pyx -# This file is part of the Portato-Project, a graphical portage-frontend. -# -# Copyright (C) 2006-2009 René 'Necoro' Neumann -# This is free software. You may redistribute copies of it under the terms of -# the GNU General Public License version 2. -# There is NO WARRANTY, to the extent permitted by law. -# -# Written by René 'Necoro' Neumann - -class MessageQueueError(Exception): - pass - -class MessageQueueRemovedError (MessageQueueError): - pass - -cdef class MessageQueue (object): - - CREAT = IPC_CREAT - EXCL = IPC_EXCL - - cdef int msgid - cdef readonly key_t key - - def __init__ (self, key = None, int flags = 0): - - if (flags & IPC_EXCL) and not (flags & IPC_CREAT): - raise MessageQueueError("EXCL must be combined with CREAT.") - - if key is None and not (flags & IPC_EXCL): - raise MessageQueueError("The key can only be None if EXCL is set.") - - # make sure there is nothing ... obscure - flags &= (IPC_CREAT | IPC_EXCL) - - flags |= 0600 # mode - - if key is None: - check = True - while check: - self.key = self.random_key() - self.msgid = msgget(self.key, flags) - check = (self.msgid == -1 and errno == EEXIST) - else: - self.key = key - self.msgid = msgget(key, flags) - - if self.msgid == -1: - if errno == EACCES: - raise MessageQueueError("Permission denied.") - elif errno == EEXIST: - raise MessageQueueError("Queue already exists.") - elif errno == ENOENT: - raise MessageQueueError("Queue does not exist and CREAT is not set.") - elif errno == ENOMEM or errno == ENOSPC: - raise MessageQueueError("Insufficient ressources.") - else: - raise OSError(errno, strerror(errno)) - - def remove (self): - cdef msqid_ds info - cdef int ret - - ret = msgctl(self.msgid, IPC_RMID, &info) - - if ret == -1: - if errno == EIDRM or errno == EINVAL: - raise MessageQueueRemovedError("Queue already removed.") - elif errno == EPERM: - raise MessageQueueError("Permission denied.") - else: - raise OSError(errno, strerror(errno)) - - def send (self, message, int type = 1): - cdef msg_data * msg - cdef int ret - cdef long size = len(message) - - if type <= 0: - raise ValueError("type must be > 0") - - if size >= MAX_MESSAGE_SIZE: - raise ValueError("Message must be smaller than %d", MAX_MESSAGE_SIZE) - - msg = PyMem_Malloc(sizeof(msg_data) + size) - - if msg is NULL: - raise MemoryError("Out of memory") - - memcpy(msg.mtext, message, size) - msg.mtype = type - - with nogil: - ret = msgsnd(self.msgid, msg, size, 0) - - try: - if ret == -1: - if errno == EIDRM or errno == EINVAL: - raise MessageQueueRemovedError("Queue was removed.") - elif errno == EINTR: - raise MessageQueueError("Signaled while waiting.") - elif errno == EACCES: - raise MessageQueueError("Permission denied.") - else: - raise OSError(errno, strerror(errno)) - finally: - PyMem_Free(msg) - - def receive (self): - cdef msg_data * msg - cdef int ret - cdef object retTuple - - msg = PyMem_Malloc(sizeof(msg_data) + MAX_MESSAGE_SIZE) - - if msg is NULL: - raise MemoryError("Out of memory") - - msg.mtype = 0 - - with nogil: - ret = msgrcv(self.msgid, msg, MAX_MESSAGE_SIZE, 0, 0) - - try: - if ret == -1: - if errno == EIDRM or errno == EINVAL: - raise MessageQueueRemovedError("Queue was removed.") - elif errno == EINTR: - raise MessageQueueError("Signaled while waiting.") - elif errno == EACCES: - raise MessageQueueError("Permission denied.") - else: - raise OSError(errno, strerror(errno)) - - retTuple = (PyString_FromStringAndSize(msg.mtext, ret), msg.mtype) - finally: - PyMem_Free(msg) - - return retTuple - - cdef key_t random_key (self): - return (rand() / (RAND_MAX + 1) * INT_MAX) diff --git a/setup.py b/setup.py index 8628cbb..700ab4b 100644 --- a/setup.py +++ b/setup.py @@ -40,7 +40,7 @@ data_files = [ (PLUGIN_DIR, plugin_list("gpytage", "notify", "etc_proposals", "reload_portage", "package_details"))] # extension stuff -ext_modules = [Extension("portato.mq", ["portato/mq.pyx"])] +ext_modules = [Extension("portato.ipc", ["portato/ipc.pyx"])] if "--disable-eix" in sys.argv: sys.argv.remove("--disable-eix") -- cgit v1.2.3-70-g09d2