mercurial/version.py
author Thomas Arendsen Hein <thomas@intevation.de>
Sun, 19 Jun 2005 21:04:32 +0100
changeset 427 36e644d28edf
parent 425 719663b7f235
child 429 688d03d6997a
permissions -rw-r--r--
Make it possible to specify a version number in setup.py. -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Make it possible to specify a version number in setup.py. manifest hash: 905feb305205801eb3833e5a84161fb57b83c86e -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFCtc/QW7P1GVgWeRoRAlCaAJ9G2GRf0wIEVEbYNoV4PjV4b024bQCfcUFf WVYQlTXqninDXyKas2yQYdo= =ofg/ -----END PGP SIGNATURE-----
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
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    16
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    17
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
    18
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
    19
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    20
def get_version():
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    21
    """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
    22
    try:
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    23
        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
    24
    except ImportError:
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    25
        version = unknown_version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    26
    return version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    27
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    28
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
    29
    """Overwrite version file."""
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    30
    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
    31
    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
    32
    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
    33
    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
    34
    f.close()
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    35
427
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    36
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
    37
    """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
    38
    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
    39
    if not version and os.path.isdir(".hg"):
425
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    40
        f = os.popen("hg identify 2>/dev/null")  # use real hg installation
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    41
        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
    42
        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
    43
            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
    44
            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
    45
            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
    46
                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
    47
                modified = True
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    48
            else:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    49
                modified = False
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    50
            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
    51
                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
    52
                    # 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
    53
                    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
    54
                        version = tag
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    55
                        break
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    56
            if modified:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    57
                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
    58
    if version:
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    59
        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
    60
        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
    61
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    62
def forget_version():
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    63
    """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
    64
    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
    65
        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
    66