Skip to content

Commit

Permalink
Merge pull request #184 from acep-uaf/nicole/zip-symlinks
Browse files Browse the repository at this point in the history
Nicole/zip symlinks
  • Loading branch information
nicolemah99 authored Jul 31, 2024
2 parents a44031d + 3666066 commit 7b428bd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ config.yml
>>>>>>> ot-dev-ftp-meter-download
*.download_progress
*.message
config_*.yml
archive_config_*.yml
7 changes: 4 additions & 3 deletions cli_meter/meters/sel735/download.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ for event_info in $events; do

# LOCATION-TYPE-METER_ID-YYYYMM-EVENT_ID
zip_filedate="${date_dir//-/}"
zip_filename="$location-$data_type-$meter_id-$zip_filedate-$event_id.zip"

symlink_name="$location-$data_type-$meter_id-$zip_filedate-$event_id"
zip_filename="${symlink_name}.zip"

# Zip the event files and empty the working event directory
"$current_dir/zip_event.sh" "$output_dir" "$event_zipped_output_dir" "$event_id" "$zip_filename"|| {
"$current_dir/zip_event.sh" "$output_dir" "$event_zipped_output_dir" "$event_id" "$symlink_name"|| {
handle_fail "$event_id" "$output_dir" "$STREAMS_ZIP_FAIL" "Failed to zip event: $event_id" "$meter_id" "$download_start" "$download_end"
continue
}
Expand Down
24 changes: 15 additions & 9 deletions cli_meter/meters/sel735/zip_event.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/bin/bash
# ==============================================================================
# Script Name: zip_event.sh
# Description: This script zips the event files in the source directory
# and saves the zip file in the destination directory and removes
# the files in the source directory, but leaves the directory to
# be used to compare with the history file.
# Description: This script creates a symbolic link for the event directory,
# zips the contents via the symbolic link, saves the zip file in the
# destination directory, and then removes the symbolic link. It also
# removes the files in the source directory but leaves the directory
# itself to be used for comparison with the history file.
#
# Usage: ./zip_event.sh <source_dir> <dest_dir> <event_id> <zip_filename>
# Usage: ./zip_event.sh <source_dir> <dest_dir> <event_id> <symlink_name>
# Called by: download.sh
#
# Arguments:
# source_dir The directory containing the event files
# dest_dir The directory where the zip file will be saved
# event_id The event ID (ex.10000)
# zip_filename The name of the zip file (ex. location-dataType-meterId-YYYYMM-eventId.zip)
# symlink_name The name of the symlink and base of the zip file (ex. location-dataType-meterId-YYYYMM-eventId)
#
# Requirements: zip
# common_utils.sh
Expand All @@ -23,18 +24,23 @@ script_name=$(basename "$0")
source "$current_dir/../../common_utils.sh"

# Check for exactly 4 arguments
[ "$#" -ne 4 ] && failure $STREAMS_INVALID_ARGS "Usage: $script_name <source_dir> <dest_dir> <event_id> <zip_filename>"
[ "$#" -ne 4 ] && failure $STREAMS_INVALID_ARGS "Usage: $script_name <source_dir> <dest_dir> <event_id> <symlink_name>"

source_dir="$1"
dest_dir="$2"
event_id="$3"
zip_filename="$4"
symlink_name="$4"
zip_filename="${symlink_name}.zip"

[[ -d "$source_dir/$event_id" ]] || failure $STREAMS_DIR_NOT_FOUND "Source directory does not exist: $source_dir/$event_id"

# Zip the files in the source directory
pushd "$source_dir" > /dev/null
zip -r -q "${dest_dir}/${zip_filename}" $event_id && log "Zipped files for event: $event_id" || failure $STREAMS_ZIP_FAIL "Failed to zip event: $event_id in directory: $source_dir"
ln -s "$event_id" "$symlink_name" && log "Created symlink: $symlink_name" || failure $STREAMS_SYMLINK_FAIL "Failed to create symlink: $symlink_name"
zip -r -q "${dest_dir}/${zip_filename}" "$symlink_name" && \
log "Successfully zipped symlink: $symlink_name to ${dest_dir}/${zip_filename} for event: $event_id" || \
failure $STREAMS_ZIP_FAIL "Failed to zip symlink: $symlink_name located at ${source_dir}/${symlink_name} to ${dest_dir}/${zip_filename}"
rm "$symlink_name" && log "Removed symlink: $symlink_name" || failure $STREAMS_SYMLINK_FAIL "Failed to remove symlink: $symlink_name"
popd > /dev/null

# Delete the files in the event_id directory but not the directory itself
Expand Down
3 changes: 2 additions & 1 deletion cli_meter/test/test_helper/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ _common_setup() {
EVENT_ID="1234"
METER_IP="123.123.123"
DATA_TYPE="events"
ZIP_FILENAME="$LOCATION-$METER_TYPE-$METER_ID-YYYYMM-$EVENT_ID.zip"
SYMLINK_NAME="$LOCATION-$METER_TYPE-$METER_ID-YYYYMM-$EVENT_ID"
ZIP_FILENAME="${SYMLINK_NAME}.zip"
}

_common_teardown() {
Expand Down
8 changes: 4 additions & 4 deletions cli_meter/test/test_scripts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ teardown() {

@test "zip_event.sh execution test" {
mkdir -p "$TMP_DIR/$EVENT_ID"
run ./zip_event.sh "$TMP_DIR" "$TMP_DIR" "$EVENT_ID" "$ZIP_FILENAME"
run ./zip_event.sh "$TMP_DIR" "$TMP_DIR" "$EVENT_ID" "$SYMLINK_NAME"
assert_success
assert_output --partial "Zipped files for event: $EVENT_ID"
assert_output --partial "Successfully zipped symlink: $SYMLINK_NAME to $TMP_DIR/$ZIP_FILENAME for event: $EVENT_ID"
assert [ -f "$TMP_DIR/$ZIP_FILENAME" ]
assert [ -d "$TMP_DIR/$EVENT_ID" ]
}

@test "zip_event.sh test 0 arguments" {
run ./zip_event.sh
assert_failure $(($STREAMS_INVALID_ARGS % 256))
assert_output --partial "Usage: zip_event.sh <source_dir> <dest_dir> <event_id> <zip_filename>"
assert_output --partial "Usage: zip_event.sh <source_dir> <dest_dir> <event_id> <symlink_name>"
}

@test "zip_event.sh test invalid event_id" {
run ./zip_event.sh "$TMP_DIR" "$TMP_DIR" "not_a_directory" "$ZIP_FILENAME"
run ./zip_event.sh "$TMP_DIR" "$TMP_DIR" "not_a_directory" "$SYMLINK_NAME"
assert_failure $(($STREAMS_DIR_NOT_FOUND % 256))
}

0 comments on commit 7b428bd

Please sign in to comment.