aboutsummaryrefslogtreecommitdiffstats
path: root/src/unified_posix_ipc.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/unified_posix_ipc.py')
-rw-r--r--src/unified_posix_ipc.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/unified_posix_ipc.py b/src/unified_posix_ipc.py
new file mode 100644
index 0000000..cf60362
--- /dev/null
+++ b/src/unified_posix_ipc.py
@@ -0,0 +1,76 @@
+# -*- python -*-
+'''
+cmdipc — System V and POSIX IPC from the command line
+Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+'''
+
+import os
+import posix_ipc # http://semanchuk.com/philip/posix_ipc
+
+
+SignalError = posix_ipc.SignalError
+PermissionsError = posix_ipc.PermissionsError
+ExistentialError = posix_ipc.ExistentialError
+BusyError = posix_ipc.BusyError
+
+CREAT = posix_ipc.O_CREAT
+EXCL = posix_ipc.O_EXCL
+
+def keycat(*keys):
+ return ''.join(keys)
+
+def keysep(keys):
+ return ['/' + key for key in keys[1:].split('/')]
+
+class Semaphore(posix_ipc.Semaphore):
+ def __init__(self, *args, **kwargs):
+ posix_ipc.Semaphore.__init__(self, *args, **kwargs)
+ def P(timeout = None):
+ self.acquire(timeout)
+ def V():
+ self.release()
+ def set_value(self, value):
+ for _ in range(value):
+ self.V()
+ def remove(self):
+ self.unlink()
+
+class SharedMemory(posix_ipc.SharedMemory):
+ def __init__(self, *args, **kwargs):
+ posix_ipc.SharedMemory.__init__(self, *args, **kwargs)
+ def read(self, byte_count = 0, offset = 0):
+ rc = []
+ byte_count = min(byte_count, self.size - offset)
+ os.lseek(self.fd, offset, os.SEEK_SET)
+ while len(rc) < byte_count:
+ rc += list(os.read(self.fd), byte_count - len(rc))
+ return bytes(rc)
+ def write(self, s, offset = 0):
+ s = s[:min(len(s), self.size - offset)]
+ os.lseek(self.fd, offset, os.SEEK_SET)
+ while len(s) > 0:
+ s = s[os.write(self.fd, s):]
+ def close(self):
+ self.close_fd()
+ def remove(self):
+ self.unlink()
+
+class MessageQueue(posix_ipc.MessageQueue):
+ def __init__(self, *args, **kwargs):
+ posix_ipc.MessageQueue.__init__(self, *args, **kwargs)
+ def remove(self):
+ self.unlink()
+