Merge ~jocave/plainbox-provider-checkbox:i2c-argparse-rename into plainbox-provider-checkbox:master

Proposed by Jonathan Cave
Status: Merged
Approved by: Jonathan Cave
Approved revision: 7f6cc60e1763b46301447ab5bc011f91d1643cfc
Merged at revision: eb04c11c105d005cc802fd624d745ef7a0a4c850
Proposed branch: ~jocave/plainbox-provider-checkbox:i2c-argparse-rename
Merge into: plainbox-provider-checkbox:master
Diff against target: 145 lines (+30/-37)
2 files modified
bin/i2c_driver_test.py (+27/-34)
units/i2c/jobs.pxu (+3/-3)
Reviewer Review Type Date Requested Status
Paul Larson Approve
Review via email: mp+378053@code.launchpad.net

Description of the change

Rename i2c_driver_test script and swap guacamole for argparse

To post a comment you must log in.
Revision history for this message
Paul Larson (pwlars) wrote :

We could probably drop invoked and classes for the subcommands, but nothing wrong with it like this. lgtm, +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/bin/i2c_driver_test b/bin/i2c_driver_test.py
2similarity index 66%
3rename from bin/i2c_driver_test
4rename to bin/i2c_driver_test.py
5index 4af3bf8..c6aae78 100755
6--- a/bin/i2c_driver_test
7+++ b/bin/i2c_driver_test.py
8@@ -1,28 +1,28 @@
9 #!/usr/bin/env python3
10-# Copyright 2016 Canonical Ltd.
11+# Copyright 2016-2020 Canonical Ltd.
12 # All rights reserved.
13 #
14 # Written by:
15 # Authors: Gavin Lin <gavin.lin@canonical.com>
16 # Sylvain Pineau <sylvain.pineau@canonical.com>
17+# Jonathan Cave <jonathan.cave@canonical.com>
18
19 """
20-This script will check number of detected I2C buses or devices
21+This script will check number of detected I2C buses or devices
22
23-To see how to use, please run "./i2c_driver_test"
24+To see how to use, please run "./i2c_driver_test.py"
25 """
26
27+import argparse
28 import os
29 import subprocess
30
31-from guacamole import Command
32
33-
34-class Bus(Command):
35+class Bus():
36
37 """Detect I2C bus."""
38
39- def invoked(self, ctx):
40+ def invoked(self, args):
41 """Method called when the command is invoked."""
42 # Detect I2C buses and calculate number of them
43 result = subprocess.check_output(['i2cdetect', '-l'],
44@@ -37,25 +37,19 @@ class Bus(Command):
45
46 # Verify if detected number of buses is as expected
47 else:
48- if ctx.args.bus != 0:
49- if bus_number == ctx.args.bus:
50+ if args.bus != 0:
51+ if bus_number == args.bus:
52 print('Test passed')
53 else:
54 raise SystemExit('Test failed, expecting {} I2C '
55- 'buses.'.format(ctx.args.bus))
56-
57- def register_arguments(self, parser):
58- """Register command line arguments for the bus sub-command."""
59- parser.add_argument(
60- '-b', '--bus', type=int, help='Expected number of I2C bus.',
61- default=0)
62+ 'buses.'.format(args.bus))
63
64
65-class Device(Command):
66+class Device():
67
68 """Detect I2C device."""
69
70- def invoked(self, ctx):
71+ def invoked(self, args):
72 # Make sure that we have root privileges
73 if os.geteuid() != 0:
74 raise SystemExit('Error: please run this command as root')
75@@ -78,30 +72,29 @@ class Device(Command):
76 for l in result_line:
77 address_value = l.strip('\n').split(':')[1].split()
78 for v in address_value:
79- if v != '--': exit_code = 0
80+ if v != '--':
81+ exit_code = 0
82 if exit_code == 1:
83 raise SystemExit('No I2C device detected on any I2C bus')
84- else:
85- print('I2C device detected')
86- return exit_code
87+ print('I2C device detected')
88
89
90-class I2cDriverTest(Command):
91+class I2cDriverTest():
92
93 """I2C driver test."""
94
95- sub_commands = (
96- ('bus', Bus),
97- ('device', Device)
98- )
99-
100- def invoked(self, ctx):
101- """Method called when the command is invoked."""
102- if not ctx.early_args.rest:
103- ctx.parser.print_help()
104- return 1
105+ def main(self):
106+ subcommands = {
107+ 'bus': Bus,
108+ 'device': Device
109+ }
110+ parser = argparse.ArgumentParser()
111+ parser.add_argument('subcommand', type=str, choices=subcommands)
112+ parser.add_argument('-b', '--bus', type=int, default=0,
113+ help='Expected number of I2C bus.')
114+ args = parser.parse_args()
115+ subcommands[args.subcommand]().invoked(args)
116
117
118 if __name__ == '__main__':
119 I2cDriverTest().main()
120-
121diff --git a/units/i2c/jobs.pxu b/units/i2c/jobs.pxu
122index db90509..a928917 100644
123--- a/units/i2c/jobs.pxu
124+++ b/units/i2c/jobs.pxu
125@@ -16,9 +16,9 @@ _steps:
126 detected there's at least one i2c bus.
127 command:
128 if [ -z ${I2C_BUS_NUMBER+x} ]; then
129- i2c_driver_test bus
130+ i2c_driver_test.py bus
131 else
132- i2c_driver_test bus -b $I2C_BUS_NUMBER
133+ i2c_driver_test.py bus -b $I2C_BUS_NUMBER
134 fi
135 user: root
136 plugin: shell
137@@ -37,7 +37,7 @@ _steps:
138 1. This task is fully automatic, test will pass if there's at least one
139 i2c device detected on any I2C bus.
140 command:
141- i2c_driver_test device
142+ i2c_driver_test.py device
143 user: root
144 plugin: shell
145 category_id: i2c

Subscribers

People subscribed via source and target branches