2022-11-30 12:52:25 +00:00
|
|
|
#!/usr/bin/python3
|
2016-09-18 00:44:04 +00:00
|
|
|
|
2024-10-15 17:38:26 +00:00
|
|
|
import os, re, stat, sys
|
2020-02-29 02:15:05 +00:00
|
|
|
from io import open
|
2016-09-18 00:44:04 +00:00
|
|
|
|
2022-08-03 19:24:48 +00:00
|
|
|
|
2016-09-18 00:44:04 +00:00
|
|
|
def list_whence():
|
2022-08-03 19:24:48 +00:00
|
|
|
with open("WHENCE", encoding="utf-8") as whence:
|
2016-09-18 00:44:04 +00:00
|
|
|
for line in whence:
|
2023-09-01 06:52:24 +00:00
|
|
|
match = re.match(r'(?:RawFile|File|Source):\s*"(.*)"', line)
|
2018-12-13 16:35:11 +00:00
|
|
|
if match:
|
|
|
|
yield match.group(1)
|
|
|
|
continue
|
2023-09-01 06:52:24 +00:00
|
|
|
match = re.match(r"(?:RawFile|File|Source):\s*(\S*)", line)
|
2016-09-18 00:44:04 +00:00
|
|
|
if match:
|
|
|
|
yield match.group(1)
|
|
|
|
continue
|
2022-08-03 19:24:48 +00:00
|
|
|
match = re.match(
|
|
|
|
r"Licen[cs]e: (?:.*\bSee (.*) for details\.?|(\S*))\n", line
|
|
|
|
)
|
2016-09-18 00:44:04 +00:00
|
|
|
if match:
|
|
|
|
if match.group(1):
|
2022-08-03 19:24:48 +00:00
|
|
|
for name in re.split(r", | and ", match.group(1)):
|
2016-09-18 00:44:04 +00:00
|
|
|
yield name
|
|
|
|
continue
|
|
|
|
if match.group(2):
|
|
|
|
# Just one word - may or may not be a filename
|
2022-08-03 19:24:48 +00:00
|
|
|
if not re.search(
|
|
|
|
r"unknown|distributable", match.group(2), re.IGNORECASE
|
|
|
|
):
|
2016-09-18 00:44:04 +00:00
|
|
|
yield match.group(2)
|
|
|
|
continue
|
|
|
|
|
2022-08-03 19:24:48 +00:00
|
|
|
|
2023-03-01 18:51:31 +00:00
|
|
|
def list_whence_files():
|
2022-08-03 19:24:48 +00:00
|
|
|
with open("WHENCE", encoding="utf-8") as whence:
|
2023-03-01 18:51:31 +00:00
|
|
|
for line in whence:
|
2023-09-01 06:52:24 +00:00
|
|
|
match = re.match(r"(?:RawFile|File):\s*(.*)", line)
|
2023-03-01 18:51:31 +00:00
|
|
|
if match:
|
2024-09-22 19:44:59 +00:00
|
|
|
yield match.group(1).replace(r"\ ", " ").replace('"', "")
|
2023-03-01 18:51:31 +00:00
|
|
|
continue
|
|
|
|
|
2022-08-03 19:24:48 +00:00
|
|
|
|
2023-06-05 13:58:07 +00:00
|
|
|
def list_links_list():
|
2022-08-03 19:24:48 +00:00
|
|
|
with open("WHENCE", encoding="utf-8") as whence:
|
2023-06-05 13:58:07 +00:00
|
|
|
for line in whence:
|
2022-08-03 19:24:48 +00:00
|
|
|
match = re.match(r"Link:\s*(.*)", line)
|
2023-06-05 13:58:07 +00:00
|
|
|
if match:
|
|
|
|
linkname, target = match.group(1).split("->")
|
|
|
|
|
2024-09-22 19:44:59 +00:00
|
|
|
linkname = linkname.strip().replace(r"\ ", " ").replace('"', "")
|
|
|
|
target = target.strip().replace(r"\ ", " ").replace('"', "")
|
2023-06-05 13:58:07 +00:00
|
|
|
|
|
|
|
# Link target is relative to the link
|
|
|
|
target = os.path.join(os.path.dirname(linkname), target)
|
|
|
|
target = os.path.normpath(target)
|
|
|
|
|
|
|
|
yield (linkname, target)
|
|
|
|
continue
|
|
|
|
|
2022-08-03 19:24:48 +00:00
|
|
|
|
2016-09-18 00:44:04 +00:00
|
|
|
def list_git():
|
2024-10-18 12:58:01 +00:00
|
|
|
git_files = os.popen("git ls-files")
|
|
|
|
for line in git_files:
|
|
|
|
yield line.rstrip("\n")
|
|
|
|
|
|
|
|
if git_files.close():
|
|
|
|
sys.stderr.write("W: git file listing failed, skipping some validation\n")
|
2022-08-03 19:24:48 +00:00
|
|
|
|
2016-09-18 00:44:04 +00:00
|
|
|
|
|
|
|
def main():
|
2020-02-29 01:32:48 +00:00
|
|
|
ret = 0
|
2016-09-18 00:44:04 +00:00
|
|
|
whence_list = list(list_whence())
|
2023-03-01 18:51:31 +00:00
|
|
|
whence_files = list(list_whence_files())
|
2023-06-05 13:58:07 +00:00
|
|
|
links_list = list(list_links_list())
|
2024-02-02 14:41:02 +00:00
|
|
|
whence_links = list(zip(*links_list))[0]
|
2022-08-03 19:24:48 +00:00
|
|
|
known_files = set(name for name in whence_list if not name.endswith("/")) | set(
|
|
|
|
[
|
2023-08-29 22:00:06 +00:00
|
|
|
".codespell.cfg",
|
2024-09-22 15:02:38 +00:00
|
|
|
".editorconfig",
|
2024-09-22 16:29:49 +00:00
|
|
|
".gitignore",
|
2023-08-30 03:00:04 +00:00
|
|
|
".gitlab-ci.yml",
|
2023-08-29 22:00:06 +00:00
|
|
|
".pre-commit-config.yaml",
|
2024-09-22 16:29:49 +00:00
|
|
|
"Dockerfile",
|
2022-08-03 19:24:48 +00:00
|
|
|
"Makefile",
|
2023-08-15 17:33:17 +00:00
|
|
|
"README.md",
|
2022-08-03 19:24:48 +00:00
|
|
|
"WHENCE",
|
2024-09-22 16:29:49 +00:00
|
|
|
"build_packages.py",
|
|
|
|
"check_whence.py",
|
|
|
|
"contrib/process_linux_firmware.py",
|
2023-09-18 21:15:35 +00:00
|
|
|
"contrib/templates/debian.changelog",
|
|
|
|
"contrib/templates/debian.control",
|
|
|
|
"contrib/templates/debian.copyright",
|
|
|
|
"contrib/templates/rpm.spec",
|
2024-09-22 16:29:49 +00:00
|
|
|
"copy-firmware.sh",
|
2024-09-22 15:21:44 +00:00
|
|
|
"dedup-firmware.sh",
|
2022-08-03 19:24:48 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
known_prefixes = set(name for name in whence_list if name.endswith("/"))
|
2016-09-18 00:44:04 +00:00
|
|
|
git_files = set(list_git())
|
2024-10-15 17:38:26 +00:00
|
|
|
executable_files = set(
|
|
|
|
[
|
|
|
|
"build_packages.py",
|
|
|
|
"carl9170fw/genapi.sh",
|
|
|
|
"carl9170fw/autogen.sh",
|
|
|
|
"check_whence.py",
|
|
|
|
"contrib/process_linux_firmware.py",
|
|
|
|
"copy-firmware.sh",
|
|
|
|
"dedup-firmware.sh",
|
|
|
|
]
|
|
|
|
)
|
2016-09-18 00:44:04 +00:00
|
|
|
|
2022-08-03 19:24:48 +00:00
|
|
|
for name in set(name for name in whence_files if name.endswith("/")):
|
|
|
|
sys.stderr.write("E: %s listed in WHENCE as File, but is directory\n" % name)
|
2023-03-01 18:56:21 +00:00
|
|
|
ret = 1
|
|
|
|
|
2024-09-22 18:11:09 +00:00
|
|
|
for name in set(name for name in whence_files if whence_files.count(name) > 1):
|
2022-08-03 19:24:48 +00:00
|
|
|
sys.stderr.write("E: %s listed in WHENCE twice\n" % name)
|
2023-03-01 18:51:31 +00:00
|
|
|
ret = 1
|
|
|
|
|
2024-02-02 14:41:02 +00:00
|
|
|
for name in set(link for link in whence_links if whence_links.count(link) > 1):
|
|
|
|
sys.stderr.write("E: %s listed in WHENCE twice\n" % name)
|
|
|
|
ret = 1
|
|
|
|
|
2024-09-22 18:11:09 +00:00
|
|
|
for name in set(file for file in whence_files if os.path.islink(file)):
|
2022-08-03 19:24:48 +00:00
|
|
|
sys.stderr.write("E: %s listed in WHENCE as File, but is a symlink\n" % name)
|
2023-06-05 13:58:06 +00:00
|
|
|
ret = 1
|
|
|
|
|
2023-06-05 13:58:07 +00:00
|
|
|
for name in set(link[0] for link in links_list if os.path.islink(link[0])):
|
2022-08-03 19:24:48 +00:00
|
|
|
sys.stderr.write("E: %s listed in WHENCE as Link, is in tree\n" % name)
|
2023-06-05 13:58:07 +00:00
|
|
|
ret = 1
|
|
|
|
|
2024-09-22 16:31:24 +00:00
|
|
|
invalid_targets = set(link[0] for link in links_list)
|
|
|
|
for link, target in sorted(links_list):
|
|
|
|
if target in invalid_targets:
|
|
|
|
sys.stderr.write(
|
|
|
|
"E: target %s of link %s is also a link\n" % (target, link)
|
|
|
|
)
|
|
|
|
ret = 1
|
|
|
|
|
2024-10-18 12:58:01 +00:00
|
|
|
for name in sorted(list(known_files - git_files) if len(git_files) else list()):
|
2022-08-03 19:24:48 +00:00
|
|
|
sys.stderr.write("E: %s listed in WHENCE does not exist\n" % name)
|
2020-02-29 01:32:48 +00:00
|
|
|
ret = 1
|
2016-09-18 00:44:04 +00:00
|
|
|
|
2024-09-22 16:31:24 +00:00
|
|
|
# A link can point to a file...
|
|
|
|
valid_targets = set(git_files)
|
2023-06-26 15:08:22 +00:00
|
|
|
|
|
|
|
# ... or to a directory
|
|
|
|
for target in set(valid_targets):
|
|
|
|
dirname = target
|
|
|
|
while True:
|
|
|
|
dirname = os.path.dirname(dirname)
|
2022-08-03 19:24:48 +00:00
|
|
|
if dirname == "":
|
2023-06-26 15:08:22 +00:00
|
|
|
break
|
|
|
|
valid_targets.add(dirname)
|
|
|
|
|
2024-10-18 12:58:01 +00:00
|
|
|
for link, target in sorted(links_list if len(git_files) else list()):
|
2023-06-26 15:08:22 +00:00
|
|
|
if target not in valid_targets:
|
2022-08-03 19:24:48 +00:00
|
|
|
sys.stderr.write(
|
2024-10-18 12:58:01 +00:00
|
|
|
"E: target %s of link %s in WHENCE does not exist\n" % (target, link)
|
2022-08-03 19:24:48 +00:00
|
|
|
)
|
2023-06-26 15:08:22 +00:00
|
|
|
ret = 1
|
|
|
|
|
2016-09-18 00:44:04 +00:00
|
|
|
for name in sorted(list(git_files - known_files)):
|
|
|
|
# Ignore subdirectory changelogs and GPG detached signatures
|
2022-08-03 19:24:48 +00:00
|
|
|
if name.endswith("/ChangeLog") or (
|
|
|
|
name.endswith(".asc") and name[:-4] in known_files
|
|
|
|
):
|
2016-09-18 00:44:04 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Ignore unknown files in known directories
|
|
|
|
for prefix in known_prefixes:
|
|
|
|
if name.startswith(prefix):
|
|
|
|
break
|
|
|
|
else:
|
2022-08-03 19:24:48 +00:00
|
|
|
sys.stderr.write("E: %s not listed in WHENCE\n" % name)
|
2020-02-29 01:32:48 +00:00
|
|
|
ret = 1
|
2024-10-15 17:38:26 +00:00
|
|
|
|
|
|
|
for name in sorted(list(executable_files)):
|
|
|
|
mode = os.stat(name).st_mode
|
|
|
|
if not (mode & stat.S_IXUSR and mode & stat.S_IXGRP and mode & stat.S_IXOTH):
|
|
|
|
sys.stderr.write("E: %s is missing execute bit\n" % name)
|
|
|
|
ret = 1
|
|
|
|
|
|
|
|
for name in sorted(list(git_files - executable_files)):
|
|
|
|
mode = os.stat(name).st_mode
|
|
|
|
if stat.S_ISDIR(mode):
|
|
|
|
if not (
|
|
|
|
mode & stat.S_IXUSR and mode & stat.S_IXGRP and mode & stat.S_IXOTH
|
|
|
|
):
|
|
|
|
sys.stderr.write("E: %s is missing execute bit\n" % name)
|
|
|
|
ret = 1
|
|
|
|
elif stat.S_ISREG(mode):
|
|
|
|
if mode & stat.S_IXUSR or mode & stat.S_IXGRP or mode & stat.S_IXOTH:
|
|
|
|
sys.stderr.write("E: %s incorrectly has execute bit\n" % name)
|
|
|
|
ret = 1
|
|
|
|
else:
|
|
|
|
sys.stderr.write("E: %s is neither a directory nor regular file\n" % name)
|
|
|
|
ret = 1
|
|
|
|
|
2020-02-29 01:32:48 +00:00
|
|
|
return ret
|
2016-09-18 00:44:04 +00:00
|
|
|
|
2022-08-03 19:24:48 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-02-29 01:32:48 +00:00
|
|
|
sys.exit(main())
|