[PATCH 4 of 5] setup: simplify the code by using a local function to run commands

Gilles Moris gilles.moris at free.fr
Fri Oct 16 04:16:28 CDT 2009


 setup.py |  52 +++++++++++++++++++++++++++-------------------------
 1 files changed, 27 insertions(+), 25 deletions(-)


# HG changeset patch
# User Gilles Moris <gilles.moris at free.fr>
# Date 1255684436 -7200
# Node ID 85521e9406339fe34ba928414cb2deb55dccafff
# Parent  39b4734384fa6bed8a4fb75f1d7f2b0cef83fd33
setup: simplify the code by using a local function to run commands

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -97,6 +97,22 @@
 except ImportError:
     pass
 
+def runcmd(cmd):
+    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                         stderr=subprocess.PIPE, env=env)
+    out, err = p.communicate()
+    # If root is executing setup.py, but the repository is owned by
+    # another user (as in "sudo python setup.py install") we will get
+    # trust warnings since the .hg/hgrc file is untrusted. That is
+    # fine, we don't want to load it anyway.
+    err = [e for e in err.splitlines()
+           if not e.startswith('Not trusting file')]
+    if err:
+        sys.stderr.write('warning: could not establish Mercurial '
+                         'version:\n%s\n' % '\n'.join(err))
+        return ''
+    return out
+
 version = None
 
 if os.path.isdir('.hg'):
@@ -114,31 +130,17 @@
         env['SystemRoot'] = os.environ['SystemRoot']
     cmd = [sys.executable, 'hg', 'id', '-i', '-t']
 
-    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
-                         stderr=subprocess.PIPE, env=env)
-    out, err = p.communicate()
-
-    # If root is executing setup.py, but the repository is owned by
-    # another user (as in "sudo python setup.py install") we will get
-    # trust warnings since the .hg/hgrc file is untrusted. That is
-    # fine, we don't want to load it anyway.
-    err = [e for e in err.splitlines()
-           if not e.startswith('Not trusting file')]
-    if err:
-        sys.stderr.write('warning: could not establish Mercurial '
-                         'version:\n%s\n' % '\n'.join(err))
-    else:
-        l = out.split()
-        while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
-            l.pop()
-        if l:
-            version = l[-1] # latest tag or revision number
-            if l[0].endswith('+') and len(l) > 1:
-                # for an unclean working directory based on a tag, we also
-                # need a '+'
-                version += '+'
-            if version.endswith('+'):
-                version += time.strftime('%Y%m%d')
+    l = runcmd(cmd).split()
+    while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
+        l.pop()
+    if l:
+        version = l[-1] # latest tag or revision number
+        if l[0].endswith('+') and len(l) > 1:
+            # for an dirty working directory based on a tag, we also
+            # need a '+'
+            version += '+'
+        if version.endswith('+'):
+            version += time.strftime('%Y%m%d')
 elif os.path.exists('.hg_archival.txt'):
     hgarchival = open('.hg_archival.txt')
     for line in hgarchival:


More information about the Mercurial-devel mailing list