Skip to contentSkip to conversation

perf test: Replace optparse with argparse in attr.py

PATCH In review Leo Yan 3 participants 4 messages Oct 1 – Oct 2 perf events

lore
Share & tools
lore

Leo Yan

author
#0 lore raw

PATCH 1/1 perf test: Replace optparse with argparse in attr.py

Depending on the Python and pylint versions, pylint can report that optparse is deprecated and fail the lint check. Use argparse to avoid this potential warning.

Preserve the existing options and defaults, and let argparse handle help and unexpected positional arguments.

Tested with:

  # ./perf test -v 'Perf attribute expectations test'
  78: Perf attribute expectations test              : Ok
---
 tools/perf/tests/shell/lib/attr.py | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)
tools/perf/tests/shell/lib/attr.py +14 -23
index 7f3d5b64b00dd7de45baff4f7d9baff89e003147..68f06df9565a081174c788b964a5b051fdedf626 100644
@@ -4,7 +4,7 @@ import configparser
 import os
 import sys
 import glob
-import optparse
+import argparse
 import platform
 import tempfile
 import logging
@@ -426,29 +426,20 @@ def setup_log(verbose):
     ch.setFormatter(formatter)
     log.addHandler(ch)
 
-USAGE = '''%s [OPTIONS]
-  -d dir  # tests dir
-  -p path # perf binary
-  -t test # single test
-  -v      # verbose level
-''' % sys.argv[0]
-
 def main():
-    parser = optparse.OptionParser(usage=USAGE)
-
-    parser.add_option("-t", "--test",
-                      action="store", type="string", dest="test")
-    parser.add_option("-d", "--test-dir",
-                      action="store", type="string", dest="test_dir")
-    parser.add_option("-p", "--perf",
-                      action="store", type="string", dest="perf")
-    parser.add_option("-v", "--verbose",
-                      default=0, action="count", dest="verbose")
-
-    options, args = parser.parse_args()
-    if args:
-        parser.error('FAILED wrong arguments %s' %  ' '.join(args))
-        return -1
+    parser = argparse.ArgumentParser()
+
+    parser.add_argument("-t", "--test",
+                        action="store", dest="test", help="single test")
+    parser.add_argument("-d", "--test-dir",
+                        action="store", dest="test_dir", help="tests dir")
+    parser.add_argument("-p", "--perf",
+                        action="store", dest="perf", help="perf binary")
+    parser.add_argument("-v", "--verbose",
+                        default=0, action="count", dest="verbose",
+                        help="verbose level")
+
+    options = parser.parse_args()
 
     setup_log(options.verbose)
 

---
base-commit: 705da5b15ab89ba97b11eedbe507c2fd83d31cb9
change-id: 20261001-perf_replace_optparse_with_argparse-e7f51849a08c

Best regards,

Signature · 1 line
Leo Yan <[email protected]>
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

Depending on the Python and pylint versions, pylint can report that optparse is deprecated and fail the lint check. Use argparse to avoid this potential warning.

Preserve the existing options and defaults, and let argparse handle help and unexpected positional arguments.

Quoted 7 lines

Tested with:

  # ./perf test -v 'Perf attribute expectations test'
  78: Perf attribute expectations test              : Ok

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20261001-perf_replace_optparse_with_argparse-v1-1-268b604f8e55@arm.com?part=1
reviewer
#2 lore raw
In reply to Leo Yan
Quoted 8 lines · Leo Yan

Depending on the Python and pylint versions, pylint can report that optparse is deprecated and fail the lint check. Use argparse to avoid this potential warning.

Preserve the existing options and defaults, and let argparse handle help and unexpected positional arguments.

Quoted 7 lines

Tested with:

  # ./perf test -v 'Perf attribute expectations test'
  78: Perf attribute expectations test              : Ok

Hi Leo, thanks for the fix! I see Arnaldo already took this equivalent change:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/commit/?h=tmp.perf-tools-next&id=cd116fcbc0bec7340a659e62210b963c49f93c1e

Sorry for the extra work, but again thanks!
Ian

Quoted 69 lines
---
 tools/perf/tests/shell/lib/attr.py | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)
tools/perf/tests/shell/lib/attr.py +14 -24
index 7f3d5b64b00dd7de45baff4f7d9baff89e003147..68f06df9565a081174c788b964a5b051fdedf626 100644
@@ -4,7 +4,7 @@ import configparser
 import os
 import sys
 import glob
-import optparse
+import argparse
 import platform
 import tempfile
 import logging
@@ -426,29 +426,20 @@ def setup_log(verbose):
     ch.setFormatter(formatter)
     log.addHandler(ch)
 
-USAGE = '''%s [OPTIONS]
-  -d dir  # tests dir
-  -p path # perf binary
-  -t test # single test
-  -v      # verbose level
-''' % sys.argv[0]
-
 def main():
-    parser = optparse.OptionParser(usage=USAGE)
-
-    parser.add_option("-t", "--test",
-                      action="store", type="string", dest="test")
-    parser.add_option("-d", "--test-dir",
-                      action="store", type="string", dest="test_dir")
-    parser.add_option("-p", "--perf",
-                      action="store", type="string", dest="perf")
-    parser.add_option("-v", "--verbose",
-                      default=0, action="count", dest="verbose")
-
-    options, args = parser.parse_args()
-    if args:
-        parser.error('FAILED wrong arguments %s' %  ' '.join(args))
-        return -1
+    parser = argparse.ArgumentParser()
+
+    parser.add_argument("-t", "--test",
+                        action="store", dest="test", help="single test")
+    parser.add_argument("-d", "--test-dir",
+                        action="store", dest="test_dir", help="tests dir")
+    parser.add_argument("-p", "--perf",
+                        action="store", dest="perf", help="perf binary")
+    parser.add_argument("-v", "--verbose",
+                        default=0, action="count", dest="verbose",
+                        help="verbose level")
+
+    options = parser.parse_args()
 
     setup_log(options.verbose)
 
 
---

base-commit: 705da5b15ab89ba97b11eedbe507c2fd83d31cb9
change-id: 20261001-perf_replace_optparse_with_argparse-e7f51849a08c

Best regards,
--
Leo Yan <[email protected]>

author
#3 lore raw
Quoted 14 lines · Ian Rogers
Quoted 13 lines · Leo Yan

Depending on the Python and pylint versions, pylint can report that optparse is deprecated and fail the lint check. Use argparse to avoid this potential warning.

Preserve the existing options and defaults, and let argparse handle help and unexpected positional arguments.

Tested with:

  # ./perf test -v 'Perf attribute expectations test'
  78: Perf attribute expectations test              : Ok
Quoted 7 lines
Quoted 2 lines

Hi Leo, thanks for the fix! I see Arnaldo already took this equivalent change:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/commit/?h=tmp.perf-tools-next&id=cd116fcbc0bec7340a659e62210b963c49f93c1e

Sorry for the extra work, but again thanks!

It is fine, thanks let me know.