Skip to contentSkip to conversation

Refactor and improve full help output from tools

PATCH Superseded Paul Barker 2 participants 5 messages Aug 13, 2021 – Sep 1, 2021

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Refactor and improve full help output from tools

Sorry this took so long to get back to! Got there in the end though :)

Changes from v1:
* Collected the full help printing code from patman, buildman & binman into a single function so that when support for PAGER containing arguments is added it applies to all the relevant tools.

Paul Barker (2):
tools: Refactor full help printing
tools: Handle PAGER containing arguments

 tools/binman/control.py   |  9 +++------
 tools/buildman/control.py | 10 ++++------
 tools/patman/main.py      | 12 ++++--------
 tools/patman/tools.py     | 14 ++++++++++++++
 4 files changed, 25 insertions(+), 20 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/2 tools: Refactor full help printing

Collect the code for printing the full help message of patman, buildman and binman into a single function in patman.tools.

---
 tools/binman/control.py   |  9 +++------
 tools/buildman/control.py | 10 ++++------
 tools/patman/main.py      | 12 ++++--------
 tools/patman/tools.py     | 13 +++++++++++++
 4 files changed, 24 insertions(+), 20 deletions(-)
tools/binman/control.py +3 -6
index dcba02ff7f..0dbcbc28e9 100644
@@ -565,12 +565,9 @@ def Binman(args):
     global state
 
     if args.full_help:
-        pager = os.getenv('PAGER')
-        if not pager:
-            pager = 'more'
-        fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
-                            'README.rst')
-        command.Run(pager, fname)
+        tools.PrintFullHelp(
+            os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'README.rst')
+        )
         return 0
 
     # Put these here so that we can import this module without libfdt
tools/buildman/control.py +4 -6
index a98d1b4c06..fd9664c85d 100644
@@ -16,6 +16,7 @@ from patman import command
 from patman import gitutil
 from patman import patchstream
 from patman import terminal
+from patman import tools
 from patman.terminal import Print
 
 def GetPlural(count):
@@ -133,12 +134,9 @@ def DoBuildman(options, args, toolchains=None, make_func=None, boards=None,
     global builder
 
     if options.full_help:
-        pager = os.getenv('PAGER')
-        if not pager:
-            pager = 'more'
-        fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
-                             'README')
-        command.Run(pager, fname)
+        tools.PrintFullHelp(
+            os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'README')
+        )
         return 0
 
     gitutil.Setup()
tools/patman/main.py +4 -8
index 04e37a5931..e5be28e331 100755
@@ -28,6 +28,7 @@ from patman import settings
 from patman import terminal
 from patman import test_util
 from patman import test_checkpatch
+from patman import tools
 
 epilog = '''Create patches from commits in a branch, check them and email them
 as specified by tags you place in the commits. Use -n to do a dry run first.'''
@@ -170,14 +171,9 @@ elif args.cmd == 'send':
         fd.close()
 
     elif args.full_help:
-        pager = os.getenv('PAGER')
-        if not pager:
-            pager = shutil.which('less')
-        if not pager:
-            pager = 'more'
-        fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
-                             'README')
-        command.Run(pager, fname)
+        tools.PrintFullHelp(
+            os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'README')
+        )
 
     else:
         # If we are not processing tags, no need to warning about bad ones
tools/patman/tools.py +13 -0
index 877e37cd8d..96882264a2 100644
@@ -581,3 +581,16 @@ def ToHexSize(val):
         hex value of size, or 'None' if the value is None
     """
     return 'None' if val is None else '%#x' % len(val)
+
+def PrintFullHelp(fname):
+    """Print the full help message for a tool using an appropriate pager.
+
+    Args:
+        fname: Path to a file containing the full help message
+    """
+    pager = os.getenv('PAGER')
+    if not pager:
+        pager = shutil.which('less')
+    if not pager:
+        pager = 'more'
+    command.Run(pager, fname)
Signature · 1 line
2.31.1
#2 lore raw

PATCH v2 2/2 tools: Handle PAGER containing arguments

In reply to Paul Barker

When printing full help output from a tool, we should be able to handle a PAGER variable which includes arguments, e.g. PAGER='less -F'.

---
 tools/patman/tools.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/patman/tools.py +5 -4
index 96882264a2..92e3240470 100644
@@ -5,6 +5,7 @@
 
 import glob
 import os
+import shlex
 import shutil
 import struct
 import sys
@@ -588,9 +589,9 @@ def PrintFullHelp(fname):
     Args:
         fname: Path to a file containing the full help message
     """
-    pager = os.getenv('PAGER')
+    pager = shlex.split(os.getenv('PAGER'))
     if not pager:
-        pager = shutil.which('less')
+        pager = [shutil.which('less')]
     if not pager:
-        pager = 'more'
-    command.Run(pager, fname)
+        pager = ['more']
+    command.Run(*pager, fname)
Signature · 1 line
2.31.1
#4 lore raw
In reply to Paul Barker
Quoted 11 lines · On Fri, 13 Aug 2021 17:24:58 +0100 Paul Barker <[email protected]> wrote:

Sorry this took so long to get back to! Got there in the end though :)

Changes from v1:
* Collected the full help printing code from patman, buildman & binman into a single function so that when support for PAGER containing arguments is added it applies to all the relevant tools.

Paul Barker (2):
tools: Refactor full help printing
tools: Handle PAGER containing arguments

Quoted 6 lines
 tools/binman/control.py   |  9 +++------
 tools/buildman/control.py | 10 ++++------
 tools/patman/main.py      | 12 ++++--------
 tools/patman/tools.py     | 14 ++++++++++++++
 4 files changed, 25 insertions(+), 20 deletions(-)

Sending a quick ping on these patches as it's been 2 weeks.

Signature · 3 lines
Paul Barker
Principal Software Engineer
SanCloud Ltd