check_downloads_ini.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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(
  36. '-d',
  37. '--downloads-ini',
  38. type=Path,
  39. nargs='*',
  40. default=default_downloads_ini,
  41. help='List of downloads.ini files to check. Default: %(default)s')
  42. args = parser.parse_args()
  43. if check_downloads_ini(args.downloads_ini):
  44. exit(1)
  45. exit(0)
  46. if __name__ == '__main__':
  47. main()