From 52f8fb5ea864442166aca4165a2f8a28ee01cdc5 Mon Sep 17 00:00:00 2001 From: "taha@luxor" Date: Mon, 31 Aug 2020 19:46:36 +0200 Subject: [PATCH] external assets for thesis now handled more robustly we can now find all external.assets file in the entire assets/ tree also copy operation does not overwrite --- chertex.sh | 54 +++++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/chertex.sh b/chertex.sh index 6699ef2..3dd4372 100755 --- a/chertex.sh +++ b/chertex.sh @@ -30,7 +30,12 @@ else echo "--- This job did not request LaTeXMK RC file" fi -# define some commands +# define some constants +path_wd=${PWD} +dir_wd=$(basename $path_wd) +path_thesis="/media/bay/taha/chepec/thesis" + +# define some file types Rfiletype="R" TeXfiletype="tex" tikzfiles="*.tikz" @@ -120,30 +125,23 @@ if [ $# -eq 1 ]; then fi #### Special treatment for thesis - if [[ ${PWD} == "/media/bay/taha/chepec/thesis" && $jobname == "thesis" ]]; then + if [[ $path_wd == "$path_thesis" && $jobname == "$dir_wd" ]]; then # Fetch external assets by reading any assets.external files in assets/ tree # NOTE: be careful NOT to leave empty lines in your assets.external files echo " -------------------------------" echo " Getting external assets" echo " -------------------------------" - # summary of operations in this if-clause: - # trawl the assets/ subdirectories looking for a file "external.assets", if found, - # read its contents line-by-line (each line is a path) and copy that file at that path to the current assets/ subdir - # note that we only look for assets.external inside the subdirectories, and not in assets/ itself - assetsubdirectories="$(find /media/bay/taha/chepec/thesis/assets/ -maxdepth 1 -mindepth 1 -type d)" - # loop through $assetdirs, and look for the file external.assets in each path - for assetdir in $assetsubdirectories; do - assetfilename="assets.external" - if [ -e ${assetdir}/${assetfilename} ]; then - # https://stackoverflow.com/questions/10929453/read-a-file-line-by-line-assigning-the-value-to-a-variable - # $assets is the current assets.external file - assets="${assetdir}/${assetfilename}" - while IFS='' read -r asset || [[ -n "$asset" ]]; do - echo "$asset" - # copy this filepath into the current assets subdirectory - cp --preserve=timestamps $asset $assetdir - done < "$assets" - fi + # find all files named "assets.external" in the assets/ tree + assetsexternalfiles=$(find "$path_wd/assets/" -type f -name "assets.external") + for externalfilepath in $assetsexternalfiles; do + # this weird-looking while-loop reads the assets.external file line-by-line and copies each asset into the target path inside the thesis' assets/ tree + # https://stackoverflow.com/questions/10929453/read-a-file-line-by-line-assigning-the-value-to-a-variable + while IFS='' read -r asset || [[ -n "$asset" ]]; do + assetpathtarget=$(dirname "$externalfilepath") + echo "Copying $asset to $assetpathtarget" + # cp but don't overwwrite existing files + cp --preserve=timestamps --no-clobber $asset $assetpathtarget + done < "$externalfilepath" done # Create low-res photos on-the-fly from existing photos/ @@ -157,15 +155,15 @@ if [ $# -eq 1 ]; then # rsync can't do that. We need to use a different tool. See e.g. # https://superuser.com/questions/260092/rsync-switch-to-only-compare-timestamps # copy only the "large" photos that have file modtimes more recent than the last time this operation was run - photoslastrun="/media/bay/taha/chepec/thesis/assets/photos/.lowres/lastrun" + photoslastrun="$path_wd/assets/photos/.lowres/lastrun" if [ ! -f "$photoslastrun" ]; then # if, for some reason, the lastrun file does not exist # copy over everything and then create the file - rsync -av /media/bay/taha/chepec/thesis/assets/photos/* /media/bay/taha/chepec/thesis/assets/photos/.lowres/ --exclude /media/bay/taha/chepec/thesis/assets/photos/.lowres/ + rsync -av "$path_wd/assets/photos/*" "$path_wd/assets/photos/.lowres/" --exclude "$path_wd/assets/photos/.lowres/" touch "$photoslastrun" fi # cd and use --parents arg to preserve directory structure in .lowres target - cd /media/bay/taha/chepec/thesis/assets/photos + cd "$path_wd/assets/photos" newphotos="$(find . -type f -cnewer $photoslastrun ! -path './.lowres/*')" if [ -n "$newphotos" ]; then for newphoto in $newphotos; do @@ -175,7 +173,7 @@ if [ $# -eq 1 ]; then # revert the effects of cd above. Redirect to null suppresses the output. cd - >/dev/null # in the low-res tree, find any photo larger than specific size (500kB) - largephotos="$(find /media/bay/taha/chepec/thesis/assets/photos/.lowres/ -size +500k)" + largephotos="$(find $path_wd/assets/photos/.lowres/ -size +500k)" for largephotofilename in $largephotos; do # for the next statement to work reliably, we should probably convert other formats to JPEG # detect file extension, and based on it, convert to jpg using mogrify @@ -299,8 +297,8 @@ else # exit 1 # fi #else - # # There is exactly one *.Rnw file is current directory - # # Fetch the jobname from the .Rnw filename by stripping off the file extension + ## There is exactly one *.Rnw file is current directory + ## Fetch the jobname from the .Rnw filename by stripping off the file extension # Rnwfilename=$(ls -1 $Rnwfiles) # jobname=${Rnwfilename%.*} #fi @@ -407,9 +405,7 @@ runtime=$(( $endtime - $starttime )) # send push message via Gotify CLI # if runtime is longer than X minutes (suitable limit perhaps 3 min) if (( $runtime > 180 )); then - # cwd is the current directory (basename) - cwd=$(basename $PWD) - gotify push --quiet --title "$cwd" --priority 5 "chertex.sh $@ \nCompleted in $runtime s" + gotify push --quiet --title "$dir_wd" --priority 5 "chertex.sh $@ \nCompleted in $runtime s" fi echo "-------------------------------------"