Created
January 17, 2025 22:30
-
-
Save msheiny/c50937632e3fede224b55a7087135cc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 2ca0c386fd81b246d5c5ab063aa5ed7df1e71581 Mon Sep 17 00:00:00 2001 | |
From: Michael Sheinberg <[email protected]> | |
Date: Fri, 17 Jan 2025 14:28:19 -0800 | |
Subject: [PATCH] Add xml output, web browser open helper | |
--- | |
tasks.py | 36 +++++++++++++++++++++++++++++++++++- | |
1 file changed, 35 insertions(+), 1 deletion(-) | |
diff --git a/tasks.py b/tasks.py | |
index 8f50f81..48502f4 100644 | |
--- a/tasks.py | |
+++ b/tasks.py | |
@@ -13,12 +13,16 @@ limitations under the License. | |
""" | |
import os | |
+import platform | |
import re | |
import sys | |
+import xml.etree.ElementTree as ET | |
from pathlib import Path | |
from time import sleep | |
+from xml.etree import ElementTree | |
from invoke.collection import Collection | |
+from invoke.context import Context | |
from invoke.exceptions import Exit, UnexpectedExit | |
from invoke.tasks import task as invoke_task | |
@@ -110,7 +114,7 @@ def task(function=None, *args, **kwargs): | |
return task_wrapper | |
-def docker_compose(context, command, **kwargs): | |
+def docker_compose(context: Context, command, **kwargs): | |
"""Helper function for running a specific docker compose command with all appropriate parameters and environment. | |
Args: | |
@@ -848,6 +852,25 @@ def unittest_coverage(context): | |
run_command(context, command) | |
+@task(pre=[unittest]) | |
+def unittest_xml_coverage(context): | |
+ """Produce an XML coverage report that can be ingested into other tools.""" | |
+ produce_xml_cmd = "coverage xml --include 'nautobot_floor_plan/*'" | |
+ run_command(context, produce_xml_cmd) | |
+ | |
+ | |
+ # Since the container runs from a different working dir, | |
+ # We have to edit the XML to inject the current directory | |
+ # to play nice with tools outside of the container. | |
+ cur_dir = os.path.dirname(__file__) | |
+ cov_xml_path = os.path.join(cur_dir, "coverage.xml") | |
+ | |
+ tree = ET.parse(cov_xml_path) # noqa: S314 | |
+ root = tree.getroot() | |
+ | |
+ root.find(".//sources/source").text = cur_dir | |
+ tree.write(cov_xml_path, encoding="utf-8", xml_declaration=True) | |
+ | |
@task( | |
help={ | |
"failfast": "fail as soon as a single test fails don't run the entire test suite. (default: False)", | |
@@ -913,3 +936,14 @@ def validate_app_config(context): | |
file="development/app_config_schema.py", | |
env={"APP_CONFIG_SCHEMA_COMMAND": "validate"}, | |
) | |
+ | |
+@task | |
+def open_nautobot_web(context: Context): | |
+ """Open Nautobot web interface in the default browser.""" | |
+ nautobot_uri = docker_compose(context, "port nautobot 8080").stdout.rstrip() | |
+ | |
+ if platform.system() == "Darwin": | |
+ open_cmd = "open" | |
+ else: | |
+ open_cmd = "xdg-open" | |
+ context.run(f"{open_cmd} http://{nautobot_uri}", asynchronous=True) | |
-- | |
2.39.5 (Apple Git-154) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment