jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/360948 )
Change subject: Don't loop over user contribs ......................................................................
Don't loop over user contribs
- The current implementation loops over user contribs when the total number is below given max which is 500. Just use site.usercontribs with the given parameters to solve this bug. - pagegenerators aren't used and could be removed. - StopIteration is senseless here. The for-loop in revert_contribs stops when the generator has finished. - Keep lines below 80 chars due to PEP8
Bug: T168666 Change-Id: Ib34fcb193e76c37910eda6e15f031afe10b6ce5e --- M scripts/revertbot.py 1 file changed, 14 insertions(+), 36 deletions(-)
Approvals: Dvorapa: Looks good to me, but someone else must approve Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/revertbot.py b/scripts/revertbot.py index 1f649fd..3752a3a 100755 --- a/scripts/revertbot.py +++ b/scripts/revertbot.py @@ -14,7 +14,7 @@ """ # # (C) Bryan Tong Minh, 2008 -# (C) Pywikibot team, 2008-2016 +# (C) Pywikibot team, 2008-2017 # # Ported by Geoffrey "GEOFBOT" Mon - User:Sn1per # for Google Code-In 2013 @@ -29,15 +29,8 @@ import re
import pywikibot - from pywikibot import i18n -from pywikibot import pagegenerators - from pywikibot.tools.formatter import color_format - -docuReplacements = { - '¶ms;': pagegenerators.parameterHelp -}
class BaseRevertBot(object): @@ -59,39 +52,22 @@
def get_contributions(self, max=500, ns=None): """Get contributions.""" - count = 0 - iterator = pywikibot.tools.empty_iterator() - never_continue = False - while count != max or never_continue: - try: - item = next(iterator) - except StopIteration: - self.log(u'Fetching new batch of contributions') - data = list(pywikibot.Site().usercontribs(user=self.user, namespaces=ns, total=max)) - never_continue = True - iterator = iter(data) - else: - count += 1 - yield item + return self.site.usercontribs(user=self.user, namespaces=ns, total=max)
def revert_contribs(self, callback=None): """Revert contributions.""" if callback is None: callback = self.callback
- contribs = self.get_contributions() - for item in contribs: - try: - if callback(item): - result = self.revert(item) - if result: - self.log(u'%s: %s' % (item['title'], result)) - else: - self.log(u'Skipped %s' % item['title']) + for item in self.get_contributions(): + if callback(item): + result = self.revert(item) + if result: + self.log(u'%s: %s' % (item['title'], result)) else: - self.log(u'Skipped %s by callback' % item['title']) - except StopIteration: - return + self.log(u'Skipped %s' % item['title']) + else: + self.log(u'Skipped %s by callback' % item['title'])
def callback(self, item): """Callback function.""" @@ -130,11 +106,13 @@ 'markbot': True}).submit() except pywikibot.data.api.APIError as e: if e.code == 'badtoken': - pywikibot.error("There was an API token error rollbacking the edit") + pywikibot.error( + 'There was an API token error rollbacking the edit') else: pywikibot.exception() return False - return u"The edit(s) made in %s by %s was rollbacked" % (page.title(), self.user) + return 'The edit(s) made in %s by %s was rollbacked' % (page.title(), + self.user)
def log(self, msg): """Log the message msg."""
pywikibot-commits@lists.wikimedia.org