summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/testinstall.py
blob: 5e5288bfbd21fd108febc307820cb0694f3e42b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
##############################################################################
# Copyright (c) 2013, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Written by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://scalability-llnl.github.io/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (as published by
# the Free Software Foundation) version 2.1 dated February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
from external import argparse
import xml.etree.ElementTree as ET
import itertools

import llnl.util.tty as tty
from llnl.util.filesystem import *

import spack
import spack.cmd

description = "Build and install packages"

def setup_parser(subparser):
    #subparser.add_argument(
    #    '-i', '--ignore-dependencies', action='store_true', dest='ignore_deps',
    #    help="Do not try to install dependencies of requested packages.")
    
    subparser.add_argument(
        '-j', '--jobs', action='store', type=int,
        help="Explicitly set number of make jobs.  Default is #cpus.")
        
    #always false for test
    #subparser.add_argument(
    #    '--keep-prefix', action='store_true', dest='keep_prefix',
    #    help="Don't remove the install prefix if installation fails.")
    
    #always true for test
    #subparser.add_argument(
    #    '--keep-stage', action='store_true', dest='keep_stage',
    #    help="Don't remove the build stage if installation succeeds.")
    
    subparser.add_argument(
        '-n', '--no-checksum', action='store_true', dest='no_checksum',
        help="Do not check packages against checksum")
    subparser.add_argument(
        '-v', '--verbose', action='store_true', dest='verbose',
        help="Display verbose build output while installing.")
    
    #subparser.add_argument(
    #    '--fake', action='store_true', dest='fake',
    #    help="Fake install.  Just remove the prefix and touch a fake file in it.")
    
    subparser.add_argument(
        'output', help="test output goes in this file")
    
    subparser.add_argument(
        'package', help="spec of package to install")


class JunitResultFormat(object):
    def __init__(self):
        self.root = ET.Element('testsuite')
        self.tests = []
        
    def addTest(self, buildId, passed=True, buildInfo=None):
        self.tests.append((buildId, passed, buildInfo))
    
    def writeTo(self, stream):
        self.root.set('tests', '{0}'.format(len(self.tests)))
        for buildId, passed, buildInfo in self.tests:
            testcase = ET.SubElement(self.root, 'testcase')
            testcase.set('classname', buildId.name)
            testcase.set('name', buildId.stringId())
            if not passed:
                failure = ET.SubElement(testcase, 'failure')
                failure.set('type', "Build Error")
                failure.text = buildInfo
        ET.ElementTree(self.root).write(stream)


class BuildId(object):
    def __init__(self, name, version, hashId):
        self.name = name
        self.version = version
        self.hashId = hashId
    
    def stringId(self):
        return "-".join(str(x) for x in (self.name, self.version, self.hashId))


def createTestOutput(spec, handled, output):
    if spec in handled:
        return handled[spec]
    
    childSuccesses = list(createTestOutput(dep, handled, output) 
            for dep in spec.dependencies.itervalues())
    package = spack.db.get(spec)
    handled[spec] = package.installed
    
    if all(childSuccesses):
        bId = BuildId(spec.name, spec.version, spec.dag_hash())

        if package.installed:
            buildLogPath = spack.install_layout.build_log_path(spec)
        else:
            #TODO: search recursively under stage.path instead of only within
            #    stage.source_path
            buildLogPath = join_path(package.stage.source_path, 'spack-build.out')            

        with open(buildLogPath, 'rb') as F:
            buildLog = F.read() #TODO: this may not return all output
            #TODO: add the whole build log? it could be several thousand
            #    lines. It may be better to look for errors.
            output.addTest(bId, package.installed, buildLogPath + '\n' +
                spec.to_yaml() + buildLog)
    #TODO: create a failed test if a dependency didn't install?

    return handled[spec]


def testinstall(parser, args):
    if not args.package:
        tty.die("install requires a package argument")

    if args.jobs is not None:
        if args.jobs <= 0:
            tty.die("The -j option must be a positive integer!")

    if args.no_checksum:
        spack.do_checksum = False        # TODO: remove this global.

    #TODO: should a single argument be wrapped in a list?
    specs = spack.cmd.parse_specs(args.package, concretize=True)
    newInstalls = set()
    for spec in itertools.chain.from_iterable(spec.traverse() 
            for spec in specs):
        package = spack.db.get(spec)
        if not package.installed:
            newInstalls.add(spec)
    
    try:
        for spec in specs:
            package = spack.db.get(spec)
            if not package.installed:
                package.do_install(
                    keep_prefix=False,
                    keep_stage=False,
                    ignore_deps=False,
                    make_jobs=args.jobs,
                    verbose=args.verbose,
                    fake=False)
    finally:        
        #Find all packages that are not a dependency of another package
        topLevelNewInstalls = newInstalls - set(itertools.chain.from_iterable(
                spec.dependencies for spec in newInstalls))
        
        jrf = JunitResultFormat()
        handled = {}
        for spec in topLevelNewInstalls:
            createTestOutput(spec, handled, jrf)

        with open(args.output, 'wb') as F:
            jrf.writeTo(F)