Merge ~sylvain-pineau/plainbox-provider-resource:udev_resource_filter_option into plainbox-provider-resource:master

Proposed by Sylvain Pineau
Status: Merged
Approved by: Sylvain Pineau
Approved revision: e8dabc847112686c1fc9853016c6dba5afa3a0bf
Merged at revision: c69eba2f4239d7c4818958ec177e2df4dad90447
Proposed branch: ~sylvain-pineau/plainbox-provider-resource:udev_resource_filter_option
Merge into: plainbox-provider-resource:master
Diff against target: 114 lines (+35/-11)
1 file modified
bin/udev_resource (+35/-11)
Reviewer Review Type Date Requested Status
Sylvain Pineau (community) Approve
Review via email: mp+322940@code.launchpad.net

Description of the change

When reviewing the plainbox-provider-snappy sru branch, I've found that we're still using the run/filter-template scripts and having to duplicate those legacy tools should be avoided.

Since they are only used to perform various filter operations, this MR proposes to port the feature to the udev_resource script itself.

A patch for the checkbox provider will follow.

[1] https://code.launchpad.net/~jocave/plainbox-provider-snappy/+git/plainbox-provider-snappy/+ref/sru-plans-and-jobs

To post a comment you must log in.
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

self-approved

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/bin/udev_resource b/bin/udev_resource
2index c389fee..3521530 100755
3--- a/bin/udev_resource
4+++ b/bin/udev_resource
5@@ -19,6 +19,7 @@
6 #
7 import argparse
8 import shlex
9+import sys
10
11 from collections import OrderedDict
12 from subprocess import check_output, CalledProcessError
13@@ -26,22 +27,21 @@ from subprocess import check_output, CalledProcessError
14 from checkbox_support.parsers.udevadm import UdevadmParser
15
16 categories = ("ACCELEROMETER", "AUDIO", "BLUETOOTH", "CAPTURE", "CARDREADER",
17- "DISK", "KEYBOARD", "MOUSE", "NETWORK", "OTHER", "TOUCHPAD",
18- "TOUCHSCREEN", "USB", "VIDEO", "WIRELESS")
19+ "CDROM", "DISK", "KEYBOARD", "MOUSE", "NETWORK", "OTHER",
20+ "TOUCHPAD", "TOUCHSCREEN", "USB", "VIDEO", "WIRELESS", "WWAN")
21
22+attributes = ("path", "name", "bus", "category", "driver", "product_id",
23+ "vendor_id", "subproduct_id", "subvendor_id", "product",
24+ "vendor", "interface", "mac", "product_slug", "vendor_slug")
25
26-class UdevResultDump:
27
28- attributes = ("path", "name", "bus", "category", "driver", "product_id",
29- "vendor_id", "subproduct_id", "subvendor_id", "product",
30- "vendor", "interface", "mac", "product_slug", "vendor_slug")
31+class UdevResultDump:
32
33 def addDevice(self, device):
34- for attribute in self.attributes:
35+ for attribute in attributes:
36 value = getattr(device, attribute)
37 if value is not None:
38 print("%s: %s" % (attribute, value))
39-
40 print()
41
42
43@@ -50,19 +50,33 @@ class UdevResultLister:
44 def __init__(self, categories):
45 self.categories = categories
46 self._data = OrderedDict()
47+ self._devices = []
48 for c in categories:
49 self._data[c] = []
50
51 def display(self):
52+ if not self._devices:
53+ return 1
54 for c, devices in self._data.items():
55 print("{} ({}):".format(c, len(devices)))
56 for d in devices:
57 print(" - {}".format(d))
58 print()
59
60+ def filter(self):
61+ if not self._devices:
62+ return 1
63+ for device in self._devices:
64+ for attribute in attributes:
65+ value = getattr(device, attribute)
66+ if value is not None:
67+ print("%s: %s" % (attribute, value))
68+ print()
69+
70 def addDevice(self, device):
71 c = getattr(device, "category", None)
72 if c in self.categories:
73+ self._devices.append(device)
74 p = getattr(device, "product", "Unknow product")
75 v = getattr(device, "vendor", "Unknow vendor")
76 self._data[c].append("{} {}".format(v, p))
77@@ -78,12 +92,18 @@ def main():
78 default="lsblk -i -n -P -o KNAME,TYPE,MOUNTPOINT",
79 help="""Command to execute to get lsblk information.
80 Only change it if you know what you're doing.""")
81- parser.add_argument('-l','--list', nargs='+', choices=categories,
82+ parser.add_argument('-l', '--list', nargs='+', choices=categories,
83 metavar=("CATEGORY"),
84 help="""List devices found under the requested
85 categories.
86 Acceptable categories to list are:
87 {}""".format(', '.join(categories)))
88+ parser.add_argument('-f', '--filter', nargs='+', choices=categories,
89+ metavar=("CATEGORY"),
90+ help="""Filter devices found under the requested
91+ categories.
92+ Acceptable categories to list are:
93+ {}""".format(', '.join(categories)))
94 args = parser.parse_args()
95 try:
96 output = check_output(shlex.split(args.command))
97@@ -98,11 +118,15 @@ def main():
98 if args.list:
99 result = UdevResultLister(args.list)
100 udev.run(result)
101- result.display()
102+ return result.display()
103+ if args.filter:
104+ result = UdevResultLister(args.filter)
105+ udev.run(result)
106+ return result.filter()
107 else:
108 result = UdevResultDump()
109 udev.run(result)
110
111
112 if __name__ == "__main__":
113- main()
114+ sys.exit(main())

Subscribers

People subscribed via source and target branches