jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/509907 )
Change subject: [FEAT] Suggest help for missing dependencies in scripts ......................................................................
[FEAT] Suggest help for missing dependencies in scripts
- standardize missing dependencies exceptions across scripts - suggest help if dependency is missing - unify behavior for tests (e.g. script_tests)
Change-Id: I8e96dd816d73184e072f5a5b7e400d39ed9851fd --- M pywikibot/bot.py M scripts/data_ingestion.py M scripts/flickrripper.py M scripts/imageharvest.py M scripts/match_images.py M scripts/patrol.py M scripts/states_redirect.py M scripts/weblinkchecker.py 8 files changed, 54 insertions(+), 25 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 954d07f..e161e27 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -1050,7 +1050,8 @@
def suggest_help(missing_parameters=[], missing_generator=False, unknown_parameters=[], exception=None, - missing_action=False, additional_text=''): + missing_action=False, additional_text='', + missing_dependencies=[]): """ Output error message to use -help with additional text before it.
@@ -1066,6 +1067,9 @@ @type missing_action: bool @param additional_text: Additional text added to the end. @type additional_text: str + @param missing_dependencies: A list of dependencies which can not + be imported. + @type missing_dependencies: list of str """ messages = [] if exception: @@ -1083,6 +1087,11 @@ messages.append('Unknown parameter{s} "{params}".' .format(s='s' if len(unknown_parameters) > 1 else '', params='", "'.join(unknown_parameters))) + if missing_dependencies: + messages.append('Missing dependenc{s} "{deps}".' + .format( + s='ies' if len(missing_dependencies) > 1 else 'y', + deps='", "'.join(missing_dependencies))) if additional_text: messages.append(additional_text.strip()) if messages: diff --git a/scripts/data_ingestion.py b/scripts/data_ingestion.py index 149d8f1..ce846a0 100755 --- a/scripts/data_ingestion.py +++ b/scripts/data_ingestion.py @@ -32,7 +32,10 @@ import csv from urllib.parse import urlparse else: - import unicodecsv as csv + try: + import unicodecsv as csv + except ImportError as e: + csv = e from urlparse import urlparse
@@ -261,10 +264,16 @@
config_generator = genFactory.getCombinedGenerator()
- if not config_generator or not csv_dir: + if isinstance(csv, ImportError): + missing_dependencies = ('unicodecsv',) + else: + missing_dependencies = None + + if not config_generator or not csv_dir or missing_dependencies: pywikibot.bot.suggest_help( missing_parameters=[] if csv_dir else ['-csvdir'], - missing_generator=not config_generator) + missing_generator=not config_generator, + missing_dependencies=missing_dependencies) return False
for config_page in config_generator: diff --git a/scripts/flickrripper.py b/scripts/flickrripper.py index c9cb22f..3b1b62c 100755 --- a/scripts/flickrripper.py +++ b/scripts/flickrripper.py @@ -501,14 +501,15 @@ elif arg == '-autonomous': autonomous = True
- if isinstance(flickrapi, Exception): - pywikibot.error('This script requires the python flickrapi module. \n' - 'See: http://stuvel.eu/projects/flickrapi') + if isinstance(flickrapi, ImportError): + pywikibot.bot.suggest_help(missing_dependencies=('flickrapi',))
elif not config.flickr['api_key']: - pywikibot.warning('Flickr api key not found! Get yourself an api key\n' - 'Any flickr user can get a key at\n' - 'https://www.flickr.com/services/api/keys/apply/') + additional_message = ( + 'Flickr api key not found! Get yourself an api key\n' + 'Any flickr user can get a key at\n' + 'https://www.flickr.com/services/api/keys/apply/') + pywikibot.bot.suggest_help(additional_message=additional_message)
elif user_id or group_id or photoset_id: if 'api_secret' in config.flickr and config.flickr['api_secret']: diff --git a/scripts/imageharvest.py b/scripts/imageharvest.py index cf41dea..c4f9607 100644 --- a/scripts/imageharvest.py +++ b/scripts/imageharvest.py @@ -154,7 +154,10 @@ desc = ' '.join(desc)
mysite = pywikibot.Site() - run_bot(url, image_url, desc) + try: + run_bot(url, image_url, desc) + except ImportError: + pywikibot.bot.suggest_help(missing_dependencies=('beautifulsoup4',))
if __name__ == '__main__': diff --git a/scripts/match_images.py b/scripts/match_images.py index 69607ed..442e944 100755 --- a/scripts/match_images.py +++ b/scripts/match_images.py @@ -172,10 +172,12 @@ additional_text = ('Unable to execute script because it ' 'requires two images to work on.' if len(images) != 2 else None) - exception = Image if isinstance(Image, Exception) else None + missing_dependencies = ('Pillow',) if isinstance( + Image, ImportError) else None
- if additional_text or exception: - suggest_help(exception=exception, additional_text=additional_text) + if additional_text or missing_dependencies: + suggest_help(missing_dependencies=missing_dependencies, + additional_text=additional_text) return False
imagePageA = pywikibot.page.FilePage(pywikibot.Site(), diff --git a/scripts/patrol.py b/scripts/patrol.py index a07ed54..318c46a 100755 --- a/scripts/patrol.py +++ b/scripts/patrol.py @@ -61,7 +61,7 @@
from pywikibot import pagegenerators
-from pywikibot.bot import SingleSiteBot, QuitKeyboardInterrupt +from pywikibot.bot import SingleSiteBot, QuitKeyboardInterrupt, suggest_help
_logger = 'patrol'
@@ -127,7 +127,7 @@
def load_whitelist(self): """Load most recent watchlist_page for further processing.""" - if isinstance(mwparserfromhell, Exception): + if isinstance(mwparserfromhell, ImportError): raise mwparserfromhell # Check for a more recent version after versionchecktime in sec. if (self.whitelist_load_ts and (time.time() - self.whitelist_load_ts @@ -479,6 +479,10 @@
bot = PatrolBot(**options)
+ if isinstance(mwparserfromhell, ImportError): + suggest_help(missing_dependencies=('mwparserfromhell',)) + return + if newpages or usercontribs: pywikibot.output('Newpages:') gen = site.newpages diff --git a/scripts/states_redirect.py b/scripts/states_redirect.py index 7b1e8a1..8f4ac5b 100755 --- a/scripts/states_redirect.py +++ b/scripts/states_redirect.py @@ -134,16 +134,14 @@ else: unknown_parameters.append(arg)
- if isinstance(pycountry, Exception): - additional_text = ('This script requires the python-pycountry module\n' - 'See: https://pypi.org/project/pycountry') - exception = pycountry + if isinstance(pycountry, ImportError): + missing_dependencies = ('pycountry',) else: - additional_text = exception = None + missing_dependencies = None
- if additional_text or exception or unknown_parameters: + if missing_dependencies or unknown_parameters: suggest_help(unknown_parameters=unknown_parameters, - exception=exception, additional_text=additional_text) + missing_dependencies=missing_dependencies) return
bot = StatesRedirectBot(start, force) diff --git a/scripts/weblinkchecker.py b/scripts/weblinkchecker.py index f8cb5ca..a00b34a 100755 --- a/scripts/weblinkchecker.py +++ b/scripts/weblinkchecker.py @@ -135,7 +135,7 @@ comms, i18n, config, pagegenerators, textlib, weblib, config2, )
-from pywikibot.bot import ExistingPageBot, SingleSiteBot +from pywikibot.bot import ExistingPageBot, SingleSiteBot, suggest_help from pywikibot.pagegenerators import ( XMLDumpPageGenerator as _XMLDumpPageGenerator, ) @@ -1015,6 +1015,9 @@ bot = WeblinkCheckerRobot(gen, HTTPignore, config.weblink_dead_days) try: bot.run() + except ImportError: + suggest_help(missing_dependencies=('memento_client',)) + return False finally: waitTime = 0 # Don't wait longer than 30 seconds for threads to finish. @@ -1047,7 +1050,7 @@ bot.history.save() return True else: - pywikibot.bot.suggest_help(missing_generator=True) + suggest_help(missing_generator=True) return False
pywikibot-commits@lists.wikimedia.org