Skip to content

Commit

Permalink
refactor: Restructure semantic browser auto-refresh disabling
Browse files Browse the repository at this point in the history
- Replace hardcoded workspace directory with env variable
- Use functions for better readibility
- Use context manager and `enumerate` for file iteration
- Use `replace` statement to replace old with new
  • Loading branch information
MoritzWeber0 committed Oct 10, 2024
1 parent 3772d25 commit f1aa65e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 52 deletions.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ LOG_LEVEL ?= DEBUG
MEMORY_MAX ?= 90%
MEMORY_MIN ?= 70%

# Disable the semantic browser auto refresh
# More information is available in the "Capella/Base" documentation.
CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH ?= 1

# If this option is set to 1, all tests that require a running t4c server
# will be executed. To run these tests, you need a Makefile in
# t4c/server with a target t4c/server/server that builds the t4c server
Expand Down Expand Up @@ -287,6 +291,7 @@ capella/builder:

run-capella/base: capella/base
docker run $(DOCKER_RUN_FLAGS) \
-e CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH=$(CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH) \
$(DOCKER_PREFIX)$<:$$(echo "$(DOCKER_TAG_SCHEMA)" | envsubst)

run-jupyter-notebook: jupyter-notebook
Expand All @@ -311,6 +316,8 @@ run-capella/remote: capella/remote
-e XPRA_SUBPATH=$(XPRA_SUBPATH) \
-e MEMORY_MIN=$(MEMORY_MIN) \
-e MEMORY_MAX=$(MEMORY_MAX) \
-e CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH=$(CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH) \
-e LOG_LEVEL="$(LOG_LEVEL)" \
-p $(RDP_PORT):3389 \
-p $(WEB_PORT):10000 \
-p $(METRICS_PORT):9118 \
Expand Down Expand Up @@ -378,6 +385,8 @@ run-t4c/client/remote-legacy: t4c/client/remote
-e XPRA_SUBPATH=$(XPRA_SUBPATH) \
-e MEMORY_MIN=$(MEMORY_MIN) \
-e MEMORY_MAX=$(MEMORY_MAX) \
-e CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH=$(CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH) \
-e LOG_LEVEL="$(LOG_LEVEL)" \
-p $(RDP_PORT):3389 \
-p $(WEB_PORT):10000 \
-p $(METRICS_PORT):9118 \
Expand All @@ -394,6 +403,8 @@ run-t4c/client/remote: t4c/client/remote
-e XPRA_SUBPATH=$(XPRA_SUBPATH) \
-e MEMORY_MIN=$(MEMORY_MIN) \
-e MEMORY_MAX=$(MEMORY_MAX) \
-e CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH=$(CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH) \
-e LOG_LEVEL="$(LOG_LEVEL)" \
-p $(RDP_PORT):3389 \
-p $(WEB_PORT):10000 \
-p $(METRICS_PORT):9118 \
Expand All @@ -413,6 +424,8 @@ run-t4c/client/remote/pure-variants: t4c/client/remote/pure-variants
-e CONNECTION_METHOD=$(CONNECTION_METHOD) \
-e XPRA_SUBPATH=$(XPRA_SUBPATH) \
-e WORKSPACE_DIR=/workspace/capella_pv \
-e CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH=$(CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH) \
-e LOG_LEVEL="$(LOG_LEVEL)" \
-p $(RDP_PORT):3389 \
-p $(WEB_PORT):10000 \
-p $(METRICS_PORT):9118 \
Expand Down
149 changes: 97 additions & 52 deletions capella/setup/disable_semantic_browser_auto_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,84 +19,129 @@
"menu": "http://www.eclipse.org/ui/2010/UIModel/application/ui/menu",
}
XPATH_EXPR = (
"//persistedState[@key='memento' and"
" contains(@value, 'listeningToWorkbenchPageSelectionEvents')]"
"//persistedState[@key='memento'"
" and contains(@value, 'listeningToWorkbenchPageSelectionEvents')]"
)
WS = "/home/techuser/workspace"

WORKSPACE_DIR = os.getenv("WORKSPACE_DIR", "/workspace")

logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"))
logger = logging.getLogger(__file__)

