-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,34 +9,42 @@ SCRIPT_NAME="$(basename ${0})" | |
SCRIPT_NAME="${SCRIPT_NAME%.*sh}" | ||
|
||
echo "INFO: processing samplesheets from ${sampleSheetsDir}/new/..." | ||
|
||
for sampleSheet in $(ls -1 "${sampleSheetsDir}/new/"*'.csv') | ||
do | ||
# | ||
# Create a copy of the original, so we preserve the original owner | ||
# and can check who to notify when something is wrong with the samplesheet. | ||
# | ||
cp "${sampleSheet}"{,.converted} | ||
|
||
# | ||
# Make sure | ||
# 1. The last line ends with a line end character. | ||
# 2. We have the right line end character: convert any carriage return (\r) to newline (\n). | ||
# 3. We remove empty lines. | ||
# | ||
cp "${sampleSheet}"{,.converted} \ | ||
&& printf '\n' >> "${sampleSheet}.converted" \ | ||
&& sed -i 's/\r/\n/g' "${sampleSheet}.converted" \ | ||
&& sed -i '/^\s*$/d' "${sampleSheet}.converted" \ | ||
&& mv -f "${sampleSheet}"{.converted,} | ||
printf '\n' >> "${sampleSheet}.converted" | ||
sed -i 's/\r/\n/g' "${sampleSheet}.converted" | ||
sed -i '/^\s*$/d' "${sampleSheet}.converted" | ||
# | ||
# Parse content with Python sanity check script. | ||
# | ||
"${sampleSheetsDir}"/"${SCRIPT_NAME}".py --input "${sampleSheet}" --logfile "${sampleSheet}.log" | ||
filename=$(basename "${sampleSheet}") | ||
check=$(cat "${sampleSheet}.log") | ||
if [[ "${check}" == "OK" ]] | ||
check='failed' # default. | ||
fileName=$(basename "${sampleSheet}") | ||
if "${sampleSheetsDir}"/"${SCRIPT_NAME}".py --input "${sampleSheet}.converted" --log "${sampleSheet}.converted.log" | ||
then | ||
echo "INFO: Samplesheet is OK, moving ${sampleSheet} to ${sampleSheetsDir}..." | ||
mv "${sampleSheet}" "${sampleSheetsDir}" | ||
rm -f "${sampleSheet}.log.mailed" | ||
rm -f "${sampleSheet}.log" | ||
check=$(cat "${sampleSheet}.converted.log") | ||
fi | ||
|
||
if [[ "${check}" == 'OK' ]] | ||
then | ||
echo "INFO: Samplesheet is OK, moving ${sampleSheet}.converted to ${sampleSheetsDir}/${fileName}..." | ||
mv "${sampleSheet}.converted" "${sampleSheetsDir}/${fileName}" | ||
rm -f "${sampleSheet}"* # cleanup. | ||
else | ||
echo "ERROR: Samplesheet ${filename} is not correct, see log." | ||
if [[ -e "${sampleSheet}.log.mailed" ]] | ||
echo "ERROR: Samplesheet ${fileName} is not correct, see ${sampleSheet}.converted.log." | ||
if [[ -e "${sampleSheet}.converted.log.mailed" ]] | ||
then | ||
echo "INFO: Notification was already sent." | ||
else | ||
|
@@ -49,33 +57,35 @@ do | |
then | ||
mailAddress="$(cat "${baseDir}/logs/${SCRIPT_NAME}.mailinglist" | tr '\n' ' ')" | ||
else | ||
echo -e "ERROR: ${baseDir}/logs/${SCRIPT_NAME}.mailinglist is missing on $(hostname -s)\n" \ | ||
| mail -s "Samplesheet is wrong, but we cannot send email to the relevant users." | ||
printf '%s\n' "ERROR: ${baseDir}/logs/${SCRIPT_NAME}.mailinglist is missing on $(hostname -s)." \ | ||
| mail -s "Samplesheet is wrong, but we cannot send email to the relevant users." '[email protected]' | ||
fi | ||
# | ||
# Get email address for owner of the samplesheet. | ||
# | ||
fileOwner=$(stat -c "%U" "${sampleSheet}" | tr -d '\n') | ||
fileOwner=$(stat -c '%U' "${sampleSheet}" | tr -d '\n') | ||
mailAddressOwner="$(getent passwd "${fileOwner}" | cut -d ':' -s -f 5)" | ||
if [[ -z "${mailAddressOwner:-}" ]] | ||
then | ||
echo -e "WARN: We do not have an email address for this user: ${fileOwner}\n" \ | ||
printf '%s\n' "WARN: We do not have an email address for this user: ${fileOwner}." \ | ||
| mail -s "Samplesheet is wrong on $(hostname -s), but we cannot email the owner." "${mailAddress:-}" | ||
else | ||
mailAddress="${mailAddress:-} ${mailAddressOwner:-} | ||
mailAddress="${mailAddress:-} ${mailAddressOwner:-}" | ||
fi | ||
# | ||
# Prepare message content. | ||
# | ||
header="Dear ${fileOwner}," | ||
body="${SCRIPT_NAME} detected an error when parsing ${sampleSheet} on $(hostname -s): $(<"${sampleSheet}.log")" | ||
body="${SCRIPT_NAME} detected an error when parsing ${sampleSheet} on $(hostname -s): $(<"${sampleSheet}.converted.log")" | ||
footer='Cheers from the GCC.' | ||
# | ||
# Send email to notify users. | ||
# | ||
printf '%s\n\n%s\n\n%s\n' "${header}" "${body}" "${footer}" \ | ||
| mail -s "Samplesheet is wrong on $(hostname -s)" "${mailAddress:-}" | ||
touch "${sampleSheet}.log.mailed" | ||
| mail -s "Samplesheet is wrong on $(hostname -s)." "${mailAddress:-}" | ||
touch "${sampleSheet}.converted.log.mailed" | ||
fi | ||
fi | ||
done | ||
|
||
echo "INFO: finished processing samplesheets." |