Created
May 13, 2021 11:16
-
-
Save rameezk/5113057794852ab801223a9b81a26b09 to your computer and use it in GitHub Desktop.
Sync remote library changes for Pycharm
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
#!/usr/bin/bash | |
dest_dir="dest_dir" | |
watch_dir="watch_dir" | |
function sync() { | |
rsync -arhv --exclude ".git/" --delete "$1/" "$dest_dir" | |
} | |
function get_relative_path() { | |
fixed_path=$(echo "$1" | sed "s#$watch_dir/#$dest_dir/#") | |
echo $fixed_path | |
} | |
function delete_dest_file_dir() { | |
path_to_delete=$(get_relative_path "$1") | |
echo "rm -rf $path_to_delete" | |
rm -rf "$path_to_delete" | |
} | |
function copy_dest_file_dir() { | |
path_to_copy_from="$1" | |
path_to_copy_to=$(get_relative_path "$1") | |
echo "cp -r "$path_to_copy_from" $path_to_copy_to" | |
cp -r "$path_to_copy_from" "$path_to_copy_to" | |
} | |
echo "** Running initial rsync" | |
sync "$watch_dir" | |
echo "** Starting watcher" | |
inotifywait --monitor --recursive -e close_write,delete,create --format '%w %f %e' "$watch_dir" | while read -r dir file event; do | |
echo "dir = $dir" | |
echo "file = $file" | |
echo "event = $event" | |
if [ "$event" == "CREATE" ]; then | |
echo "** Create event" | |
echo "Will copy $dir$file" | |
copy_dest_file_dir "$dir$file" | |
elif [ "$event" == "CLOSE_WRITE,CLOSE" ]; then | |
echo "** Copy event" | |
echo "Will copy $dir$file" | |
copy_dest_file_dir "$dir$file" | |
elif [ "$event" == "DELETE" ]; then | |
echo "** Delete event" | |
echo "Will delete $dir$file" | |
delete_dest_file_dir "$dir$file" | |
elif [ "$event" == "CREATE,ISDIR" ]; then | |
echo "** Create directory event" | |
echo "Will create directory at $dir$file" | |
copy_dest_file_dir "$dir$file" | |
elif [ "$event" == "DELETE,ISDIR" ]; then | |
echo "** Delete directory event" | |
echo "Will delete directory at $dir$file" | |
delete_dest_file_dir "$dir$file" | |
else | |
echo "** Unknown event $event. Ignoring" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment