-
Notifications
You must be signed in to change notification settings - Fork 2
/
pdflatex-remote
executable file
·119 lines (95 loc) · 4.97 KB
/
pdflatex-remote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
# Author: Gokberk Cinbis, 2021
# set -e # -- disabled currently to allow nonstopmode pdf compilation. can be handled in a better way.
function __increment_argi__() # just increment argi variable
{
argi=$((argi+1))
}
function __decrement_argi__() # just decrement argi variable
{
argi=$((argi-1))
}
_TEXARGS_DEFAULT="-interaction=nonstopmode"
function __help_and_exit__()
{
echo "pdflatex-remote [--id <project_id>] <src.tex>"
echo "Compile latex project on a remote server via rsync & pdflatex. Does the following:"
echo "- Transfer all (only those updated) files from current folder to a temporary folder in a remote host, efficiently via rsync. "
echo "- Run pdflatex and bibtex once on the remote host."
echo "- Transfer back the resulting pdf to the local host (overwrites if file already exists)."
echo ""
echo "ARGUMENTS"
echo "--id <id> Defines the remote temporary subfolder name. Useful for avoiding clashes across projects. (Default=current folder name)"
echo "<src.tex> Replace with main"
echo ""
echo "REQUIRED ENVIRONMENT VARIABLES"
echo "* PDFLATEX_REMOTE_HOST [username@]hostname (eg. [email protected])"
echo ""
echo "OPTIONAL ENVIRONMENT VARIABLES"
echo "* PDFLATEX_REMOTE_SSHPORT SSH port (default=22)"
echo "* PDFLATEX_REMOTE_RSYNCPATH rsync path on remote host (default=/usr/bin/rsync)"
echo "* PDFLATEX_REMOTE_TEX_ARGS arguments to pdflatex (default=$_TEXARGS_DEFAULT)"
echo ""
echo "NOTES"
echo "* It can be practical to define and export environment variables within .bashrc as they are typically project-agnostic."
echo "* .git folder (if exists) is ignored in rsync."
echo "* Local src.{pdf,aux,blg,bbl,log,out,aux,dvi} files are also ignored to avoid local/remote compilation conflicts."
echo "* Any file locally deleted will also be deleted on the remote server via rsync."
echo "* This utility is much more useful if ssh works without password, via a ssh key configuration."
echo ""
exit 1
}
function __main__()
{
local narg=$#
if [ $narg -lt 1 ]
then
__help_and_exit__
fi
# default project id
local _PROJID=`basename "$PWD"`
argi=0 # argument reading index
for (( argi=1; argi<=$((narg-1)); argi++ )); do
if [[ "${!argi}" == '--id' ]]; then
__increment_argi__
if [ $argi -eq $narg ]; then
__help_and_exit__ # end of arguments
fi
_PROJID="${!argi}"
else
command echo "Unrecognized argument: {!argi}"
__help_and_exit__
fi
done
local _TEXFILE="${!narg}" # last argument
# environemnt variables with defaults
local _SSHPORT="${PDFLATEX_REMOTE_SSHPORT:-22}"
local _RSYNCPATH="${PDFLATEX_REMOTE_RSYNCPATH:-/usr/bin/rsync}"
local _TEXARGS="${PDFLATEX_REMOTE_TEX_ARGS:-$_TEXARGS_DEFAULT}"
# define target temp directory that will host a copy of the current files. note that this shall not be stochastic because
# each call will *update* the tarrget host files as fast as possible via rsync.
local _TOPATH="/tmp/pdflatex-remote/$_PROJID"
# Create the temporary folder at target host if doesn't exist already
echo "pdflatex-remote::Create the temporary folder at the target host, if does not exist. ($_TOPATH on $PDFLATEX_REMOTE_HOST)"
ssh -p "$_SSHPORT" "$PDFLATEX_REMOTE_HOST" "command mkdir -p \"$_TOPATH\"" # works with spaces well
# define rsync paths
local _SRCPATH="." # source path is always current directory
local _TEXFILE_NOEXT="${_TEXFILE/.tex/}" # eg. paper.tex -> paper
echo "pdflatex-remote::rsync source from local to remote host."
# --exclude: exclude .git and temporary latex files to avoid overwriting remote temporary files
# --filter 'protect ...': avoid deletion of temporarry pdflatex files on the remote side to continue from previous compilation
rsync --protect-args --progress --stats --exclude='.git/' \
--exclude="$_TEXFILE_NOEXT.pdf" --exclude="$_TEXFILE_NOEXT.blg" --exclude="$_TEXFILE_NOEXT.bbl" --exclude="$_TEXFILE_NOEXT.log" --exclude="$_TEXFILE_NOEXT.out" --exclude="$_TEXFILE_NOEXT.aux" --exclude="$_TEXFILE_NOEXT.dvi" --filter "protect $_TEXFILE_NOEXT.*" \
--delete -avzh -e "ssh -p $_SSHPORT" \
--chmod=Du=rwx,Dgo=rx,Fu=rw,Fog=r --rsync-path="$_RSYNCPATH" "$_SRCPATH" "$PDFLATEX_REMOTE_HOST":"$_TOPATH"
# compile remotely
echo "pdflatex-remote::Compile on remote host."
ssh -p "$_SSHPORT" "$PDFLATEX_REMOTE_HOST" "(cd \"$_TOPATH\" && command pdflatex $_TEXARGS \"$_TEXFILE\" && bibtex \"$_TEXFILE_NOEXT\")" # works with spaces well
# transfer back
echo "pdflatex-remote::Transfering back the compiled pdf file."
rsync --protect-args --progress -vzh -e "ssh -p $_SSHPORT" --rsync-path="$_RSYNCPATH" \
"$PDFLATEX_REMOTE_HOST":"$_TOPATH/$_TEXFILE_NOEXT.pdf" "$_SRCPATH"
}
# ok with multiple quoted arguments
__main__ "$@"
exit 0