check-uniq-files 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. import sys
  3. import argparse
  4. from collections import defaultdict
  5. warn = 'Warning: {0} file "{1}" is touched by more than one package: {2}\n'
  6. def main():
  7. parser = argparse.ArgumentParser()
  8. parser.add_argument('packages_file_list', nargs='*',
  9. help='The packages-file-list to check from')
  10. parser.add_argument('-t', '--type', metavar="TYPE",
  11. help='Report as a TYPE file (TYPE is either target, staging, or host)')
  12. args = parser.parse_args()
  13. if not len(args.packages_file_list) == 1:
  14. sys.stderr.write('No packages-file-list was provided.\n')
  15. return False
  16. if args.type is None:
  17. sys.stderr.write('No type was provided\n')
  18. return False
  19. file_to_pkg = defaultdict(list)
  20. with open(args.packages_file_list[0], 'rb') as pkg_file_list:
  21. for line in pkg_file_list.readlines():
  22. pkg, _, file = line.rstrip(b'\n').partition(b',')
  23. file_to_pkg[file].append(pkg)
  24. for file in file_to_pkg:
  25. if len(file_to_pkg[file]) > 1:
  26. # If possible, try to decode the binary strings with
  27. # the default user's locale
  28. try:
  29. sys.stderr.write(warn.format(args.type, file.decode(),
  30. [p.decode() for p in file_to_pkg[file]]))
  31. except UnicodeDecodeError:
  32. # ... but fallback to just dumping them raw if they
  33. # contain non-representable chars
  34. sys.stderr.write(warn.format(args.type, file,
  35. file_to_pkg[file]))
  36. if __name__ == "__main__":
  37. sys.exit(main())