Commits

Amaury Forgeot d'Arc committed 9a60739 Merge

Merge from default

Comments (0)

Files changed (5)

pypy/module/sys/test/test_sysmodule.py

         assert revision.isdigit()
 
     def test__mercurial(self):
-        info = sys._mercurial
-        print info
-        if info:
-            project, hgtag, hgid = info
-            assert project == 'PyPy'
-            assert hgtag  # no clue how to check something more :-/
-            assert hgid
+        import re
+        project, hgtag, hgid = sys._mercurial
+        assert project == 'PyPy'
+        # the tag or branch may be anything, including the empty string
+        assert isinstance(hgtag, str)
+        # the id is either nothing, or an id of 12 hash digits, with a possible
+        # suffix of '+' if there are local modifications
+        assert hgid == '' or re.match('[0-9a-f]{12}\+?', hgid)
 
     def test_trace_exec_execfile(self):
         found = []

pypy/tool/test/test_version.py

+import os, sys
+import py
+from pypy.tool.version import get_mercurial_info
+
+def test_get_mercurial_info():
+    assert get_mercurial_info(py.path.local.sysfind(
+        'completely broken mercurial'))
+    assert get_mercurial_info(os.devnull)
+    assert get_mercurial_info(sys.executable)

pypy/tool/version.py

 import pypy
 pypydir = os.path.dirname(os.path.abspath(pypy.__file__))
 
-
-def get_mercurial_info():
+def get_mercurial_info(hgexe=None):
     '''Obtain Mercurial version information by invoking the 'hg' command.'''
     # TODO: support extracting from .hg_archival.txt
 
     pypyroot = os.path.abspath(os.path.join(pypydir, '..'))
-    hgexe = py.path.local.sysfind('hg')
+    if hgexe is None:
+        hgexe = py.path.local.sysfind('hg')
 
-    if hgexe and os.path.isdir(os.path.join(pypyroot, '.hg')):
+    def maywarn(err):
+        if not err:
+            return
+
+        from pypy.tool.ansi_print import ansi_log
+        log = py.log.Producer("version")
+        py.log.setconsumer("version", ansi_log)
+        log.WARNING('Errors getting Mercurial information: %s' % err)
+
+    if not os.path.isdir(os.path.join(pypyroot, '.hg')):
+        maywarn('Not running from a Mercurial repository!')
+        return 'PyPy', '', ''
+    elif not hgexe:
+        maywarn('Cannot find Mercurial command!')
+        return 'PyPy', '', ''
+    else:
         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)
+        try:
+            p = Popen([str(hgexe), 'version', '-q'],
+                      stdout=PIPE, stderr=PIPE, env=env)
+        except OSError, e:
+            maywarn(e)
+            return 'PyPy', '', ''
+
+        if not p.stdout.read().startswith('Mercurial Distributed SCM'):
+            maywarn('command does not identify itself as Mercurial')
+            return 'PyPy', '', ''
+
+        p = Popen([str(hgexe), 'id', '-i', pypyroot],
+                  stdout=PIPE, stderr=PIPE, env=env)
         hgid = p.stdout.read().strip()
+        maywarn(p.stderr.read())
 
-        p = Popen([str(hgexe), 'id', '-t', pypyroot], stdout=PIPE, env=env)
-        hgtag = p.stdout.read().strip().split()[0]
+        p = Popen([str(hgexe), 'id', '-t', pypyroot],
+                  stdout=PIPE, stderr=PIPE, env=env)
+        hgtags = [t for t in p.stdout.read().strip().split() if t != 'tip']
+        maywarn(p.stderr.read())
 
-        if hgtag == 'tip':
+        if hgtags:
+            return 'PyPy', hgtags[0], hgid
+        else:
             # use the branch instead
-            p = Popen([str(hgexe), 'id', '-b', pypyroot], stdout=PIPE, env=env)
-            hgtag = p.stdout.read().strip()
+            p = Popen([str(hgexe), 'id', '-b', pypyroot],
+                      stdout=PIPE, stderr=PIPE, env=env)
+            hgbranch = p.stdout.read().strip()
+            maywarn(p.stderr.read())
 
-        return 'PyPy', hgtag, hgid
-    else:
-        return None
+            return 'PyPy', hgbranch, hgid

pypy/translator/goal/app_main.py

         break      # found!
     return newpath
 
-def setup_initial_paths(executable, ignore_environment=False, **extra):
+def setup_sys_executable(executable, nanos):
+    # a substituted os if we are translated
+    global os
+    os = nanos
     # find the full path to the executable, assuming that if there is no '/'
     # in the provided one then we must look along the $PATH
     if we_are_translated() and IS_WINDOWS and not executable.lower().endswith('.exe'):
                     break
     sys.executable = os.path.abspath(executable)
 
-    newpath = get_library_path(executable)
+def setup_initial_paths(ignore_environment=False, **extra):
+    newpath = get_library_path(sys.executable)
     readenv = not ignore_environment
     path = readenv and os.getenv('PYTHONPATH')
     if path:
            '"license" for more information.')
 
 def entry_point(executable, argv, nanos):
-    # a substituted os if we are translated
-    global os
-    os = nanos
+    setup_sys_executable(executable, nanos)
     try:
         cmdline = parse_command_line(argv)
     except CommandLineError, e:
         print_error(str(e))
         return 2
-    setup_initial_paths(executable, **cmdline)
+    setup_initial_paths(**cmdline)
     return run_command_line(**cmdline)
 
 

pypy/translator/goal/test2/test_app_main.py

         child.sendline("'' in sys.path")
         child.expect("True")
 
+    def test_help(self):
+        # test that -h prints the usage, including the name of the executable
+        # which should be /full/path/to/app_main.py in this case
+        child = self.spawn(['-h'])
+        child.expect(r'usage: .*app_main.py \[options\]')
+
     def test_run_script(self):
         child = self.spawn([demo_script])
         idx = child.expect(['hello', 'Python ', '>>> '])