contrib/python3-ratchet.py
changeset 32873 28f429d19a71
child 36618 3790610c2793
equal deleted inserted replaced
32872:7a877e569ed6 32873:28f429d19a71
       
     1 # Copyright 2012 Facebook
       
     2 #
       
     3 # This software may be used and distributed according to the terms of the
       
     4 # GNU General Public License version 2 or any later version.
       
     5 """Find tests that newly pass under Python 3.
       
     6 
       
     7 The approach is simple: we maintain a whitelist of Python 3 passing
       
     8 tests in the repository, and periodically run all the /other/ tests
       
     9 and look for new passes. Any newly passing tests get automatically
       
    10 added to the whitelist.
       
    11 
       
    12 You probably want to run it like this:
       
    13 
       
    14   $ cd tests
       
    15   $ python3 ../contrib/python3-ratchet.py \
       
    16   >   --working-tests=../contrib/python3-whitelist
       
    17 """
       
    18 from __future__ import print_function
       
    19 from __future__ import absolute_import
       
    20 
       
    21 import argparse
       
    22 import json
       
    23 import os
       
    24 import subprocess
       
    25 import sys
       
    26 
       
    27 _hgenv = dict(os.environ)
       
    28 _hgenv.update({
       
    29     'HGPLAIN': '1',
       
    30     })
       
    31 
       
    32 _HG_FIRST_CHANGE = '9117c6561b0bd7792fa13b50d28239d51b78e51f'
       
    33 
       
    34 def _runhg(*args):
       
    35     return subprocess.check_output(args, env=_hgenv)
       
    36 
       
    37 def _is_hg_repo(path):
       
    38     return _runhg('hg', 'log', '-R', path,
       
    39                   '-r0', '--template={node}').strip() == _HG_FIRST_CHANGE
       
    40 
       
    41 def _py3default():
       
    42     if sys.version_info[0] >= 3:
       
    43         return sys.executable
       
    44     return 'python3'
       
    45 
       
    46 def main(argv=()):
       
    47     p = argparse.ArgumentParser()
       
    48     p.add_argument('--working-tests',
       
    49                    help='List of tests that already work in Python 3.')
       
    50     p.add_argument('--commit-to-repo',
       
    51                    help='If set, commit newly fixed tests to the given repo')
       
    52     p.add_argument('-j', default=os.sysconf(r'SC_NPROCESSORS_ONLN'), type=int,
       
    53                    help='Number of parallel tests to run.')
       
    54     p.add_argument('--python3', default=_py3default(),
       
    55                    help='python3 interpreter to use for test run')
       
    56     p.add_argument('--commit-user',
       
    57                    default='python3-ratchet@mercurial-scm.org',
       
    58                    help='Username to specify when committing to a repo.')
       
    59     opts = p.parse_args(argv)
       
    60     if opts.commit_to_repo:
       
    61         if not _is_hg_repo(opts.commit_to_repo):
       
    62             print('abort: specified repository is not the hg repository')
       
    63             sys.exit(1)
       
    64     if not opts.working_tests or not os.path.isfile(opts.working_tests):
       
    65         print('abort: --working-tests must exist and be a file (got %r)' %
       
    66               opts.working_tests)
       
    67         sys.exit(1)
       
    68     elif opts.commit_to_repo:
       
    69         root = _runhg('hg', 'root').strip()
       
    70         if not opts.working_tests.startswith(root):
       
    71             print('abort: if --commit-to-repo is given, '
       
    72                   '--working-tests must be from that repo')
       
    73             sys.exit(1)
       
    74     try:
       
    75         subprocess.check_call([opts.python3, '-c',
       
    76                                'import sys ; '
       
    77                                'assert ((3, 5) <= sys.version_info < (3, 6) '
       
    78                                'or sys.version_info >= (3, 6, 2))'])
       
    79     except subprocess.CalledProcessError:
       
    80         print('warning: Python 3.6.0 and 3.6.1 have '
       
    81               'a bug which breaks Mercurial')
       
    82         print('(see https://bugs.python.org/issue29714 for details)')
       
    83         # TODO(augie): uncomment exit when Python 3.6.2 is available
       
    84         # sys.exit(1)
       
    85 
       
    86     rt = subprocess.Popen([opts.python3, 'run-tests.py', '-j', str(opts.j),
       
    87                            '--blacklist', opts.working_tests, '--json'])
       
    88     rt.wait()
       
    89     with open('report.json') as f:
       
    90         data = f.read()
       
    91     report = json.loads(data.split('=', 1)[1])
       
    92     newpass = set()
       
    93     for test, result in report.items():
       
    94         if result['result'] != 'success':
       
    95             continue
       
    96         # A new passing test! Huzzah!
       
    97         newpass.add(test)
       
    98     if newpass:
       
    99         # We already validated the repo, so we can just dive right in
       
   100         # and commit.
       
   101         if opts.commit_to_repo:
       
   102             print(len(newpass), 'new passing tests on Python 3!')
       
   103             with open(opts.working_tests) as f:
       
   104                 oldpass = {l for l in f.read().splitlines() if l}
       
   105             with open(opts.working_tests, 'w') as f:
       
   106                 for p in sorted(oldpass | newpass):
       
   107                     f.write('%s\n' % p)
       
   108             _runhg('hg', 'commit', '-R', opts.commit_to_repo,
       
   109                    '--user', opts.commit_user,
       
   110                    '--message', 'python3: expand list of passing tests')
       
   111         else:
       
   112             print('Newly passing tests:', '\n'.join(sorted(newpass)))
       
   113             sys.exit(2)
       
   114 
       
   115 if __name__ == '__main__':
       
   116     main(sys.argv[1:])