Skip to contentSkip to conversation

check_whence.py: Add function to get listing of files

DISCUSSION Vivek Puar 3 participants 8 messages Oct 18, 2024 – Oct 25, 2024

lore
Share & tools
lore

Vivek Puar

author
#0 lore raw

Hi,

Please find below git pull request to update check_whence.py file.

The following changes since commit 8dbcf94bfc68bdb1c6df3a42762b275557095558:

  Merge branch 'robot/patch-0-1729102102' into 'main' (2024-10-17 15:40:04 +0000)

are available in the Git repository at:

  https://github.com/vivpuar/linux-firmware

for you to fetch changes up to 95c238e821c95749b50f24f9b6fed080417daadf:

  check_whence.py: Add function to get listing of files (2024-10-18 15:19:04 +0530)

----------------------------------------------------------------
Vivek Puar (1):
      check_whence.py: Add function to get listing of files

 check_whence.py | 33 ++++++++++++++++++++++++---------

 1 file changed, 24 insertions(+), 9 deletions(-)

Regards,

Vivek

#1 lore raw
Quoted 15 lines · Vivek Puar

Hi,

Please find below git pull request to update check_whence.py file.

The following changes since commit 8dbcf94bfc68bdb1c6df3a42762b275557095558:

   Merge branch 'robot/patch-0-1729102102' into 'main' (2024-10-17

15:40:04 +0000)

are available in the Git repository at:

https://github.com/vivpuar/linux-firmware

Quoted 8 lines

for you to fetch changes up to 95c238e821c95749b50f24f9b6fed080417daadf:

check_whence.py: Add function to get listing of files (2024-10-18
15:19:04 +0530)

----------------------------------------------------------------
Vivek Puar (1):
check_whence.py: Add function to get listing of files

This doesn't apply cleanly any longer. Can you rebase it?

josh

Quoted 9 lines
  check_whence.py | 33 ++++++++++++++++++++++++---------
  1 file changed, 24 insertions(+), 9 deletions(-)

Regards,

Vivek

#2 lore raw

Hi Vivek,

Quoted 19 lines · Josh Boyer
Quoted 17 lines · Vivek Puar

Hi,

Please find below git pull request to update check_whence.py file.

The following changes since commit 8dbcf94bfc68bdb1c6df3a42762b275557095558:

   Merge branch 'robot/patch-0-1729102102' into 'main' (2024-10-17

15:40:04 +0000)

are available in the Git repository at:

https://github.com/vivpuar/linux-firmware

for you to fetch changes up to 95c238e821c95749b50f24f9b6fed080417daadf:

Quoted 9 lines
Quoted 6 lines

check_whence.py: Add function to get listing of files (2024-10-18
15:19:04 +0530)

----------------------------------------------------------------
Vivek Puar (1):
check_whence.py: Add function to get listing of files

This doesn't apply cleanly any longer. Can you rebase it?

I worked around the root issue in a different way with
https://gitlab.com/kernel-firmware/linux-firmware/-/merge_requests/339.
Your approach looks reasonable, plus I don't have an opinion on the route we take.

If you prefer we fallback to file listing, can you please open a MR against the gitlab repo and I would happily review it.

Thanks o/
Emil

author
#3 lore raw

Hi,

I've created another pull request, please review it.

The following changes since commit e1d95775eb3db64ab3740701bf9682e5856aab2e:

  Merge branch 'intel/xe3lpd-dmc' into 'main' (2024-10-22 14:50:56 +0000)

are available in the Git repository at:

  https://github.com/vivpuar/linux-firmware mine

for you to fetch changes up to 306d081594df8a449d07565217153c40b6136646:

  check_whence.py: Add function to get listing of files (2024-10-23 00:32:57 +0530)

----------------------------------------------------------------
Vivek Puar (1):
      check_whence.py: Add function to get listing of files

 check_whence.py | 36 ++++++++++++++++++++++++------------

 1 file changed, 24 insertions(+), 12 deletions(-)
Quoted 33 lines · Emil Velikov

Hi Vivek,

Quoted 17 lines · Josh Boyer
Quoted 16 lines · Vivek Puar

Hi,

Please find below git pull request to update check_whence.py file.

The following changes since commit 8dbcf94bfc68bdb1c6df3a42762b275557095558:

    Merge branch 'robot/patch-0-1729102102' into 'main' (2024-10-17

15:40:04 +0000)

are available in the Git repository at:

https://github.com/vivpuar/linux-firmware

for you to fetch changes up to 95c238e821c95749b50f24f9b6fed080417daadf:

Quoted 8 lines
Quoted 6 lines

check_whence.py: Add function to get listing of files (2024-10-18
15:19:04 +0530)

----------------------------------------------------------------
Vivek Puar (1):
check_whence.py: Add function to get listing of files

This doesn't apply cleanly any longer. Can you rebase it?

