Use argparse to take CLI input, check if sermon uploaded and notify the user of success/failure.

Este commit está contenido en:
Josh Mudge 2018-10-20 16:37:27 -06:00
padre c75ec75627
commit 741c2effca
Se han modificado 3 ficheros con 101 adiciones y 30 borrados

Ver fichero

@ -1,3 +1,3 @@
# Sermon Upload # Sermon Upload
Finds latest sermon, uploads it, notifies the user. Sounds simple, it isn't. Finds latest sermon based on input, uploads it, notifies the user whether or not it works. Sounds simple, it isn't.

Ver fichero

@ -1,39 +1,73 @@
# Sermon Upload v1.2 Alpha # Sermon Upload v2.0 Alpha
import subprocess
from subprocess import call # We can do BASH in Python if needed. from subprocess import call # We can do BASH in Python if needed.
import os # Lots of system stuff import os # Lots of system stuff
from shutil import copyfile # Allow copying of files from shutil import copyfile # Allow copying of files
import glob # Import glob tools for file listings. import glob # Import glob tools for file listings.
import notify2 # Notification utility. import notify2 # Notification utility.
import time # Import time into the galaxy import time # Import time into the galaxy
import re
import argparse
def upload(sermonsrc, sermondst, key): # Input has to end in / parser = argparse.ArgumentParser(usage="python3 sermon-upload.py source destination keyfile sshinfo \nFor example, python3 sermon-upload.py /home/josh/Downloads/Downloads/ /home/josh/Downloads/Downloads2/ /home/josh/.ssh/id_rsa josh@127.0.0.1")
parser.add_argument("scrdir")
parser.add_argument("dstdir")
parser.add_argument("key")
parser.add_argument("ssh")
args = parser.parse_args()
time.sleep(10) # Wait 10 seconds. def upload(scrdir, dstdir, key, ssh): # Input has to end in /
os.chdir(sermonsrc) # Go to sermon directory.
#time.sleep(10) # Wait 10 seconds.
os.chdir(scrdir) # Go to sermon directory.
list = sorted(glob.glob('*.mp3')) # Sort files alphabetically. list = sorted(glob.glob('*.mp3')) # Sort files alphabetically.
mostrecent = list[-1] # Find most recent sermon mostrecent = list[-1] # Find most recent sermon
secondrecent = list[-2] # Find second most recent sermon secondrecent = list[-2] # Find second most recent sermon
sermonsrc = sermonsrc + mostrecent # Grab the most recent sermon to copy. sermonsrc = scrdir + mostrecent # Grab the most recent sermon to copy.
sermondst = sermondst + mostrecent # Destination for most recent sermon. sermondst = dstdir + mostrecent # Destination for most recent sermon.
copyfile(sermonsrc, sermondst) # Copy the sermon copyfile(sermonsrc, sermondst) # Copy the sermon
os.chdir(sermondst) # Go to the directory where the sermon has been copied to # bash = subprocess.check_output('./sermon-upload.sh')
call(sermondst=sermondst) # print(bash)
call("export sermondst") os.chdir(dstdir) # Go to the directory where the sermon has been copied to
print(call("echo sermondst")) os.system("export sermondst")
os.system("rsync --update --progress -e 'ssh -i /home/berean/sermons/.tempssh/temp' *.mp3 sermons@bereanbibleutah.org:/home/josh/sermons/2018/") # Upload the sermon. os.environ['sermondst'] = str(sermondst)
call("findsermon='ls $sermondst'") # os.system(sermondst=sermondst)
call("export findsermon") #print(os.system("echo $sermondst"))
call('sermons=$("ssh -i $key sermons@bereanbibleutah.org $findsermon")') os.system("export key")
call("export sermons") # Export the list to Python os.environ['key'] = key
recentsermon = os.environ["sermons"] # Set it to a variable # os.system(key=key)
recentsermon = recentsermon[-1] # Try to grab most recent sermon. os.system("export ssh")
print(recentsermon) # Print most recent sermon. os.environ['ssh'] = ssh
# os.system(ssh=ssh)
os.system('rsync --update --progress -e "ssh -i $key" *.mp3 $ssh:$sermondst') # Upload the sermon.
os.system("export dstdir")
os.environ['dstdir'] = dstdir
# os.system(dstdir=dstdir)
# os.system("findsermon='ls $dstdir'")
# os.system("export findsermon")
# os.environ['findsermon'] = 'ls $dstdir'
os.system("export recentsermon") # Export the list to Python
# subprocess.call("ssh -i $key $ssh ls $dstdir")
recentsermon = os.popen('ssh -i $key $ssh ls $dstdir').read()
# print(re.sub(' +', ' ', recentsermon))
recentsermon = "".join(recentsermon.split()) # Strip whitespace.
# recentsermon = re.sub (r'([^(\s+)])', '', recentsermon)
# print(recentsermon)
# print(ser)
# os.system(sermons=$("ssh -i $key $ssh ls $dstdir"))
# os.environ["recentsermon"] # Set it to a variable
# comparesermon = ""
# for c in recentsermon:
# comparesermon = comparesermon + recentsermon[c] # Try to grab most recent sermon.
# print(recentsermon) # Print most recent sermon.
#print(recentsermon)
#print(mostrecent)
if mostrecent == recentsermon: # If the most recent sermon uploaded matches the most recent sermon locally, we're good. if str(mostrecent) == str(recentsermon): # If the most recent sermon uploaded matches the most recent sermon locally, we're good.
notify2.init('Sermon Script') notify2.init('Sermon Script')
n = notify2.Notification("Sermon Uploaded", n = notify2.Notification("Sermon Uploaded",
@ -49,4 +83,5 @@ def upload(sermonsrc, sermondst, key): # Input has to end in /
) )
n.show() n.show()
upload("/mnt/52265C99265C8041/Users/Berean-Sound/sermons", "/home/josh/sermons/2018", "/home/berean/sermons/.tempssh/temp") #upload("/mnt/52265C99265C8041/Users/Berean-Sound/sermons", "/home/josh/sermons/2018", "/home/berean/sermons/.tempssh/temp", "sermons@bereanbibleutah.org")
#upload("/home/josh/Downloads/Downloads/", "/home/josh/Downloads/Downloads2/", "/home/josh/.ssh/id_rsa", "josh@127.0.0.1")

