blob: 5da9d51a38841a598766eff686b6944b46323262 [file] [log] [blame]
Abhishek Pandit-Subedi7c76aaf2020-06-23 14:47:22 -07001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4# Lint as: python3
5""" Udevadm helper classes and functions.
6"""
7
8import subprocess
9
10class UdevadmInfo():
11 """ Use udevadm info on a specific path.
12 """
13
14 @classmethod
15 def GetProperties(cls, syspath):
16 """ Get all properties of given syspath as a dict.
17
18 Args:
19 syspath: System path to get properties for.
20
21 Returns:
22 Dict with attribute/property as key and it's value. All keys are
23 converted to lowercase. Example: {'subsystem': 'input'}
24 """
25 props = {}
26 rawprops = subprocess.check_output(' '.join(
27 ['udevadm', 'info', '-q', 'property', '-p', syspath]),
28 shell=True)
29
30 for line in rawprops.splitlines():
31 upper_key, value = line.split('=', 1)
32 props[upper_key.lower()] = value.strip('"')
33
34 return props
35
36class UdevadmTrigger():
37 """ Use udevadm trigger with specific rules.
38 """
39
40 def __init__(self,
41 verbose=True,
42 event_type=None,
43 attr_match=[],
44 attr_nomatch=[],
45 subsystem_match=[],
46 subsystem_nomatch=[]):
47 """ Constructor
48
49 Args:
50 verbose: Whether to output triggered syspaths
51 event_type: What type of events to trigger (device or subsystem)
52 attr_match: What attributes to match
53 attr_nomatch: What attributes not to match
54 subsystem_match: What subsystems to match
55 subsystem_nomatch: What subsystems not to match
56 """
57 cmd = ['udevadm', 'trigger']
58
59 if verbose:
60 cmd.append('-v')
61
62 if event_type:
63 cmd.append('-t')
64 cmd.append('"{}"'.format(event_type))
65
66 for attr in attr_match:
67 cmd.append('-a')
68 cmd.append('"{}"'.format(attr))
69
70 for attr in attr_nomatch:
71 cmd.append('-A')
72 cmd.append('"{}"'.format(attr))
73
74 for subsystem in subsystem_match:
75 cmd.append('-s')
76 cmd.append('"{}"'.format(subsystem))
77
78 for subsystem in subsystem_nomatch:
79 cmd.append('-S')
80 cmd.append('"{}"'.format(subsystem))
81
82 self.cmd = cmd
83
84 def DryRun(self):
85 """ Do a dry run using initialized trigger rules.
86
87 Returns:
88 List of syspaths that would be triggered.
89 """
90 cmd = self.cmd + ['-n']
91 lines = subprocess.check_output(' '.join(cmd), shell=True)
92 return lines.splitlines() if lines else []