I worked around the root issue in a different way with
https://gitlab.com/kernel-firmware/linux-firmware/-/merge_requests/339.
Your approach looks reasonable, plus I don't have an opinion on the route we take.

Quoted 5 lines

If you prefer we fallback to file listing, can you please open a MR against the gitlab repo and I would happily review it.

Thanks o/
Emil

Regards,

Vivek

#4 lore raw
Quoted 5 lines · Vivek Puar

Hi,

I've created another pull request, please review it.

Pull requests are not for reviewing but for merging code. They have no code to review.
Please either post your patch or open a MR upstream.

Thanks
Emil

author
#5 lore raw

Attaching the patch here.

Quoted 11 lines · Emil Velikov
Quoted 4 lines · Vivek Puar

Hi,

I've created another pull request, please review it.

Pull requests are not for reviewing but for merging code. They have no code to review.
Please either post your patch or open a MR upstream.

Thanks
Emil

Regards,

Vivek

From 306d081594df8a449d07565217153c40b6136646 Mon Sep 17 00:00:00 2001
From: Vivek Puar <[email protected]>
Date: Wed, 23 Oct 2024 00:32:57 +0530
Subject: [PATCH] check_whence.py: Add function to get listing of files

Commit [1] introduces check_whence.py inside
copy-firmware.sh before the firmware is consumed.

But current check_whence.py supports only git repo, and it will cause error when linux-firmware tar is used, as it will not be a git repo.

So adding a check to parse local directory to create a set of files to validate it with WHENCE file.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/commit/?id=6edd0fd36acb93c8075bbd700ad91f53fc810c0e

---
 check_whence.py | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)
check_whence.py +24 -12
index 1fece4f4..1ea75a68 100755
@@ -1,6 +1,6 @@
 #!/usr/bin/python3
 
-import os, re, stat, sys
+import os, re, stat, sys, subprocess
 from io import open
 
 
@@ -60,12 +60,21 @@ def list_links_list():
 
 
 def list_git():
-    git_files = os.popen("git ls-files")
-    for line in git_files:
-        yield line.rstrip("\n")
+    try:
+        subprocess.check_output(['git', 'rev-parse', '--is-inside-work-tree'], stderr=subprocess.STDOUT)
+        with os.popen("git ls-files") as git_files:
+            for line in git_files:
+                yield line.rstrip("\n")
+    except subprocess.CalledProcessError:
+        return None
 
-    if git_files.close():
-        sys.stderr.write("W: git file listing failed, skipping some validation\n")
+
+def list_dir():
+    pwd = os.getcwd()
+    # Parse every directory to create set of files to be matched with WHENCE entries
+    for root, dirs, files in os.walk(pwd):
+        for file in files:
+            yield os.path.relpath(os.path.join(root, file.rstrip("\n")), pwd)
 
 
 def main():
@@ -97,7 +106,10 @@ def main():
         ]
     )
     known_prefixes = set(name for name in whence_list if name.endswith("/"))