Ver fichero

@ -1,16 +1,52 @@
cd /home/berean/sermons/2018 while [[ $# -gt 0 ]]
sermons=$(ls /mnt/52265C99265C8041/Users/Berean-Sound/sermons) do
key="$1"
case $key in
--srcdir)
srcdir="$2"
shift # past argument
;;
--dstdir)
dstdir="$2"
shift # past argument
;;
--key)
key="$2"
shift # past argument
;;
--ssh)
ssh="$2"
shift # past argument
;;
--file)
file="$2"
shift # past argument
;;
*)
# unknown option
if test -z "${unknown}"
then
unknown=$1
else
echo "Unknown Option"
exit 1
fi
;;
esac
shift # past argument or value
done
cd $dstdir
sermons=$(ls $srcdir)
IFS=$'\n' IFS=$'\n'
lastsermon=$(echo "${sermons[*]}" | sort -nr | head -n1) lastsermon=$(echo "${sermons[*]}" | sort -nr | head -n1)
cp /mnt/52265C99265C8041/Users/Berean-Sound/sermons/$lastsermon . cp $srcdir/$lastsermon .
rsync --update --progress -e "ssh -i /home/berean/sermons/.tempssh/temp" *.mp3 sermons@bereanbibleutah.org:/home/josh/sermons/2018/ rsync --update --progress -e "ssh -i $key" *.mp3 $ssh:$dstdir
sermons=$(ssh -i /home/berean/sermons/.tempssh/temp sermons@bereanbibleutah.org 'ls /home/josh/sermons/2018') sermons=$(ssh -i $key $ssh 'ls $dstdir')
IFS=$'\n' IFS=$'\n'
recentsermon=$(echo "${sermons[*]}" | sort -nr) #| head -n2) recentsermon=$(echo "${sermons[*]}" | sort -nr) #| head -n2)
echo $recentsermon echo $recentsermon
#cd ..
#cd 2016
#rsync --progress -e "ssh -i ../.tempssh/temp" *.mp3 sermons@bereanbibleutah.org:/home/josh/sermons/2016/