<commit_before>#!/bin/dash

# usage: bwrap_launcher.sh
#         <cleanup basedir and other directories - true\false>
#         <basedir>
#         <additional dir to cleanup on exit> <additional dir> ...
#         -- - end of launcher script parameters
#         <parameters for bwrap>

cleanup="$1"
lock_dirname="$2"
basedir="$3"
shift 3

extra_paths_counter=0

while test "$1" != "--" -a "$1" != ""
do
  extra_paths_counter=`expr $extra_paths_counter + 1`
  eval 'extra_path_'"$extra_paths_counter"'="'"$1"'"'
  shift 1
done
shift 1

lock_path="$basedir/$lock_dirname"
lock_entered="false"
nowait=""

#echo "cleanup=$cleanup" # debug
#echo "lock_dirname=$lock_dirname" # debug
#echo "basedir=$basedir" # debug
#echo "extra_paths_counter=$extra_paths_counter" # debug

check_sessions() {
  for session in "$basedir/control/"*.in "$basedir/control/"*.out
  do
    test "$session" = "$basedir/control/*.in" && continue
    test "$session" = "$basedir/control/*.out" && continue
    return 0
  done
  return 1
}

lock_enter() {
  nowait="$1"
  if mkdir "$lock_path" 2>/dev/null; then
    lock_entered="true"
    return 0
  else
    test ! -z "$nowait" && return 1
    #echo "awaiting lock release" # debug
    while ! lock_enter "nowait"; do
      sleep 1
    done
    return 0
  fi
}

lock_exit() {
  if test "$lock_entered" = "true"; then
    rmdir "$lock_path" 2>/dev/null
    lock_entered="false"
  fi
  return 0
}

trap "{ echo \"bwrap_launcher.sh: trap triggered, ignoring\"; }" INT HUP

>"$basedir/bwrap.log" 2>&1 bwrap "$@"

err_code="$?"
test "$err_code" != "0" && echo "bwrap failed with error code $err_code, bwrap.log output:" && cat "$basedir/bwrap.log" && exit 1

#below is a cleanup-on-exit logic that may be activated when bwrap is complete
test "$cleanup" != "true" && exit 0

lock_enter

if check_sessions; then
  lock_exit<commit_msg>bwrap_launcher.sh: minor fix - use nohup to start bwrap<commit_after>#!/bin/dash

# usage: bwrap_launcher.sh
#         <cleanup basedir and other directories - true\false>
#         <basedir>
#         <additional dir to cleanup on exit> <additional dir> ...
#         -- - end of launcher script parameters
#         <parameters for bwrap>

cleanup="$1"
lock_dirname="$2"
basedir="$3"
shift 3

extra_paths_counter=0

while test "$1" != "--" -a "$1" != ""
do
  extra_paths_counter=`expr $extra_paths_counter + 1`
  eval 'extra_path_'"$extra_paths_counter"'="'"$1"'"'
  shift 1
done
shift 1

lock_path="$basedir/$lock_dirname"
lock_entered="false"
nowait=""

#echo "cleanup=$cleanup" # debug
#echo "lock_dirname=$lock_dirname" # debug
#echo "basedir=$basedir" # debug
#echo "extra_paths_counter=$extra_paths_counter" # debug

check_sessions() {
  for session in "$basedir/control/"*.in "$basedir/control/"*.out
  do
    test "$session" = "$basedir/control/*.in" && continue
    test "$session" = "$basedir/control/*.out" && continue
    return 0
  done
  return 1
}

lock_enter() {
  nowait="$1"
  if mkdir "$lock_path" 2>/dev/null; then
    lock_entered="true"
    return 0
  else
    test ! -z "$nowait" && return 1
    #echo "awaiting lock release" # debug
    while ! lock_enter "nowait"; do
      sleep 1
    done
    return 0
  fi
}

lock_exit() {
  if test "$lock_entered" = "true"; then
    rmdir "$lock_path" 2>/dev/null
    lock_entered="false"
  fi
  return 0
}

trap "{ echo \"bwrap_launcher.sh: trap triggered, ignoring\"; }" INT HUP

</dev/null 1>"$basedir/bwrap.log" 2>&1 nohup bwrap "$@"

err_code="$?"
test "$err_code" != "0" && echo "bwrap failed with error code $err_code, bwrap.log output:" && cat "$basedir/bwrap.log" && exit 1

#below is a cleanup-on-exit logic that may be activated when bwrap is complete
test "$cleanup" != "true" && exit 0

lock_enter

if check_sessions; then
  lock_exit
{'fraction_non_alphanumeric': 0.1514346439957492, 'fraction_numerical': 0.0140807651434644, 'mean_word_length': 3.608323133414933, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 21, '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': '1763829', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1314, 'n_words': 364}