Skip to contentSkip to conversation

binman: bintool: Add support for tool directories

PATCH In review Neha Malcom Francis 2 participants 4 messages Feb 24, 2023 – Mar 15, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 binman: bintool: Add support for tool directories

Currently, bintool supports external compilable tools as single executable files. Adding support for git repos that can be used to run non-compilable scripting tools that cannot otherwise be present in binman.

---
Changes in v3:
- moved back to using DOWNLOAD_DIR as community is making relevant changes
- extended coverage for bintool_test.py
- added function comment for new parameter

Changes in v2:
- added parameter to obtain path to download the directory optionally, enables flexibility to avoid using DOWNLOAD_DESTDIR
- added test to bintool_test.py
- s/FETCH_NO_BUILD/FETCH_SOURCE
- code reformatting

 tools/binman/bintool.py        | 47 +++++++++++++++++++++++++++++-----
 tools/binman/bintool_test.py   | 43 +++++++++++++++++++++++++++++++
 tools/binman/btool/_testing.py |  4 +++
 tools/patman/tools.py          |  2 +-
 4 files changed, 88 insertions(+), 8 deletions(-)
tools/binman/bintool.py +40 -7
index 8fda13ff01..33f563c46f 100644
@@ -32,12 +32,13 @@ FORMAT = '%-16.16s %-12.12s %-26.26s %s'
 modules = {}
 
 # Possible ways of fetching a tool (FETCH_COUNT is number of ways)
-FETCH_ANY, FETCH_BIN, FETCH_BUILD, FETCH_COUNT = range(4)
+FETCH_ANY, FETCH_BIN, FETCH_BUILD, FETCH_SOURCE, FETCH_COUNT = range(5)
 
 FETCH_NAMES = {
     FETCH_ANY: 'any method',
     FETCH_BIN: 'binary download',
-    FETCH_BUILD: 'build from source'
+    FETCH_BUILD: 'build from source',
+    FETCH_SOURCE: 'download source without building'
     }
 
 # Status of tool fetching
@@ -201,12 +202,13 @@ class Bintool:
                 print(f'- trying method: {FETCH_NAMES[try_method]}')
                 result = try_fetch(try_method)
                 if result:
+                    method = try_method
                     break
         else:
             result = try_fetch(method)
         if not result:
             return FAIL
-        if result is not True:
+        if result is not True and method != FETCH_SOURCE:
             fname, tmpdir = result
             dest = os.path.join(DOWNLOAD_DESTDIR, self.name)
             print(f"- writing to '{dest}'")
@@ -261,7 +263,7 @@ class Bintool:
                 show_status(col.RED, 'Failures', status[FAIL])
         return not status[FAIL]
 
-    def run_cmd_result(self, *args, binary=False, raise_on_error=True):
+    def run_cmd_result(self, *args, binary=False, raise_on_error=True, add_name=True):
         """Run the bintool using command-line arguments
 
         Args:
@@ -270,6 +272,7 @@ class Bintool:
             binary (bool): True to return output as bytes instead of str
             raise_on_error (bool): True to raise a ValueError exception if the
                 tool returns a non-zero return code
+            add_name (bool): True to add bintool name to the beginning of command
 
         Returns:
             CommandResult: Resulting output from the bintool, or None if the
@@ -278,7 +281,10 @@ class Bintool:
         if self.name in self.missing_list:
             return None
         name = os.path.expanduser(self.name)  # Expand paths containing ~
-        all_args = (name,) + args
+        if add_name:
+            all_args = (name,) + args
+        else:
+            all_args = args
         env = tools.get_env_with_path()
         tout.detail(f"bintool: {' '.join(all_args)}")
         result = command.run_pipe(
@@ -304,18 +310,19 @@ class Bintool:
             tout.debug(result.stderr)
         return result
 
-    def run_cmd(self, *args, binary=False):
+    def run_cmd(self, *args, binary=False, add_name=True):
         """Run the bintool using command-line arguments
 
         Args:
             args (list of str): Arguments to provide, in addition to the bintool
                 name
             binary (bool): True to return output as bytes instead of str
+            add_name (bool): True to add bintool name to the beginning of command
 
         Returns:
             str or bytes: Resulting stdout from the bintool
         """
-        result = self.run_cmd_result(*args, binary=binary)
+        result = self.run_cmd_result(*args, binary=binary, add_name=add_name)
         if result:
             return result.stdout
 
@@ -354,6 +361,32 @@ class Bintool:
             return None
         return fname, tmpdir
 
+    @classmethod
+    def fetch_from_git(cls, git_repo, name):
+        """Fetch a bintool git repo
+
+        This clones the repo and returns
+
+        Args:
+            git_repo (str): URL of git repo
+            name (str): Bintool name assigned as tool directory name
+
+        Returns:
+            str: Directory of fetched repo
+            or None on error
+        """
+        dir = os.path.join(DOWNLOAD_DESTDIR, name)
+        if os.path.exists(dir):
+            print(f"- {dir} repo already exists")
+            return None
+        os.mkdir(dir)
+        print(f"- clone git repo '{git_repo}' to '{dir}'")
+        tools.run('git', 'clone', '--depth', '1', git_repo, dir)
+        if len(os.listdir(dir)) == 0:
+            print(f"- '{dir}' repo is empty")
+            return None
+        return dir
+
     @classmethod
     def fetch_from_url(cls, url):
         """Fetch a bintool from a URL
tools/binman/bintool_test.py +43 -0

61 lines not on this page · raw message

