|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +############################################################################### |
| 7 | +# PURPOSE: This script provides utility functions to download and verify the |
| 8 | +# hash of NLTK pickles. |
| 9 | +# |
| 10 | +# PRELIMINARY: |
| 11 | +# Sourced from within another script. |
| 12 | +# |
| 13 | +# USAGE: |
| 14 | +# source "${path}/nltk_download_functions.sh" |
| 15 | +# |
| 16 | +# Where ${path} is the directory containing this script. If the calling |
| 17 | +# script is in the same directory, use "$(dirname "${BASH_SOURCE[0]}")". |
| 18 | +# Example: source "$(dirname "${BASH_SOURCE[0]}")/nltk_download_functions.sh" |
| 19 | +# |
| 20 | +############################################################################### |
| 21 | + |
| 22 | +verify_hash() { |
| 23 | + local file_path="$1" |
| 24 | + local expected_hash="$2" |
| 25 | + local actual_hash=$(sha256sum "$file_path" | awk '{print $1}') |
| 26 | + |
| 27 | + if [ "$actual_hash" != "$expected_hash" ]; then |
| 28 | + echo "Hash mismatch found for $file_path" |
| 29 | + echo "Expected: $expected_hash" |
| 30 | + echo "Found: $actual_hash" |
| 31 | + return 1 |
| 32 | + else |
| 33 | + return 0 |
| 34 | + fi |
| 35 | +} |
| 36 | + |
| 37 | +function download_punkt() { |
| 38 | + cleanup_punkt "${1}" |
| 39 | + echo "Starting download of punkt zip file..." |
| 40 | + |
| 41 | + local -r source_dir="${1}" |
| 42 | + local -r zip_url="https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/packages/tokenizers/punkt.zip" |
| 43 | + local -r zip_file="punkt.zip" |
| 44 | + # this hash was verified to be working, it may need to be updated if nltk ever updates punkt pickles |
| 45 | + local -r expected_hash="51c3078994aeaf650bfc8e028be4fb42b4a0d177d41c012b6a983979653660ec" |
| 46 | + local temp_dir |
| 47 | + |
| 48 | + # Download the punkt zip file |
| 49 | + if ! curl -sSL "$zip_url" -o "$zip_file"; then |
| 50 | + echo "Error: Failed to download the zip file." |
| 51 | + return 1 |
| 52 | + fi |
| 53 | + |
| 54 | + echo "Verifying hash against the last known valid hash..." |
| 55 | + |
| 56 | + # Verify the hash |
| 57 | + if ! verify_hash "$zip_file" "$expected_hash"; then |
| 58 | + echo "Hash verification failed. Please check if the new punkt file is valid. Exiting." |
| 59 | + rm -f "$zip_file" |
| 60 | + return 1 |
| 61 | + fi |
| 62 | + |
| 63 | + # Create a temporary directory for extraction |
| 64 | + temp_dir=$(mktemp -d) |
| 65 | + if [[ ! -d "$temp_dir" ]]; then |
| 66 | + echo "Error: Failed to create a temporary directory." |
| 67 | + rm -f "$zip_file" |
| 68 | + return 1 |
| 69 | + fi |
| 70 | + |
| 71 | + # Extract the zip file into the temporary directory |
| 72 | + if ! unzip -q "$zip_file" -d "$temp_dir"; then |
| 73 | + echo "Error: Failed to unzip the file." |
| 74 | + rm -rf "$temp_dir" |
| 75 | + rm -f "$zip_file" |
| 76 | + return 1 |
| 77 | + fi |
| 78 | + |
| 79 | + # Create the destination directory and copy files |
| 80 | + # interacting directly with the app under /operators/translate for now until more apps |
| 81 | + # within the solution need punkt pickles |
| 82 | + local -r dest_dir="$source_dir/operators/translate/nltk_data/tokenizers/punkt" |
| 83 | + mkdir -p "$dest_dir" |
| 84 | + cp -r "$temp_dir/punkt/PY3/"* "$dest_dir/" |
| 85 | + |
| 86 | + # Clean up temporary files |
| 87 | + rm -rf "$temp_dir" |
| 88 | + rm -f "$zip_file" |
| 89 | + |
| 90 | + echo "Punkt zip file downloaded and extracted successfully." |
| 91 | +} |
| 92 | + |
| 93 | +function cleanup_punkt() { |
| 94 | + local -r source_dir="${1}" |
| 95 | + local -r target_dir="$source_dir/operators/translate/nltk_data" |
| 96 | + |
| 97 | + echo "Starting cleanup process for punkt data..." |
| 98 | + |
| 99 | + # Check if the target directory exists |
| 100 | + if [[ -d "$target_dir" ]]; then |
| 101 | + # Attempt to remove the directory |
| 102 | + if rm -rf "$target_dir"; then |
| 103 | + echo "Successfully removed the nltk_data directory at $target_dir." |
| 104 | + else |
| 105 | + echo "Error: Failed to remove the nltk_data directory at $target_dir." |
| 106 | + return 1 |
| 107 | + fi |
| 108 | + else |
| 109 | + echo "The directory $target_dir does not exist. No cleanup necessary." |
| 110 | + fi |
| 111 | + |
| 112 | + echo "Cleanup process completed." |
| 113 | + return 0 |
| 114 | +} |
0 commit comments