Commit 1622e206 authored by Guillaume Tucker's avatar Guillaume Tucker

run-servod: fix all pep8 warnings and errors

* convert tabs to 4 spaces
* fix code formatting and some coding style
Signed-off-by: Guillaume Tucker's avatarGuillaume Tucker <guillaume.tucker@collabora.com>
parent 98977666
......@@ -42,139 +42,143 @@ from servo import client, multiservo, servod
DEV_DIR = None
CHILD_PID = 0
def do_cleanup(status):
shutil.rmtree(DEV_DIR, ignore_errors=True)
shutil.rmtree(DEV_DIR, ignore_errors=True)
try:
os.kill(CHILD_PID, signal.SIGTERM)
except:
pass
try:
os.kill(CHILD_PID, signal.SIGTERM)
except:
pass
sys.exit(status)
sys.exit(status)
def handle_sigterm(sig, stack):
do_cleanup(128 + signal.SIGTERM)
do_cleanup(128 + signal.SIGTERM)
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"])
servo_client = client.ServoClient(host="127.0.0.1", port=port)
results = servo_client.set_get_all(["ec_uart_pty", "cpu_uart_pty"])
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"])))
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)
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 {} on port {}'"
.format(board_name, port))
# 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 {} on port {}'"\
.format(board_name, port))
def run_servod():
logger = logging.getLogger()
try:
dev_path = sys.argv[1]
except:
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")
sys.exit(1)
# XXX: This isn't very pleasant. What I'd hoped to do is use %P from
# the udev rule to get the parent device, but this didn't play
# well with systemd's BindsTo: the service just never started.
# Would be good to fix this.
#
# Anyway, the thinking is: get the device path from the usbmisc
# device, walk up to the usb subdevice which actually represents
# the interface, then walk up to its parent which represents the
# entire device, and thus has the serial property.
try:
ud_dev = pyudev.Device.from_device_file(ud_ctx, dev_path).parent.parent
except:
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 {} has no serial attribute"\
.format(dev_path))
sys.exit(1)
target_serial = ud_dev.attributes.get("serial")
board_name = None
all_boards = multiservo.parse_rc(logger, "/etc/google-servo.conf")
for board in all_boards:
if not all_boards[board]["sn"] == target_serial:
continue
board_name = board
if not board_name:
print("Couldn't get board name for serial {}".format(
target_serial))
sys.exit(91)
print("Board name: {}".format(board_name))
global DEV_DIR
DEV_DIR = os.path.join("/dev/google-servo/", board_name)
os.makedirs(DEV_DIR)
servod_output = os.pipe()
pid = os.fork()
if pid == 0: # child
os.dup2(servod_output[1], 1)
os.dup2(servod_output[1], 2)
os.close(servod_output[0])
os.close(servod_output[1])
# 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")]
servod.main_function()
else: # parent
# Run servod and pull its stdout/stderr.
# FIXME: Smarter mainloop:
# - remove devices when killed.
# - always take child down with us.
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.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")
sys.exit(99)
do_connect(port, board_name)
try:
pass
except:
print("Could not connect to servo daemon")
sys.exit(90)
line = servod_in.readline()
print("Child exited - closing")
(pid_again, child_status) = os.waitpid(CHILD_PID, 0)
do_cleanup(os.WEXITSTATUS(child_status))
logger = logging.getLogger()
try:
dev_path = sys.argv[1]
except:
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")
sys.exit(1)
# XXX: This isn't very pleasant. What I'd hoped to do is use %P from
# the udev rule to get the parent device, but this didn't play
# well with systemd's BindsTo: the service just never started.
# Would be good to fix this.
#
# Anyway, the thinking is: get the device path from the usbmisc
# device, walk up to the usb subdevice which actually represents
# the interface, then walk up to its parent which represents the
# entire device, and thus has the serial property.
try:
ud_dev = pyudev.Device.from_device_file(ud_ctx, dev_path).parent.parent
except:
print("couldn't find udev device from {}".format(dev_path))
sys.exit(1)
if "serial" not in ud_dev.attributes.available_attributes:
print("parent-of-parent device {} has no serial attribute"
.format(dev_path))
sys.exit(1)
target_serial = ud_dev.attributes.get("serial")
board_name = None
all_boards = multiservo.parse_rc(logger, "/etc/google-servo.conf")
for board in all_boards:
if not all_boards[board]["sn"] == target_serial:
continue
board_name = board
if not board_name:
print("Couldn't get board name for serial {}".format(target_serial))
sys.exit(91)
print("Board name: {}".format(board_name))
global DEV_DIR
DEV_DIR = os.path.join("/dev/google-servo/", board_name)
os.makedirs(DEV_DIR)
servod_output = os.pipe()
pid = os.fork()
if pid == 0: # child
os.dup2(servod_output[1], 1)
os.dup2(servod_output[1], 2)
os.close(servod_output[0])
os.close(servod_output[1])
# 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")]
servod.main_function()
else: # parent
# Run servod and pull its stdout/stderr.
# FIXME: Smarter mainloop:
# - remove devices when killed.
# - always take child down with us.
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.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")
sys.exit(99)
do_connect(port, board_name)
try:
pass
except:
print("Could not connect to servo daemon")
sys.exit(90)
line = servod_in.readline()
print("Child exited - closing")
(pid_again, child_status) = os.waitpid(CHILD_PID, 0)
do_cleanup(os.WEXITSTATUS(child_status))
if __name__ == '__main__':
run_servod()
run_servod()
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