You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.4 KiB

#!/usr/bin/env python
# Copyright (c) 2015-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import os
import logging
import subprocess
import traceback
def get_build_output(build_cmd):
from inferlib import utils
# TODO make it return generator to be able to handle large builds
proc = subprocess.Popen(build_cmd, stdout=subprocess.PIPE)
(out_chars, err_chars) = proc.communicate()
out = utils.decode(out_chars) if out_chars is not None else ''
err = utils.decode(err_chars) if err_chars is not None else ''
if proc.returncode != os.EX_OK:
[python] return more errors when external commands fail Summary: Better to bail early than have mysterious failures. These used to have the wrong error messages: ``` $ cd examples/android_hello/ $ infer -- ./gradlew rubbish Capturing in gradle mode... Running and capturing gradle compilation... 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception. 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong: 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Task 'rubbish' not found in root project 'android_hello'. 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try: 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Run gradlew tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Nothing to compile. Try running `./gradlew clean` first. ERROR: (Unix.Exit_or_signal (Exit_non_zero 66)) $ cd - $ infer --flavors --Xbuck --keep-going -- buck build //toto:toto Capturing in buck mode... moving files in buck-out to buck-out/.trash/buck-outwiiL6N Not using buckd because watchman isn't installed. BUILD FAILED: No build file at toto/BUCK when resolving target //toto:toto#infer. Buck failed, but continuing the analysis because --keep-going was passed Not using buckd because watchman isn't installed. No build file at toto/BUCK when resolving target //toto:toto#infer. Traceback (most recent call last): File "/home/jul/infer/infer/bin/../lib/python/infer.py", line 186, in <module> main() File "/home/jul/infer/infer/bin/../lib/python/infer.py", line 168, in main capture_exitcode = imported_module.gen_instance(args, cmd).capture() File "/home/jul/infer/infer/lib/python/inferlib/capture/buck.py", line 89, in capture return self.capture_with_flavors() File "/home/jul/infer/infer/lib/python/inferlib/capture/buck.py", line 242, in capture_with_flavors result_paths = self._get_analysis_result_paths() File "/home/jul/infer/infer/lib/python/inferlib/capture/buck.py", line 148, in _get_analysis_result_paths out = [x.split(None, 1)[1] for x in buck_output.strip().split('\n')] IndexError: list index out of range ERROR: (Unix.Exit_or_signal (Exit_non_zero 1)) $ cd infer/build_systems/ant/ && infer -- ant rubbish Capturing in ant mode... BUILD FAILED Target "rubbish" does not exist in the project "null". at org.apache.tools.ant.Project.tsort(Project.java:1929) at org.apache.tools.ant.Project.topoSort(Project.java:1837) at org.apache.tools.ant.Project.topoSort(Project.java:1800) at org.apache.tools.ant.Project.executeTarget(Project.java:1376) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1260) at org.apache.tools.ant.Main.runBuild(Main.java:857) at org.apache.tools.ant.Main.startAnt(Main.java:236) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:287) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:113) Total time: 0 seconds Nothing to compile. Try running `ant clean` first. ERROR: (Unix.Exit_or_signal (Exit_non_zero 66)) ``` Now we fail better, for instance: ``` $ infer --flavors --Xbuck --keep-going -- buck build //toto:toto Capturing in buck mode... moving files in buck-out to buck-out/.trash/buck-outHag8Ji Not using buckd because watchman isn't installed. BUILD FAILED: No build file at toto/BUCK when resolving target //toto:toto#infer. Buck failed, but continuing the analysis because --keep-going was passed Not using buckd because watchman isn't installed. No build file at toto/BUCK when resolving target //toto:toto#infer. ERROR: (Unix.Exit_or_signal (Exit_non_zero 70)) $ cd infer/build_systems/ant/ && infer -- ant rubbish Capturing in ant mode... BUILD FAILED Target "rubbish" does not exist in the project "null". at org.apache.tools.ant.Project.tsort(Project.java:1929) at org.apache.tools.ant.Project.topoSort(Project.java:1837) at org.apache.tools.ant.Project.topoSort(Project.java:1800) at org.apache.tools.ant.Project.executeTarget(Project.java:1376) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1260) at org.apache.tools.ant.Main.runBuild(Main.java:857) at org.apache.tools.ant.Main.startAnt(Main.java:236) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:287) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:113) Total time: 0 seconds ERROR: couldn't run compilation command `['ant', '-verbose', u'rubbish']` ERROR: (Unix.Exit_or_signal (Exit_non_zero 1)) ``` Reviewed By: jeremydubreil Differential Revision: D5469607 fbshipit-source-id: a3eb05c
7 years ago
utils.stderr(
'ERROR: couldn\'t run compilation command `{}`'.format(build_cmd))
logging.error(
'ERROR: couldn\'t run compilation command `{}`:\n\
*** stdout:\n{}\n*** stderr:\n{}\n'
.format(build_cmd, out, err))
return (proc.returncode, (out, err))
def run_compilation_commands(cmds):
"""runs all the commands passed as argument
"""
# TODO call it in parallel
[python] return more errors when external commands fail Summary: Better to bail early than have mysterious failures. These used to have the wrong error messages: ``` $ cd examples/android_hello/ $ infer -- ./gradlew rubbish Capturing in gradle mode... Running and capturing gradle compilation... 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception. 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong: 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Task 'rubbish' not found in root project 'android_hello'. 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try: 10:46:56.730 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Run gradlew tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Nothing to compile. Try running `./gradlew clean` first. ERROR: (Unix.Exit_or_signal (Exit_non_zero 66)) $ cd - $ infer --flavors --Xbuck --keep-going -- buck build //toto:toto Capturing in buck mode... moving files in buck-out to buck-out/.trash/buck-outwiiL6N Not using buckd because watchman isn't installed. BUILD FAILED: No build file at toto/BUCK when resolving target //toto:toto#infer. Buck failed, but continuing the analysis because --keep-going was passed Not using buckd because watchman isn't installed. No build file at toto/BUCK when resolving target //toto:toto#infer. Traceback (most recent call last): File "/home/jul/infer/infer/bin/../lib/python/infer.py", line 186, in <module> main() File "/home/jul/infer/infer/bin/../lib/python/infer.py", line 168, in main capture_exitcode = imported_module.gen_instance(args, cmd).capture() File "/home/jul/infer/infer/lib/python/inferlib/capture/buck.py", line 89, in capture return self.capture_with_flavors() File "/home/jul/infer/infer/lib/python/inferlib/capture/buck.py", line 242, in capture_with_flavors result_paths = self._get_analysis_result_paths() File "/home/jul/infer/infer/lib/python/inferlib/capture/buck.py", line 148, in _get_analysis_result_paths out = [x.split(None, 1)[1] for x in buck_output.strip().split('\n')] IndexError: list index out of range ERROR: (Unix.Exit_or_signal (Exit_non_zero 1)) $ cd infer/build_systems/ant/ && infer -- ant rubbish Capturing in ant mode... BUILD FAILED Target "rubbish" does not exist in the project "null". at org.apache.tools.ant.Project.tsort(Project.java:1929) at org.apache.tools.ant.Project.topoSort(Project.java:1837) at org.apache.tools.ant.Project.topoSort(Project.java:1800) at org.apache.tools.ant.Project.executeTarget(Project.java:1376) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1260) at org.apache.tools.ant.Main.runBuild(Main.java:857) at org.apache.tools.ant.Main.startAnt(Main.java:236) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:287) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:113) Total time: 0 seconds Nothing to compile. Try running `ant clean` first. ERROR: (Unix.Exit_or_signal (Exit_non_zero 66)) ``` Now we fail better, for instance: ``` $ infer --flavors --Xbuck --keep-going -- buck build //toto:toto Capturing in buck mode... moving files in buck-out to buck-out/.trash/buck-outHag8Ji Not using buckd because watchman isn't installed. BUILD FAILED: No build file at toto/BUCK when resolving target //toto:toto#infer. Buck failed, but continuing the analysis because --keep-going was passed Not using buckd because watchman isn't installed. No build file at toto/BUCK when resolving target //toto:toto#infer. ERROR: (Unix.Exit_or_signal (Exit_non_zero 70)) $ cd infer/build_systems/ant/ && infer -- ant rubbish Capturing in ant mode... BUILD FAILED Target "rubbish" does not exist in the project "null". at org.apache.tools.ant.Project.tsort(Project.java:1929) at org.apache.tools.ant.Project.topoSort(Project.java:1837) at org.apache.tools.ant.Project.topoSort(Project.java:1800) at org.apache.tools.ant.Project.executeTarget(Project.java:1376) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1260) at org.apache.tools.ant.Main.runBuild(Main.java:857) at org.apache.tools.ant.Main.startAnt(Main.java:236) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:287) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:113) Total time: 0 seconds ERROR: couldn't run compilation command `['ant', '-verbose', u'rubbish']` ERROR: (Unix.Exit_or_signal (Exit_non_zero 1)) ``` Reviewed By: jeremydubreil Differential Revision: D5469607 fbshipit-source-id: a3eb05c
7 years ago
if cmds is None or len(cmds) == 0:
# nothing to capture, the OCaml side will detect that and
# display the appropriate warning
return os.EX_OK
for cmd in cmds:
if cmd.start() != os.EX_OK:
return os.EX_SOFTWARE
return os.EX_OK
def run_cmd_ignore_fail(cmd):
try:
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except:
return 'calling {cmd} failed\n{trace}'.format(
cmd=' '.join(cmd),
trace=traceback.format_exc())
def log_java_version():
java_version = run_cmd_ignore_fail(['java', '-version'])
javac_version = run_cmd_ignore_fail(['javac', '-version'])
logging.info('java versions:\n%s%s', java_version, javac_version)
def base_argparser(description, module_name):
def _func(group_name=module_name):
"""This creates an empty argparser for the module, which provides only
description/usage information and no arguments."""
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument_group(
'{grp} module'.format(grp=group_name),
description=description,
)
return parser
return _func