mercurial/version.py
author Thomas Arendsen Hein <thomas@intevation.de>
Thu, 04 Aug 2005 16:56:44 +0100
changeset 828 7a6acd56cd5a
parent 464 50da4bb9cab6
child 1977 7eb694a1c1af
permissions -rw-r--r--
Hide error message of type command.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
423
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     1
# Copyright (C) 2005 by Intevation GmbH
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     2
# Author(s):
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     3
# Thomas Arendsen Hein <thomas@intevation.de>
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     4
#
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     5
# This program is free software under the GNU GPL (>=v2)
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     6
# Read the file COPYING coming with the software for details.
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     7
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     8
"""
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     9
Mercurial version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    10
"""
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    11
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    12
import os
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    13
import os.path
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    14
import re
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    15
import time
461
9ae0034f2772 [PATCH] /dev/null for other OS
mpm@selenic.com
parents: 429
diff changeset
    16
import util
423
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    17
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    18
unknown_version = 'unknown'
425
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    19
remembered_version = False
423
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    20
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    21
def get_version():
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    22
    """Return version information if available."""
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    23
    try:
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    24
        from mercurial.__version__ import version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    25
    except ImportError:
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    26
        version = unknown_version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    27
    return version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    28
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    29
def write_version(version):
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    30
    """Overwrite version file."""
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    31
    filename = os.path.join(os.path.dirname(__file__), '__version__.py')
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    32
    f = open(filename, 'w')
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    33
    f.write("# This file is auto-generated.\n")
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    34
    f.write("version = %r\n" % version)
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    35
    f.close()
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    36
427
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    37
def remember_version(version=None):
423
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    38
    """Store version information."""
425
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    39
    global remembered_version
427
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    40
    if not version and os.path.isdir(".hg"):
461
9ae0034f2772 [PATCH] /dev/null for other OS
mpm@selenic.com
parents: 429
diff changeset
    41
        f = os.popen("hg identify 2> %s" % util.nulldev)  # use real hg installation
425
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    42
        ident = f.read()[:-1]
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    43
        if not f.close() and ident:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    44
            ids = ident.split(' ', 1)
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    45
            version = ids.pop(0)
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    46
            if version[-1] == '+':
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    47
                version = version[:-1]
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    48
                modified = True
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    49
            else:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    50
                modified = False
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    51
            if version.isalnum() and ids:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    52
                for tag in ids[0].split('/'):
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    53
                    # is a tag is suitable as a version number?
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    54
                    if re.match(r'^(\d+\.)+[\w.-]+$', tag):
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    55
                        version = tag
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    56
                        break
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    57
            if modified:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    58
                version += time.strftime('+%Y%m%d')
427
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    59
    if version:
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    60
        remembered_version = True
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    61
        write_version(version)
423
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    62
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    63
def forget_version():
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    64
    """Remove version information."""
425
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    65
    if remembered_version:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    66
        write_version(unknown_version)
423
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    67