Files
modoboa/modoboa/lib/sysutils.py
T
Antoine Nguyen 22c811f3da wip: relay domains support.
* Integration with the amavis frontend
* Per relay domain policies
* One policy shared between a relay domain and its aliases
* One 'users' record per object
* Extra: better event handlers management

see #491
2013-11-01 16:48:04 +01:00

41 lines
1.1 KiB
Python

# coding: utf-8
"""
This module extra functions/shortcuts to communicate with the system
(executing commands, etc.)
"""
import subprocess
import inspect
import re
def exec_cmd(cmd, sudo_user=None, **kwargs):
"""Execute a shell command.
Run a command using the current user. Set :keyword:`sudo_user` if
you need different privileges.
:param str cmd: the command to execute
:param str sudo_user: a valid system username
:rtype: tuple
:return: return code, command output
"""
if sudo_user is not None:
cmd = "sudo -u %s %s" % (sudo_user, cmd)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, **kwargs)
output = p.communicate()[0]
return p.returncode, output
def guess_extension_name():
"""Tries to guess the application's name by inspecting the stack.
:return: a string or None
"""
modname = inspect.getmodule(inspect.stack()[2][0]).__name__
match = re.match(r"(?:modoboa\.)?(?:extensions\.)?([^\.$]+)", modname)
if match is not None:
return match.group(1)
return None