1#!/usr/bin/env python
2# Copyright 2017 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Parse the output of 'huddly-updater --info --log_to=stdout'.
6"""
7
8from __future__ import print_function
9
10TOKEN_FW_CHUNK_HEADER = 'Firmware package:'
11TOKEN_PERIPHERAL_CHUNK_HEADER = 'Camera Peripheral:'
12TOKEN_BOOT = 'bootloader:'
13TOKEN_APP = 'app:'
14TOKEN_REV = 'hw_rev:'
15
16
17def parse_fw_vers(chunk):
18    """Parse huddly-updater command output.
19
20    The parser logic heavily depends on the output format.
21
22    @param chunk: The huddly-updater output. See CHUNK_FILENAME for example.
23
24    @returns a dictionary containing the version strings
25            for the firmware package and for the peripheral.
26    """
27    dic = {}
28    target = ''
29    for line in chunk.split('\n'):
30        if TOKEN_FW_CHUNK_HEADER in line:
31            target = 'package'
32            dic[target] = {}
33            continue
34        elif TOKEN_PERIPHERAL_CHUNK_HEADER in line:
35            target = 'peripheral'
36            dic[target] = {}
37            continue
38
39        if not target:
40            continue
41
42        fields = line.split(':')
43        if fields.__len__() < 2:
44            continue
45
46        val = fields[1].strip()
47
48        if TOKEN_BOOT in line:
49            dic[target]['boot'] = val
50        elif TOKEN_APP in line:
51            dic[target]['app'] = val
52        elif TOKEN_REV in line:
53            dic[target]['hw_rev'] = val
54        else:
55            continue
56
57    return dic
58