[pypy-svn] r80036 - pypy/trunk/pypy/module/sys

danchr at codespeak.net danchr at codespeak.net
Mon Dec 13 16:33:41 CET 2010


Author: danchr
Date: Mon Dec 13 16:33:39 2010
New Revision: 80036

Modified:
   pypy/trunk/pypy/module/sys/__init__.py
   pypy/trunk/pypy/module/sys/version.py
Log:
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.

Modified: pypy/trunk/pypy/module/sys/__init__.py
==============================================================================
--- pypy/trunk/pypy/module/sys/__init__.py	(original)
+++ pypy/trunk/pypy/module/sys/__init__.py	Mon Dec 13 16:33:39 2010
@@ -59,6 +59,7 @@
         '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', 

Modified: pypy/trunk/pypy/module/sys/version.py
==============================================================================
--- pypy/trunk/pypy/module/sys/version.py	(original)
+++ pypy/trunk/pypy/module/sys/version.py	Mon Dec 13 16:33:39 2010
@@ -84,6 +84,39 @@
                            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