Skip to content

Commit

Permalink
Merge pull request #3 from Laura190/replaceMv
Browse files Browse the repository at this point in the history
Replace mv
  • Loading branch information
Laura190 authored Mar 11, 2022
2 parents 3350a55 + a24cd36 commit 86e0b33
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.pass.txt
OldData.list
*.list
TestImport.sh
subfunctiontest.sh
tmp.cfg
Expand Down
37 changes: 14 additions & 23 deletions Examples/MicroscopeImport.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
#!/bin/bash

##Import for individual Windows computer
# Parameters:
# Name of microscope
micro=laura
# Location where data is stored
path=/mnt/d/Data
# Location of Imported folder
imp_path=$path/Imported
# Location where log files will be stored
archive=$path/ImportLogs
# Location of password file
pass_file=/home/user/.pass.txt
# Path to OMERO environment
env=/home/user/omero-env
# Path to configuration file
conf_file=/home/user/import.cfg

#Moves old data
bash OMEROimport move_old --location="$path/"
## Import for individua Windows computer
#Read config file
config=$( bash OMEROimport check_config )
if [ "${config//ERROR/}" != "${config}" ]; then
echo "${config}"
exit 1
else
source $config
fi

#Import data for each folder on path
log_files=()
Expand All @@ -27,16 +17,18 @@ for folder in "$path"/*; do
if [ $(basename "$folder") != "Imported" ] && [ $(basename "$folder") != "OldData" ] && [ $(basename "$folder") != "imported" ]; then
user=$(bash OMEROimport read_file --file="$folder"/.username.txt)
if [[ $user != ERROR* ]]; then
result=$(bash OMEROimport import_folder --password_file="$pass_file" \
--environment="$env" --user="$user" --folder="$folder" \
--config="$conf_file")
# Move old data
bash OMEROimport move_old --location="$folder"
# Import data
result=$(bash OMEROimport import_folder --user="$user" --folder="$folder")
else
result=$user
fi
if [ "${result//=== No files/}" != "${result}" ] || \
[ "${result//ERROR/}" != "${result}" ]; then
echo "${result}"
else
bash OMEROimport move_imports --logfile="${result}" --target="$imp_path"
log_files[$i]="$result"
i=$((i+1))
fi
Expand All @@ -45,7 +37,6 @@ done

# Move imported files to imp_path
for log in ${log_files[@]}; do
bash OMEROimport move_imports --logfile="$log" --target="$imp_path"
mv "$log" "$archive"/"$micro"_$(date +%Y%m%d)_"$log"
rm "Before_$log"
done
Expand Down
8 changes: 4 additions & 4 deletions Examples/import.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
# Name of microscope
micro=microscope
# Location where data is stored
path=/home/laura/WMS_Files/CAMDU/SoftwareDevelopment/Autoimport/test/testDrive
path=/home/data
# Location of Imported folder
imp_path=$path/Imported
# Location where log files will be stored
archive=$path/ImportLogs
# Location of encrypted password file
pass_file=/home/laura/WMS_Files/CAMDU/SoftwareDevelopment/OMEROimport/.pass.txt
pass_file=/home/user/.pass.txt
# Path to OMERO environment
enviro=/home/user/omero-env

## Server
# OMERO server details
server=camdu.warwick.ac.uk
server=my.omero.server
port=4064
# Imported user
username=importer1
importer=importer
6 changes: 2 additions & 4 deletions commands/check_config
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function check_config::run () {
fi

## Define variables

local file=$(get_file "$file_path")
check_error $file
declare -a list=("micro" "path" "imp_path" "archive" "pass_file" "enviro" "server" "port" "importer")
Expand Down Expand Up @@ -56,11 +55,11 @@ get_file() {
fi
}

# Read password from password file
source_file() {
local configfile="${1}"
local configfile_secured='tmp.cfg'
local micro path imp_path archive pass_file enviro server port importer
declare -a list=("micro" "path" "imp_path" "archive" "pass_file" "enviro" "server" "port" "importer")
for i in ${list[@]}; do local $i; done

# check if the file contains something we don't want
if egrep -q -v '^#|^[^ ]*=[^;]*' "$configfile"; then
Expand All @@ -69,7 +68,6 @@ source_file() {
egrep '^#|^[^ ]*=[^;&]*' "$configfile" > "$configfile_secured"
configfile="$configfile_secured"
fi

# now source it, either the original or the filtered variant
source "$configfile"

Expand Down
6 changes: 2 additions & 4 deletions commands/import_folder
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ EOT

function import_folder::usage() {
cat <<EOT
-c --config mandatory config file containing server info and importer details
-c --config optional config file containing server info and importer details, if not given will look for import.cfg
-f --folder mandatory Folder of data to be imported
-u --user mandatory OMERO (LDAP) user ID, who data will be
imported for
Expand All @@ -32,8 +32,7 @@ function import_folder::run() {
# Check if config file is given
local conf=$(readopt -c --config)
if [ -z "${conf}" ]; then
echo "ERROR: import_folder - no config file given with --config"
exit 1
conf="import.cfg"
fi

# Check if user is given
Expand Down Expand Up @@ -149,7 +148,6 @@ import() {
done
logname=$logname-$i
fi
touch -- "$logname".log
#Increase Java Heap size for imports
export JAVA_OPTS=-Xmx4G
#Check if there are any files to import
Expand Down
21 changes: 6 additions & 15 deletions commands/move_imports
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function move_imports::run () {

local summary=$(get_summary "$img_log")
check_error $summary
echo "$summary"

local images=$(get_imported_images "$img_log")
check_error $images
Expand All @@ -57,10 +58,6 @@ function move_imports::run () {
local result=$(move_imports "$target")
check_error $result

#Print warnings
local errors=$(check_omero_errors "$summary" "$user")
echo $errors

# Print message if successful
echo "=== Successfully moved imported files"
}
Expand Down Expand Up @@ -118,28 +115,28 @@ get_summary() {
get_imported_images() {
local log_file=${1}
# List of imported image files
images=$(grep -o 'IMPORT_DONE Imported file:.*' $log_file | cut -f 4- -d ' ')
echo $images
grep -o 'IMPORT_DONE Imported file:.*' $log_file | cut -f 4- -d ' ' > importedImages.list
echo importedImages.list
}

# Get files associated to imported image files and save to temp file
get_imported_files(){
local grp_file=${1}
local images=${3}
# If it exists remove importsFiles so do not append to already existing file
rm -f importedFiles.list 2> /tmp/om_imp
rm -f allImportedFiles.list 2> /tmp/om_imp
# Parse group log file to get all associated/additional files
local end=$(grep "#===" $grp_file | head -1)
local start
local escaped_start
for img in $images; do
cat ${3} | while read img; do
start=$(grep "# Group: $img" $grp_file | head -1)
escaped_start=$(printf '%s\n' "$start" | sed -e 's/[\/&]/\\&/g')
#Read all the lines between, and including, escaped_start and end; then delete first and last lines
sed -n "/${escaped_start}/,/${end}/ p" $grp_file | sed '1d' | sed '$d' >> allImportedFiles.list
done
sort allImportedFiles.list | uniq > importedFiles.list
cat importedFiles.list
}

# Move imports data to imported folder
Expand All @@ -155,7 +152,7 @@ move_imports() {
if [ $? -ne 0 ]; then
echo "ERROR: unable to create folder for $newFilename"
fi
mv "$filename" "$newFilename"
rsync -a --remove-source-files "$filename" "$newFilename"
ec=$?
if [ $ec -ne 0 ]; then
echo "ERROR: errorcode: $ec, unable to move $filename"
Expand Down Expand Up @@ -187,12 +184,6 @@ check_file() {
fi
}

check_omero_errors() {
local errors=$(echo "${1}" | cut -d ' ' -f 10)
local user=${2}
echo "Warning: OMERO reported $errors errors occured when importing files for $user"
}

# Check for errors - all error messages start with ERROR:
# If doing multiple levels of functions, send to error file and then parse file at end
check_error() {
Expand Down
4 changes: 2 additions & 2 deletions commands/move_old
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ move_old_data() {
echo "ERROR: unable to create OldData folder"
exit 1
fi
mv "$filename" "$newFilename"
rsync -a --remove-source-files "$filename" "$newFilename"
if [ $? -ne 0 ]; then
echo "ERROR: unable to move $filename to $newFilename"
exit 1
Expand Down Expand Up @@ -116,7 +116,7 @@ move_DV_olddata() {
# Send command to remote microscope computer to move files
# Need to point to specific ssh key to prevent request for password
printf -v makeStr 'mkdir --parents %q' "$(dirname "$newFilename")"
printf -v moveStr1 'mv %q %q/' "$oldFilename" "$(dirname "$newFilename")"
printf -v moveStr1 'rsync -a --remove-source-files %q %q/' "$oldFilename" "$(dirname "$newFilename")"
# -n needed to prevent ssh breaking while loop
ssh -n camdu@$IP -i $ssh_key $makeStr
if [ $? -ne 0 ]; then
Expand Down

0 comments on commit 86e0b33

Please sign in to comment.