tracker_tasks.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013 The Distro Tracker Developers
  3. # See the COPYRIGHT file at the top-level directory of this distribution and
  4. # at http://deb.li/DTAuthors
  5. #
  6. # This file is part of Distro Tracker. It is subject to the license terms
  7. # in the LICENSE file found in the top-level directory of this
  8. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  9. # including this file, may be copied, modified, propagated, or distributed
  10. # except according to the terms contained in the LICENSE file.
  11. """
  12. Distro Tracker tasks for the :mod:`distro_tracker.stdver_warnings` app.
  13. """
  14. from __future__ import unicode_literals
  15. from distro_tracker.core.tasks import BaseTask
  16. from distro_tracker.core.utils import get_or_none
  17. from distro_tracker.core.models import SourcePackageName
  18. from distro_tracker.core.models import ActionItem, ActionItemType
  19. class UpdateStandardsVersionWarnings(BaseTask):
  20. """
  21. The task updates warnings for packages which have an outdated
  22. Standards-Version.
  23. """
  24. DEPENDS_ON_EVENTS = (
  25. 'new-source-package-version',
  26. )
  27. ACTION_ITEM_TYPE = 'debian-std-ver-outdated'
  28. FULL_DESCRIPTION_TEMPLATE = \
  29. 'stdver_warnings/standards-version-action-item.html'
  30. ITEM_DESCRIPTION = "Standards version of the package is outdated."
  31. def __init__(self, force_update=False, *args, **kwargs):
  32. super(UpdateStandardsVersionWarnings, self).__init__(*args, **kwargs)
  33. self.force_update = force_update
  34. self.action_type = ActionItemType.objects.create_or_update(
  35. type_name=self.ACTION_ITEM_TYPE,
  36. full_description_template=self.FULL_DESCRIPTION_TEMPLATE)
  37. def set_parameters(self, parameters):
  38. if 'force_update' in parameters:
  39. self.force_update = parameters['force_update']
  40. def get_packages_from_events(self):
  41. """
  42. :returns: A list of
  43. :class:`distro_tracker.core.models.SourcePackageName` instances
  44. which are found from all raised events.
  45. """
  46. package_pks = [
  47. event.arguments['pk']
  48. for event in self.get_all_events()
  49. ]
  50. qs = SourcePackageName.objects.filter(
  51. source_package_versions__pk__in=package_pks)
  52. qs.prefetch_related('action_items')
  53. return qs
  54. def get_policy_version(self):
  55. """
  56. :returns: The latest version of the ``debian-policy`` package.
  57. """
  58. debian_policy = get_or_none(SourcePackageName, name='debian-policy')
  59. if not debian_policy:
  60. return
  61. policy_version = debian_policy.main_version.version
  62. # Minor patch level should be disregarded for the comparison
  63. policy_version, _ = policy_version.rsplit('.', 1)
  64. return policy_version
  65. def create_action_item(self, package, policy_version):
  66. """
  67. Creates a :class:`distro_tracker.core.models.ActionItem` instance if the
  68. Standards-Version of the given package is outdated when compared to the
  69. given policy version.
  70. """
  71. if not package.main_version:
  72. return
  73. # Get the old action item entry
  74. action_item = package.get_action_item_for_type(self.ACTION_ITEM_TYPE)
  75. standards_version = package.main_version.standards_version
  76. if standards_version.startswith(policy_version):
  77. # The std-ver of the package is up to date.
  78. # Remove any possibly existing action item.
  79. if action_item is not None:
  80. action_item.delete()
  81. return
  82. major_policy_version_number, _ = policy_version.split('.', 1)
  83. severely_outdated = not standards_version.startswith(
  84. major_policy_version_number)
  85. if action_item is None:
  86. action_item = ActionItem(
  87. package=package,
  88. item_type=self.action_type)
  89. # Remove the minor patch level from the package's Std-Ver, if it has it
  90. if standards_version.count('.') == 3:
  91. standards_version, _ = standards_version.rsplit('.', 1)
  92. if severely_outdated:
  93. action_item.severity = ActionItem.SEVERITY_HIGH
  94. else:
  95. action_item.severity = ActionItem.SEVERITY_WISHLIST
  96. action_item.short_description = self.ITEM_DESCRIPTION
  97. action_item.extra_data = {
  98. 'lastsv': policy_version,
  99. 'standards_version': standards_version,
  100. 'severely_outdated': severely_outdated,
  101. }
  102. action_item.save()
  103. def execute(self):
  104. # Get the current policy version
  105. policy_version = self.get_policy_version()
  106. if policy_version is None:
  107. # Nothing to do if there is no ``debian-policy``
  108. return
  109. if self.is_initial_task():
  110. # If the task is directly ran, update all packages
  111. packages = SourcePackageName.objects.all()
  112. packages.prefetch_related('action_items')
  113. else:
  114. # If the task is ran as part of a job, get the packages from raised
  115. # events
  116. packages = self.get_packages_from_events()
  117. for package in packages:
  118. self.create_action_item(package, policy_version)