title: Mercurial scripting with python
body: <p>I am trying to get the mercurial revision number/id (it's a hash not a number) programmatically in python.</p>
  <p>The reason is that I want to add it to the css/js files on our website like so:</p>
  <pre><code>&lt;link rel=&quot;stylesheet&quot; href=&quot;example.css?{% mercurial_revision &quot;example.css&quot; %}&quot; /&gt;
  </code></pre>
  <p>So that whenever a change is made to the stylesheet, it will get a new url and no longer use the old cached version.</p>
  <p><em><strong>OR</strong></em> if you know where to find good documentation for the mercurial <strong>python module</strong>, that would also be helpful. I can't seem to find it anywhere.</p>
  <h3>My Solution</h3>
  <p>I ended up using subprocess to just run a command that gets the hg node. I chose this solution because the api is not guaranteed to stay the same, but the bash interface probably will:</p>
  <pre><code>import subprocess
  
  def get_hg_rev(file_path):
      pipe = subprocess.Popen(
          [&quot;hg&quot;, &quot;log&quot;, &quot;-l&quot;, &quot;1&quot;, &quot;--template&quot;, &quot;{node}&quot;, file_path],
          stdout=subprocess.PIPE
          )
      return pipe.stdout.read()
  </code></pre>
  <p>example use:</p>
  <pre><code>&gt; path_to_file = &quot;/home/jim/workspace/lgr/pinax/projects/lgr/site_media/base.css&quot;
  &gt; get_hg_rev(path_to_file)
  '0ed525cf38a7b7f4f1321763d964a39327db97c4'
  </code></pre>