toolbox/completion/generate_completions.py
Debarshi Ray a0569fdc3e build: Don't try to handle exceptions when spawning subprocesses
In short, it's a lot of effort to cover all possible exceptions that can
be thrown, and things work reasonably well even without handling them.
Since this is just part of the build, there's no point in complicating
things for aesthetic reasons.

More details below.

First, not every runtime error leads to a subprocess.CalledProcessError.
It's only thrown if the spawned process returns with a non-zero exit
code.  There can be other problems.  eg., if the gofmt file isn't
executable then a PermissionError is thrown that's currently not
handled, and the wrapper Python script returns with a non-zero exit
code:
  Traceback (most recent call last):
    File "toolbox/src/meson_go_fmt.py", line 28, in <module>
      gofmt = subprocess.run(['gofmt', '-d', source_dir],
          capture_output=True, check=True)
    File "/usr/lib64/python3.10/subprocess.py", line 501, in run
      with Popen(*popenargs, **kwargs) as process:
    File "/usr/lib64/python3.10/subprocess.py", line 969, in __init__
      self._execute_child(args, executable, preexec_fn, close_fds,
    File "/usr/lib64/python3.10/subprocess.py", line 1845, in
        _execute_child
      raise child_exception_type(errno_num, err_msg, err_filename)
  PermissionError: [Errno 13] Permission denied: 'gofmt'

Second, when a subprocess.CalledProcessError is thrown, the wrapper
Python script will still return with a non-zero exit code with an
understandable error message, even if the exception isn't handled.  eg.,
if 'meson install' is called without the adequate permissions, then
systemd-tmpfiles(8) will return with a non-zero exit code, which shows
up as:
  --- stdout ---
  Calling systemd-tmpfiles --create ...

  --- stderr ---
  Failed to open directory 'cryptsetup': Permission denied
  Failed to open directory 'certs': Permission denied
  Failed to create directory or subvolume "/var/spool/cups/tmp":
    Permission denied
  ...
  ...
  ...
  Traceback (most recent call last):
    File "toolbox/meson_post_install.py", line 26, in <module>
      subprocess.run(['systemd-tmpfiles', '--create'], check=True)
    File "/usr/lib64/python3.10/subprocess.py", line 524, in run
      raise CalledProcessError(retcode, process.args,
  subprocess.CalledProcessError: Command '['systemd-tmpfiles',
      '--create']' returned non-zero exit status 73.

Similarly, if there problems generating the shell completions:
  --- stderr ---
  Error: unknown command "__completion" for "toolbox"
  Run 'toolbox --help' for usage.
  exit status 1
  Traceback (most recent call last):
    File "toolbox/completion/generate_completions.py", line 35, in
        <module>
    output = subprocess.run(['go', 'run', '.', '__completion',
        completion_type], check=True)
    File "/usr/lib64/python3.10/subprocess.py", line 524, in run
      raise CalledProcessError(retcode, process.args,
  subprocess.CalledProcessError: Command '['go', 'run', '.',
      '__completion', 'bash']' returned non-zero exit status 1.

https://github.com/containers/toolbox/pull/1122
2022-09-09 18:35:55 +02:00

37 lines
1.2 KiB
Python

#!/usr/bin/env python3
#
# Copyright © 2022 Ondřej Míchal
# Copyright © 2022 Red Hat Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import subprocess
import sys
if len(sys.argv) != 3:
print('{}: wrong arguments'.format(sys.argv[0]), file=sys.stderr)
print('Usage: {} [SOURCE DIR] [COMPLETION TYPE]'.format(sys.argv[0]), file=sys.stderr)
print()
print("SOURCE DIR is path to the Toolbox Go source code")
print("COMPLETION TYPE is either 'bash', 'zsh' or 'fish'")
sys.exit(1)
source_dir = sys.argv[1]
completion_type = sys.argv[2]
os.chdir(source_dir)
output = subprocess.run(['go', 'run', '.', 'completion', completion_type], check=True)
sys.exit(0)