Commit 98977666 authored by Guillaume Tucker's avatar Guillaume Tucker

run-servod: code clean-up

This is a quick refresh of the code to make it more maintainable
and closer to Python 3.

* update copyright notice
* use print function
* use shutil.rmtree to simplify code
* use os.path.join to make paths
* make global variables UPPER case
* "don't repeat yourself" in some places
Signed-off-by: Guillaume Tucker's avatarGuillaume Tucker <guillaume.tucker@collabora.com>
parent f1fd314b
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2015 Collabora, Ltd.
# Copyright © 2015, 2017 Collabora, Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
......@@ -23,6 +23,7 @@
# DEALINGS IN THE SOFTWARE.
#
# Author: Daniel Stone <daniels@collabora.com>
# Author: Guillaume Tucker <guillaume.tucker@collabora.com>
#
#
# A wrapper around servod: fork servod itself, wait for it to start, and create
......@@ -31,33 +32,21 @@
import logging
import os
import pyudev
import shutil
import signal
import sys
from servo import client, multiservo, servod
# Icky global variables. But then, icky signal handling.
dev_dir = None
child_pid = 0
DEV_DIR = None
CHILD_PID = 0
def do_cleanup(status):
try:
os.unlink("%s/ec-uart" % dev_dir)
except:
pass
try:
os.unlink("%s/cpu-uart" % dev_dir)
except:
pass
shutil.rmtree(DEV_DIR, ignore_errors=True)
try:
os.rmdir("%s" % dev_dir)
except:
pass
try:
os.kill(pid, signal.SIGTERM)
os.kill(CHILD_PID, signal.SIGTERM)
except:
pass
......@@ -65,25 +54,22 @@ def do_cleanup(status):
def handle_sigterm(sig, stack):
do_cleanup(128 + signal.SIGTERM)
sys.exit(0)
def do_connect(port, board_name):
servo_client = client.ServoClient(host="127.0.0.1", port=port)
results = servo_client.set_get_all(["ec_uart_pty", "cpu_uart_pty"])
print "EC UART: %s" % results[0]
print "CPU UART: %s" % results[1]
os.symlink(results[0], "%s/ec-uart" % dev_dir)
os.symlink(results[1], "%s/cpu-uart" % dev_dir)
for pts, dev in zip(results, ["ec", "cpu"]):
print("{} UART: {}".format(dev.upper(), pts))
os.symlink(pts, os.path.join(DEV_DIR, "-".join([dev, "uart"])))
signal.signal(signal.SIGTERM, handle_sigterm)
# Yes, this should really be python-systemd; however, that's
# only available for python3, and servo only works with
# python2 ...
os.system("systemd-notify --ready --status='Board %s on port %d'" % (board_name, port))
os.system("systemd-notify --ready --status='Board {} on port {}'"\
.format(board_name, port))
def run_servod():
logger = logging.getLogger()
......@@ -91,12 +77,12 @@ def run_servod():
try:
dev_path = sys.argv[1]
except:
print "usage: %s device-path" % sys.argv[0]
print("usage: {} device-path".format(sys.argv[0]))
sys.exit(1)
ud_ctx = pyudev.Context()
if not ud_ctx:
print "couldn't create udev context"
print("couldn't create udev context")
sys.exit(1)
# XXX: This isn't very pleasant. What I'd hoped to do is use %P from
......@@ -111,11 +97,12 @@ def run_servod():
try:
ud_dev = pyudev.Device.from_device_file(ud_ctx, dev_path).parent.parent
except:
print "couldn't find udev device from %s" % dev_path
print("couldn't find udev device from {}".format(dev_path))
sys.exit(1)
if not "serial" in ud_dev.attributes.available_attributes:
print "parent-of-parent device %s has no serial attribute" % dev_path
print("parent-of-parent device {} has no serial attribute"\
.format(dev_path))
sys.exit(1)
target_serial = ud_dev.attributes.get("serial")
......@@ -128,14 +115,15 @@ def run_servod():
board_name = board
if not board_name:
print "Couldn't get board name for serial %s" % target_serial
print("Couldn't get board name for serial {}".format(
target_serial))
sys.exit(91)
print "Board name: %s" % board_name
print("Board name: {}".format(board_name))
global dev_dir
dev_dir = "/dev/google-servo/%s" % board_name
os.makedirs(dev_dir)
global DEV_DIR
DEV_DIR = os.path.join("/dev/google-servo/", board_name)
os.makedirs(DEV_DIR)
servod_output = os.pipe()
pid = os.fork()
......@@ -148,7 +136,7 @@ def run_servod():
# A comment in servod says they should fix it to not parse
# sys.argv directly. Quite.
sys.argv = ['servod', '--rcfile', '/etc/google-servo.conf',
'--serialname', ud_dev.attributes.get("serial")]
'--serialname', ud_dev.attributes.get("serial")]
servod.main_function()
else: # parent
# Run servod and pull its stdout/stderr.
......@@ -156,36 +144,36 @@ def run_servod():
# - remove devices when killed.
# - always take child down with us.
global child_pid
child_pid = pid
global CHILD_PID
CHILD_PID = pid
servod_in = os.fdopen(servod_output[0])
os.close(servod_output[1])
line = servod_in.readline()
while line:
print line
print(line.strip())
# Oh dear.
if 'INFO - Listening on' in line:
try:
port = int(line.split(' ')[11])
except:
print "Couldn't get port - shut it all down"
print("Couldn't get port - shut it all down")
sys.exit(99)
do_connect(port, board_name)
try:
pass
except:
print "Could not connect to servo daemon"
print("Could not connect to servo daemon")
sys.exit(90)
line = servod_in.readline()
print "Child exited - closing"
print("Child exited - closing")
(pid_again, child_status) = os.waitpid(child_pid, 0)
(pid_again, child_status) = os.waitpid(CHILD_PID, 0)
do_cleanup(os.WEXITSTATUS(child_status))
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment