DEPRECATED -- Rewritten and moved to https://github.com/NiklasRosenstein/shut/. 🌊 Shore is a distribution and release management tool for pure Python packages.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.1 KiB

  1. # -*- coding: utf8 -*-
  2. # Copyright (c) 2020 Niklas Rosenstein
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to
  6. # deal in the Software without restriction, including without limitation the
  7. # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. # sell copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. from shore.model import Monorepo
  22. from shore.core.plugins import CheckResult
  23. from . import pkg, load_package_manifest
  24. from nr.stream import Stream
  25. from termcolor import colored
  26. import click
  27. import logging
  28. import sys
  29. logger = logging.getLogger(__name__)
  30. def _run_for_subject(subject, func): # type: (Union[Package, Monorepo], func) -> List[Any]
  31. if isinstance(subject, Monorepo):
  32. subjects = [subject] + sorted(subject.get_packages(), key=lambda x: x.name)
  33. return [func(x) for x in subjects]
  34. else:
  35. return [func(subject)]
  36. def run_checks(package, warnings_as_errors): # type: (Package, bool) -> bool
  37. package_name_c = colored(package.name, 'yellow')
  38. checks = list(Stream.concat(x.get_checks(package) for x in package.get_plugins()))
  39. if not checks:
  40. print('✔ no checks triggered on package {}'.format(package_name_c))
  41. return True
  42. max_level = max(x.level for x in checks)
  43. if max_level == CheckResult.Level.INFO:
  44. status = 0
  45. elif max_level == CheckResult.Level.WARNING:
  46. status = 1 if warnings_as_errors else 0
  47. elif max_level == CheckResult.Level.ERROR:
  48. status = 1
  49. else:
  50. assert False, max_level
  51. icon = '' if status != 0 else ''
  52. print(icon, len(checks), 'check(s) triggered on package {}:'.format(package_name_c))
  53. colors = {'ERROR': 'red', 'WARNING': 'magenta', 'INFO': None}
  54. for check in checks:
  55. level = colored(check.level.name, colors[check.level.name])
  56. print('-', level, check.message)
  57. logger.debug('exiting with status %s', status)
  58. return False
  59. @pkg.command()
  60. @click.option('-w', '--warnings-as-errors', is_flag=True)
  61. def sanity(warnings_as_errors):
  62. """
  63. Sanity-check the package configuration and package files. Which checks are performed
  64. depends on the features that are enabled in the package configuration. Usually that
  65. will at least include the "setuptools" feature which will perform basic sanity checks
  66. on the package configuration and entrypoint definition.
  67. """
  68. package = load_package_manifest()
  69. result = run_checks(package, warnings_as_errors)
  70. if not result:
  71. sys.exit(1)