move all python code into the bash helper function

much tidy, fewer quoting, wow