jenkins-bot has submitted this change and it was merged.
Change subject: Port replace.py -replacementfile from compat ......................................................................
Port replace.py -replacementfile from compat
Added missing -replacementfile parameter. Checked if the argument set being accepted are always taken in pattern replacement pair.
Bug: T70503 Change-Id: I3c68edf090c605b78b5864bc19b68e8448604a4e --- M scripts/replace.py 1 file changed, 46 insertions(+), 2 deletions(-)
Approvals: John Vandenberg: Looks good to me, but someone else must approve Ladsgroup: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/replace.py b/scripts/replace.py old mode 100755 new mode 100644 index 44f8ecb..19a7855 --- a/scripts/replace.py +++ b/scripts/replace.py @@ -72,6 +72,11 @@ (or no replacements are defined via -fix or the arguments) it'll ask for additional replacements at start.
+-replacementfile Lines from the given file name(s) will be read as replacement + arguments. i.e. a file containing lines "a" and "b", used as + python replace.py -page:X -replacementfile:file c d + will replace 'a' with 'b' and 'c' with 'd'. + -always Don't prompt you for each replacement
-recursive Recurse replacement as long as possible. Be careful, this @@ -127,6 +132,7 @@ __version__ = '$Id$' #
+import codecs import collections import re import time @@ -714,6 +720,9 @@ sleep = None # Request manual replacements even if replacements are already defined manual_input = False + # Replacements loaded from a file + replacement_file = None + replacement_file_arg_misplaced = False
# Read commandline parameters.
@@ -770,13 +779,48 @@ allowoverlap = True elif arg.startswith('-manualinput'): manual_input = True + elif arg.startswith('-replacementfile'): + if len(commandline_replacements) % 2: + replacement_file_arg_misplaced = True + + if arg == '-replacementfile': + replacement_file = pywikibot.input( + u'Please enter the filename to read replacements from:') + else: + replacement_file = arg[len('-replacementfile:'):] else: commandline_replacements.append(arg)
site = pywikibot.Site()
- if (len(commandline_replacements) % 2): - raise pywikibot.Error('require even number of replacements.') + if len(commandline_replacements) % 2: + pywikibot.error('Incomplete command line pattern replacement pair.') + return False + + if replacement_file_arg_misplaced: + pywikibot.error( + '-replacementfile used between a pattern replacement pair.') + return False + + if replacement_file: + try: + with codecs.open(replacement_file, 'r', 'utf-8') as f: + file_replacements = f.readlines() + except (IOError, OSError) as e: + pywikibot.error(u'Error loading {0}: {1}'.format( + replacement_file, e)) + return False + + if len(file_replacements) % 2: + pywikibot.error( + '{0} contains an incomplete pattern replacement pair.'.format( + replacement_file)) + return False + + # Strip BOM from first line + file_replacements[0].lstrip(u'\uFEFF') + commandline_replacements.extend(file_replacements) + if not(commandline_replacements or fixes_set) or manual_input: old = pywikibot.input(u'Please enter the text that should be replaced:') while old:
pywikibot-commits@lists.wikimedia.org