<commit_before>#!/usr/bin/env bash

# To setup `aa` tool, you need to execute (from base/head repo)
# echo "source `pwd`/startup-os/tools/aa/aa_tool.sh" >> ~/.bashrc
# source ~/.bashrc
#
# If you're on macOS, substitute ~/.bashrc with ~/.bash_profile


RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
RESET=$(tput sgr0)


function _aa_completions()
{
    local cur_word prev_word type_list

    # COMP_WORDS is an array of words in the current command line.
    # COMP_CWORD is the index of the current word (the one the cursor is
    # in). So COMP_WORDS[COMP_CWORD] is the current word; we also record
    # the previous word here
    cur_word="${COMP_WORDS[COMP_CWORD]}"
    prev_word="${COMP_WORDS[COMP_CWORD-1]}"

    commands="init workspace diff fix sync snapshot"
    init_options="--base_path --startupos_repo --user"

    if [ "$prev_word" = "aa" ] ; then
        # completing command name
        unset command
        COMPREPLY=( $(compgen -W "${commands}" -- ${cur_word}) )
    elif [ "$prev_word" = "workspace" ] || [ "$prev_word" = "aaw" ] ; then
        # completing names of workspaces
        find_base_folder
        workspaces=$(ls -1 $AA_BASE/ws/)
        COMPREPLY=( $(compgen -W "${workspaces}" -- "${cur_word}") )
    elif [ "$prev_word" = "init" ]; then
        # user entered "aa init" already
        command="init"
    else
        COMPREPLY=()
    fi

    if [[ $command = "init" && ${cur_word} == -* ]] ; then
        # completing params for `init`
        COMPREPLY=( $(compgen -W "${init_options}" -- ${cur_word}) )
    fi

    return 0
}

# Find base folder based on existence of BASE file, and put it in AA_BASE
function find_base_folder {
  CWD=`pwd`
  while [[ `pwd` != / ]]; do
    if [ -f `pwd`/BASE ]; then
      AA_BASE=`pwd`
      cd $CWD
      return 0
    else
      cd ..
    fi
  done
  cd $CWD
  return 0
}

