Commits

Rafael Martins  committed 20a311d

changed the hg version detection to use the api instead of the cli.

  • Participants
  • Parent commits cc400ec

Comments (0)

Files changed (1)

File blohg/version.py

     :license: GPL-2, see LICENSE for more details.
 """
 
+import re
+
+
 # CHANGE ME BEFORE THE RELEASE!
+# when this file got installed from a mercurial repository, the hash of the
+# current changeset is appended to the 'version' variable.
 version = '0.8+'
 
-# code snippet from sphinx
-# http://bitbucket.org/birkenfeld/sphinx/src/tip/sphinx/__init__.py
-if '+' in version or 'pre' in version:
-    # try to find out the changeset hash if checked out from hg, and append
-    # it to version (since we use this value from setup.py, it gets
-    # automatically propagated to an installed copy as well)
-    try:
-        import os
-        import subprocess
-        cwd = os.path.dirname(os.path.abspath(__file__))
-        p = subprocess.Popen(
-            ['hg', 'id', '-i', '-R', os.path.join(cwd, '..')],
-            stdout=subprocess.PIPE,
-            stderr=subprocess.PIPE
-        )
-        out, err = p.communicate()
-        if out:
-            version += '/' + out.strip()
-    except Exception:
-        pass
+# the file isn't installed but this isn't a stable release, then we may be
+# running from a mercurial repository. let's verify.
+if re.match(r'^[0-9.]+\+$', version):
+
+    # we don't need to care about use mercurial from command-line, because we
+    # are GPL already anyway :)
+    from mercurial import hg, ui as hgui
+    from mercurial.commands import identify
+    import os
+
+    # root dir is where the parent dir of the dir where this file is.
+    root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
+
+    # if we are inside a mercurial repository...
+    if os.path.isdir(os.path.join(root_dir, '.hg')):
+
+        # create an ui object
+        ui = hgui.ui()
+
+        # create the repo object
+        repo = hg.repository(ui, root_dir)
+
+        # use the ui stack to get the return value from the 'hg identify'
+        # command, and run the command itself.
+        ui.pushbuffer()
+        identify(ui, repo, id=True)
+        rv = ui.popbuffer()
+
+        # append the changeset hash to the version.
+        if rv:
+            version += '/' + rv.strip()