changeset 7632:9626819b2e3d

refactor version code - simplify version detection code - move detection code into setup.py - move version reading function into util.py - drop version.py code This makes hg more closely follow its own recommendation of how to deal with versioning your builds: use hg id in your build script.
author Matt Mackall <mpm@selenic.com>
date Sat, 10 Jan 2009 18:02:38 -0600
parents 0b2ee57dfdb1
children 08cabecfa8a8
files mercurial/commands.py mercurial/dispatch.py mercurial/util.py mercurial/version.py setup.py tests/test-demandimport tests/test-demandimport.out
diffstat 7 files changed, 28 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Tue Jan 13 23:17:19 2009 +0100
+++ b/mercurial/commands.py	Sat Jan 10 18:02:38 2009 -0600
@@ -11,7 +11,6 @@
 import os, re, sys
 import hg, util, revlog, bundlerepo, extensions, copies, context
 import difflib, patch, time, help, mdiff, tempfile, url
-import version
 import archival, changegroup, cmdutil, hgweb.server, sshserver, hbisect
 import merge as merge_
 
@@ -2961,7 +2960,7 @@
 def version_(ui):
     """output version and copyright information"""
     ui.write(_("Mercurial Distributed SCM (version %s)\n")
-             % version.get_version())
+             % util.version())
     ui.status(_(
         "\nCopyright (C) 2005-2008 Matt Mackall <mpm@selenic.com> and others\n"
         "This is free software; see the source for copying conditions. "
--- a/mercurial/dispatch.py	Tue Jan 13 23:17:19 2009 +0100
+++ b/mercurial/dispatch.py	Sat Jan 10 18:02:38 2009 -0600
@@ -8,7 +8,7 @@
 from i18n import _
 from repo import RepoError
 import os, sys, atexit, signal, pdb, socket, errno, shlex, time
-import util, commands, hg, lock, fancyopts, revlog, version, extensions, hook
+import util, commands, hg, lock, fancyopts, revlog, extensions, hook
 import cmdutil
 import ui as _ui
 
@@ -145,7 +145,7 @@
                  "http://www.selenic.com/mercurial/bts\n"))
         ui.warn(_("** or mercurial@selenic.com\n"))
         ui.warn(_("** Mercurial Distributed SCM (version %s)\n")
-               % version.get_version())
+               % util.version())
         ui.warn(_("** Extensions loaded: %s\n")
                % ", ".join([x[0] for x in extensions.extensions()]))
         raise
--- a/mercurial/util.py	Tue Jan 13 23:17:19 2009 +0100
+++ b/mercurial/util.py	Sat Jan 10 18:02:38 2009 -0600
@@ -142,6 +142,14 @@
     """Find the length in characters of a local string"""
     return len(s.decode(_encoding, "replace"))
 
