[pypy-svn] pypy commit 1afa61e2bcb8: Add sys._mercurial attribute.

Bitbucket commits-noreply at bitbucket.org
Mon Dec 13 17:58:22 CET 2010


# HG changeset patch -- Bitbucket.org
# Project pypy
# URL http://bitbucket.org/pypy/pypy/overview
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1292254419 0
# Node ID 1afa61e2bcb88068ceabee8ccbe39a39d5019e64
# Parent  805815216ca7038c2ac1bc6cd620b28b96aef298
Add sys._mercurial attribute.

Based on the equivalent patch to CPython 2.7:
<http://hg.python.org/pymigr/file/tip/build-identification/add-hg-build-id.diff>

Example output:

$ ./python.exe -c 'import sys; print sys.subversion; print sys._mercurial'
('CPython', 'branches/release27-maint', '')
('CPython', 'add-hg-build-id.diff qbase qtip tip', 'a40affe1a0c3')
$ ./pypy-c -c 'import sys; print(sys.subversion); print(sys._mercurial)'
('PyPy', '', '0')
('PyPy', 'default', 'e5edda84e0bb')

The Mercurial version detection calls out to 'hg' rather than parsing
the Mercurial dirstate directly. There are two reasons for this:

1) The Mercurial dirstate is a binary file, and its format is an
   implementation detail of Mercurial.
2) Tags are used in the output, and the Mercurial logic for
   determining what tags to apply is fairly complex and often extended
   by extensions.

--- a/pypy/module/sys/__init__.py
+++ b/pypy/module/sys/__init__.py
@@ -59,6 +59,7 @@ class Module(MixedModule):
         'pypy_version_info'     : 'version.get_pypy_version_info(space)',
         'pypy_svn_url'          : 'version.get_svn_url(space)',
         'subversion'            : 'version.get_subversion_info(space)',
+        '_mercurial'            : 'version.get_mercurial_info(space)',
         'hexversion'            : 'version.get_hexversion(space)',
 
         'displayhook'           : 'hook.displayhook', 

--- a/pypy/module/sys/version.py
+++ b/pypy/module/sys/version.py
@@ -84,6 +84,39 @@ def get_subversion_info(space):
                            space.wrap(svnbranch),
                            space.wrap(str(svn_revision()))])
 
+def get_mercurial_info(space):
+    '''Obtain Mercurial version information by invoking the 'hg' command.'''
+    # TODO: support extracting from .hg_archival.txt
+    import py
+    from subprocess import Popen, PIPE
+
+    pypyroot = os.path.abspath(os.path.join(pypydir, '..'))
+    hgexe = py.path.local.sysfind('hg')
+
+    if hgexe and os.path.isdir(os.path.join(pypyroot, '.hg')):
+        env = dict(os.environ)
+        # get Mercurial into scripting mode
+        env['HGPLAIN'] = '1'
+        # disable user configuration, extensions, etc.
+        env['HGRCPATH'] = os.devnull
+
+        p = Popen([str(hgexe), 'id', '-i', pypyroot], stdout=PIPE, env=env)
+        hgid = p.stdout.read().strip()
+
+        p = Popen([str(hgexe), 'id', '-t', pypyroot], stdout=PIPE, env=env)
+        hgtag = p.stdout.read().strip().split()[0]
+
+        if hgtag == 'tip':
+            # use the branch instead
+            p = Popen([str(hgexe), 'id', '-b', pypyroot], stdout=PIPE, env=env)
+            hgtag = p.stdout.read().strip()
+
+        return space.newtuple([space.wrap('PyPy'),
+                               space.wrap(hgtag),
+                               space.wrap(hgid)])
+    else:
+        return space.w_None
+
 def tuple2hex(ver):
     d = {'alpha':     0xA,
          'beta':      0xB,



More information about the Pypy-commit mailing list