check_downloads_ini.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. # Copyright (c) 2019 The ungoogled-chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Run sanity checking algorithms over downloads.ini files
  7. It checks the following:
  8. * downloads.ini has the correct format (i.e. conforms to its schema)
  9. Exit codes:
  10. * 0 if no problems detected
  11. * 1 if warnings or errors occur
  12. """
  13. import argparse
  14. import sys
  15. from pathlib import Path
  16. sys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'utils'))
  17. from downloads import DownloadInfo, schema
  18. sys.path.pop(0)
  19. def check_downloads_ini(downloads_ini_iter):
  20. """
  21. Combines and checks if the the downloads.ini files provided are valid.
  22. downloads_ini_iter must be an iterable of strings to downloads.ini files.
  23. Returns True if errors occured, False otherwise.
  24. """
  25. try:
  26. DownloadInfo(downloads_ini_iter)
  27. except schema.SchemaError:
  28. return True
  29. return False
  30. def main():
  31. """CLI entrypoint"""
  32. root_dir = Path(__file__).resolve().parent.parent
  33. default_downloads_ini = [str(root_dir / 'downloads.ini')]
  34. parser = argparse.ArgumentParser(description=__doc__)
  35. parser.add_argument('-d',
  36. '--downloads-ini',
  37. type=Path,
  38. nargs='*',
  39. default=default_downloads_ini,
  40. help='List of downloads.ini files to check. Default: %(default)s')
  41. args = parser.parse_args()
  42. if check_downloads_ini(args.downloads_ini):
  43. sys.exit(1)
  44. sys.exit(0)
  45. if __name__ == '__main__':
  46. main()