jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/398492 )
Change subject: diff_checker.py: Add a check for double quoted strings
......................................................................
diff_checker.py: Add a check for double quoted strings
The new check will fail if there is a double quoted string in the
newly added/ modified lines that does not contain any single quotes.
A single quoted string should be used in this situation.
Now that we are tokenizing the modules, we can rewrite the u-prefix
check using the new logic that will not have any false positive.
Use `line.path` instead of `line.target_file` in check check. The former
the actual file path. `target_file` starts with an unnecessary 'b/'
prefix.
Fix the encoding issue in get_latest_patchset.
Change-Id: I84afcc0817a48514d1a99e8cae2091847931aff3
Signed-off-by: dalba <dalba.wiki(a)gmail.com>
---
M scripts/maintenance/diff_checker.py
1 file changed, 51 insertions(+), 36 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/maintenance/diff_checker.py b/scripts/maintenance/diff_checker.py
index bd428d8..957c247 100644
--- a/scripts/maintenance/diff_checker.py
+++ b/scripts/maintenance/diff_checker.py
@@ -6,13 +6,14 @@
- Line lengths: Newly added lines longer than 79 chars are not allowed.
- Unicode literal notations: They are not allowed since we instead import
unicode_literals from __future__.
+ - Any added double quoted string should contain a single quote.
See [[Manual:Pywikibot/Development/Guidelines]] for more info.
Todo: The following rules can be added in the future:
- For any changes or new lines use single quotes for strings, or double
quotes if the string contains a single quote. But keep older code
- unchanged.
+ unchanged. (partially implemented)
- Prefer string.format() instead of modulo operator % for string
formatting. The modulo operator might be deprecated by a future
python release.
@@ -25,8 +26,13 @@
from __future__ import absolute_import, unicode_literals
-from re import compile as re_compile, IGNORECASE, VERBOSE
+from re import compile as re_compile, IGNORECASE
from subprocess import check_output
+from sys import version_info
+if version_info.major == 3:
+ from tokenize import tokenize, STRING
+else:
+ from tokenize import generate_tokens as tokenize, STRING
from unidiff import PatchSet
@@ -35,28 +41,9 @@
# It does not make sense to break long URLs.
# https://github.com/PyCQA/pylint/blob/d42e74bb9428f895f36808d30bd8a1fe31e28f…
IGNORABLE_LONG_LINE = re_compile(r'\s*(# )?<?https?://\S+>?$').match
-# The U_PREFIX regex cannot be 100% accurate without tokenizing the whole
-# module, but thee accuracy of this simple regex should be enough for our
-# purposes.
-U_PREFIX = re_compile(
- r'''
- (.*?)\b
- (?=
- ur?
- (?=
- ['"]{3}
- |
- # to reduce the false positive possibilities like 's t u',
- # check that the non-triple-quoted strings are closed on the
- # same line.
- '[^']*'
- |
- "[^"]*"
- )
- )
- (?<!['"])
- ''',
- IGNORECASE | VERBOSE
+
+STRING_MATCH = re_compile(
+ r'(?P<unicode_literal>u)?[bfr]*(?P<quote>\'+|"+)', IGNORECASE,
).match
@@ -65,12 +52,44 @@
# regex from https://github.com/PyCQA/pylint/blob/master/pylintrc
output = check_output(
['git', 'diff', '-U0', '@~..@'], universal_newlines=True)
+ if version_info.major == 2:
+ return PatchSet.from_string(output, encoding='utf-8')
return PatchSet.from_string(output)
def print_error(path, line_no, col_no, error):
"""Print the error."""
print('{0}:{1}:{2}: {3}'.format(path, line_no, col_no, error))
+
+
+def check_tokens(file_path, line_nos):
+ """Check the style of lines in the given file_path."""
+ error = False
+ max_line = max(line_nos)
+ with open(file_path, 'rb') as f:
+ token_generator = tokenize(f.readline)
+ for type_, string, start, end, line_val in token_generator:
+ if max_line < start[0]:
+ break
+ if start[0] not in line_nos or type_ != STRING:
+ continue
+ match = STRING_MATCH(string)
+ if match.group('unicode_literal'):
+ error = True
+ print_error(
+ file_path, start[0], start[1] + 1,
+ 'newly-added/modified line with u"" prefixed string '
+ 'literal',
+ )
+ if match.group('quote') == '"' and "'" not in string:
+ error = True
+ print_error(
+ file_path, start[0], start[1] + 1,
+ 'newly-added/modified line with "double quoted string" not'
+ ' containing any single quotes; use a \'single quoted '
+ 'string\' instead',
+ )
+ return not error
def check(latest_patchset):
@@ -81,30 +100,26 @@
"""
error = False
for patched_file in latest_patchset:
- target_file = patched_file.target_file
- if not (target_file.endswith('.py') or patched_file.is_removed_file):
+ path = patched_file.path
+ if not (path.endswith('.py') or patched_file.is_removed_file):
continue
+ added_lines = set()
for hunk in patched_file:
for line in hunk:
if not line.is_added:
continue
+ line_no = line.target_line_no
+ added_lines.add(line_no)
line_val = line.value
# Check line length
if len(line_val) > 80 and not IGNORABLE_LONG_LINE(line_val):
print_error(
- target_file, line.target_line_no, len(line_val),
+ path, line_no, len(line_val),
'newly-added/modified line longer than 79 characters',
)
error = True
- # Check that u-prefix is not used
- u_match = U_PREFIX(line_val)
- if u_match:
- print_error(
- target_file, line.target_line_no, u_match.end() + 1,
- 'newly-added/modified line with u\"" prefixed string '
- 'literal',
- )
- error = True
+ if added_lines:
+ error = check_tokens(path, added_lines) and error
return not error
--
To view, visit https://gerrit.wikimedia.org/r/398492
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I84afcc0817a48514d1a99e8cae2091847931aff3
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/398420 )
Change subject: dff_checker.py: Add u'' prefix detection for added lines
......................................................................
dff_checker.py: Add u'' prefix detection for added lines
Change-Id: I4f0aec3b117b7abd1f1a4610ce074de7dadb6508
---
M scripts/maintenance/diff_checker.py
1 file changed, 72 insertions(+), 23 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/maintenance/diff_checker.py b/scripts/maintenance/diff_checker.py
index 678b808..bd428d8 100644
--- a/scripts/maintenance/diff_checker.py
+++ b/scripts/maintenance/diff_checker.py
@@ -2,17 +2,20 @@
# -*- coding: utf-8 -*-
"""Check that the latest commit diff follows the guidelines.
-Currently the script only checks line lengths. Lines longer than 79 characters
-should be avoided.
+Currently the script checks the following code style issues:
+ - Line lengths: Newly added lines longer than 79 chars are not allowed.
+ - Unicode literal notations: They are not allowed since we instead import
+ unicode_literals from __future__.
+
See [[Manual:Pywikibot/Development/Guidelines]] for more info.
Todo: The following rules can be added in the future:
- For any changes or new lines use single quotes for strings, or double
quotes if the string contains a single quote. But keep older code
unchanged.
- - Do not use a u'' prefix on strings, as it is meaningless due to
- __future__.unicode_literals. But keep older code unchanged.
-
+ - Prefer string.format() instead of modulo operator % for string
+ formatting. The modulo operator might be deprecated by a future
+ python release.
"""
#
# (C) Pywikibot team, 2017
@@ -22,13 +25,39 @@
from __future__ import absolute_import, unicode_literals
-from re import compile as re_compile
+from re import compile as re_compile, IGNORECASE, VERBOSE
from subprocess import check_output
from unidiff import PatchSet
-IGNORABLE_LONG_LINE = re_compile(r'^\s*(# )?<?https?://\S+>?$').search
+# Regexp for a line that is allowed to be longer than the limit.
+# It does not make sense to break long URLs.
+# https://github.com/PyCQA/pylint/blob/d42e74bb9428f895f36808d30bd8a1fe31e28f…
+IGNORABLE_LONG_LINE = re_compile(r'\s*(# )?<?https?://\S+>?$').match
+# The U_PREFIX regex cannot be 100% accurate without tokenizing the whole
+# module, but thee accuracy of this simple regex should be enough for our
+# purposes.
+U_PREFIX = re_compile(
+ r'''
+ (.*?)\b
+ (?=
+ ur?
+ (?=
+ ['"]{3}
+ |
+ # to reduce the false positive possibilities like 's t u',
+ # check that the non-triple-quoted strings are closed on the
+ # same line.
+ '[^']*'
+ |
+ "[^"]*"
+ )
+ )
+ (?<!['"])
+ ''',
+ IGNORECASE | VERBOSE
+).match
def get_latest_patchset():
@@ -39,37 +68,57 @@
return PatchSet.from_string(output)
-def check_line_lengths(latest_patchset):
- """Check that none of the added/modified lines are longer than 79 chars.
+def print_error(path, line_no, col_no, error):
+ """Print the error."""
+ print('{0}:{1}:{2}: {3}'.format(path, line_no, col_no, error))
- @return: True if line lengths are OK. False otherwise.
+
+def check(latest_patchset):
+ """Check that the added/modified lines do not violate the guideline.
+
+ @return: True if line added/modified OK. False otherwise.
@rtype: bool
"""
- found_long = False
+ error = False
for patched_file in latest_patchset:
target_file = patched_file.target_file
if not (target_file.endswith('.py') or patched_file.is_removed_file):
continue
for hunk in patched_file:
for line in hunk:
- if (
- line.is_added and len(line.value) > 80
- and not IGNORABLE_LONG_LINE(line.value)
- ):
- print(
- target_file + ':' + str(line.target_line_no) + ':'
- + str(len(line.value))
- + ': line is too long (more than 79 characters)'
+ if not line.is_added:
+ continue
+ line_val = line.value
+ # Check line length
+ if len(line_val) > 80 and not IGNORABLE_LONG_LINE(line_val):
+ print_error(
+ target_file, line.target_line_no, len(line_val),
+ 'newly-added/modified line longer than 79 characters',
)
- found_long = True
- return not found_long
+ error = True
+ # Check that u-prefix is not used
+ u_match = U_PREFIX(line_val)
+ if u_match:
+ print_error(
+ target_file, line.target_line_no, u_match.end() + 1,
+ 'newly-added/modified line with u\"" prefixed string '
+ 'literal',
+ )
+ error = True
+ return not error
def main():
"""Run this script."""
latest_patchset = get_latest_patchset()
- if not check_line_lengths(latest_patchset):
- raise SystemExit('diff-checker failed')
+ if not check(latest_patchset):
+ raise SystemExit(
+ 'diff-checker failed.\n'
+ 'Please review '
+ '<https://www.mediawiki.org/wiki/'
+ 'Manual:Pywikibot/Development/Guidelines#Miscellaneous> '
+ 'and update your patch-set accordingly.'
+ )
if __name__ == '__main__':
--
To view, visit https://gerrit.wikimedia.org/r/398420
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I4f0aec3b117b7abd1f1a4610ce074de7dadb6508
Gerrit-PatchSet: 8
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/398296 )
Change subject: imagecopy.py: Improve the module's docstring
......................................................................
imagecopy.py: Improve the module's docstring
Add copyright and license lines to diff_checker.py.
Change-Id: Ibcc8a442ab3ac415db853c2cb7352b8727636962
---
M scripts/imagecopy.py
M scripts/maintenance/diff_checker.py
2 files changed, 9 insertions(+), 5 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/imagecopy.py b/scripts/imagecopy.py
index 9116155..656525e 100644
--- a/scripts/imagecopy.py
+++ b/scripts/imagecopy.py
@@ -19,11 +19,11 @@
Examples
-Work on a single image: ::
+Work on a single image::
python pwb.py imagecopy.py -page:Image:<imagename>
-Work on the 100 newest images: ::
+Work on the 100 newest images::
python pwb.py imagecopy.py -newimages:100
@@ -31,13 +31,13 @@
python pwb.py imagecopy.py -cat:<cat>
-Work on all images which transclude a template: ::
+Work on all images which transclude a template::
python pwb.py imagecopy.py -transcludes:<template>
Work on a single image and deletes the image when the transfer is complete
(only works if the user has sysops privilege, otherwise it will be marked for
-deletion): ::
+deletion)::
python pwb.py imagecopy.py -page:Image:<imagename> -delete
diff --git a/scripts/maintenance/diff_checker.py b/scripts/maintenance/diff_checker.py
index 358d0d3..678b808 100644
--- a/scripts/maintenance/diff_checker.py
+++ b/scripts/maintenance/diff_checker.py
@@ -14,7 +14,11 @@
__future__.unicode_literals. But keep older code unchanged.
"""
-
+#
+# (C) Pywikibot team, 2017
+#
+# Distributed under the terms of the MIT license.
+#
from __future__ import absolute_import, unicode_literals
--
To view, visit https://gerrit.wikimedia.org/r/398296
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ibcc8a442ab3ac415db853c2cb7352b8727636962
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/398288 )
Change subject: imagecopy.py: Fix docstring
......................................................................
imagecopy.py: Fix docstring
Use literal code blocks instead of definition lists. The terms in a definition
list should be short and therefore it is not suitable here.
Bug: T182895
Change-Id: Ie96ec8d5def6d5423315ef75511d76bd2154f5c2
---
M scripts/imagecopy.py
1 file changed, 14 insertions(+), 5 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/imagecopy.py b/scripts/imagecopy.py
index f16a43a..9116155 100644
--- a/scripts/imagecopy.py
+++ b/scripts/imagecopy.py
@@ -19,17 +19,26 @@
Examples
-Work on a single image
+Work on a single image: ::
+
python pwb.py imagecopy.py -page:Image:<imagename>
-Work on the 100 newest images:
+
+Work on the 100 newest images: ::
+
python pwb.py imagecopy.py -newimages:100
-Work on all images in a category:<cat>
+
+Work on all images in a category:<cat>::
+
python pwb.py imagecopy.py -cat:<cat>
-Work on all images which transclude a template
+
+Work on all images which transclude a template: ::
+
python pwb.py imagecopy.py -transcludes:<template>
+
Work on a single image and deletes the image when the transfer is complete
(only works if the user has sysops privilege, otherwise it will be marked for
-deletion)
+deletion): ::
+
python pwb.py imagecopy.py -page:Image:<imagename> -delete
See pagegenerators.py for more ways to get a list of images.
--
To view, visit https://gerrit.wikimedia.org/r/398288
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie96ec8d5def6d5423315ef75511d76bd2154f5c2
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/398262 )
Change subject: tox.ini: Configure Jenkins to fail when lines are longer than 79 characters
......................................................................
tox.ini: Configure Jenkins to fail when lines are longer than 79 characters
Put the checker script under the maintenance directory.
Currently, it only checks the length of changed or added lines of Python files.
Add W503 to ignore list of flake8 as it is against the current PEP
recommendation.[1]
[1]: https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-aft…
Change-Id: I7c430431ebdc942e9f8c1c8b576feee22d7ff7d3
---
A scripts/maintenance/diff_checker.py
M tox.ini
2 files changed, 80 insertions(+), 3 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/maintenance/diff_checker.py b/scripts/maintenance/diff_checker.py
new file mode 100644
index 0000000..358d0d3
--- /dev/null
+++ b/scripts/maintenance/diff_checker.py
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""Check that the latest commit diff follows the guidelines.
+
+Currently the script only checks line lengths. Lines longer than 79 characters
+should be avoided.
+See [[Manual:Pywikibot/Development/Guidelines]] for more info.
+
+Todo: The following rules can be added in the future:
+ - For any changes or new lines use single quotes for strings, or double
+ quotes if the string contains a single quote. But keep older code
+ unchanged.
+ - Do not use a u'' prefix on strings, as it is meaningless due to
+ __future__.unicode_literals. But keep older code unchanged.
+
+"""
+
+
+from __future__ import absolute_import, unicode_literals
+
+from re import compile as re_compile
+from subprocess import check_output
+
+from unidiff import PatchSet
+
+
+IGNORABLE_LONG_LINE = re_compile(r'^\s*(# )?<?https?://\S+>?$').search
+
+
+def get_latest_patchset():
+ """Return the PatchSet for the latest commit."""
+ # regex from https://github.com/PyCQA/pylint/blob/master/pylintrc
+ output = check_output(
+ ['git', 'diff', '-U0', '@~..@'], universal_newlines=True)
+ return PatchSet.from_string(output)
+
+
+def check_line_lengths(latest_patchset):
+ """Check that none of the added/modified lines are longer than 79 chars.
+
+ @return: True if line lengths are OK. False otherwise.
+ @rtype: bool
+ """
+ found_long = False
+ for patched_file in latest_patchset:
+ target_file = patched_file.target_file
+ if not (target_file.endswith('.py') or patched_file.is_removed_file):
+ continue
+ for hunk in patched_file:
+ for line in hunk:
+ if (
+ line.is_added and len(line.value) > 80
+ and not IGNORABLE_LONG_LINE(line.value)
+ ):
+ print(
+ target_file + ':' + str(line.target_line_no) + ':'
+ + str(len(line.value))
+ + ': line is too long (more than 79 characters)'
+ )
+ found_long = True
+ return not found_long
+
+
+def main():
+ """Run this script."""
+ latest_patchset = get_latest_patchset()
+ if not check_line_lengths(latest_patchset):
+ raise SystemExit('diff-checker failed')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/tox.ini b/tox.ini
index a59f7be..906911d 100644
--- a/tox.ini
+++ b/tox.ini
@@ -3,13 +3,13 @@
minversion = 1.7.2
skipsdist = True
skip_missing_interpreters = True
-envlist = commit-message,flake8,pyflakes-{py26,py3,pypy},doctest-{py27,py34},py26,py27,py34
+envlist = diff-checker,commit-message,flake8,pyflakes-{py26,py3,pypy},doctest-{py27,py34},py26,py27,py34
[tox:jenkins]
# Override default for WM Jenkins
# Others are run in their own individual jobs on WM Jenkins
# Wikimedia Jenkins does not have Python 2.6
-envlist = commit-message,flake8,pyflakes-{py3,pypy}
+envlist = diff-checker,commit-message,flake8,pyflakes-{py3,pypy}
[params]
doctest_skip = --ignore-files=(gui\.py|botirc\.py|eventstreams\.py)
@@ -20,6 +20,10 @@
PYWIKIBOT2_NO_USER_CONFIG=2
usedevelop = True
commands = python setup.py test
+
+[testenv:diff-checker]
+deps = unidiff
+commands = python pwb.py diff_checker
[testenv:commit-message]
deps = commit-message-validator
@@ -146,6 +150,7 @@
# H404: docstring multiline start
# H405: docstring summary line
# H301,I100,I101,202: import order rules; Pywikibot uses rules H306 and I201
+# W503: line break before binary operator; against current PEP 8 recommendation
# The following are to be fixed
# C401, C402, C405: does not work with py 2.6
@@ -160,7 +165,7 @@
# D413: Missing blank line after last section
# D412: No blank lines allowed between a section header and its content
-ignore = C401,C402,C405,E402,D105,D211,FI10,FI12,FI13,FI15,FI16,FI17,FI5,H101,H236,H301,H404,H405,H903,I100,I101,I202,N802,N803,N806,D401,D413,D103,D412
+ignore = C401,C402,C405,E402,D105,D211,FI10,FI12,FI13,FI15,FI16,FI17,FI5,H101,H236,H301,H404,H405,H903,I100,I101,I202,N802,N803,N806,D401,D413,D103,D412,W503
exclude = .tox,.git,./*.egg,ez_setup.py,build,externals,user-config.py,./scripts/i18n/*
min-version = 2.6
max_line_length = 100
--
To view, visit https://gerrit.wikimedia.org/r/398262
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I7c430431ebdc942e9f8c1c8b576feee22d7ff7d3
Gerrit-PatchSet: 6
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/396910 )
Change subject: scripts/imagecopy.py: Add -delete option
......................................................................
scripts/imagecopy.py: Add -delete option
Add `-delete` option to `scripts/imagecopy.py`.
This option will delete the page in the wiki after the
moving process finished if the user has `sysops`
privilege in the wiki, otherwise it will be marked
for deletion.
Bug: T57522
Change-Id: I495647883eb223bebf79eb50463785841f179fb3
---
M scripts/imagecopy.py
1 file changed, 23 insertions(+), 8 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
Zoranzoki21: Looks good to me, but someone else must approve
diff --git a/scripts/imagecopy.py b/scripts/imagecopy.py
index aa06cbb..f16a43a 100644
--- a/scripts/imagecopy.py
+++ b/scripts/imagecopy.py
@@ -27,6 +27,10 @@
python pwb.py imagecopy.py -cat:<cat>
Work on all images which transclude a template
python pwb.py imagecopy.py -transcludes:<template>
+Work on a single image and deletes the image when the transfer is complete
+(only works if the user has sysops privilege, otherwise it will be marked for
+deletion)
+ python pwb.py imagecopy.py -page:Image:<imagename> -delete
See pagegenerators.py for more ways to get a list of images.
By default the bot works on your home wiki (set in user-config)
@@ -210,11 +214,12 @@
"""Facilitate transfer of image/file to commons."""
- def __init__(self, imagePage, newname, category):
+ def __init__(self, imagePage, newname, category, delete_after_done=False):
"""Constructor."""
self.imagePage = imagePage
self.newname = newname
self.category = category
+ self.delete_after_done = delete_after_done
threading.Thread.__init__(self)
def run(self):
@@ -294,21 +299,27 @@
self.gen = pagegenerators.FileLinksGenerator(self.imagePage)
self.preloadingGen = pagegenerators.PreloadingGenerator(self.gen)
+ moveSummary = i18n.twtranslate(
+ self.imagePage.site,
+ 'commons-file-moved',
+ {'localfile': self.imagePage.title(withNamespace=False),
+ 'commonsfile': self.newname})
+
# If the image is uploaded under a different name, replace all
# instances
if self.imagePage.title(withNamespace=False) != self.newname:
- moveSummary = i18n.twtranslate(
- self.imagePage.site,
- 'commons-file-moved',
- {'localfile': self.imagePage.title(withNamespace=False),
- 'commonsfile': self.newname})
-
imagebot = image.ImageRobot(
generator=self.preloadingGen,
oldImage=self.imagePage.title(withNamespace=False),
newImage=self.newname,
summary=moveSummary, always=True, loose=True)
imagebot.run()
+
+ # If the user want to delete the page and
+ # the user has sysops privilege, delete the page, otherwise
+ # it will be marked for deletion.
+ if self.delete_after_done:
+ self.imagePage.delete(moveSummary, False)
return
def fixAuthor(self, pageText):
@@ -460,6 +471,7 @@
imagepage = None
always = False
category = u''
+ delete_after_done = False
# Load a lot of default generators
local_args = pywikibot.handle_args(args)
genFactory = pagegenerators.GeneratorFactory()
@@ -469,6 +481,8 @@
always = True
elif arg.startswith('-cc:'):
category = arg[len('-cc:'):]
+ elif arg == '-delete':
+ delete_after_done = True
else:
genFactory.handleArg(arg)
@@ -535,7 +549,8 @@
# the start of the loop
if not skip:
- imageTransfer(imagepage, newname, category).start()
+ imageTransfer(imagepage, newname, category,
+ delete_after_done).start()
pywikibot.output('Still ' + str(threading.activeCount()) +
' active threads, lets wait')
--
To view, visit https://gerrit.wikimedia.org/r/396910
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I495647883eb223bebf79eb50463785841f179fb3
Gerrit-PatchSet: 6
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Rafidaslam <rafidteam(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: MtDu <justin.d128(a)gmail.com>
Gerrit-Reviewer: Rafidaslam <rafidteam(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>