XZise has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/216876

Change subject: [FIX] version: Support even if pywikibot won't
......................................................................

[FIX] version: Support even if pywikibot won't

If there is a problem with the pywikibot package it won't be possible to run
that script but sometimes it might be helpful to get that running.

Change-Id: I550f6d9abab5815d4c4941ab098a38e8e3dcad19
---
M scripts/version.py
1 file changed, 44 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/76/216876/1

diff --git a/scripts/version.py b/scripts/version.py
index 3debf0b..44887f4 100755
--- a/scripts/version.py
+++ b/scripts/version.py
@@ -16,8 +16,13 @@
 import sys
 import os
 import codecs
-import pywikibot
-from pywikibot.version import getversion
+
+try:
+    import pywikibot
+    from pywikibot.version import getversion
+except ImportError as e:
+    pywikibot = e
+
 try:
     import requests
 except ImportError:
@@ -26,55 +31,69 @@
 WMF_CACERT = 'MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs'
 
 
+def output(text):
+    """Output the text via print in an error."""
+    if isinstance(pywikibot, ImportError):
+        print(text)
+    else:
+        pywikibot.output(text)
+
+
 def check_environ(environ_name):
     """Print environment variable."""
-    pywikibot.output('{0}: {1}'.format(environ_name, 
os.environ.get(environ_name, 'Not set')))
+    output('{0}: {1}'.format(environ_name, os.environ.get(environ_name, 'Not 
set')))
 
 
 if __name__ == '__main__':
-    pywikibot.output('Pywikibot: %s' % getversion())
-    pywikibot.output('Release version: %s' % pywikibot.__release__)
-    pywikibot.output('requests version: %s' % requests.__version__)
+    if not isinstance(pywikibot, ImportError):
+        pywikibot.output('Pywikibot: %s' % getversion())
+        pywikibot.output('Release version: %s' % pywikibot.__release__)
+    else:
+        output('Pywikibot: Unavailable ({0})'.format(pywikibot))
+    output('requests version: %s' % requests.__version__)
 
     has_wikimedia_cert = False
     if (not hasattr(requests, 'certs') or
             not hasattr(requests.certs, 'where') or
             not callable(requests.certs.where)):
-        pywikibot.output('  cacerts: not defined')
+        output('  cacerts: not defined')
     elif not os.path.isfile(requests.certs.where()):
-        pywikibot.output('  cacerts: %s (missing)' % requests.certs.where())
+        output('  cacerts: %s (missing)' % requests.certs.where())
     else:
-        pywikibot.output('  cacerts: %s' % requests.certs.where())
+        output('  cacerts: %s' % requests.certs.where())
 
         with codecs.open(requests.certs.where(), 'r', 'utf-8') as cert_file:
             text = cert_file.read()
             if WMF_CACERT in text:
                 has_wikimedia_cert = True
-        pywikibot.output(u'    certificate test: %s'
+        output(u'    certificate test: %s'
                          % ('ok' if has_wikimedia_cert else 'not ok'))
     if not has_wikimedia_cert:
-        pywikibot.output(
+        output(
             '  Please reinstall requests!')
 
-    pywikibot.output('Python: %s' % sys.version)
+    output('Python: %s' % sys.version)
     normalize_text = u'\u092e\u093e\u0930\u094d\u0915 
\u091c\u093c\u0941\u0915\u0947\u0930\u092c\u0930\u094d\u0917'
 
     if normalize_text != __import__('unicodedata').normalize(
             'NFC', normalize_text):
-        pywikibot.output(u'  unicode test: triggers problem #3081100')
+        output(u'  unicode test: triggers problem #3081100')
     else:
-        pywikibot.output(u'  unicode test: ok')
+        output(u'  unicode test: ok')
     check_environ('PYWIKIBOT2_DIR')
     check_environ('PYWIKIBOT2_DIR_PWB')
     check_environ('PYWIKIBOT2_NO_USER_CONFIG')
-    pywikibot.output('Config base dir: {0}'.format(pywikibot.config2.base_dir))
-    for family, usernames in pywikibot.config2.usernames.items():
-        if usernames:
-            pywikibot.output('Usernames for family "{0}":'.format(family))
-            for lang, username in usernames.items():
-                sysop_name = pywikibot.config2.sysopnames.get(family, 
{}).get(lang)
-                if not sysop_name:
-                    sysop_name = 'no sysop configured'
-                elif sysop_name == username:
-                    sysop_name = 'also sysop'
-                pywikibot.output('\t{0}: {1} ({2})'.format(lang, username, 
sysop_name))
+    if not isinstance(pywikibot, ImportError):
+        pywikibot.output('Config base dir: 
{0}'.format(pywikibot.config2.base_dir))
+        for family, usernames in pywikibot.config2.usernames.items():
+            if usernames:
+                pywikibot.output('Usernames for family "{0}":'.format(family))
+                for lang, username in usernames.items():
+                    sysop_name = pywikibot.config2.sysopnames.get(family, 
{}).get(lang)
+                    if not sysop_name:
+                        sysop_name = 'no sysop configured'
+                    elif sysop_name == username:
+                        sysop_name = 'also sysop'
+                    pywikibot.output('\t{0}: {1} ({2})'.format(lang, username, 
sysop_name))
+    else:
+        output('pywikibot could not be loaded: unknown base dir.')

-- 
To view, visit https://gerrit.wikimedia.org/r/216876
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I550f6d9abab5815d4c4941ab098a38e8e3dcad19
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <commodorefabia...@gmx.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to