Skip to content

Commit

Permalink
[report] Fix traceback when running plugins_overview.py
Browse files Browse the repository at this point in the history
Running plugins_overview.py after commit be1ad32 resulted
in the following traceback:

Traceback (most recent call last):
  File "/sos_plugin_overview_fix/plugins_overview.py", line 92, in <module>
with open(os.path.join(PLUGDIR, plugfile),
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'str' object does not support the context manager protocol

This is because the call to read() returns a str and it was
 happening within the 'with' statement. This fix separates the
action of opening the file via 'with' and the action of reading its
content via 'read()'.

Signed-off-by: Jose Castillo <[email protected]>
  • Loading branch information
jcastill authored and TurboTurtle committed Oct 23, 2024
1 parent a53991f commit 98631ec
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions plugins_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,29 @@ def add_all_items(method, dest, plugfd, wrapopen=r'\(', wrapclose=r'\)'):
'env': [],
}
with open(os.path.join(PLUGDIR, plugfile),
encoding='utf-8').read().replace('\n', '') as pfd:
encoding='utf-8') as pfd:
pfd_content = pfd.read().replace('\n', '')
add_all_items(
"from sos.report.plugins import ", plugs_data[plugname]['distros'],
pfd, wrapopen='', wrapclose='(class|from|import)'
pfd_content, wrapopen='', wrapclose='(class|from|import)'
)
add_all_items("profiles = ", plugs_data[plugname]['profiles'],
pfd, wrapopen='')
pfd_content, wrapopen='')
add_all_items("packages = ", plugs_data[plugname]['packages'],
pfd, wrapopen='')
add_all_items("add_copy_spec", plugs_data[plugname]['copyspecs'], pfd)
pfd_content, wrapopen='')
add_all_items("add_copy_spec", plugs_data[plugname]['copyspecs'],
pfd_content)
add_all_items("add_forbidden_path",
plugs_data[plugname]['forbidden'], pfd)
add_all_items("add_cmd_output", plugs_data[plugname]['commands'], pfd)
plugs_data[plugname]['forbidden'], pfd_content)
add_all_items("add_cmd_output", plugs_data[plugname]['commands'],
pfd_content)
add_all_items("collect_cmd_output",
plugs_data[plugname]['commands'], pfd)
plugs_data[plugname]['commands'], pfd_content)
add_all_items("add_service_status",
plugs_data[plugname]['service_status'], pfd)
add_all_items("add_journal", plugs_data[plugname]['journals'], pfd)
add_all_items("add_env_var", plugs_data[plugname]['env'], pfd)
plugs_data[plugname]['service_status'], pfd_content)
add_all_items("add_journal", plugs_data[plugname]['journals'],
pfd_content)
add_all_items("add_env_var", plugs_data[plugname]['env'], pfd_content)

# print output; if "csv" is cmdline argument, print in CSV format, else JSON
if (len(sys.argv) > 1) and (sys.argv[1] == "csv"):
Expand Down

0 comments on commit 98631ec

Please sign in to comment.