jenkins-bot has submitted this change and it was merged.
Change subject: [FEAT] upload: Support -always option ......................................................................
[FEAT] upload: Support -always option
This adds an `-always` option to automatically upload files.
Bug: T106412 Change-Id: I478ff3152d8c086e2104cd2db21d01b12aead6ed --- M scripts/upload.py 1 file changed, 78 insertions(+), 18 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/upload.py b/scripts/upload.py index 5c50ab0..f70e228 100755 --- a/scripts/upload.py +++ b/scripts/upload.py @@ -22,6 +22,11 @@ 'Ki': Kibibytes (1024 B) 'Mi': Mebibytes (1024x1024 B) The suffixes are case insensitive. + -always Don't ask the user anything. This will imply -keep and + -noverify and require that either -abortonwarn or -ignorewarn + is defined for all. It will also require a valid file name and + description. It'll only overwrite files if -ignorewarn includes + the 'exists' warning.
It is possible to combine -abortonwarn and -ignorewarn so that if the specific warning is given it won't apply the general one but more specific one. So if it @@ -59,6 +64,7 @@ import pywikibot import pywikibot.data.api from pywikibot import config +from pywikibot.bot import suggest_help, BaseBot from pywikibot.tools import ( deprecated ) @@ -72,14 +78,15 @@ from urllib import URLopener
-class UploadRobot: +class UploadRobot(BaseBot):
"""Upload bot."""
def __init__(self, url, urlEncoding=None, description=u'', useFilename=None, keepFilename=False, verifyDescription=True, ignoreWarning=False, - targetSite=None, uploadByUrl=False, aborts=[], chunk_size=0): + targetSite=None, uploadByUrl=False, aborts=[], chunk_size=0, + **kwargs): """ Constructor.
@@ -112,11 +119,23 @@ restartable) specified in bytes. If no value is specified the file will be uploaded as whole. @type chunk_size: integer + @param always: Disables any input, requires that either ignoreWarning or + aborts are set to True and that the description is also set. It will + overwrite verifyDescription to False and keepFilename to True. + @type always: bool
@deprecated: Using upload_image() is deprecated, use upload_file() with file_url param instead
""" + super(UploadRobot, self).__init__(**kwargs) + always = self.getOption('always') + if (always and ignoreWarning is not True and aborts is not True): + raise ValueError('When always is set to True, either ignoreWarning ' + 'or aborts must be set to True.') + if always and not description: + raise ValueError('When always is set to True the description must ' + 'be set.') self.url = url if isinstance(self.url, basestring): pywikibot.warning("url as string is deprecated. " @@ -124,8 +143,8 @@ self.urlEncoding = urlEncoding self.description = description self.useFilename = useFilename - self.keepFilename = keepFilename - self.verifyDescription = verifyDescription + self.keepFilename = keepFilename or always + self.verifyDescription = verifyDescription and not always self.ignoreWarning = ignoreWarning self.aborts = aborts self.chunk_size = chunk_size @@ -249,6 +268,7 @@ pywikibot.warning("file_url is not given. " "Set to self.url by default.")
+ always = self.getOption('always') # Isolate the pure name filename = file_url # Filename may be either a URL or a local file path @@ -262,6 +282,7 @@ pywikibot.output( u"The filename on the target wiki will default to: %s" % filename) + assert not always newfn = pywikibot.input( u'Enter a better name, or press enter to accept:') if newfn != "": @@ -277,8 +298,11 @@ first_check = True while True: if not first_check: - filename = pywikibot.input('Enter a better name, or press ' - 'enter to skip the file:') + if always: + filename = None + else: + filename = pywikibot.input('Enter a better name, or press ' + 'enter to skip the file:') if not filename: return None first_check = False @@ -291,22 +315,30 @@ 'Invalid character(s): %s. Please try again' % c) continue if ext not in allowed_formats: - if not pywikibot.input_yn( + if always: + pywikibot.output('File format is not one of ' + '[{0}]'.format(' '.join(allowed_formats))) + continue + elif not pywikibot.input_yn( u"File format is not one of [%s], but %s. Continue?" % (u' '.join(allowed_formats), ext), default=False, automatic_quit=False): continue potential_file_page = pywikibot.FilePage(self.targetSite, filename) if potential_file_page.exists(): - if self.aborts is True: + overwrite = self._handle_warning('exists') + if overwrite is False: pywikibot.output("File exists and you asked to abort. Skipping.") return None if potential_file_page.canBeEdited(): - if pywikibot.input_yn(u"File with name %s already exists. " - "Would you like to change the name? " - "(Otherwise file will be overwritten.)" - % filename, default=True, - automatic_quit=False): + if overwrite is None: + overwrite = not pywikibot.input_yn( + "File with name %s already exists. " + "Would you like to change the name? " + "(Otherwise file will be overwritten.)" + % filename, default=True, + automatic_quit=False) + if not overwrite: continue else: break @@ -341,6 +373,7 @@ u'\03{lightred}It is not possible to upload a file ' 'without a summary/description.\03{default}')
+ assert not always # if no description, default is 'yes' if pywikibot.input_yn( u'Do you want to change this description?', @@ -468,6 +501,7 @@ url = u'' description = [] keepFilename = False + always = False useFilename = None verifyDescription = True aborts = set() @@ -480,7 +514,11 @@ # returns a list of non-global args, i.e. args for upload.py for arg in pywikibot.handle_args(args): if arg: - if arg.startswith('-keep'): + if arg == '-always': + keepFilename = True + always = True + verifyDescription = False + elif arg.startswith('-keep'): keepFilename = True elif arg.startswith('-filename:'): useFilename = arg[10:] @@ -524,12 +562,35 @@ url = arg else: description.append(arg) + description = u' '.join(description) while not ("://" in url or os.path.exists(url)): if not url: - pywikibot.output(u'No input filename given.') + error = 'No input filename given.' else: - pywikibot.output(u'Invalid input filename given. Try again.') + error = 'Invalid input filename given.' + if not always: + error += ' Try again.' + if always: + url = None + break + else: + pywikibot.output(error) url = pywikibot.input(u'URL, file or directory where files are now:') + if always and ((aborts is not True and ignorewarn is not True) or + not description or url is None): + additional = '' + missing = [] + if url is None: + missing += ['filename'] + additional = error + ' ' + if description is None: + missing += ['description'] + if aborts is not True and ignorewarn is not True: + additional += ('Either -ignorewarn or -abortonwarn must be ' + 'defined for all codes. ') + additional += 'Unable to run in -always mode' + suggest_help(missing_parameters=missing, additional_text=additional) + return False if os.path.isdir(url): file_list = [] for directory_info in os.walk(url): @@ -538,12 +599,11 @@ url = file_list else: url = [url] - description = u' '.join(description) bot = UploadRobot(url, description=description, useFilename=useFilename, keepFilename=keepFilename, verifyDescription=verifyDescription, aborts=aborts, ignoreWarning=ignorewarn, - chunk_size=chunk_size) + chunk_size=chunk_size, always=always) bot.run()
if __name__ == "__main__":
pywikibot-commits@lists.wikimedia.org