[Git][reproducible-builds/diffoscope][master] diffoscope.tools.get_tools() to programmatically fetch config

Chris Lamb gitlab at salsa.debian.org
Mon Mar 15 17:01:55 UTC 2021



Chris Lamb pushed to branch master at Reproducible Builds / diffoscope


Commits:
248b06aa by Hans-Christoph Steiner at 2021-03-15T17:50:12+01:00
diffoscope.tools.get_tools() to programmatically fetch config

In F-Droid, we use diffoscope in automated verification of APKs.  We need to
include the diffoscope config and version in a JSON report so that the
scheduler can know whether it needs to rerun diffoscope on a file. If the
diffoscope configuration or version has changed, then it should be run again.

- - - - -


3 changed files:

- diffoscope/main.py
- diffoscope/tools.py
- tests/test_tools.py


Changes:

=====================================
diffoscope/main.py
=====================================
@@ -31,6 +31,7 @@ import traceback
 from . import VERSION
 from .path import set_path
 from .tools import (
+    get_tools,
     tool_check_installed,
     tool_prepend_prefix,
     python_module_missing,
@@ -535,34 +536,8 @@ class ListToolsAction(argparse.Action):
         # populated.
         ComparatorManager().reload()
 
-        external_tools = sorted(tool_required.all)
-        if self.only_missing:
-            external_tools = [
-                tool
-                for tool in external_tools
-                if not tool_check_installed(tool)
-            ]
-
-        print("External-Tools-Required: ", end="")
-        print(", ".join(external_tools))
-
-        current_os = get_current_os()
-        os_list = [current_os] if (current_os in OS_NAMES) else iter(OS_NAMES)
-        if os_override:
-            os_list = [os_override]
-
-        for os_ in os_list:
-            tools = set()
-            print("Available-in-{}-packages: ".format(OS_NAMES[os_]), end="")
-            for x in external_tools:
-                try:
-                    tools.add(EXTERNAL_TOOLS[x][os_])
-                except KeyError:
-                    pass
-            print(", ".join(sorted(tools)))
-
-        print("Missing-Python-Modules: ", end="")
-        print(", ".join(sorted(python_module_missing.modules)))
+        for k, v in sorted(get_tools(self.only_missing).items()):
+            print("%s: %s" % (k, ", ".join(v)))
 
         sys.exit(0)
 


=====================================
diffoscope/tools.py
=====================================
@@ -46,6 +46,36 @@ OS_NAMES = collections.OrderedDict(
 )
 
 
+def get_tools(only_missing=False):
+    """Return the tool configuration in a dict"""
+
+    d = {}
+
+    external_tools = sorted(tool_required.all)
+    if only_missing:
+        external_tools = [
+            tool for tool in external_tools if not tool_check_installed(tool)
+        ]
+    d["External-Tools-Required"] = tuple(external_tools)
+
+    current_os = get_current_os()
+    os_list = [current_os] if (current_os in OS_NAMES) else iter(OS_NAMES)
+    for os_ in os_list:
+        tools = set()
+        for x in external_tools:
+            try:
+                tools.add(EXTERNAL_TOOLS[x][os_])
+            except KeyError:
+                pass
+        d["Available-in-{}-packages".format(OS_NAMES[os_])] = tuple(
+            sorted(tools)
+        )
+
+    d["Missing-Python-Modules"] = tuple(sorted(python_module_missing.modules))
+
+    return d
+
+
 def get_tool_name(tool):
     return REMAPPED_TOOL_NAMES.get(tool, tool)
 


=====================================
tests/test_tools.py
=====================================
@@ -35,6 +35,23 @@ def test_all_tools_are_listed():
             pytest.fail(f"{x} is not present in EXTERNAL_TOOLS")
 
 
+def test_get_tools():
+    # Note the ordering of this test (see: f1d744da16)
+    from diffoscope.comparators import ComparatorManager
+    from diffoscope.tools import get_tools
+
+    ComparatorManager().reload()
+
+    tools = get_tools()
+    missing_tools = get_tools(only_missing=True)
+    k = "External-Tools-Required"
+    for x in missing_tools[k]:
+        if x not in tools[k]:
+            pytest.fail(
+                f"{x} must be present for {k} in tools and only_missing"
+            )
+
+
 def test_sbin_added_to_path():
     from diffoscope.tools import tool_required
 



View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/commit/248b06aacbd35f3cbec04ae3de28826b70d54338

-- 
View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/commit/248b06aacbd35f3cbec04ae3de28826b70d54338
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.reproducible-builds.org/pipermail/rb-commits/attachments/20210315/fd46b745/attachment.htm>


More information about the rb-commits mailing list