# we do something if it is configured like that to keep with defaults
# and act only, if deviating from defaults has been configured
if os.getenv("CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH", "0") != "1":
sys.exit(0)
logger.debug(
"Identified that the environment variable"
" `CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH` is set to `1`."
" We will disable the auto-refresh of the semantic browser."
)
logger.debug("Expecting the workspace to be at: `%s`", WS)
FILE_PATH = pathlib.Path(
f"{WS}/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi"
)
if not FILE_PATH.is_file():
logger.debug(
"File not found: `%s`."
" Cannot disable auto-refresh of semantic browser.",
FILE_PATH,

def _check_environment_variable() -> None:
disable_semantic_browser_auto_refresh = os.getenv(
"CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH", "0"
)
if disable_semantic_browser_auto_refresh not in ("0", "1"):
raise ValueError(
"Unsupported value for environment variable"
" `CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH`."
" Only `0` or `1` are supported."
)

if disable_semantic_browser_auto_refresh == "0":
logger.info(
"Environment variable"
" `CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH` is set to 0."
" The semantic browser auto refresh will not be disabled. ",
)
sys.exit(0)

logger.info(
"Identified that the environment variable"
" `CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH` is set to `1`."
" We will disable the auto-refresh of the semantic browser."
)
sys.exit(0)


def main() -> None:
tree = etree.parse(FILE_PATH)
root = tree.getroot()
def _check_for_file_existence(file_path: pathlib.Path) -> None:
if not file_path.is_file():
logger.debug(
"File not found: `%s`."
" Cannot disable auto-refresh of semantic browser.",
file_path,
)
sys.exit(0)


def _identify_semantic_browser_line_no(file_path: pathlib.Path) -> int:
root = etree.parse(file_path).getroot()
logger.debug(
"Searching for XPath expression: `%s` in the file `%s`",
XPATH_EXPR,
FILE_PATH,
file_path,
)
hit_elements = root.xpath(XPATH_EXPR, namespaces=NS_MAP)
if not hit_elements:
logger.debug("No elements found. Exiting.")
sys.exit(0)
# runtime lands here, when we found a setting that controls if the semantic

# Runtime lands here, when we found a setting that controls if the semantic
# browser should auto-refresh or not
persisted_state = hit_elements[0]
parent_element = persisted_state.getparent()
if parent_element is None:
sys.exit(0)
if "elementId" not in parent_element.attrib:
sys.exit(0)
if "semanticbrowser" not in parent_element.attrib["elementId"].lower():
if (
parent_element is None
or "elementId" not in parent_element.attrib
or "semanticbrowser" not in parent_element.attrib["elementId"].lower()
):
sys.exit(0)
# get line number of the element we want to modify

browser_autorefresh_line_no = persisted_state.sourceline
logger.debug(
"Found element at line number: `%d`", browser_autorefresh_line_no
"Found element in line %s:%d",
file_path,
browser_autorefresh_line_no,
)
line_no = 0
old = new = None
for line in fileinput.input(FILE_PATH, inplace=True):
line_no += 1
if line_no == browser_autorefresh_line_no:
old = "listeningToWorkbenchPageSelectionEvents=&quot;1&quot;"
new = "listeningToWorkbenchPageSelectionEvents=&quot;0&quot;"
return browser_autorefresh_line_no


def _replace_content_in_line_of_file(
file_path: pathlib.Path, line_no: int, old: str, new: str
) -> None:
with fileinput.input(file_path, inplace=True, backup=".bak") as file:
for cur_line_no, line in enumerate(file, start=1):
if cur_line_no != line_no:
sys.stdout.write(line)
continue
if old in line:
line = line.replace(old, new)
logger.info(
"Replaced `%s` with `%s` in line number: `%d`",
old,
new,
line_no,
)
else:
old = new = None
sys.stdout.write(line)
fileinput.close()
if old is not None:
logger.debug(
"Replaced `%s` with `%s` in file `%s` at line number %d.",
old,
new,
FILE_PATH,
browser_autorefresh_line_no,
)
logger.debug(
"Skipping replacement in line '%d' as it does not contain '%s'",
cur_line_no,
old,
)
sys.stdout.write(line)


def main() -> None:
_check_environment_variable()
logger.debug("Expecting the workspace to be at: `%s`", WORKSPACE_DIR)
file_path = (
pathlib.Path(WORKSPACE_DIR)
/ ".metadata"
/ ".plugins"
/ "org.eclipse.e4.workbench"
/ "workbench.xmi"
)
_check_for_file_existence(file_path)

browser_autorefresh_line_no = _identify_semantic_browser_line_no(file_path)

_replace_content_in_line_of_file(
file_path,
browser_autorefresh_line_no,
"listeningToWorkbenchPageSelectionEvents=&quot;1&quot;",
"listeningToWorkbenchPageSelectionEvents=&quot;0&quot;",
)


if __name__ == "__main__":
Expand Down

0 comments on commit f1aa65e

Please sign in to comment.