http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10960
Revision: 10960 Author: drtrigon Date: 2013-01-20 16:03:08 +0000 (Sun, 20 Jan 2013) Log Message: ----------- bug fix; follow-up to r10917 in order to properly solve the issues: * catch the case version=None and import the module as last try (should not be needed at all! -> see next) * properly determine the directory with _get_program_dir() based on __file__ (should work with PYTHONPATH pointing somewhere else too) * added a final try...except in order to catch the case when everything breaks (which should never happen!) * merged with rewrite (the are now exactly the same)
Modified Paths: -------------- trunk/pywikipedia/pywikibot/version.py trunk/pywikipedia/version.py
Modified: trunk/pywikipedia/pywikibot/version.py =================================================================== --- trunk/pywikipedia/pywikibot/version.py 2013-01-20 13:24:15 UTC (rev 10959) +++ trunk/pywikipedia/pywikibot/version.py 2013-01-20 16:03:08 UTC (rev 10960) @@ -11,7 +11,6 @@
import os import time -import sys import urllib
cache = None @@ -19,59 +18,61 @@ class ParseError(Exception): """ Parsing went wrong """
+def _get_program_dir(): + _program_dir = os.path.normpath(os.path.split(os.path.dirname(__file__))[0]) +# _program_dir = _program_dir.rstrip(os.path.basename(_program_dir)) +## if not os.path.isabs(_program_dir): +## _program_dir = os.path.normpath(os.path.join(os.getcwd(), _program_dir)) + return _program_dir + def getversion(): data = getversiondict() try: rev2 = int(getversion_onlinerepo()) rev1 = int(str(data['rev']).split()[0]) data['cmp_ver'] = 'OUTDATED' if rev1 < rev2 else 'ok' - except ParseError: + except Exception: data['cmp_ver'] = 'n/a' return '%(tag)s (r%(rev)s, %(date)s, %(cmp_ver)s)' % data
-def unknown_version(): - """Version cannot be determined""" - cache = dict(tag='', rev='-1 (unknown)', date='0 (unknown)') - return cache - def getversiondict(): global cache if cache: return cache try: (tag, rev, date) = getversion_svn() - except Exception, e: + except Exception: try: (tag, rev, date) = getversion_nightly() - except Exception, e: - version = getfileversion('wikipedia.py') - if version: - try: - id, file, rev, date, ts, author, dollar = version.split(' ') - tag = '' - date = time.strptime('%sT%s' % (date, ts), '%Y-%m-%dT%H:%M:%SZ') - rev += ' (wikipedia.py)' - except ValueError: - # the value is most likely '$Id' + '$', it means that - # wikipedia.py got imported without using svn at all - return unknown_version() - else: - return unknown_version() + except Exception: + try: + version = getfileversion('wikipedia.py') + if not version: + # fall-back in case everything breaks (should not be used) + import wikipedia + version = wikipedia.__version__ + + id, file, rev, date, ts, author, dollar = version.split(' ') + tag = '' + date = time.strptime('%sT%s' % (date, ts), '%Y-%m-%dT%H:%M:%SZ') + rev += ' (wikipedia.py)' + except: # nothing worked; version unknown (but suppress exceptions) + # the value is most likely '$Id' + '$', it means that + # wikipedia.py got imported without using svn at all + return dict(tag='', rev='-1 (unknown)', date='0 (unknown)') + datestring = time.strftime('%Y/%m/%d, %H:%M:%S', date) cache = dict(tag=tag, rev=rev, date=datestring) return cache
def getversion_svn(path=None): - _program_dir = path or os.path.normpath(os.path.dirname(sys.argv[0])) -# if not os.path.isabs(_program_dir): -# _program_dir = os.path.normpath(os.path.join(os.getcwd(), _program_dir)) + _program_dir = path or _get_program_dir() entries = open(os.path.join(_program_dir, '.svn/entries')) version = entries.readline().strip() #use sqlite table for new entries format if version == "12": entries.close() from sqlite3 import dbapi2 as sqlite - from datetime import datetime con = sqlite.connect(os.path.join(_program_dir, ".svn/wc.db")) cur = con.cursor() cur.execute( '''select local_relpath, repos_path, revision, changed_date from nodes order by revision desc, changed_date desc''') @@ -124,7 +125,7 @@ # without importing it (thus can be done for any file) # def getfileversion(filename): - _program_dir = os.path.normpath(os.path.dirname(sys.argv[0])) + _program_dir = _get_program_dir() __version__ = None fn = os.path.join(_program_dir, filename) if os.path.exists(fn):
Modified: trunk/pywikipedia/version.py =================================================================== --- trunk/pywikipedia/version.py 2013-01-20 13:24:15 UTC (rev 10959) +++ trunk/pywikipedia/version.py 2013-01-20 16:03:08 UTC (rev 10960) @@ -12,6 +12,7 @@ from pywikibot.version import * import config
+ if __name__ == '__main__': print 'Pywikipedia %s' % getversion() print 'Python %s' % sys.version
pywikipedia-svn@lists.wikimedia.org