tools/binman/btool/_testing.py +4 -0
index 4005e8a8a5..b291d35c58 100644
@@ -5,6 +5,8 @@
 
 This is not a real bintool, just one used for testing"""
 
+import tempfile
+
 from binman import bintool
 
 # pylint: disable=C0103
@@ -33,4 +35,6 @@ class Bintool_testing(bintool.Bintool):
             return self.fetch_from_drive('junk')
         if method == bintool.FETCH_BUILD:
             return self.build_from_git('url', 'target', 'pathname')
+        if method == bintool.FETCH_SOURCE:
+            return self.fetch_from_git('giturl', 'mygit')
         return None
tools/patman/tools.py +1 -1
index 2ac814d476..b69a651eab 100644
@@ -397,7 +397,7 @@ def tool_find(name):
         paths += tool_search_paths
     for path in paths:
         fname = os.path.join(path, name)
-        if os.path.isfile(fname) and os.access(fname, os.X_OK):
+        if (os.path.isfile(fname) or os.path.isdir(fname)) and os.access(fname, os.X_OK):
             return fname
 
 def run(name, *args, **kwargs):
Signature · 1 line
2.34.1
#1 lore raw

Hi Neha,

Quoted 22 lines · Neha Malcom Francis

Currently, bintool supports external compilable tools as single executable files. Adding support for git repos that can be used to run non-compilable scripting tools that cannot otherwise be present in binman.

---
Changes in v3:
- moved back to using DOWNLOAD_DIR as community is making relevant changes
- extended coverage for bintool_test.py
- added function comment for new parameter

Changes in v2:
- added parameter to obtain path to download the directory optionally, enables flexibility to avoid using DOWNLOAD_DESTDIR
- added test to bintool_test.py
- s/FETCH_NO_BUILD/FETCH_SOURCE
- code reformatting

Quoted 5 lines
 tools/binman/bintool.py        | 47 +++++++++++++++++++++++++++++-----
 tools/binman/bintool_test.py   | 43 +++++++++++++++++++++++++++++++
 tools/binman/btool/_testing.py |  4 +++
 tools/patman/tools.py          |  2 +-
 4 files changed, 88 insertions(+), 8 deletions(-)

I am OK with doing this but worried that it will be used for shell scripts, which we are trying to avoid.

The code looks OK for now. Perhaps we can revisit this when we have a use case? I also think we should have each tool individually shown in the list, rather than having them be 'hidden' behind a btool.

Regards,
Simon

#2 lore raw

Hi Simon

Quoted 31 lines · Simon Glass

Hi Neha,

Quoted 22 lines · Neha Malcom Francis

Currently, bintool supports external compilable tools as single executable files. Adding support for git repos that can be used to run non-compilable scripting tools that cannot otherwise be present in binman.

---
Changes in v3:
- moved back to using DOWNLOAD_DIR as community is making relevant changes
- extended coverage for bintool_test.py
- added function comment for new parameter

Changes in v2:
- added parameter to obtain path to download the directory optionally, enables flexibility to avoid using DOWNLOAD_DESTDIR
- added test to bintool_test.py
- s/FETCH_NO_BUILD/FETCH_SOURCE
- code reformatting

Quoted 5 lines
  tools/binman/bintool.py        | 47 +++++++++++++++++++++++++++++-----
  tools/binman/bintool_test.py   | 43 +++++++++++++++++++++++++++++++
  tools/binman/btool/_testing.py |  4 +++
  tools/patman/tools.py          |  2 +-
  4 files changed, 88 insertions(+), 8 deletions(-)
Quoted 7 lines

I am OK with doing this but worried that it will be used for shell scripts, which we are trying to avoid.

The code looks OK for now. Perhaps we can revisit this when we have a use case? I also think we should have each tool individually shown in the list, rather than having them be 'hidden' behind a btool.

I get the intention, let me know your reply to our thread [1] and we can work on it from there.

Quoted 2 lines

Regards,
Simon

[1] https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

Signature · 2 lines
Thanking You
Neha Malcom Francis
#3 lore raw

Hi Neha,

Quoted 38 lines · Neha Malcom Francis

Hi Simon

Quoted 34 lines · Simon Glass

Hi Neha,

Quoted 22 lines · Neha Malcom Francis

Currently, bintool supports external compilable tools as single executable files. Adding support for git repos that can be used to run non-compilable scripting tools that cannot otherwise be present in binman.

---
Changes in v3:
- moved back to using DOWNLOAD_DIR as community is making relevant changes
- extended coverage for bintool_test.py
- added function comment for new parameter

Changes in v2:
- added parameter to obtain path to download the directory optionally, enables flexibility to avoid using DOWNLOAD_DESTDIR
- added test to bintool_test.py
- s/FETCH_NO_BUILD/FETCH_SOURCE
- code reformatting

Quoted 5 lines
  tools/binman/bintool.py        | 47 +++++++++++++++++++++++++++++-----
  tools/binman/bintool_test.py   | 43 +++++++++++++++++++++++++++++++
  tools/binman/btool/_testing.py |  4 +++
  tools/patman/tools.py          |  2 +-
  4 files changed, 88 insertions(+), 8 deletions(-)

I am OK with doing this but worried that it will be used for shell scripts, which we are trying to avoid.

Quoted 7 lines
Quoted 4 lines

The code looks OK for now. Perhaps we can revisit this when we have a use case? I also think we should have each tool individually shown in the list, rather than having them be 'hidden' behind a btool.

I get the intention, let me know your reply to our thread [1] and we can work on it from there.

OK I replied to that.

- Simon

Quoted 6 lines

[1]
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

--
Thanking You
Neha Malcom Francis