|
| 1 | +name: Check consistency of tokens.txt file |
| 2 | + |
| 3 | +# Define the file paths under `paths` to trigger this check only when specific files are modified. |
| 4 | +# This script will then execute checks only on files that have changed, rather than all files listed in `paths`. |
| 5 | + |
| 6 | +# **Note** : To add a new token file for checks, include its path in: |
| 7 | +# - `on` -> `push` and `pull_request` sections |
| 8 | +# - `jobs` -> `check_tokens` -> `steps` -> Set global variable for multiple tokens.txt paths -> `TOKENS_FILES` |
| 9 | + |
| 10 | +on: |
| 11 | + push: |
| 12 | + paths: |
| 13 | + - "chebai/preprocessing/bin/smiles_token/tokens.txt" |
| 14 | + - "chebai/preprocessing/bin/smiles_token_unlabeled/tokens.txt" |
| 15 | + - "chebai/preprocessing/bin/selfies/tokens.txt" |
| 16 | + - "chebai/preprocessing/bin/protein_token/tokens.txt" |
| 17 | + - "chebai/preprocessing/bin/graph_properties/tokens.txt" |
| 18 | + - "chebai/preprocessing/bin/graph/tokens.txt" |
| 19 | + - "chebai/preprocessing/bin/deepsmiles_token/tokens.txt" |
| 20 | + - "chebai/preprocessing/bin/protein_token_3_gram/tokens.txt" |
| 21 | + pull_request: |
| 22 | + paths: |
| 23 | + - "chebai/preprocessing/bin/smiles_token/tokens.txt" |
| 24 | + - "chebai/preprocessing/bin/smiles_token_unlabeled/tokens.txt" |
| 25 | + - "chebai/preprocessing/bin/selfies/tokens.txt" |
| 26 | + - "chebai/preprocessing/bin/protein_token/tokens.txt" |
| 27 | + - "chebai/preprocessing/bin/graph_properties/tokens.txt" |
| 28 | + - "chebai/preprocessing/bin/graph/tokens.txt" |
| 29 | + - "chebai/preprocessing/bin/deepsmiles_token/tokens.txt" |
| 30 | + - "chebai/preprocessing/bin/protein_token_3_gram/tokens.txt" |
| 31 | + |
| 32 | +jobs: |
| 33 | + check_tokens: |
| 34 | + runs-on: ubuntu-latest |
| 35 | + |
| 36 | + steps: |
| 37 | + - name: Checkout code |
| 38 | + uses: actions/checkout@v2 |
| 39 | + |
| 40 | + - name: Get list of changed files |
| 41 | + id: changed_files |
| 42 | + run: | |
| 43 | + git fetch origin dev |
| 44 | +
|
| 45 | + # Get the list of changed files compared to origin/dev and save them to a file |
| 46 | + git diff --name-only origin/dev > changed_files.txt |
| 47 | +
|
| 48 | + # Print the names of changed files on separate lines |
| 49 | + echo "Changed files:" |
| 50 | + while read -r line; do |
| 51 | + echo "Changed File name : $line" |
| 52 | + done < changed_files.txt |
| 53 | +
|
| 54 | + - name: Set global variable for multiple tokens.txt paths |
| 55 | + run: | |
| 56 | + # All token files that needs to checked must be included here too, same as in `paths`. |
| 57 | + TOKENS_FILES=( |
| 58 | + "chebai/preprocessing/bin/smiles_token/tokens.txt" |
| 59 | + "chebai/preprocessing/bin/smiles_token_unlabeled/tokens.txt" |
| 60 | + "chebai/preprocessing/bin/selfies/tokens.txt" |
| 61 | + "chebai/preprocessing/bin/protein_token/tokens.txt" |
| 62 | + "chebai/preprocessing/bin/graph_properties/tokens.txt" |
| 63 | + "chebai/preprocessing/bin/graph/tokens.txt" |
| 64 | + "chebai/preprocessing/bin/deepsmiles_token/tokens.txt" |
| 65 | + "chebai/preprocessing/bin/protein_token_3_gram/tokens.txt" |
| 66 | + ) |
| 67 | + echo "TOKENS_FILES=${TOKENS_FILES[*]}" >> $GITHUB_ENV |
| 68 | +
|
| 69 | + - name: Process only changed tokens.txt files |
| 70 | + run: | |
| 71 | + # Convert the TOKENS_FILES environment variable into an array |
| 72 | + TOKENS_FILES=(${TOKENS_FILES}) |
| 73 | +
|
| 74 | + # Iterate over each token file path |
| 75 | + for TOKENS_FILE_PATH in "${TOKENS_FILES[@]}"; do |
| 76 | + # Check if the current token file path is in the list of changed files |
| 77 | + if grep -q "$TOKENS_FILE_PATH" changed_files.txt; then |
| 78 | + echo "----------------------- Processing $TOKENS_FILE_PATH -----------------------" |
| 79 | +
|
| 80 | + # Get previous tokens.txt version |
| 81 | + git fetch origin dev |
| 82 | + git diff origin/dev -- $TOKENS_FILE_PATH > tokens_diff.txt || echo "No previous tokens.txt found for $TOKENS_FILE_PATH" |
| 83 | +
|
| 84 | + # Check for deleted or added lines in tokens.txt |
| 85 | + if [ -f tokens_diff.txt ]; then |
| 86 | +
|
| 87 | + # Check for deleted lines (lines starting with '-') |
| 88 | + deleted_lines=$(grep '^-' tokens_diff.txt | grep -v '^---' | sed 's/^-//' || true) |
| 89 | + if [ -n "$deleted_lines" ]; then |
| 90 | + echo "Error: Lines have been deleted from $TOKENS_FILE_PATH." |
| 91 | + echo -e "Deleted Lines: \n$deleted_lines" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | +
|
| 95 | + # Check for added lines (lines starting with '+') |
| 96 | + added_lines=$(grep '^+' tokens_diff.txt | grep -v '^+++' | sed 's/^+//' || true) |
| 97 | + if [ -n "$added_lines" ]; then |
| 98 | +
|
| 99 | + # Count how many lines have been added |
| 100 | + num_added_lines=$(echo "$added_lines" | wc -l) |
| 101 | +
|
| 102 | + # Get last `n` lines (equal to num_added_lines) of tokens.txt |
| 103 | + last_lines=$(tail -n "$num_added_lines" $TOKENS_FILE_PATH) |
| 104 | +
|
| 105 | + # Check if the added lines are at the end of the file |
| 106 | + if [ "$added_lines" != "$last_lines" ]; then |
| 107 | +
|
| 108 | + # Find lines that were added but not appended at the end of the file |
| 109 | + non_appended_lines=$(diff <(echo "$added_lines") <(echo "$last_lines") | grep '^<' | sed 's/^< //') |
| 110 | +
|
| 111 | + echo "Error: New lines have been added to $TOKENS_FILE_PATH, but they are not at the end of the file." |
| 112 | + echo -e "Added lines that are not at the end of the file: \n$non_appended_lines" |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + fi |
| 116 | +
|
| 117 | + if [ "$added_lines" == "" ]; then |
| 118 | + echo "$TOKENS_FILE_PATH validation successful: No lines were deleted, and no new lines were added." |
| 119 | + else |
| 120 | + echo "$TOKENS_FILE_PATH validation successful: No lines were deleted, and new lines were correctly appended at the end." |
| 121 | + fi |
| 122 | + else |
| 123 | + echo "No previous version of $TOKENS_FILE_PATH found." |
| 124 | + fi |
| 125 | + else |
| 126 | + echo "$TOKENS_FILE_PATH was not changed, skipping." |
| 127 | + fi |
| 128 | + done |
0 commit comments