DrTrigon has submitted this change and it was merged.
Change subject: adopt version handling and output to transition from SVN to GIT pywikibot/version.py sync-ed with core again ......................................................................
adopt version handling and output to transition from SVN to GIT pywikibot/version.py sync-ed with core again
Change-Id: I79efeff22636db7fb1a9258ab6e95615afb8aad6 --- M pwb.py M pywikibot/version.py M wikipedia.py 3 files changed, 83 insertions(+), 41 deletions(-)
Approvals: DrTrigon: Verified; Looks good to me, approved jenkins-bot: Verified
diff --git a/pwb.py b/pwb.py index 8388ca3..f3e538a 100755 --- a/pwb.py +++ b/pwb.py @@ -149,12 +149,11 @@
error = u'' try: - __release_ver = str(version.getversion_svn(pywikibot.config.datafilepath('..'))[1]) - #__release_ver = version.getversion().strip() + __release = version.getversiondict() d = shelve.open(pywikibot.config.datafilepath('cache', 'state_bots')) - d['bot_control'] = { 'release_ver': __release_ver, - 'framework_ver': __release_ver, - 'online_ver': version.getversion_onlinerepo(), + d['bot_control'] = {'release_rev': str(__release['rev']), + 'release_ver': __release['hsh'][:7], + 'online_ver': version.getversion_onlinerepo()[:7], } pywikibot.output(d['bot_control']) pywikibot.output(u'=== ' * 14) diff --git a/pywikibot/version.py b/pywikibot/version.py index 571ebe4..a9c6344 100644 --- a/pywikibot/version.py +++ b/pywikibot/version.py @@ -11,13 +11,16 @@
import os import sys -import time +import time, datetime import urllib +import subprocess
cache = None
+ class ParseError(Exception): """ Parsing went wrong """ +
def _get_program_dir(): _program_dir = os.path.normpath(os.path.split(os.path.dirname(__file__))[0]) @@ -26,44 +29,52 @@ ## _program_dir = os.path.normpath(os.path.join(os.getcwd(), _program_dir)) return _program_dir
+ def getversion(): - data = getversiondict() + data = dict(getversiondict()) # copy dict to prevent changes in 'chache' try: - rev2 = int(getversion_onlinerepo()) - rev1 = int(str(data['rev']).split()[0]) - data['cmp_ver'] = 'OUTDATED' if rev1 < rev2 else 'ok' + hsh2 = getversion_onlinerepo() + hsh1 = data['hsh'] + data['cmp_ver'] = 'OUTDATED' if hsh1 != hsh2 else 'ok' except Exception: data['cmp_ver'] = 'n/a' - return '%(tag)s (r%(rev)s, %(date)s, %(cmp_ver)s)' % data + data['hsh'] = data['hsh'][:7] # make short hash from full hash + return '%(tag)s (r%(rev)s, %(hsh)s, %(date)s, %(cmp_ver)s)' % data
def getversiondict(): global cache if cache: - return cache + return cache try: - (tag, rev, date) = getversion_svn() + (tag, rev, date, hsh) = getversion_git() except Exception: try: - (tag, rev, date) = getversion_nightly() + (tag, rev, date, hsh) = getversion_nightly() 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__ + version = getfileversion(wikipedia.__file__[:-1])
- id, file, rev, date, ts, author, dollar = version.split(' ') + file, hsh_short, date, ts = version.split(' ') tag = 'wikipedia.py' - date = time.strptime('%sT%s' % (date, ts), '%Y-%m-%dT%H:%M:%SZ') - except: # nothing worked; version unknown (but suppress exceptions) + rev = '-1 (unknown)' + ts = ts.split('.')[0] + date = time.strptime('%sT%s' % (date, ts), '%Y-%m-%dT%H:%M:%S') + hsh = hsh_short + ('?' * 33) # enhance the short hash w. '?' + 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)') + return dict(tag='', rev='-1 (unknown)', date='0 (unknown)', + hsh='(unknown)')
datestring = time.strftime('%Y/%m/%d, %H:%M:%S', date) - cache = dict(tag=tag, rev=rev, date=datestring) + cache = dict(tag=tag, rev=rev, date=datestring, hsh=hsh) return cache +
def getversion_svn(path=None): _program_dir = path or _get_program_dir() @@ -75,7 +86,7 @@ from sqlite3 import dbapi2 as sqlite 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''') + cur.execute('''select local_relpath, repos_path, revision, changed_date from nodes order by revision desc, changed_date desc''') name, tag, rev, date = cur.fetchone() con.close() tag = tag[:-len(name)] @@ -89,58 +100,90 @@ tag = '[%s] %s' % (t[0], t[1]) for i in xrange(4): entries.readline() - date = time.strptime(entries.readline()[:19],'%Y-%m-%dT%H:%M:%S') + date = time.strptime(entries.readline()[:19], '%Y-%m-%dT%H:%M:%S') rev = entries.readline()[:-1] entries.close() if (not date or not tag or not rev) and not path: raise ParseError return (tag, rev, date)
+def getversion_git(path=None): + _program_dir = path or _get_program_dir() + #(try to use .git directory for new entries format) + #tag = subprocess.Popen('git config --get remote.origin.url', + # shell=True, + # stdout=subprocess.PIPE).stdout.read() + tag = open(os.path.join(_program_dir, '.git/config'), 'r').read() + s = tag.find('url = ', tag.find('[remote "origin"]')) + e = tag.find('\n', s) + tag = tag[(s+6):e] + t = tag.strip().split('/') + tag = '[%s] %s' % (t[0][:-1], '/'.join(t[3:])[:-4]) + info = subprocess.Popen("git log --pretty=format:'%ad|%an|%h|%H|%d' --abbrev-commit --date=iso -1 | cat -", + shell=True, + stdout=subprocess.PIPE).stdout.read() + info = info.split('|') + date = info[0][:-6] + date = time.strptime(date, '%Y-%m-%d %H:%M:%S') + rev = subprocess.Popen('git rev-list HEAD | wc -l', + shell=True, + stdout=subprocess.PIPE).stdout.read() + rev = int(rev.strip()) + hsh = info[3] # also stored in '.git/refs/heads/master' + if (not date or not tag or not rev) and not path: + raise ParseError + return (tag, rev, date, hsh) + def getversion_nightly(): data = open(os.path.join(wikipediatools.get_base_dir(), 'version')) tag = data.readline().strip() - date = time.strptime(data.readline()[:19],'%Y-%m-%dT%H:%M:%S') + date = time.strptime(data.readline()[:19], '%Y-%m-%dT%H:%M:%S') rev = data.readline().strip() if not date or not tag or not rev: raise ParseError - return (tag, rev, date) + return (tag, rev, date, '(unknown)')
-## Retrieve revision number of framework online repository's svnroot -# def getversion_onlinerepo(repo=None): - url = repo or 'http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/' - rev = None + """ Retrieve revision number of framework online repository's svnroot """ + url = repo or 'https://git.wikimedia.org/feed/pywikibot/compat' + hsh = None try: - buf = urllib.urlopen(url).read() - rev = buf.split(' ')[3][:-1] + buf = urllib.urlopen(url).readlines() + hsh = buf[13].split('/')[5][:-1] except: raise ParseError - return rev + return hsh
## Simple version comparison # cmp_ver = lambda a, b, tol=1: {-1: '<', 0: '~', 1: '>'}[cmp((a-b)//tol, 0)]
-## Retrieve revision number of file (__version__ variable containing Id tag) -# without importing it (thus can be done for any file) -# + def getfileversion(filename): + """ Retrieve revision number of file (__version__ variable containing Id tag) + without importing it (thus can be done for any file) + """ _program_dir = _get_program_dir() __version__ = None + size, mtime = None, None fn = os.path.join(_program_dir, filename) if os.path.exists(fn): for line in open(fn, 'r').readlines(): if line.find('__version__') == 0: exec(line) break - return __version__ + stat = os.stat(fn) + mtime = datetime.datetime.fromtimestamp(stat.st_mtime).isoformat(' ') + if mtime and __version__: + return u'%s %s %s' % (filename, __version__[5:-1][:7], mtime) + else: + return None
-## Get the tail path component and file name of the currently executing -# script. Returns a tuple. -# + def get_executing_script(): - """Get the last path component and filename of the currently - executing script.""" + """ Get the last path component (tail) and filename of the currently + executing script. Returns a tuple. + """ try: script = os.path.abspath(sys.modules['__main__'].__file__) except (KeyError, AttributeError): diff --git a/wikipedia.py b/wikipedia.py index ccdc5d7..028987a 100644 --- a/wikipedia.py +++ b/wikipedia.py @@ -9231,7 +9231,7 @@ output(u'MODULES:') for item in sys.modules.keys(): ver = version.getfileversion('%s.py' % item.replace('.', '/')) - if ver and (ver[0] == u'$'): + if ver: output(u' %s' % ver)
# messages on bot discussion page?
pywikibot-commits@lists.wikimedia.org