Browse Source

check_whence: reformat using python black

Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
main
Mario Limonciello 2 years ago
parent
commit
27fb2f6368
  1. 85
      check_whence.py

85
check_whence.py

@ -3,48 +3,53 @@
import os, re, sys import os, re, sys
from io import open from io import open



def list_whence(): def list_whence():
with open('WHENCE', encoding='utf-8') as whence: with open("WHENCE", encoding="utf-8") as whence:
for line in whence: for line in whence:
match = re.match(r'(?:File|Source):\s*"(.*)"', line) match = re.match(r'(?:File|Source):\s*"(.*)"', line)
if match: if match:
yield match.group(1) yield match.group(1)
continue continue
match = re.match(r'(?:File|Source):\s*(\S*)', line) match = re.match(r"(?:File|Source):\s*(\S*)", line)
if match: if match:
yield match.group(1) yield match.group(1)
continue continue
match = re.match(r'Licen[cs]e: (?:.*\bSee (.*) for details\.?|(\S*))\n', match = re.match(
line) r"Licen[cs]e: (?:.*\bSee (.*) for details\.?|(\S*))\n", line
)
if match: if match:
if match.group(1): if match.group(1):
for name in re.split(r', | and ', match.group(1)): for name in re.split(r", | and ", match.group(1)):
yield name yield name
continue continue
if match.group(2): if match.group(2):
# Just one word - may or may not be a filename # Just one word - may or may not be a filename
if not re.search(r'unknown|distributable', match.group(2), if not re.search(
re.IGNORECASE): r"unknown|distributable", match.group(2), re.IGNORECASE
):
yield match.group(2) yield match.group(2)
continue continue



def list_whence_files(): def list_whence_files():
with open('WHENCE', encoding='utf-8') as whence: with open("WHENCE", encoding="utf-8") as whence:
for line in whence: for line in whence:
match = re.match(r'File:\s*(.*)', line) match = re.match(r"File:\s*(.*)", line)
if match: if match:
yield match.group(1).replace("\ ", " ").replace("\"", "") yield match.group(1).replace("\ ", " ").replace('"', "")
continue continue



def list_links_list(): def list_links_list():
with open('WHENCE', encoding='utf-8') as whence: with open("WHENCE", encoding="utf-8") as whence:
for line in whence: for line in whence:
match = re.match(r'Link:\s*(.*)', line) match = re.match(r"Link:\s*(.*)", line)
if match: if match:
linkname, target = match.group(1).split("->") linkname, target = match.group(1).split("->")


linkname = linkname.strip().replace("\ ", " ").replace("\"", "") linkname = linkname.strip().replace("\ ", " ").replace('"', "")
target = target.strip().replace("\ ", " ").replace("\"", "") target = target.strip().replace("\ ", " ").replace('"', "")


# Link target is relative to the link # Link target is relative to the link
target = os.path.join(os.path.dirname(linkname), target) target = os.path.join(os.path.dirname(linkname), target)
@ -53,42 +58,49 @@ def list_links_list():
yield (linkname, target) yield (linkname, target)
continue continue



def list_git(): def list_git():
with os.popen('git ls-files') as git_files: with os.popen("git ls-files") as git_files:
for line in git_files: for line in git_files:
yield line.rstrip('\n') yield line.rstrip("\n")



def main(): def main():
ret = 0 ret = 0
whence_list = list(list_whence()) whence_list = list(list_whence())
whence_files = list(list_whence_files()) whence_files = list(list_whence_files())
links_list = list(list_links_list()) links_list = list(list_links_list())
known_files = set(name for name in whence_list if not name.endswith('/')) | \ known_files = set(name for name in whence_list if not name.endswith("/")) | set(
set(['check_whence.py', 'configure', 'Makefile', [
'README', 'copy-firmware.sh', 'WHENCE']) "check_whence.py",
known_prefixes = set(name for name in whence_list if name.endswith('/')) "configure",
"Makefile",
"README",
"copy-firmware.sh",
"WHENCE",
]
)
known_prefixes = set(name for name in whence_list if name.endswith("/"))
git_files = set(list_git()) git_files = set(list_git())


for name in set(name for name in whence_files if name.endswith('/')): 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' % sys.stderr.write("E: %s listed in WHENCE as File, but is directory\n" % name)
name)
ret = 1 ret = 1


for name in set(fw for fw in whence_files if whence_files.count(fw) > 1): for name in set(fw for fw in whence_files if whence_files.count(fw) > 1):
sys.stderr.write('E: %s listed in WHENCE twice\n' % name) sys.stderr.write("E: %s listed in WHENCE twice\n" % name)
ret = 1 ret = 1


for name in set(link for link in whence_files if os.path.islink(link)): for name in set(link for link in whence_files if os.path.islink(link)):
sys.stderr.write('E: %s listed in WHENCE as File, but is a symlink\n' % sys.stderr.write("E: %s listed in WHENCE as File, but is a symlink\n" % name)
name)
ret = 1 ret = 1


for name in set(link[0] for link in links_list if os.path.islink(link[0])): for name in set(link[0] for link in links_list if os.path.islink(link[0])):
sys.stderr.write('E: %s listed in WHENCE as Link, is in tree\n' % name) sys.stderr.write("E: %s listed in WHENCE as Link, is in tree\n" % name)
ret = 1 ret = 1


for name in sorted(list(known_files - git_files)): for name in sorted(list(known_files - git_files)):
sys.stderr.write('E: %s listed in WHENCE does not exist\n' % name) sys.stderr.write("E: %s listed in WHENCE does not exist\n" % name)
ret = 1 ret = 1


# A link can point to another link, or to a file... # A link can point to another link, or to a file...
@ -99,20 +111,22 @@ def main():
dirname = target dirname = target
while True: while True:
dirname = os.path.dirname(dirname) dirname = os.path.dirname(dirname)
if dirname == '': if dirname == "":
break break
valid_targets.add(dirname) valid_targets.add(dirname)


for name, target in sorted(links_list): for name, target in sorted(links_list):
if target not in valid_targets: if target not in valid_targets:
sys.stderr.write('E: target %s of link %s in WHENCE' sys.stderr.write(
' does not exist\n' % (target, name)) "E: target %s of link %s in WHENCE" " does not exist\n" % (target, name)
)
ret = 1 ret = 1


for name in sorted(list(git_files - known_files)): for name in sorted(list(git_files - known_files)):
# Ignore subdirectory changelogs and GPG detached signatures # Ignore subdirectory changelogs and GPG detached signatures
if (name.endswith('/ChangeLog') or if name.endswith("/ChangeLog") or (
(name.endswith('.asc') and name[:-4] in known_files)): name.endswith(".asc") and name[:-4] in known_files
):
continue continue


# Ignore unknown files in known directories # Ignore unknown files in known directories
@ -120,9 +134,10 @@ def main():
if name.startswith(prefix): if name.startswith(prefix):
break break
else: else:
sys.stderr.write('E: %s not listed in WHENCE\n' % name) sys.stderr.write("E: %s not listed in WHENCE\n" % name)
ret = 1 ret = 1
return ret return ret


if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(main()) sys.exit(main())

Loading…
Cancel
Save