First time here? Check out the FAQ!
1

can git tags be created for each of the releases into pypi?

I was considering installing using git, but couldn't find any tags to use.

Evgeny's avatar
13.2k
Evgeny
updated 2011-10-10 03:00:48 -0500
tfoote's avatar
161
tfoote
asked 2011-03-16 22:38:07 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

1 Answer

1

Added a post-commit hook, which adds a tag if version increments, so the future versions will have tags.

#!/usr/bin/python
# place in .git/hooks/post-commit
"""Post-commit hook for git, which automatically adds tag
corresponding to the new version, to adapt it to your needs,
change the ``VERSION_FILE`` variable below.
"""
VERSION_FILE = 'askbot/__init__.py'

import commands, re

class Version(object):
    """basic implementation of version utility,
    only supports numeric triples like '0.1.3'
    """
    def __init__(self, vstring = None):
        if vstring:
            self.parse(vstring)

    def parse(self, vstring):
        major, minor, micro = vstring.split('.')
        self.major = int(major)
        self.minor = int(minor)
        self.micro = int(micro)

    def __str__(self):
        return "%s.%s.%s" % (self.major, self.minor, self.micro)

    def __cmp__(self, other):
        cmp_major = cmp(self.major, other.major)
        if cmp_major == 0:
            cmp_minor = cmp(self.minor, other.minor)
            if cmp_minor == 0:
                return cmp(self.micro, other.micro)
            else:
                return cmp_minor
        else:
            return cmp_major

def add_tag(tag):
    """tag the git repo with the new version"""
    status, output = commands.getstatusoutput('git tag -f %s' % tag)
    if status:
        raise Exception('tagging not successful: %s %s' % (output, status))

def get_version(version_file_string):
    """returns instance of :class:`Version`
    based on the string input,
    which is expected to be contents of some file or output"""
    regex_string = r"VERSION\s?\=\s?" + \
                    r"\((?P<major>[0-9]+)\s?\," + \
                    r"\s?(?P<minor>[0-9]+)\s?\," + \
                    r"\s?(?P<micro>[0-9]+)\s?\)"
    for line in version_file_string.split("\n"):
        regex = re.compile(regex_string)
        match  = regex.search(line)
        if match:
            vstring = '.'.join(match.groups())
            return Version(vstring = vstring)
    return Version('0.0.0')


def get_versions():
    """returns name of a new version as string
    if there is a new version, otherwise
    ``None``
    """
    status, output = commands.getstatusoutput(
        'git show HEAD:' + VERSION_FILE
    )
    new_version = get_version(output)

    status, output = commands.getstatusoutput(
        'git show HEAD^:' + VERSION_FILE
    )
    old_version = get_version(output)
    return new_version, old_version

if __name__ == '__main__':

    NEW, OLD = get_versions()
    if NEW > OLD:
        add_tag(NEW)
        print "added new tag %s" % NEW
    elif NEW < OLD:
        raise ValueError("Error: new version is older than the previous")
Evgeny's avatar
13.2k
Evgeny
updated 2011-10-09 21:03:09 -0500, answered 2011-03-16 22:47:48 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments