mercurial/version.py
author Kirill Smelkov <kirr@mns.spb.ru>
Wed, 23 Jan 2008 21:49:44 +0300
changeset 5933 28a79c259fcf
parent 5931 6482d34974ac
child 6237 4a85a9077136
permissions -rw-r--r--
qrecord: add tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5931
6482d34974ac Remove unneeded "import os.path"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5481
diff changeset
     1
# Copyright (C) 2005, 2006, 2008 by Intevation GmbH
423
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 re
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    14
import time
461
9ae0034f2772 [PATCH] /dev/null for other OS
mpm@selenic.com
parents: 429
diff changeset
    15
import util
423
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
4066
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    20
def get_version(doreload=False):
423
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:
4066
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    23
        import mercurial.__version__
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    24
        if doreload:
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    25
            reload(mercurial.__version__)
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    26
        version = mercurial.__version__.version
423
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    27
    except ImportError:
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    28
        version = unknown_version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    29
    return version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    30
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    31
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
    32
    """Overwrite version file."""
1977
7eb694a1c1af Don't forget version at the end of setup.py, write it only if changed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 464
diff changeset
    33
    if version == get_version():
7eb694a1c1af Don't forget version at the end of setup.py, write it only if changed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 464
diff changeset
    34
        return
7eb694a1c1af Don't forget version at the end of setup.py, write it only if changed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 464
diff changeset
    35
    directory = os.path.dirname(__file__)
7eb694a1c1af Don't forget version at the end of setup.py, write it only if changed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 464
diff changeset
    36
    for suffix in ['py', 'pyc', 'pyo']:
7eb694a1c1af Don't forget version at the end of setup.py, write it only if changed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 464
diff changeset
    37
        try:
7eb694a1c1af Don't forget version at the end of setup.py, write it only if changed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 464
diff changeset
    38
            os.unlink(os.path.join(directory, '__version__.%s' % suffix))
7eb694a1c1af Don't forget version at the end of setup.py, write it only if changed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 464
diff changeset
    39
        except OSError:
7eb694a1c1af Don't forget version at the end of setup.py, write it only if changed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 464
diff changeset
    40
            pass
7eb694a1c1af Don't forget version at the end of setup.py, write it only if changed.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 464
diff changeset
    41
    f = open(os.path.join(directory, '__version__.py'), 'w')
423
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    42
    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
    43
    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
    44
    f.close()
4066
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    45
    # reload the file we've just written
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    46
    get_version(True)
423
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    47
427
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    48
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
    49
    """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
    50
    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
    51
    if not version and os.path.isdir(".hg"):
5481
003d1f174fe1 Fix Windows os.popen bug with interleaved stdout/stderr output
Patrick Mezard <pmezard@gmail.com>
parents: 4066
diff changeset
    52
        f = util.popen("hg identify")  # 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
    53
        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
    54
        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
    55
            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
    56
            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
    57
            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
    58
                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
    59
                modified = True
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    60
            else:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    61
                modified = False
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    62
            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
    63
                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
    64
                    # 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
    65
                    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
    66
                        version = tag
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    67
                        break
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    68
            if modified:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    69
                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
    70
    if version:
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    71
        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
    72
        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
    73
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    74
def forget_version():
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    75
    """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
    76
    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
    77
        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
    78