housekeeping.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # nm.debian.org website housekeeping
  2. # pymode:lint_ignore=E501
  3. #
  4. # Copyright (C) 2014 Enrico Zini <enrico@debian.org>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. from __future__ import print_function
  19. from __future__ import absolute_import
  20. from __future__ import division
  21. from __future__ import unicode_literals
  22. from django.conf import settings
  23. from django.core.exceptions import ImproperlyConfigured
  24. from backend.housekeeping import MakeLink
  25. import django_housekeeping as hk
  26. import backend.models as bmodels
  27. import debiancontributors as dc
  28. import logging
  29. log = logging.getLogger(__name__)
  30. DC_AUTH_TOKEN = getattr(settings, "DC_AUTH_TOKEN", None)
  31. DC_SUBMIT_URL = getattr(settings, "DC_SUBMIT_URL", None)
  32. DC_GIT_REPO_NM = getattr(settings, "DC_GIT_REPO_NM", "/srv/nm.debian.org/nm2/.git")
  33. DC_GIT_REPO_DC = getattr(settings, "DC_GIT_REPO_DC", "/srv/contributors.debian.org/dc/.git")
  34. STAGES = ["main", "reports", "stats"]
  35. class SubmitContributors(hk.Task):
  36. """
  37. Compute contributions and submit them to contributors.debian.org
  38. """
  39. DEPENDS = [MakeLink]
  40. def run_reports(self, stage):
  41. from django.db.models import Min, Max
  42. if DC_AUTH_TOKEN is None:
  43. if settings.DEBUG:
  44. log.warning("DC_AUTH_TOKEN is not configured, we cannot submit to contributors.debian.org")
  45. log.warning("Stopping processing here, this would result in an exception in production.")
  46. return
  47. else:
  48. raise ImproperlyConfigured("DC_AUTH_TOKEN is not configured, we cannot submit to contributors.debian.org")
  49. datamine = dc.DataMine(configstr="""
  50. source: nm.debian.org
  51. contribution: dc-devel
  52. method: gitlogs
  53. dirs: {git_repo_dc}
  54. contribution: nm-devel
  55. method: gitlogs
  56. dirs: {git_repo_nm}
  57. """.format(git_repo_dc=DC_GIT_REPO_DC, git_repo_nm=DC_GIT_REPO_NM))
  58. datamine.scan()
  59. submission = datamine.submission
  60. for am in bmodels.AM.objects.all():
  61. res = bmodels.Log.objects.filter(changed_by=am.person, process__manager=am).aggregate(
  62. since=Min("logdate"),
  63. until=Max("logdate"))
  64. if res["since"] is None or res["until"] is None:
  65. continue
  66. submission.add_contribution_data(
  67. dc.Identifier(type="login", id=am.person.uid, desc=am.person.fullname),
  68. type="am", begin=res["since"].date(), end=res["until"].date(),
  69. url=self.hk.link(am))
  70. for am in bmodels.AM.objects.filter(is_fd=True):
  71. res = bmodels.Log.objects.filter(changed_by=am.person).exclude(process__manager=am).aggregate(
  72. since=Min("logdate"),
  73. until=Max("logdate"))
  74. if res["since"] is None or res["until"] is None:
  75. continue
  76. submission.add_contribution_data(
  77. dc.Identifier(type="login", id=am.person.uid, desc=am.person.fullname),
  78. type="fd", begin=res["since"].date(), end=res["until"].date(),
  79. url=self.hk.link(am))
  80. submission.set_auth_token(DC_AUTH_TOKEN)
  81. if DC_SUBMIT_URL:
  82. submission.baseurl = DC_SUBMIT_URL
  83. res, info = submission.post()
  84. if not res:
  85. log.error("%s: submission failed: %r", self.IDENTIFIER, info)