jenkins-bot has submitted this change and it was merged.

Change subject: (bug 54686) re-enable support for svn repo revision detection
......................................................................


(bug 54686) re-enable support for svn repo revision detection

* re-enable old code still there
* add support for git sha hash (cf. change 85523)
* try to avoid to excessive use of try...except structures
* hash is primary identifier thus swapped with revision number
* revision number for git prefixed with "g" for svn with "s"
* some PEP8

Change-Id: I0aa62bc722df77d06c6e0bc10b87c4d284c63ede
---
M pywikibot/version.py
1 file changed, 36 insertions(+), 27 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/version.py b/pywikibot/version.py
index fa4cb4b..9892cfe 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -1,8 +1,8 @@
 # -*- coding: utf-8  -*-
 """ Module to determine the pywikipedia version (tag, revision and date) """
 #
-# (C) Merlijn 'valhallasw' van Deen, 2007-2008
-# (C) xqt, 2010-2011
+# (C) Merlijn 'valhallasw' van Deen, 2007-2014
+# (C) xqt, 2010-2014
 # (C) Pywikipedia bot team, 2007-2013
 #
 # Distributed under the terms of the MIT license.
@@ -11,7 +11,8 @@
 
 import os
 import sys
-import time, datetime
+import time
+import datetime
 import urllib
 import subprocess
 
@@ -24,22 +25,19 @@
 
 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 = dict(getversiondict())   # copy dict to prevent changes in 'chache'
+    data = dict(getversiondict())  # copy dict to prevent changes in 'chache'
     try:
         hsh2 = getversion_onlinerepo()
         hsh1 = data['hsh']
         data['cmp_ver'] = 'OUTDATED' if hsh1 != hsh2 else 'ok'
     except Exception:
         data['cmp_ver'] = 'n/a'
-    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
+    data['hsh'] = data['hsh'][:7]  # make short hash from full hash
+    return '%(tag)s (%(hsh)s, %(rev)s, %(date)s, %(cmp_ver)s)' % data
 
 
 def getversiondict():
@@ -47,11 +45,15 @@
     if cache:
         return cache
     try:
-        (tag, rev, date, hsh) = getversion_git()
-    except Exception:
+        _program_dir = _get_program_dir()
+        if os.path.isdir(os.path.join(_program_dir, '.svn')):
+            (tag, rev, date, hsh) = getversion_svn(_program_dir)
+        else:
+            (tag, rev, date, hsh) = getversion_git(_program_dir)
+    except ParseError:
         try:
             (tag, rev, date, hsh) = getversion_nightly()
-        except Exception:
+        except ParseError:
             try:
                 version = getfileversion('wikipedia.py')
                 if not version:
@@ -78,6 +80,8 @@
 
 
 def getversion_svn(path=None):
+    import httplib
+    import xml.dom.minidom
     _program_dir = path or _get_program_dir()
     entries = open(os.path.join(_program_dir, '.svn/entries'))
     version = entries.readline().strip()
@@ -87,10 +91,14 @@
         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''')
-        name, tag, rev, date = cur.fetchone()
+        cur.execute("""select
+local_relpath, repos_path, revision, changed_date, checksum from nodes
+order by revision desc, changed_date desc""")
+        name, tag, rev, date, checksum = cur.fetchone()
+        cur.execute("select root from repository")
+        tag, = cur.fetchone()
         con.close()
-        tag = tag[:-len(name)]
+        tag = os.path.split(tag)[1]
         date = time.gmtime(date / 1000000)
     else:
         for i in xrange(3):
@@ -104,9 +112,18 @@
         date = time.strptime(entries.readline()[:19], '%Y-%m-%dT%H:%M:%S')
         rev = entries.readline()[:-1]
         entries.close()
+    conn = httplib.HTTPSConnection('github.com')
+    conn.request('PROPFIND', '/wikimedia/%s/!svn/vcc/default' % tag,
+                 "<?xml version='1.0' encoding='utf-8'?>"
+                 "<propfind xmlns=\"DAV:\"><allprop/></propfind>",
+                 {'Label': rev, 'User-Agent': 'SVN/1.7.5-pywikibot1'})
+    resp = conn.getresponse()
+    dom = xml.dom.minidom.parse(resp)
+    hsh = dom.getElementsByTagName("C:git-commit")[0].firstChild.nodeValue
+    rev = 's%s' % rev
     if (not date or not tag or not rev) and not path:
         raise ParseError
-    return (tag, rev, date)
+    return (tag, rev, date, hsh)
 
 
 def getversion_git(path=None):
@@ -118,16 +135,12 @@
         # some windows git versions provide git.cmd instead of git.exe
         cmd = 'git.cmd'
 
-    #(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])
+    tag = '[%s] %s' % (t[0][:-1], '-'.join(t[3:]))
     info = subprocess.Popen([cmd, '--no-pager',
                              'log', '-1',
                              '--pretty=format:"%ad|%an|%h|%H|%d"'
@@ -141,8 +154,8 @@
     rev = subprocess.Popen([cmd, 'rev-list', 'HEAD'],
                            cwd=_program_dir,
                            stdout=subprocess.PIPE).stdout.read()
-    rev = len(rev.splitlines())
-    hsh = info[3]      # also stored in '.git/refs/heads/master'
+    rev = 'g%s' % len(rev.splitlines())
+    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)
@@ -168,10 +181,6 @@
     except:
         raise ParseError
     return hsh
-
-## Simple version comparison
-#
-cmp_ver = lambda a, b, tol=1: {-1: '<', 0: '~', 1: '>'}[cmp((a - b) // tol, 0)]
 
 
 def getfileversion(filename):

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I0aa62bc722df77d06c6e0bc10b87c4d284c63ede
Gerrit-PatchSet: 10
Gerrit-Project: pywikibot/compat
Gerrit-Branch: master
Gerrit-Owner: DrTrigon <[email protected]>
Gerrit-Reviewer: DrTrigon <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to