changeset 7826:f0a7accf1d68

setup.py: fix version detection for tarball - don't do version detection if there's no .hg directory - shrink try: clause - don't write __version__.py if version is unknown (we might overwrite the real version)
author Matt Mackall <mpm@selenic.com>
date Fri, 06 Mar 2009 09:15:47 -0600
parents 6542be5df719
children 2dfe5cf92ad3
files setup.py
diffstat 1 files changed, 23 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py	Wed Mar 04 19:04:21 2009 -0600
+++ b/setup.py	Fri Mar 06 09:15:47 2009 -0600
@@ -97,7 +97,10 @@
 except ImportError:
     pass
 
-try:
+def getversion():
+    if not os.path.exists('.hg'):
+        return None # not in a repository
+
     # execute hg out of this directory with a custom environment which
     # includes the pure Python modules in mercurial/pure
     pypath = os.environ.get('PYTHONPATH', '')
@@ -105,23 +108,31 @@
     os.environ['PYTHONPATH'] = os.pathsep.join(['mercurial', purepath, pypath])
     os.environ['HGRCPATH'] = '' # do not read any config file
     cmd = '%s hg id -it' % sys.executable
-    l = os.popen(cmd).read().split()
+
+    try:
+        l = os.popen(cmd).read().split()
+    except OSError, e:
+        print "warning: could not establish Mercurial version: %s" % e
+
     os.environ['PYTHONPATH'] = pypath
+
     while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
         l.pop()
-    version = l and l[-1] or 'unknown' # latest tag or revision number
-    if version.endswith('+'):
-        version += time.strftime('%Y%m%d')
+    if l:
+        version = l[-1] # latest tag or revision number
+        if version.endswith('+'):
+            version += time.strftime('%Y%m%d')
+        return version
 
-except OSError, e:
-    print "warning: could not establish Mercurial version: %s" % e
+version = getversion()
+if version:
+    f = file("mercurial/__version__.py", "w")
+    f.write('# this file is autogenerated by setup.py\n')
+    f.write('version = "%s"\n' % version)
+    f.close()
+else:
     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):
         self.set_undefined_options('install',