diff options
Diffstat (limited to 'tools/docs/lib/python_version.py')
| -rw-r--r-- | tools/docs/lib/python_version.py | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/tools/docs/lib/python_version.py b/tools/docs/lib/python_version.py index a9fda2470a26..4fde1b882164 100644 --- a/tools/docs/lib/python_version.py +++ b/tools/docs/lib/python_version.py @@ -20,9 +20,11 @@ Python version if present. import os import re import subprocess +import shlex import sys from glob import glob +from textwrap import indent class PythonVersion: """ @@ -44,6 +46,25 @@ class PythonVersion: """Returns a version tuple as major.minor.patch""" return ".".join([str(x) for x in version]) + @staticmethod + def cmd_print(cmd, max_len=80): + cmd_line = [] + + for w in cmd: + w = shlex.quote(w) + + if cmd_line: + if not max_len or len(cmd_line[-1]) + len(w) < max_len: + cmd_line[-1] += " " + w + continue + else: + cmd_line[-1] += " \\" + cmd_line.append(w) + else: + cmd_line.append(w) + + return "\n ".join(cmd_line) + def __str__(self): """Returns a version tuple as major.minor.patch from self.version""" return self.ver_str(self.version) @@ -130,14 +151,13 @@ class PythonVersion: else: new_python_cmd = None - if show_alternatives: + if show_alternatives and available_versions: print("You could run, instead:") for _, cmd in available_versions: args = [cmd, script_path] + sys.argv[1:] - cmd_str = " ".join(args) - print(f" {cmd_str}") - print() + cmd_str = indent(PythonVersion.cmd_print(args), " ") + print(f"{cmd_str}\n") if bail_out: msg = f"Python {python_ver} not supported. Bailing out" |
