Beispiel #1
0
def android_build(src, reports_dir, lang_tools):
    """
    Automatically build android project

    :param src: Source directory
    :param reports_dir: Reports directory to store any logs
    :param lang_tools: Language specific build tools

    :return: boolean status from the build. True if the command executed successfully. False otherwise
    """
    if not os.getenv("ANDROID_SDK_ROOT") and not os.getenv("ANDROID_HOME"):
        LOG.info(
            "ANDROID_SDK_ROOT or ANDROID_HOME should be set for automatically building android projects"
        )
        return False
    lang_tools = build_tools_map.get("android")
    env = get_env()
    gradle_files = [p.as_posix() for p in Path(src).rglob("build.gradle")]
    gradle_kts_files = [
        p.as_posix() for p in Path(src).rglob("build.gradle.kts")
    ]
    if gradle_files or gradle_kts_files:
        cmd_args = get_gradle_cmd(src, lang_tools.get("gradle"))
    cp = exec_tool("auto-build",
                   cmd_args,
                   src,
                   env=env,
                   stdout=subprocess.PIPE)
    if cp:
        LOG.debug(cp.stdout)
        return cp.returncode == 0
    return False
Beispiel #2
0
def java_build(src, reports_dir, lang_tools):
    """
    Automatically build java project

    :param src: Source directory
    :param reports_dir: Reports directory to store any logs
    :param lang_tools: Language specific build tools

    :return: boolean status from the build. True if the command executed successfully. False otherwise
    """
    cmd_args = []
    pom_files = [p.as_posix() for p in Path(src).rglob("pom.xml")]
    gradle_files = [p.as_posix() for p in Path(src).rglob("build.gradle")]
    sbt_files = [p.as_posix() for p in Path(src).rglob("build.sbt")]
    env = get_env()
    if pom_files:
        cmd_args = lang_tools.get("maven")
    elif gradle_files:
        cmd_args = get_gradle_cmd(src, lang_tools.get("gradle"))
    elif sbt_files:
        cmd_args = lang_tools.get("sbt")
    if not cmd_args:
        LOG.info(
            "Java auto build is supported only for maven or gradle based projects"
        )
        return False
    cp = exec_tool("auto-build",
                   cmd_args,
                   src,
                   env=env,
                   stdout=subprocess.PIPE)
    if cp:
        LOG.debug(cp.stdout)
        return cp.returncode == 0
    return False
Beispiel #3
0
def kotlin_build(src, reports_dir, lang_tools):
    """
    Automatically build kotlin project

    :param src: Source directory
    :param reports_dir: Reports directory to store any logs
    :param lang_tools: Language specific build tools

    :return: boolean status from the build. True if the command executed successfully. False otherwise
    """
    # Check if this is a android kotlin project
    gradle_kts_files = [p.as_posix() for p in Path(src).rglob("build.gradle.kts")]
    if find_files(src, "proguard-rules.pro", False, True) or find_files(
        src, "AndroidManifest.xml", False, True
    ):
        return android_build(src, reports_dir, lang_tools)
    if gradle_kts_files:
        cmd_args = get_gradle_cmd(src, lang_tools.get("gradle"))
        cp = exec_tool(
            "auto-build", cmd_args, src, env=get_env(), stdout=subprocess.PIPE
        )
        if cp:
            LOG.debug(cp.stdout)
            return cp.returncode == 0
    else:
        return java_build(src, reports_dir, lang_tools)
Beispiel #4
0
def exec_tool(  # scan:ignore
        tool_name,
        args,
        cwd=None,
        env=utils.get_env(),
        stdout=subprocess.DEVNULL):
    """
    Convenience method to invoke cli tools

    Args:
      tool_name Tool name
      args cli command and args
      cwd Current working directory
      env Environment variables
      stdout stdout configuration for run command

    Returns:
      CompletedProcess instance
    """
    with Progress(
            console=console,
            redirect_stderr=False,
            redirect_stdout=False,
            refresh_per_second=1,
    ) as progress:
        task = None
        try:
            env = use_java(env)
            LOG.debug('⚡︎ Executing {} "{}"'.format(tool_name, " ".join(args)))
            stderr = subprocess.DEVNULL
            if LOG.isEnabledFor(DEBUG):
                stderr = subprocess.STDOUT
            tool_verb = "Scanning with"
            if "init" in tool_name:
                tool_verb = "Initializing"
            elif "build" in tool_name:
                tool_verb = "Building with"
            task = progress.add_task("[green]" + tool_verb + " " + tool_name,
                                     total=100,
                                     start=False)
            cp = subprocess.run(
                args,
                stdout=stdout,
                stderr=stderr,
                cwd=cwd,
                env=env,
                check=False,
                shell=False,
                encoding="utf-8",
            )
            if cp and stdout == subprocess.PIPE:
                for line in cp.stdout:
                    progress.update(task, completed=5)
            if (cp and LOG.isEnabledFor(DEBUG) and cp.returncode
                    and cp.stdout is not None):
                LOG.debug(cp.stdout)
            progress.update(task, completed=100, total=100)
            return cp
        except Exception as e:
            if task:
                progress.update(task, completed=20, total=10, visible=False)
            if not LOG.isEnabledFor(DEBUG):
                LOG.info(
                    f"{tool_name} has reported few errors. To view, pass the environment variable SCAN_DEBUG_MODE=debug"
                )
            LOG.debug(e)
            return None