python-review /

Full commit
#!/usr/bin/env python
This script copies latest to and refreshes
version in to upload `review` package to PyPI.
from __future__ import with_statement

import shutil
import os,sys


def get_hg_path_revision(path):
    """return latest modification revision of a path inside hg

       :raise:  EnvironmentError if `hg` isn't found or path invalid
       :return: string like 'num:hash'
    from subprocess import Popen, PIPE
        hgprocess = Popen(
            'hg log -l 1 --template "{node|short} {rev}" --cwd "%s" "%s"'
                % (os.path.dirname(path) or '.', os.path.basename(path)),
            shell=True, stdout=PIPE, stderr=PIPE)
        output = hgprocess.communicate()
        if hgprocess.returncode != 0:
            raise EnvironmentError(hgprocess.returncode, "'hg' returned error")
        hgid, hgnum = output[0].strip().split()
        return hgnum + ':' + hgid
    except OSError:
        raise EnvironmentError(2, "'hg' command not found")

def history2rst(history):
    import re

    # linkify revisions
    revision = re.compile(r'(\d{3,4}:[0-9a-f]{12}) -', re.M)
    anchors = revision.findall(history) # ['695:ba3f47e4a614', ...]
    history = revision.sub(r'`\1`_ -', history)
    history += "\n"
    history += "\n".join(
      ['.. _`%s`: %s%s' % (x, REPO_VIEW, x.split(':')[1]) for x in anchors]
    history += "\n"
    return history

if not os.path.exists(UPLOAD_PY_PATH):
  sys.exit("error: is not found in parent dir")

print "copying ../ to"
shutil.copyfile(UPLOAD_PY_PATH, "")

print "updating version and history in"
revision = get_hg_path_revision(UPLOAD_PY_PATH)
version = revision.split(':')[0].strip('+')
# after conversion to Mercurial revision number decreased, and the
# latest released r749 corresponds to 655 num in Hg
with open("") as fr:
    setup_contents = fr.readlines()
with open("HISTORY") as fh:
    history_content =
with open("","wb") as fw:
    foundver = False
    setup_iter = iter(setup_contents)
    for line in setup_iter:
        # injecting revision number
        vpos = line.find("version='")
        if vpos != -1:
            line = line[:vpos] + "version='%s',\n" % version
            foundver = True
        # injecting HISTORY
        if line.find("Changes:") != -1:
            while line[0] != '*':
                line =
    if not foundver:
        print "warning: can't find version string in"
print "done."