function start_local_server {
    # starts local_server if it is not running yet
    # server is started by tools/local_server/run.sh
    # to check whether it is running already we try
    # to communicate with it by gRPC via polyglot
    # for additional output, set LOCAL_SERVER_POLYGLOT_DEBUG env variable

    find_base_folder
    if [[ -z "$AA_BASE" ]]; then
      echo "BASE file not found in path until root"
      return 1
    fi

    export STARTUP_OS=$AA_BASE/head/startup-os

    # store stderr to debug
    POLYGLOT_STDERR_DEBUG_FILE=$(mktemp)
    SERVER_LOG_FILE=$(mktemp)
    # call `ping` method via gRPC and store result (ignored for now)
    pushd $STARTUP_OS >/dev/null
    OUTPUT=$(echo {} | bazel run //tools:grpc_polyglot -- \
      --command=call \
      --endpoint=localhost:8001 \
      --full_method=com.google.startupos.tools.reviewer.service.CodeReviewService/ping \
      2>$POLYGLOT_STDERR_DEBUG_FILE)
    POLYGLOT_EXIT_CODE=$?
    popd >/dev/null

    # if polyglot cannot reach gRPC server it exits with nonzero code
    # then we need to run server
    if [ $POLYGLOT_EXIT_CODE -ne 0 ]; then
        if [ ! -z "$LOCAL_SERVER_POLYGLOT_DEBUG" ]; then
          echo "$RED[DEBUG]: Polyglot exit code was $POLYGLOT_EXIT_CODE$RESET"
          echo "$RED[DEBUG]: Polyglot output was $RESET$OUTPUT"
          echo "$RED[DEBUG]: Polyglot stderr is stored at $RESET$POLYGLOT_STDERR_DEBUG_FILE"
        fi<commit_msg>Store logs in $AA_BASE/logs/ (#153)<commit_after>#!/usr/bin/env bash

# To setup `aa` tool, you need to execute (from base/head repo)
# echo "source `pwd`/startup-os/tools/aa/aa_tool.sh" >> ~/.bashrc
# source ~/.bashrc
#
# If you're on macOS, substitute ~/.bashrc with ~/.bash_profile


RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
RESET=$(tput sgr0)


function _aa_completions()
{
    local cur_word prev_word type_list

    # COMP_WORDS is an array of words in the current command line.
    # COMP_CWORD is the index of the current word (the one the cursor is
    # in). So COMP_WORDS[COMP_CWORD] is the current word; we also record
    # the previous word here
    cur_word="${COMP_WORDS[COMP_CWORD]}"
    prev_word="${COMP_WORDS[COMP_CWORD-1]}"

    commands="init workspace diff fix sync snapshot"
    init_options="--base_path --startupos_repo --user"

    if [ "$prev_word" = "aa" ] ; then
        # completing command name
        unset command
        COMPREPLY=( $(compgen -W "${commands}" -- ${cur_word}) )
    elif [ "$prev_word" = "workspace" ] || [ "$prev_word" = "aaw" ] ; then
        # completing names of workspaces
        find_base_folder
        workspaces=$(ls -1 $AA_BASE/ws/)
        COMPREPLY=( $(compgen -W "${workspaces}" -- "${cur_word}") )
    elif [ "$prev_word" = "init" ]; then
        # user entered "aa init" already
        command="init"
    else
        COMPREPLY=()
    fi

    if [[ $command = "init" && ${cur_word} == -* ]] ; then
        # completing params for `init`
        COMPREPLY=( $(compgen -W "${init_options}" -- ${cur_word}) )
    fi

    return 0
}

# Find base folder based on existence of BASE file, and put it in AA_BASE
function find_base_folder {
  CWD=`pwd`
  while [[ `pwd` != / ]]; do
    if [ -f `pwd`/BASE ]; then
      AA_BASE=`pwd`
      cd $CWD
      return 0
    else
      cd ..
    fi
  done
  cd $CWD
  return 0
}

function start_local_server {
    # starts local_server if it is not running yet
    # server is started by tools/local_server/run.sh
    # to check whether it is running already we try
    # to communicate with it by gRPC via polyglot
    # for additional output, set LOCAL_SERVER_POLYGLOT_DEBUG env variable

    find_base_folder
    if [[ -z "$AA_BASE" ]]; then
      echo "BASE file not found in path until root"
      return 1
    fi

    export STARTUP_OS=$AA_BASE/head/startup-os

    # store stderr to debug
    POLYGLOT_STDERR_DEBUG_FILE=$AA_BASE/logs/polyglot_stderr.log
    SERVER_LOG_FILE=$AA_BASE/logs/server.log
    # call `ping` method via gRPC and store result (ignored for now)
    pushd $STARTUP_OS >/dev/null
    OUTPUT=$(echo {} | bazel run //tools:grpc_polyglot -- \
      --command=call \
      --endpoint=localhost:8001 \
      --full_method=com.google.startupos.tools.reviewer.service.CodeReviewService/ping \
      2>$POLYGLOT_STDERR_DEBUG_FILE)
    POLYGLOT_EXIT_CODE=$?
    popd >/dev/null

    # if polyglot cannot reach gRPC server it exits with nonzero code
    # then we need to run server
    if [ $POLYGLOT_EXIT_CODE -ne 0 ]; then
        if [ ! -z "$LOCAL_SERVER_POLYGLOT_DEBUG" ]; then
          echo "$RED[DEBUG]: Polyglot exit code was $POLYGLOT_EXIT_CODE$RESET"
          echo "$RED[DEBUG]: Polyglot output was $RESET$OUTPUT"
          echo "$RED[DEBUG]: Polyglot stderr is stored at $RESET$POLYGLOT_STDERR_DEBUG_FILE"
        fi
{'fraction_non_alphanumeric': 0.11752297724875697, 'fraction_numerical': 0.004972125960524334, 'mean_word_length': 2.7823361823361825, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '726056', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2223, 'n_words': 659}