+def version():
+    """Return version information if available."""
+    try:
+        import __version__
+        return __version__.version
+    except ImportError:
+        return 'unknown'
+
 # used by parsedate
 defaultdateformats = (
     '%Y-%m-%d %H:%M:%S',
--- a/mercurial/version.py	Tue Jan 13 23:17:19 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,77 +0,0 @@
-# Copyright (C) 2005, 2006, 2008 by Intevation GmbH
-# Author(s):
-# Thomas Arendsen Hein <thomas@intevation.de>
-#
-# This program is free software under the GNU GPL (>=v2)
-# Read the file COPYING coming with the software for details.
-
-"""
-Mercurial version
-"""
-
-import os
-import re
-import time
-
-unknown_version = 'unknown'
-remembered_version = False
-
-def get_version(doreload=False):
-    """Return version information if available."""
-    try:
-        import mercurial.__version__
-        if doreload:
-            reload(mercurial.__version__)
-        version = mercurial.__version__.version
-    except ImportError:
-        version = unknown_version
-    return version
-
-def write_version(version):
-    """Overwrite version file."""
-    if version == get_version():
-        return
-    directory = os.path.dirname(__file__)
-    for suffix in ['py', 'pyc', 'pyo']:
-        try:
-            os.unlink(os.path.join(directory, '__version__.%s' % suffix))
-        except OSError:
-            pass
-    f = open(os.path.join(directory, '__version__.py'), 'w')
-    f.write("# This file is auto-generated.\n")
-    f.write("version = %r\n" % version)
-    f.close()
-    # reload the file we've just written
-    get_version(True)
-
-def remember_version(version=None):
-    """Store version information."""
-    global remembered_version
-    if not version and os.path.isdir(".hg"):
-        f = os.popen("hg identify")  # use real hg installation
-        ident = f.read()[:-1]
-        if not f.close() and ident:
-            ids = ident.split(' ', 1)
-            version = ids.pop(0)
-            if version[-1] == '+':
-                version = version[:-1]
-                modified = True
-            else:
-                modified = False
-            if version.isalnum() and ids:
-                for tag in ids[0].split('/'):
-                    # is a tag is suitable as a version number?
-                    if re.match(r'^(\d+\.)+[\w.-]+$', tag):
-                        version = tag
-                        break
-            if modified:
-                version += time.strftime('+%Y%m%d')
-    if version:
-        remembered_version = True
-        write_version(version)
-
-def forget_version():
-    """Remove version information."""
-    if remembered_version:
-        write_version(unknown_version)
-
--- a/setup.py	Tue Jan 13 23:17:19 2009 +0100
+++ b/setup.py	Sat Jan 10 18:02:38 2009 -0600
@@ -26,15 +26,13 @@
     raise SystemExit(
         "Couldn't import standard zlib (incomplete Python install).")
 
-import os
+import os, time
 import shutil
 import tempfile
 from distutils.core import setup, Extension
 from distutils.command.install_data import install_data
 from distutils.ccompiler import new_compiler
 
-import mercurial.version
-
 extra = {}
 scripts = ['hg']
 if os.name == 'nt':
@@ -95,8 +93,21 @@
 except ImportError:
     pass
 
-# specify version string, otherwise 'hg identify' will be used:
-version = ''
+try:
+    l = os.popen('hg id -it').read().split()
+    while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
+        l.pop()
+    version = l[-1] or 'unknown' # latest tag or revision number
+    if version.endswith('+'):
+        version += time.strftime('%Y%m%d')
+
+except OSError:
+    version = "unknown"
+
+f = file("mercurial/__version__.py", "w")
+f.write('# this file is autogenerated by setup.py\n')
+f.write('version = "%s"\n' % version)
+f.close()
 
 class install_package_data(install_data):
     def finalize_options(self):
@@ -104,7 +115,6 @@
                                    ('install_lib', 'install_dir'))
         install_data.finalize_options(self)
 
-mercurial.version.remember_version(version)
 cmdclass = {'install_data': install_package_data}
 
 ext_modules=[
@@ -140,7 +150,7 @@
     pass
 
 setup(name='mercurial',
-      version=mercurial.version.get_version(),
+      version=version,
       author='Matt Mackall',
       author_email='mpm@selenic.com',
       url='http://selenic.com/mercurial',
--- a/tests/test-demandimport	Tue Jan 13 23:17:19 2009 +0100
+++ b/tests/test-demandimport	Sat Jan 10 18:02:38 2009 -0600
@@ -18,13 +18,6 @@
 print "os.system =", f(os.system)
 print "os =", f(os)
 
-import mercurial.version
-
-print "mercurial.version =", f(mercurial.version)
-print "mercurial.version.get_version =", f(mercurial.version.get_version)
-print "mercurial.version =", f(mercurial.version)
-print "mercurial =", f(mercurial)
-
 from mercurial import util
 
 print "util =", f(util)
--- a/tests/test-demandimport.out	Tue Jan 13 23:17:19 2009 +0100
+++ b/tests/test-demandimport.out	Sat Jan 10 18:02:38 2009 -0600
@@ -1,10 +1,6 @@
 os = <unloaded module 'os'>
 os.system = <built-in function system>
 os = <module 'os' from '?'>
-mercurial.version = <unloaded module 'version'>
-mercurial.version.get_version = <function get_version at 0x?>
-mercurial.version = <module 'mercurial.version' from '?'>
-mercurial = <module 'mercurial' from '?'>
 util = <unloaded module 'util'>
 util.system = <function system at 0x?>
 util = <module 'mercurial.util' from '?'>