/
usr
/
local
/
stat_watch
/
includes
/
File Upload :
llllll
Current File: //usr/local/stat_watch/includes/assume.shf
#! /bin/bash function fn_find_pwd { ### Find the current directory and see if it's part of an assumption if [[ ! -f "$d_WORKING"/assumptions ]]; then return fi local v_PWD="$( pwd -P )" echo -n "$( fn_find_assume "$v_PWD" )" } function fn_find_assume { ### Find if this directory is part of an assumption local d_CUR="$( cd "$1" && pwd -P )" if [[ "${d_CUR: -1}" == "/" && "$d_CUR" != "/" ]]; then ### Remove a trailing slash d_CUR="${d_CUR:0:${#d_CUR}-1}" fi local d_SAN_CUR="$( fn_sanitize "$d_CUR" )" ### First, get only the section of lines that begin with the directory, plus the next character ### Next, select only lines where that next character was zero ### Finally, grab the line number local v_LINE="$( grep -nEao "^$d_SAN_CUR." "$d_WORKING"/assumptions 2> /dev/null | grep -Pa "\x0$" | tail -n1 | cut -d ":" -f1 )" local v_RETURN= if [[ -z "$v_LINE" ]]; then if [[ "$d_CUR" == "/" ]]; then echo -n "$v_RETURN" return fi fn_file_path "$d_CUR"; local d_PARENT="$s_DIR" if [[ -n "$d_PARENT" ]]; then if [[ $( echo "$d_PARENT" | egrep -c "^$d_SAN_CUR/" ) -gt 0 ]]; then ### If what we're given as the parent directory, is NOT actually the parent directory, we're not going to find anything ##### I get the feeling that there are still weird edge cases that could cause this to infinite loop v_RETURN= else v_RETURN="$( fn_find_assume "$d_PARENT" )" fi fi else v_RETURN="$( tail -n +"$v_LINE" "$d_WORKING"/assumptions 2> /dev/null | head -n1 | cut -d $'\000' -f2 )" fi echo -n "$v_RETURN" } function fn_assume_include { ### Modify the command line arguments to include any assumed job files local a_CL_ARGUMENTS2=( "$@" ) local v_ARG= for (( c=0; c<=$(( ${#a_CL_ARGUMENTS2[@]} - 1 )); c++ )); do v_ARG="${a_CL_ARGUMENTS2[$c]}" if [[ "$v_ARG" == "-i" || "$v_ARG" == "--include" || "$v_ARG" == "--no-assume" ]]; then b_ADD_INCLUDE=false fi done } function fn_create_assumption { local d_ASSUME if [[ -z "$1" ]]; then ### No arguments - give addumptions for the PWD local f_JOB="$( fn_find_pwd )" if [[ -n "$f_JOB" ]]; then echo -n "Job file: " echo -n "$f_JOB" | "$d_PROGRAM"/scripts/escape.pl -n else echo "No assumed job" fi exit 0 elif [[ "$1" == "--list" ]]; then ### List the assumptions if [[ ! -f "$d_WORKING"/assumptions ]]; then return fi ### This reads the assumption file, escapes both the job file and the directory, then replaces the newline character between them with " -> " perl -e 'require( '\'"$d_PROGRAM"\'' . "/modules/escape.pm" ); while(<>){chomp($_); my @line = split( m/\x0/, $_ ); print SWEscape::fn_escape_filename($line[0]) . " -> " . SWEscape::fn_escape_filename($line[1])}' "$d_WORKING"/assumptions echo exit 0 elif [[ ( ! -f "$1" && "$1" != "--remove" ) || ( -n "$2" && ! -d "$2" ) ]]; then if [[ -d "$1" ]]; then ### The first argument was a directory - give assumptions for that directory local f_JOB="$( fn_find_assume "$1" )" if [[ -n "$f_JOB" ]]; then echo -n "Job file for directory " echo -n "$1" | "$d_PROGRAM"/scripts/escape.pl echo -n " : " echo -n "$f_JOB" | "$d_PROGRAM"/scripts/escape.pl -n else echo -n "No assumed job for directory " echo -n "$1" | "$d_PROGRAM"/scripts/escape.pl -n fi fi exit 0 fi ### Create a working directory and a file to document assumptions mkdir -p "$d_WORKING" local f_TEMP=$( mktemp ) if [[ -n "$2" ]]; then d_ASSUME="$2" else d_ASSUME="$( pwd )" fi d_ASSUME="$( cd "$d_ASSUME" && pwd -P )" if [[ "${d_ASSUME: -1}" == "/" ]]; then ### Remove a trailing slash d_ASSUME="${d_ASSUME:0:${#d_ASSUME}-1}" fi local d_ASSUME_SAN="$( fn_sanitize "$d_ASSUME" )" ### Put all other assumptions into a temp file local v_LINES="$( grep -nEao "^$d_ASSUME_SAN." "$d_WORKING"/assumptions 2> /dev/null | grep -Pa "\x0$" | cut -d ":" -f1 | tr "\n" ";" | sed "s/;/d;/g" )" if [[ -n "$v_LINES" ]]; then sed -e "$v_LINES" "$d_WORKING"/assumptions > "$f_TEMP" 2> /dev/null else cat "$d_WORKING"/assumptions > "$f_TEMP" 2> /dev/null fi if [[ "$1" == "--remove" ]]; then ### we've been asked to remove an assumption if [[ -n "$( diff -q "$f_TEMP" "$d_WORKING"/assumptions )" ]]; then mv -f "$f_TEMP" "$d_WORKING"/assumptions echo -n "Assumptions for directory " echo -n "$d_ASSUME" | "$d_PROGRAM"/scripts/escape.pl echo "have been removed" else rm -f "$f_TEMP" echo -n "No assumptions to remove for directory " echo -n "$d_ASSUME" | "$d_PROGRAM"/scripts/escape.pl -n fi else ### Add the assumption to the list ### Add this assumption to the temp file printf '%b' "$d_ASSUME"'\0' >> "$f_TEMP" fn_file_path "$1" echo "$s_DIR"/"$s_FILE" >> "$f_TEMP" ### Replace the assumptions file with the temp file mv -f "$f_TEMP" "$d_WORKING"/assumptions echo -n "Assumption created for directory " echo -n "$d_ASSUME" | "$d_PROGRAM"/scripts/escape.pl -n fi }
Copyright ©2k19 -
Hexid
|
Tex7ure