mercurial/version.py
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Sun, 04 Jan 2009 21:49:37 +0100
changeset 7601 49355875c805
parent 6565 a0a726909871
permissions -rw-r--r--
merge with crew
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
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    15
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    16
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
    17
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
    18
4066
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    19
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
    20
    """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
    21
    try:
4066
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    22
        import mercurial.__version__
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    23
        if doreload:
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    24
            reload(mercurial.__version__)
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    25
        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
    26
    except ImportError:
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    27
        version = unknown_version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    28
    return version
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    29
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    30
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
    31
    """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
    32
    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
    33
        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
    34
    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
    35
    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
    36
        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
    37
            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
    38
        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
    39
            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
    40
    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
    41
    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
    42
    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
    43
    f.close()
4066
cff3e4b4a8de setup.py: reload __version__.py after writing it.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2859
diff changeset
    44
    # 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
    45
    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
    46
427
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    47
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
    48
    """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
    49
    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
    50
    if not version and os.path.isdir(".hg"):
6565
a0a726909871 Make mercurial easy installable again
Patrick Mezard <pmezard@gmail.com>
parents: 6548
diff changeset
    51
        f = os.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
    52
        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
    53
        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
    54
            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
    55
            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
    56
            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
    57
                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
    58
                modified = True
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    59
            else:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    60
                modified = False
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    61
            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
    62
                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
    63
                    # 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
    64
                    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
    65
                        version = tag
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    66
                        break
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    67
            if modified:
719663b7f235 remember_version() only writes version if called in a Mercurial repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 423
diff changeset
    68
                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
    69
    if version:
36e644d28edf Make it possible to specify a version number in setup.py.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 425
diff changeset
    70
        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
    71
        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
    72
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    73
def forget_version():
25afb21d97ba Support for 'hg --version'. setup.py stores version from hg repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    74
    """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
    75
    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
    76
        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
    77