Changeset - 7316fc631f1c
[Not reviewed]
0 1 0
Branko Majic (branko) - 5 years ago 2019-08-14 19:27:49
branko@majic.rs
MAR-145: Switch to using Bash process substitution instead of managing a bunch of temporary files.
1 file changed with 21 insertions and 29 deletions:
0 comments (0 inline, 0 general)
roles/common/files/pip_check_requirements_upgrades.sh
Show inline comments
 
@@ -145,19 +145,6 @@ function error() {
 
    echo -e "${_text_bold}${_text_red}[ERROR]${_text_reset}" "$@" >&2
 
}
 

	
 
# Clean-up command for temporary files.
 
function on_exit() {
 
    debug "Cleaning-up temporary file: $tmp_current"
 
    [[ -f $tmp_current ]] && rm "$tmp_current"
 

	
 
    debug "Cleaning-up temporary file: $tmp_new"
 
    [[ -f $tmp_new ]] && rm "$tmp_new"
 

	
 
    debug "Cleaning-up temporary file: $tmp_new_sorted"
 
    [[ -f $tmp_new_sorted ]] && rm "$tmp_new_sorted"
 
}
 
trap on_exit EXIT
 

	
 
# Define error codes.
 
SUCCESS=0
 
ERROR_ARGUMENTS=1
 
@@ -223,12 +210,6 @@ if ! which pip-compile >/dev/null 2>&1; then
 
    exit "$ERROR_PIP_TOOLS_MISSING"
 
fi
 

	
 
# Create temporary files where files where "normalised" outputs will be
 
# stored.
 
tmp_current=$(mktemp)
 
tmp_new=$(mktemp)
 
tmp_new_sorted=$(mktemp)
 

	
 
# Process each environment.
 
for environment in "$config_dir"/*; do
 
    # Empty directory.
 
@@ -252,21 +233,32 @@ for environment in "$config_dir"/*; do
 
	    continue
 
	fi
 

	
 
	# Deploy the existing requirements file and the new one.
 
	sed -e 's/[[:blank:]]*#.*//' "$req_txt" | grep -v "^$" | sort -u > "$tmp_current" 
 
	if ! pip-compile --quiet --allow-unsafe --no-header --no-annotate --no-index --output-file "$tmp_new" --upgrade "$req_in"; then
 
	    error "Failed while running pip-compile command against (see error stack trace above): $req_in"
 
        # Clean up the existing requirements file to contain only
 
        # package versioning information.
 
        current=$(sed -e 's/[[:blank:]]*#.*//' "$req_txt" | grep -v "^$" | sort -u)
 

	
 
        # Calculate up-to-date requirements.
 
        new=$(pip-compile --quiet --allow-unsafe --no-header --no-annotate --no-index --output-file - --upgrade "$req_in")
 
        result="$?"
 
        if [[ $result != 0 ]]; then
 
             error "Failed while running pip-compile command against (see error stack trace above): $req_in"
 
	    continue
 
	fi
 
        fi
 

	
 
        # Sort the new requirements file.
 
        sed -e 's/[[:blank:]]*#.*//' "$tmp_new" | grep -v "^$" | sort -u > "$tmp_new_sorted"
 
        # Clean up the new requirements to contain only package
 
        # versioning information.
 
        new=$(echo "$new" | sed -e 's/[[:blank:]]*#.*//' | grep -v "^$" | sort -u)
 

	
 
        debug "Current requirements:\n$(cat "$tmp_current")"
 
        debug "New requirements:\n$(cat "$tmp_new_sorted")"
 
        debug "Current requirements:\n$(echo "$current")"
 
        debug "New requirements:\n$(echo "$new")"
 

	
 
	# Run diff, storing the output and result.
 
	diff=$(diff -u "$tmp_current" "$tmp_new_sorted")
 
        # This is using Bash process substitution, which helps avoid
 
        # creating and keeping track of temporary files (since diff
 
        # must be fed with two input files). E.g. the <() will
 
        # essentially be path to a Bash-created/managed temporary
 
        # file.
 
	diff=$(diff -u <(echo "$current") <(echo "$new"))
 
	result="$?"
 

	
 
	# Show warning about available updates.
0 comments (0 inline, 0 general)