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
 
@@ -142,25 +142,12 @@ function warning() {
 
}
 

	
 
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
 
ERROR_CONFIG_DIR=2
 
ERROR_PIP_TOOLS_MISSING=3
 

	
 
@@ -220,18 +207,12 @@ fi
 
# Verify pip-compile is available.
 
if ! which pip-compile >/dev/null 2>&1; then
 
    error "Could not find command pip-compile from packagep pip-tools. Package might not be available in the virtual environment."
 
    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.
 
    if [[ ! -e $environment ]]; then
 
	error "Configuration directory is empty: $config_dir"
 
	exit "$ERROR_CONFIG_DIR"
 
@@ -249,27 +230,38 @@ for environment in "$config_dir"/*; do
 
	req_txt="${req_in%.in}.txt"
 
	if [[ ! -f $req_txt ]]; then
 
	    [[ $quiet == 0 ]] && info "Skipping input file with missing .txt file: $req_in"
 
	    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.
 
	if [[ $result == 0 ]]; then
 
	    [[ $quiet == 0 ]] && info "No upgrades available for: $req_txt"
 
	else
0 comments (0 inline, 0 general)