Admin/mira.py
author haftmann
Wed, 14 Mar 2012 15:24:51 +0100
changeset 47791 d2b92739038b
parent 47071 4a892432e8f1
child 47794 89cc3dfb383b
permissions -rw-r--r--
rudimentary distribution build configuration
     1 """
     2     Test configuration descriptions for mira.
     3 """
     4 
     5 import os
     6 from os import path
     7 from glob import glob
     8 import subprocess
     9 from datetime import datetime
    10 import re
    11 
    12 import util
    13 from util import Lazy
    14 
    15 from mira.report import Report, Report_Content
    16 from mira.case import Case
    17 from mira.tools import tool
    18 from mira import schedule, misc
    19 from mira.environment import scheduler
    20 from mira import repositories
    21 
    22 # build and evaluation tools
    23 
    24 default_usedir_options = "$ISABELLE_USEDIR_OPTIONS -d pdf -g true -i true -t true"
    25 
    26 def prepare_isabelle_repository(loc_isabelle, loc_contrib, loc_dependency_heaps,
    27   usedir_options=default_usedir_options, more_settings=''):
    28 
    29     # prepare components
    30     loc_contrib = path.expanduser(loc_contrib)
    31     if not path.exists(loc_contrib):
    32         raise IOError('Bad file: %s' % loc_contrib)
    33     subprocess.check_call(['ln', '-s', loc_contrib, '%s/contrib' % loc_isabelle])
    34 
    35     contributed_components = path.join(loc_isabelle, 'Admin', 'contributed_components')
    36     if path.exists(contributed_components):
    37         components = []
    38         for component in util.readfile_lines(contributed_components):
    39             loc_component = path.join(loc_isabelle, component)
    40             if path.exists(loc_component):
    41                 components.append(loc_component)
    42         writer = open(path.join(loc_isabelle, 'etc', 'components'), 'a')
    43         for component in components:
    44             writer.write(component + '\n')
    45         writer.close()
    46 
    47     # provide existing dependencies
    48     if loc_dependency_heaps:
    49         isabelle_path = loc_dependency_heaps + '/$ISABELLE_IDENTIFIER:$ISABELLE_OUTPUT'
    50     else:
    51         isabelle_path = '$ISABELLE_OUTPUT'
    52 
    53     # patch settings
    54     extra_settings = '''
    55 ISABELLE_HOME_USER="$ISABELLE_HOME/home_user"
    56 ISABELLE_OUTPUT="$ISABELLE_HOME/heaps"
    57 ISABELLE_BROWSER_INFO="$ISABELLE_HOME/browser_info"
    58 ISABELLE_PATH="%s"
    59 
    60 ISABELLE_USEDIR_OPTIONS="%s"
    61 Z3_NON_COMMERCIAL="yes"
    62 %s
    63 ''' % (isabelle_path, usedir_options, more_settings)
    64 
    65     writer = open(path.join(loc_isabelle, 'etc', 'settings'), 'a')
    66     writer.write(extra_settings)
    67     writer.close()
    68 
    69 
    70 def isabelle_getenv(isabelle_home, var):
    71 
    72     _, out = env.run_process('%s/bin/isabelle' % isabelle_home, 'getenv', var)
    73     return out.split('=', 1)[1].strip()
    74 
    75 
    76 def extract_isabelle_run_timing(logdata):
    77 
    78     def to_secs(h, m, s):
    79         return (int(h) * 60 + int(m)) * 60 + int(s)
    80     pat = r'Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time'
    81     pat2 = r'Timing (\S+) \((\d+) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time, factor (\d+\.\d+)\)'
    82     t = dict((name, {'elapsed': to_secs(eh,em,es), 'cpu': to_secs(ch,cm,cs)})
    83              for name, eh, em, es, ch, cm, cs in re.findall(pat, logdata))
    84     for name, threads, elapsed, cpu, gc, factor in re.findall(pat2, logdata):
    85 
    86         if name not in t:
    87             t[name] = {}
    88 
    89         t[name]['threads'] = int(threads)
    90         t[name]['elapsed_inner'] = elapsed
    91         t[name]['cpu_inner'] = cpu
    92         t[name]['gc'] = gc
    93         t[name]['factor'] = factor
    94 
    95     return t
    96 
    97 
    98 def extract_isabelle_run_summary(logdata):
    99 
   100     re_error = re.compile(r'^(?:make: )?\*\*\* (.*)$', re.MULTILINE)
   101     summary = '\n'.join(re_error.findall(logdata))
   102     if summary == '':
   103         summary = 'ok'
   104 
   105     return summary
   106 
   107 
   108 def extract_image_size(isabelle_home):
   109     
   110     isabelle_output = isabelle_getenv(isabelle_home, 'ISABELLE_OUTPUT')
   111     return dict((p, path.getsize(path.join(isabelle_output, p))) for p in os.listdir(isabelle_output) if p != "log")
   112 
   113 def extract_report_data(isabelle_home, logdata):
   114 
   115     return {
   116         'timing': extract_isabelle_run_timing(logdata),
   117         'image_size': extract_image_size(isabelle_home) }
   118 
   119 
   120 @tool
   121 def import_isatest_log(env, conf, logfile):
   122 
   123     """Imports isatest log file as a report."""
   124 
   125     def the_match(pat, text, name):
   126         match = re.search(pat, text)
   127         if not match: raise Exception('No match found for ' + name)
   128         return match.groups()
   129 
   130     def parse_date(d):
   131         return datetime.strptime(d, '%a %b %d %H:%M:%S %Z %Y')
   132 
   133     log = util.readfile(logfile)
   134 
   135     (begin_date, host) = the_match(r'-+ starting test -+ ([^-]*) -+ (\S*)', log, 'start tag')
   136     (isabelle_version,) = the_match(r'Isabelle version: ([a-f0-9]{12})', log, 'Isabelle version')
   137     (success, end_date) = the_match(r'-+ test (successful|FAILED) -+ ([^-]*) -', log, 'end tag')
   138     summary = extract_isabelle_run_summary(log)
   139     data = {'timing': extract_isabelle_run_timing(log)}
   140     atts = {'log': Lazy.simple(log)}
   141 
   142     content = Report_Content(summary, host, parse_date(begin_date),
   143       parse_date(end_date), Lazy.simple(data), atts)
   144     revision = ('Isabelle', env.repositories.get('Isabelle')[isabelle_version].hex())
   145     case = Case(conf, [revision])
   146 
   147     env.report_db.put(case, (success == 'successful'), content)
   148 
   149 
   150 
   151 def isabelle_usedir(env, isa_path, isabelle_usedir_opts, base_image, dir_name):
   152 
   153     return env.run_process('%s/bin/isabelle' % isa_path, 'usedir',
   154         isabelle_usedir_opts, base_image, dir_name)
   155 
   156 
   157 def isabelle_dependency_only(env, case, paths, dep_paths, playground):
   158 
   159     isabelle_home = paths[0]
   160     result = path.join(isabelle_home, 'heaps')
   161     os.makedirs(result)
   162     for dep_path in dep_paths:
   163         subprocess.check_call(['cp', '-R'] + glob(dep_path + '/*') + [result])
   164 
   165     return (True, 'ok', {}, {}, result)
   166 
   167 
   168 def build_isabelle_image(subdir, base, img, env, case, paths, dep_paths, playground,
   169   usedir_options=default_usedir_options, more_settings=''):
   170 
   171     isabelle_home = paths[0]
   172     dep_path = dep_paths[0]
   173     prepare_isabelle_repository(isabelle_home, env.settings.contrib, dep_path,
   174       usedir_options=usedir_options, more_settings=more_settings)
   175     os.chdir(path.join(isabelle_home, 'src', subdir))
   176 
   177     (return_code, log) = isabelle_usedir(env, isabelle_home, '-b', base, img)
   178 
   179     result = path.join(isabelle_home, 'heaps')
   180 
   181     return (return_code == 0, extract_isabelle_run_summary(log),
   182       extract_report_data(isabelle_home, log), {'log': log}, result)
   183 
   184 
   185 def isabelle_make(subdir, env, case, paths, dep_paths, playground, usedir_options=default_usedir_options,
   186   more_settings='', target='all', keep_results=False):
   187 
   188     isabelle_home = paths[0]
   189     dep_path = dep_paths[0] if dep_paths else None
   190     prepare_isabelle_repository(isabelle_home, env.settings.contrib, dep_path,
   191       usedir_options=usedir_options, more_settings=more_settings)
   192     os.chdir(path.join(isabelle_home, subdir))
   193 
   194     (return_code, log) = env.run_process('%s/bin/isabelle' % isabelle_home, 'make', '-k', target)
   195 
   196     result = path.join(isabelle_home, 'heaps') if keep_results else None
   197 
   198     return (return_code == 0, extract_isabelle_run_summary(log),
   199       extract_report_data(isabelle_home, log), {'log': log}, result)
   200 
   201 
   202 def isabelle_makeall(env, case, paths, dep_paths, playground, usedir_options=default_usedir_options,
   203   more_settings='', target='all', make_options=()):
   204 
   205     if 'lxbroy10' in misc.hostnames():
   206         make_options += ('-j', '8')
   207         usedir_options += " -M 4 -q 2 -i false -d false"
   208         more_settings += '''
   209 ML_PLATFORM="x86_64-linux"
   210 ML_HOME="/home/polyml/polyml-5.4.1/x86_64-linux"
   211 ML_SYSTEM="polyml-5.4.1"
   212 ML_OPTIONS="-H 4000 --gcthreads 4"
   213 
   214 ISABELLE_GHC="/usr/bin/ghc"
   215 '''
   216 
   217     isabelle_home = paths[0]
   218     dep_path = dep_paths[0] if dep_paths else None
   219     prepare_isabelle_repository(isabelle_home, env.settings.contrib, dep_path,
   220       usedir_options=usedir_options, more_settings=more_settings)
   221     os.chdir(isabelle_home)
   222 
   223     (return_code, log) = env.run_process('%s/bin/isabelle' % isabelle_home, 'makeall', '-k', *(make_options + (target,)))
   224 
   225     return (return_code == 0, extract_isabelle_run_summary(log),
   226       extract_report_data(isabelle_home, log), {'log': log}, None)
   227 
   228 
   229 def make_pure(env, case, paths, dep_paths, playground, more_settings=''):
   230 
   231     isabelle_home = paths[0]
   232     prepare_isabelle_repository(isabelle_home, env.settings.contrib, '',
   233       more_settings=more_settings)
   234     os.chdir(path.join(isabelle_home, 'src', 'Pure'))
   235 
   236     (return_code, log) = env.run_process('%s/bin/isabelle' % isabelle_home, 'make', 'Pure')
   237 
   238     result = path.join(isabelle_home, 'heaps')
   239     return (return_code == 0, extract_isabelle_run_summary(log),
   240       {'timing': extract_isabelle_run_timing(log)}, {'log': log}, result)
   241 
   242 
   243 # Isabelle configurations
   244 
   245 @configuration(repos = [Isabelle], deps = [])
   246 def Pure(*args):
   247     """Pure image"""
   248     return make_pure(*args)
   249 
   250 @configuration(repos = [Isabelle], deps = [])
   251 def Pure_64(*args):
   252     """Pure image (64 bit)"""
   253     return make_pure(*args, more_settings='ML_PLATFORM=x86_64-linux')
   254 
   255 @configuration(repos = [Isabelle], deps = [(Pure, [0])])
   256 def HOL(*args):
   257     """HOL image"""
   258     return build_isabelle_image('HOL', 'Pure', 'HOL', *args)
   259 
   260 @configuration(repos = [Isabelle], deps = [(Pure_64, [0])])
   261 def HOL_64(*args):
   262     """HOL image (64 bit)"""
   263     return build_isabelle_image('HOL', 'Pure', 'HOL', *args, more_settings='ML_PLATFORM=x86_64-linux')
   264 
   265 @configuration(repos = [Isabelle], deps = [(HOL_64, [0])])
   266 def HOL_HOLCF_64(*args):
   267     """HOL-HOLCF image (64 bit)"""
   268     return build_isabelle_image('HOL/HOLCF', 'HOL', 'HOLCF', *args, more_settings='ML_PLATFORM=x86_64-linux')
   269 
   270 @configuration(repos = [Isabelle], deps = [(HOL_64, [0])])
   271 def HOL_Nominal_64(*args):
   272     """HOL-Nominal image (64 bit)"""
   273     return build_isabelle_image('HOL/Nominal', 'HOL', 'HOL-Nominal', *args, more_settings='ML_PLATFORM=x86_64-linux')
   274 
   275 @configuration(repos = [Isabelle], deps = [(HOL_64, [0])])
   276 def HOL_Word_64(*args):
   277     """HOL-Word image (64 bit)"""
   278     return build_isabelle_image('HOL/Word', 'HOL', 'HOL-Word', *args, more_settings='ML_PLATFORM=x86_64-linux')
   279 
   280 @configuration(repos = [Isabelle], deps = [
   281     (HOL_64, [0]),
   282     (HOL_HOLCF_64, [0]),
   283     (HOL_Nominal_64, [0]),
   284     (HOL_Word_64, [0])
   285   ])
   286 def AFP_images(*args):
   287     """Isabelle images needed for the AFP (64 bit)"""
   288     return isabelle_dependency_only(*args)
   289 
   290 @configuration(repos = [Isabelle], deps = [])
   291 def Isabelle_makeall(*args):
   292     """Isabelle makeall"""
   293     return isabelle_makeall(*args)
   294 
   295 
   296 # Document and Distribution Build
   297 
   298 @configuration(repos = [Isabelle], deps = [])
   299 def Distribution(env, case, paths, dep_paths, playground):
   300     """Document and Distribution Build"""
   301     ## FIXME This is rudimentary; study Admin/CHECKLIST to complete this configuration accordingly
   302     isabelle_home = paths[0]
   303     makedist = path.join(isabelle_home, 'Admin', 'makedist')
   304     (return_code, log) = env.run_process(makedist,
   305       REPOS = repositories.get(Isabelle).local_path, DISTPREFIX = os.getcwd())
   306     return (return_code == 0, '', ## FIXME might add summary here
   307       {}, {'log': log}, None) ## FIXME might add proper result here
   308 
   309 
   310 # Mutabelle configurations
   311 
   312 def invoke_mutabelle(theory, env, case, paths, dep_paths, playground):
   313 
   314     """Mutant testing for counterexample generators in Isabelle"""
   315 
   316     (loc_isabelle,) = paths
   317     (dep_isabelle,) = dep_paths
   318     more_settings = '''
   319 ISABELLE_GHC="/usr/bin/ghc"
   320 '''
   321     prepare_isabelle_repository(loc_isabelle, env.settings.contrib, dep_isabelle,
   322       more_settings = more_settings)
   323     os.chdir(loc_isabelle)
   324     
   325     (return_code, log) = env.run_process('bin/isabelle',
   326       'mutabelle', '-O', playground, theory)
   327     
   328     try:
   329         mutabelle_log = util.readfile(path.join(playground, 'log'))
   330     except IOError:
   331         mutabelle_log = ''
   332 
   333     mutabelle_data = dict(
   334         (tool, {'counterexample': c, 'no_counterexample': n, 'timeout': t, 'error': e})
   335         for tool, c, n, t, e in re.findall(r'(\S+)\s+: C: (\d+) N: (\d+) T: (\d+) E: (\d+)', log))
   336 
   337     return (return_code == 0 and mutabelle_log != '', extract_isabelle_run_summary(log),
   338       {'mutabelle_results': {theory: mutabelle_data}},
   339       {'log': log, 'mutabelle_log': mutabelle_log}, None)
   340 
   341 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   342 def Mutabelle_Relation(*args):
   343     """Mutabelle regression suite on Relation theory"""
   344     return invoke_mutabelle('Relation', *args)
   345 
   346 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   347 def Mutabelle_List(*args):
   348     """Mutabelle regression suite on List theory"""
   349     return invoke_mutabelle('List', *args)
   350 
   351 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   352 def Mutabelle_Set(*args):
   353     """Mutabelle regression suite on Set theory"""
   354     return invoke_mutabelle('Set', *args)
   355 
   356 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   357 def Mutabelle_Map(*args):
   358     """Mutabelle regression suite on Map theory"""
   359     return invoke_mutabelle('Map', *args)
   360 
   361 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   362 def Mutabelle_Divides(*args):
   363     """Mutabelle regression suite on Divides theory"""
   364     return invoke_mutabelle('Divides', *args)
   365 
   366 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   367 def Mutabelle_MacLaurin(*args):
   368     """Mutabelle regression suite on MacLaurin theory"""
   369     return invoke_mutabelle('MacLaurin', *args)
   370 
   371 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   372 def Mutabelle_Fun(*args):
   373     """Mutabelle regression suite on Fun theory"""
   374     return invoke_mutabelle('Fun', *args)
   375 
   376 mutabelle_confs = 'Mutabelle_Relation Mutabelle_List Mutabelle_Set Mutabelle_Map Mutabelle_Divides Mutabelle_MacLaurin Mutabelle_Fun'.split(' ')
   377 
   378 @scheduler()
   379 def mutabelle_scheduler(env):
   380     """Scheduler for Mutabelle."""
   381     return schedule.age_scheduler(env, 'Isabelle', mutabelle_confs)
   382 
   383 # Judgement Day configurations
   384 
   385 judgement_day_provers = ('e', 'spass', 'vampire', 'z3', 'cvc3', 'yices')
   386 
   387 def judgement_day(base_path, theory, opts, env, case, paths, dep_paths, playground):
   388     """Judgement Day regression suite"""
   389 
   390     isa = paths[0]
   391     dep_path = dep_paths[0]
   392 
   393     os.chdir(path.join(playground, '..', base_path)) # Mirabelle requires specific cwd
   394     prepare_isabelle_repository(isa, env.settings.contrib, dep_path)
   395 
   396     output = {}
   397     success_rates = {}
   398     some_success = False
   399 
   400     for atp in judgement_day_provers:
   401 
   402         log_dir = path.join(playground, 'mirabelle_log_' + atp)
   403         os.makedirs(log_dir)
   404 
   405         cmd = ('%s/bin/isabelle mirabelle -q -O %s sledgehammer[prover=%s,%s] %s.thy'
   406                % (isa, log_dir, atp, opts, theory))
   407 
   408         os.system(cmd)
   409         output[atp] = util.readfile(path.join(log_dir, theory + '.log'))
   410 
   411         percentages = list(re.findall(r'Success rate: (\d+)%', output[atp]))
   412         if len(percentages) == 2:
   413             success_rates[atp] = {
   414                 'sledgehammer': int(percentages[0]),
   415                 'metis': int(percentages[1])}
   416             if success_rates[atp]['sledgehammer'] > 0:
   417                 some_success = True
   418         else:
   419             success_rates[atp] = {}
   420 
   421 
   422     data = {'success_rates': success_rates}
   423     raw_attachments = dict((atp + "_output", output[atp]) for atp in judgement_day_provers)
   424     # FIXME: summary?
   425     return (some_success, '', data, raw_attachments, None)
   426 
   427 
   428 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   429 def JD_NS(*args):
   430     """Judgement Day regression suite NS"""
   431     return judgement_day('Isabelle/src/HOL/Auth', 'NS_Shared', 'prover_timeout=10', *args)
   432 
   433 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   434 def JD_FTA(*args):
   435     """Judgement Day regression suite FTA"""
   436     return judgement_day('Isabelle/src/HOL/Library', 'Fundamental_Theorem_Algebra', 'prover_timeout=10', *args)
   437 
   438 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   439 def JD_Hoare(*args):
   440     """Judgement Day regression suite Hoare"""
   441     return judgement_day('Isabelle/src/HOL/IMPP', 'Hoare', 'prover_timeout=10', *args)
   442 
   443 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   444 def JD_SN(*args):
   445     """Judgement Day regression suite SN"""
   446     return judgement_day('Isabelle/src/HOL/Proofs/Lambda', 'StrongNorm', 'prover_timeout=10', *args)
   447 
   448 
   449 JD_confs = 'JD_NS JD_FTA JD_Hoare JD_SN JD_Arrow JD_FFT JD_Jinja JD_QE JD_S2S'.split(' ')
   450 
   451 @scheduler()
   452 def judgement_day_scheduler(env):
   453     """Scheduler for Judgement Day."""
   454     return schedule.age_scheduler(env, 'Isabelle', JD_confs)
   455 
   456 
   457 # SML/NJ
   458 
   459 smlnj_settings = '''
   460 ML_SYSTEM=smlnj
   461 ML_HOME="/home/smlnj/110.72/bin"
   462 ML_OPTIONS="@SMLdebug=/dev/null @SMLalloc=256"
   463 ML_PLATFORM=$(eval $("$ML_HOME/.arch-n-opsys" 2>/dev/null); echo "$HEAP_SUFFIX")
   464 '''
   465 
   466 @configuration(repos = [Isabelle], deps = [])
   467 def SML_HOL(*args):
   468     """HOL image built with SML/NJ"""
   469     return isabelle_make('src/HOL', *args, more_settings=smlnj_settings, target='HOL', keep_results=True)
   470 
   471 @configuration(repos = [Isabelle], deps = [])
   472 def SML_makeall(*args):
   473     """Makeall built with SML/NJ"""
   474     return isabelle_makeall(*args, more_settings=smlnj_settings, target='smlnj', make_options=('-j', '3'))
   475 
   476 
   477 
   478 # Importer
   479 
   480 @configuration(repos = ['Hollight'], deps = [])
   481 def Hollight_proof_objects(env, case, paths, dep_paths, playground):
   482     """Build proof object bundle from HOL Light"""
   483 
   484     hollight_home = paths[0]
   485     os.chdir(os.path.join(hollight_home, 'Proofrecording', 'hol_light'))
   486 
   487     subprocess.check_call(['make'])
   488     (return_code, _) = util.run_process.run_process(
   489        '''echo -e '#use "hol.ml";;\n export_saved_proofs None;;' | ocaml''',
   490        environment={'HOLPROOFEXPORTDIR': './proofs_extended', 'HOLPROOFOBJECTS': 'EXTENDED'},
   491        shell=True)
   492     subprocess.check_call('tar -czf proofs_extended.tar.gz proofs_extended'.split(' '))
   493     subprocess.check_call(['mv', 'proofs_extended.tar.gz', playground])
   494 
   495     return (return_code == 0, '', {}, {}, playground)
   496 
   497 
   498 @configuration(repos = [Isabelle, 'Hollight'], deps = [(Hollight_proof_objects, [1])])
   499 def HOL_Generate_HOLLight(env, case, paths, dep_paths, playground):
   500     """Generated theories by HOL Light import"""
   501 
   502     os.chdir(playground)
   503     subprocess.check_call(['tar', '-xzf', path.join(dep_paths[0], 'proofs_extended.tar.gz')])
   504     proofs_dir = path.join(playground, 'proofs_extended')
   505 
   506     return isabelle_make('src/HOL', env, case, paths, dep_paths, playground,
   507       more_settings=('HOL4_PROOFS="%s"' % proofs_dir), target='HOL-Generate-HOLLight')