1
0
Fork 0

Fix wrapper.py #lol

This commit is contained in:
Gregory Eremin 2015-02-11 02:51:52 +07:00
parent e789d02e47
commit 30c8711798
1 changed files with 9 additions and 9 deletions

View File

@ -11,8 +11,6 @@ if len(sys.argv) != 2:
exit(1) exit(1)
unit = sys.argv[1] unit = sys.argv[1]
socket_path = '/tmp/unit_%s.sock' % unit
config = json.load(open(UNITS_CONFIG_PATH)).get(unit, None) config = json.load(open(UNITS_CONFIG_PATH)).get(unit, None)
if not config: if not config:
print('Unknown unit: %s' % unit) print('Unknown unit: %s' % unit)
@ -24,26 +22,28 @@ unit_module = unit_path.pop()
unit_path = '.'.join(unit_path) unit_path = '.'.join(unit_path)
sys.path.append(YASEN_PATH) sys.path.append(YASEN_PATH)
units = __import__(unit_path, fromlist=[unit_module]) _units = __import__(unit_path, fromlist=[unit_module])
unit = getattr(units, unit_module) _unit = getattr(_units, unit_module)
actor = getattr(unit, unit_func) actor = getattr(_unit, unit_func)
def process(input): def process(input):
data = json.loads(input.decode('utf-8')) data = json.loads(input.decode('utf-8'))
new_data = actor(data) new_data = actor(data)
return json.dumps(new_data).encode('utf-8') return json.dumps(new_data).encode('utf-8')
SOCKET_PATH = '/tmp/unit_%s.sock' % unit
# Make sure the socket does not already exist # Make sure the socket does not already exist
try: os.unlink(socket_path) try: os.unlink(SOCKET_PATH)
except OSError: except OSError:
if os.path.exists(socket_path): raise if os.path.exists(SOCKET_PATH): raise
# Create a UDS socket # Create a UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Bind the socket to the port # Bind the socket to the port
print('Listening on socket %s' % socket_path) print('Listening on socket %s' % SOCKET_PATH)
sock.bind(socket_path) sock.bind(SOCKET_PATH)
# Listen for incoming connections # Listen for incoming connections
sock.listen(1) sock.listen(1)