Takayuki KONDO avatar Takayuki KONDO committed bef6fc2

add 'hg_version' directive

Comments (0)

Files changed (5)

 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
 extensions = []
 extensions += ['sphinxcontrib.hg_changelog']
+extensions += ['sphinxcontrib.hg_version']
 
 
 # Add any paths that contain templates here, relative to this directory.
 
 # Output file base name for HTML help builder.
 htmlhelp_basename = 'sphinxcontrib-mercurial-doc'
-
-
    :maxdepth: 2
    :numbered:
 
+Version
+=======
+.. hg_version::
 
 Changelog
 ============
 [egg_info]
 tag_build = dev
-tag_date = true
+;tag_date = true
 
 [aliases]
 release = egg_info -RDb ''
 
 setup(
     name='sphinxcontrib-mercurial',
-    version='0.1',
+    version='0.2',
     url='http://bitbucket.org/cointoss1973/sphinxcontrib-mercurial',
     download_url='http://pypi.python.org/pypi/sphinxcontrib-mercurial',
     license='GPLv3',

sphinxcontrib/hg_version.py

+# -*- coding: utf-8-unix -*-
+# Copyright 2012 (C) Takayuki KONDO <tkondou@gmail.com>
+#
+import sys
+import os, subprocess
+
+from docutils import nodes
+from docutils.parsers.rst import directives
+from sphinx.util.compat import Directive
+
+
+if sys.version_info[0] >= 3:
+    def b(s):
+        '''A helper function to emulate 2.6+ bytes literals using string
+        literals.'''
+        return s.encode('latin1')
+else:
+    def b(s):
+        '''A helper function to emulate 2.6+ bytes literals using string
+        literals.'''
+        return s
+
+class HgVersion(Directive):
+
+    def runcmd(self, cmd, env):
+        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                             stderr=subprocess.PIPE, env=env)
+        out, err = p.communicate()
+        return out, err
+
+    def runhg(self, cmd, env):
+        out, err = self.runcmd(cmd, env)
+
+        err = [e for e in err.splitlines()
+               if not e.startswith(b('Not trusting file')) \
+               and not e.startswith(b('warning: Not importing'))]
+        if err:
+            return ''
+        return out
+
+    def run(self):
+        env = {'LANGUAGE': 'C'}
+        cmd = ['hg', 'log', '-r', '.', '--template', '{tags}\n']
+        numerictags = [t for t in self.runhg(cmd, env).split() if t[0].isdigit()]
+        hgid = self.runhg(['hg', 'id', '-i', '-t'], env).strip()
+
+        version = ''
+
+        if numerictags: # tag(s) found
+            version = numerictags[-1]
+            if hgid.endswith('+'): # propagate the dirty status to the tag
+                version += '+'
+        else: # no tag found
+            cmd = [sys.executable, 'hg', 'parents', '--template',
+                   '{latesttag}+{latesttagdistance}-']
+            version = self.runhg(cmd, env) + hgid
+            if version.endswith('+'):
+                version += time.strftime('%Y%m%d')
+
+        l = nodes.bullet_list()
+        item = nodes.list_item()
+        item += [nodes.inline(text=version)]
+        l.append(item)
+
+        return [l]
+
+def setup(app):
+    app.add_directive('hg_version', HgVersion)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.