~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to providers/plainbox-provider-resource-generic/bin/udev2resource

  • Committer: Zygmunt Krynicki
  • Date: 2013-05-29 07:50:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2153.
  • Revision ID: zygmunt.krynicki@canonical.com-20130529075030-ngwz245hs2u3y6us
checkbox: move current checkbox code into checkbox-old

This patch cleans up the top-level directory of the project into dedicated
sub-project directories. One for checkbox-old (the current checkbox and all the
associated stuff), one for plainbox and another for checkbox-ng.

There are some associated changes, such as updating the 'source' mode of
checkbox provider in plainbox, and fixing paths in various test scripts that we
have.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python3
2
 
# This file is part of Checkbox.
3
 
#
4
 
# Copyright 2014 Canonical Ltd.
5
 
# Written by:
6
 
#   Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
7
 
#
8
 
# Checkbox is free software: you can redistribute it and/or modify
9
 
# it under the terms of the GNU General Public License version 3,
10
 
# as published by the Free Software Foundation.
11
 
#
12
 
# Checkbox is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
# GNU General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License
18
 
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
19
 
"""
20
 
udev2resource -- udev to plainbox resource converter
21
 
====================================================
22
 
 
23
 
This script converts the output of 'udev info --export-db' into a RFC822-esque
24
 
PlainBox resource syntax. It handles the P:, N:, E:, S: and L: "directives"
25
 
"""
26
 
 
27
 
import sys
28
 
import re
29
 
 
30
 
 
31
 
def udev2resource(in_stream, out_stream):
32
 
    """
33
 
    Convert the output of 'udev info --export-db' to RFC822 records.
34
 
 
35
 
    :param in_stream:
36
 
        Input stream to process
37
 
    :param out_stream:
38
 
        Output stream to process
39
 
 
40
 
    The syntax is not documented anywhere that I could find but based on simple
41
 
    experiments it looks like a sequence of lines starting with a one
42
 
    letter-code followed by a colon and a value.
43
 
 
44
 
    The following fields are recognized:
45
 
 
46
 
        'P' - device path relative to /sysfs
47
 
        'N' - device path relative to /dev
48
 
        'E' - a key-value attribute
49
 
        'S' - symlink path relative to /dev
50
 
        'L' - unknown field
51
 
    """
52
 
    symlink_count = 0
53
 
    for line in in_stream:
54
 
        line = line.rstrip()
55
 
        if line == '':
56
 
            symlink_count = 0
57
 
        elif line.startswith("P: "):
58
 
            line = line.replace("P: ", "path: ", 1)
59
 
        elif line.startswith("N: "):
60
 
            line = line.replace("N: ", "name: ", 1)
61
 
        elif line.startswith("E: "):
62
 
            line = re.sub("E: ([A-Za-z0-9_]+)=", "attr_\\1: ", line)
63
 
        elif line.startswith("S: "):
64
 
            line = re.sub("S: ", "symlink_{}: ".format(symlink_count), line)
65
 
            symlink_count += 1
66
 
        elif line.startswith("L: "):
67
 
            line = line.replace("L: ", "l_something: ", 1)
68
 
        print(line, file=out_stream)
69
 
 
70
 
 
71
 
if __name__ == '__main__':
72
 
    udev2resource(sys.stdin, sys.stdout)