-    git_files = set(list_git())
+    files_set = set(list_git())
+    # If it is not git repo call list_dir()
+    if not files_set:
+        files_set = set(list_dir())
     executable_files = set(
         [
             "build_packages.py",
@@ -138,12 +150,12 @@ def main():
             )
             ret = 1
 
-    for name in sorted(list(known_files - git_files) if len(git_files) else list()):
+    for name in sorted(list(known_files - files_set) if len(files_set) else list()):
         sys.stderr.write("E: %s listed in WHENCE does not exist\n" % name)
         ret = 1
 
     # A link can point to a file...
-    valid_targets = set(git_files)
+    valid_targets = set(files_set)
 
     # ... or to a directory
     for target in set(valid_targets):
@@ -154,14 +166,14 @@ def main():
                 break
             valid_targets.add(dirname)
 
-    for link, target in sorted(links_list if len(git_files) else list()):
+    for link, target in sorted(links_list if len(files_set) else list()):
         if target not in valid_targets:
             sys.stderr.write(
                 "E: target %s of link %s in WHENCE does not exist\n" % (target, link)
             )
             ret = 1
 
-    for name in sorted(list(git_files - known_files)):
+    for name in sorted(list(files_set - known_files)):
         # Ignore subdirectory changelogs and GPG detached signatures
         if name.endswith("/ChangeLog") or (
             name.endswith(".asc") and name[:-4] in known_files
@@ -182,7 +194,7 @@ def main():
             sys.stderr.write("E: %s is missing execute bit\n" % name)
             ret = 1
 
-    for name in sorted(list(git_files - executable_files)):
+    for name in sorted(list(files_set - executable_files)):
         mode = os.stat(name).st_mode
         if stat.S_ISDIR(mode):
             if not (
Signature · 1 line
2.34.1
author
#6 lore raw

Hi Emil,

Is this patch good to be picked so that check_whence.py check can be mandated and not ignored?

Quoted 13 lines · Vivek Puar

Attaching the patch here.

Quoted 9 lines · Emil Velikov
Quoted 4 lines · Vivek Puar

Hi,

I've created another pull request, please review it.

Pull requests are not for reviewing but for merging code. They have no code to review.
Please either post your patch or open a MR upstream.

Quoted 7 lines
Quoted 2 lines

Thanks
Emil

Regards,

Vivek

Regards,

Vivek

#7 lore raw

Hey,

Quoted 5 lines · Vivek Puar

Hi Emil,

Is this patch good to be picked so that check_whence.py check can be mandated and not ignored?

Not ignored, just slipped under the radar since it's an attachment and not inline. Either way, thanks for the poke o/

Some comments inline:

Quoted 5 lines

From 306d081594df8a449d07565217153c40b6136646 Mon Sep 17 00:00:00 2001
From: Vivek Puar <[email protected]>
Date: Wed, 23 Oct 2024 00:32:57 +0530
Subject: [PATCH] check_whence.py: Add function to get listing of files

Summary should focus a bit on why aka "Fallback to local file listing, when git fails" or anything along those lines

Quoted 30 lines

Commit [1] introduces check_whence.py inside
copy-firmware.sh before the firmware is consumed.

But current check_whence.py supports only git repo, and it will cause error when linux-firmware tar is used, as it will not be a git repo.

So adding a check to parse local directory to create a set of files to validate it with WHENCE file.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/commit/?id=6edd0fd36acb93c8075bbd700ad91f53fc810c0e

---
 check_whence.py | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)
check_whence.py +1 -1
index 1fece4f4..1ea75a68 100755
@@ -1,6 +1,6 @@
 #!/usr/bin/python3
 
-import os, re, stat, sys
+import os, re, stat, sys, subprocess
 from io import open
 
 

check_whence.py def list_links_list(): · line 64

Quoted 8 lines
check_whence.py +2 -3
@@ -60,12 +60,21 @@ def list_links_list():
 
 
 def list_git():
-    git_files = os.popen("git ls-files")
-    for line in git_files:
-        yield line.rstrip("\n")
+    try:
+        subprocess.check_output(['git', 'rev-parse', '--is-inside-work-tree'], stderr=subprocess.STDOUT)

I thought rev-parse is not meant for end-users... When was the argument introduced, will it disappear at some point? Maybe we can use something else instead?

- check that .git exists, if so try git ls-files
- if the latter fails, but print indicative message depending if .git exists
- missing "doesn't seem like a git repo, falling back to local file listing"
- available "git command failed check above log and address the issue"

All the examples and just random ones I pulled out of thin air. Please use your judgement and opt for something better if it comes to mind.

check_whence.py def list_links_list(): · line 78

Quoted 7 lines
check_whence.py +6 -0
+def list_dir():
+    pwd = os.getcwd()
+    # Parse every directory to create set of files to be matched with WHENCE entries
+    for root, dirs, files in os.walk(pwd):
+        for file in files:
+            yield os.path.relpath(os.path.join(root, file.rstrip("\n")), pwd)
 

This listing should exclude anything under .git. Otherwise we'll get thousands of entries when the repository is available and git fails due to $REASON.
Speaking of reason, some CI will have different file/group ownership so git will fail, unless safe.directory is set. Pretty sure there are various others.

check_whence.py def main(): · line 110

Quoted 8 lines
check_whence.py +2 -1
 def main():
@@ -97,7 +106,10 @@ def main():
         ]
     )
     known_prefixes = set(name for name in whence_list if name.endswith("/"))
-    git_files = set(list_git())
+    files_set = set(list_git())
+    # If it is not git repo call list_dir()

There are various reasons why list_git() can fail, so I'd just drop the comment. While in there local_files is a more indicative name than files_set.

check_whence.py def main(): · line 112

Quoted 2 lines
check_whence.py +2 -0
+    if not files_set:
+        files_set = set(list_dir())

If we cannot get any files from git ls-files and os.walk() something is horribly wrong and we should probably error out _early_ and _loudly_.

check_whence.py def main(): · line 153

Quoted 9 lines
check_whence.py +1 -1
     executable_files = set(
         [
             "build_packages.py",
@@ -138,12 +150,12 @@ def main():
             )
             ret = 1
 
-    for name in sorted(list(known_files - git_files) if len(git_files) else list()):
+    for name in sorted(list(known_files - files_set) if len(files_set) else list()):

Then the if part of this statement can be removed.

check_whence.py def main(): · line 169

Quoted 8 lines
check_whence.py +1 -1
     # ... or to a directory
     for target in set(valid_targets):
@@ -154,14 +166,14 @@ def main():
                 break
             valid_targets.add(dirname)
 
-    for link, target in sorted(links_list if len(git_files) else list()):
+    for link, target in sorted(links_list if len(files_set) else list()):

As does this one.

HTH
Emil