Skip to content

Commit df36bbc

Browse files
committed
PR fixes
1 parent ea90efa commit df36bbc

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

‎splunklib/searchcommands/environment.py‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,23 @@ def configure_logging(logger_name, filename=None):
121121
_current_logging_configuration_file = None
122122

123123

124-
def _find_app_root(app_file: str) -> str:
125-
"""Return the app root directory for a search command script."""
126-
directory: str = path.abspath(path.dirname(app_file))
127-
while True:
128-
parent, name = path.split(directory)
129-
if name == "bin":
130-
return parent
131-
if parent == directory:
132-
return path.dirname(path.abspath(path.dirname(app_file)))
133-
directory = parent
124+
def _find_app_root(app_file: str, splunk_home: str) -> str:
125+
"""Return the app root directory for a search command script.
126+
127+
An app root is $SPLUNK_HOME/etc/apps/<app-name>, and app_file is located somewhere
128+
underneath its bin directory.
129+
"""
130+
apps_directory = path.join(splunk_home, "etc", "apps")
131+
relative = path.relpath(path.abspath(app_file), apps_directory)
132+
app_name = relative.split(path.sep, 1)[0]
133+
if app_name == path.pardir: # app_file not in $SPLUNK_HOME/etc/apps
134+
return path.dirname(path.abspath(path.dirname(app_file)))
135+
return path.join(apps_directory, app_name)
134136

135137

136138
splunk_home = path.abspath(path.join(getcwd(), environ.get("SPLUNK_HOME", "")))
137139
app_file = getattr(sys.modules["__main__"], "__file__", sys.executable)
138-
app_root = _find_app_root(app_file)
140+
app_root = _find_app_root(app_file, splunk_home)
139141

140142
splunklib_logger, logging_configuration = configure_logging("splunklib")
141143

‎tests/unit/searchcommands/test_environment.py‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,29 @@
2020
_find_app_root, # pyright: ignore[reportPrivateUsage]
2121
)
2222

23+
_SPLUNK_HOME = os.path.join(os.sep, "opt", "splunk")
24+
_APPS_DIRECTORY = os.path.join(_SPLUNK_HOME, "etc", "apps")
25+
2326

2427
@pytest.mark.parametrize(
2528
("app_file_parts", "expected_app_root_parts"),
2629
[
27-
(("apps", "my_app", "bin", "command.py"), ("apps", "my_app")),
28-
(("apps", "my_app", "bin", "linux_x86_64", "command.py"), ("apps", "my_app")),
29-
(("apps", "my_app", "bin", "foo", "bar", "command.py"), ("apps", "my_app")),
30-
(("some", "other", "layout", "command.py"), ("some", "other")),
30+
# A script located directly in the app's bin directory
31+
(("my_app", "bin", "command.py"), ("my_app",)),
32+
# A script located in a subdirectory of bin, including one that happens to be
33+
# named "bin" itself; the app root is still $SPLUNK_HOME/etc/apps/my_app
34+
(("my_app", "bin", "foo", "bin", "command.py"), ("my_app",)),
3135
],
3236
)
3337
def test_find_app_root(
3438
app_file_parts: tuple[str, ...], expected_app_root_parts: tuple[str, ...]
3539
) -> None:
36-
app_file = os.path.join(*app_file_parts)
37-
assert _find_app_root(app_file) == os.path.abspath(os.path.join(*expected_app_root_parts))
40+
app_file = os.path.join(_APPS_DIRECTORY, *app_file_parts)
41+
expected_app_root = os.path.join(_APPS_DIRECTORY, *expected_app_root_parts)
42+
assert _find_app_root(app_file, _SPLUNK_HOME) == expected_app_root
43+
44+
45+
def test_find_app_root_falls_back_on_nonstandard_splunk_home() -> None:
46+
app_file = os.path.join("some", "other", "layout", "command.py")
47+
expected_app_root = os.path.dirname(os.path.abspath(os.path.dirname(app_file)))
48+
assert _find_app_root(app_file, _SPLUNK_HOME) == expected_app_root

0 commit comments

Comments
 (0)