Comment/code cleanup, fix failing on multiple files in destination directory. Proper CLI args.

This commit is contained in:
Josh Mudge 2018-10-20 17:31:13 -06:00
parent 741c2effca
commit a396e1457a
1 changed files with 28 additions and 38 deletions

View File

@ -1,4 +1,4 @@
# Sermon Upload v2.0 Alpha
# Sermon Upload v2.1 Alpha
import subprocess
from subprocess import call # We can do BASH in Python if needed.
@ -18,8 +18,7 @@ parser.add_argument("ssh")
args = parser.parse_args()
def upload(scrdir, dstdir, key, ssh): # Input has to end in /
#time.sleep(10) # Wait 10 seconds.
# time.sleep(10) # Wait 10 seconds to make sure the machine is booted if we're doing auto. NOTE: Change to only run when auto is specified.
os.chdir(scrdir) # Go to sermon directory.
list = sorted(glob.glob('*.mp3')) # Sort files alphabetically.
mostrecent = list[-1] # Find most recent sermon
@ -30,52 +29,43 @@ def upload(scrdir, dstdir, key, ssh): # Input has to end in /
copyfile(sermonsrc, sermondst) # Copy the sermon
# bash = subprocess.check_output('./sermon-upload.sh')
# print(bash)
os.chdir(dstdir) # Go to the directory where the sermon has been copied to
os.system("export sermondst")
os.environ['sermondst'] = str(sermondst)
# os.system(sermondst=sermondst)
#print(os.system("echo $sermondst"))
os.system("export key")
# Export some variables to BASH
# os.popen("export sermondst")
os.environ['sermondst'] = sermondst
# os.popen("export key")
os.environ['key'] = key
# os.system(key=key)
os.system("export ssh")
# os.popen("export ssh")
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.popen('rsync --update --progress -e "ssh -i $key" $mostrecent $ssh:$sermondst') # Upload the sermon.
# os.popen("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)
# os.popen("export recentsermon") # Export the list to Python
recentsermon = os.popen('ssh -i $key $ssh ls $sermondst').read() # Get most recent sermon from destination server.
# recentsermon = recentsermon[-1:]
recentsermon = recentsermon.rstrip() # Strip whitespace at the end of the string.
# recentsermon = recentsermon.split(',')
# print(recentsermon[-1])
# recentsermon = recentsermon[-1]
# recentsermon = "".join(recentsermon.split()) # Strip whitespace.
#print(mostrecent)
#print(recentsermon)
if str(mostrecent) == str(recentsermon): # If the most recent sermon uploaded matches the most recent sermon locally, we're good.
if recentsermon == sermondst:
notify2.init('Sermon Script')
# if str(mostrecent) == str(recentsermon): # If the most recent sermon uploaded matches the most recent sermon locally, we're good.
notify2.init('Sermon Script') # Notify user of success.
n = notify2.Notification("Sermon Uploaded",
mostrecent
)
n.show()
else: # Otherwise, something went wrong.
else: # Otherwise, something went wrong, say it failed.
notify2.init('Sermon Script')
n = notify2.Notification("Sermon Upload Failed",
@ -83,5 +73,5 @@ def upload(scrdir, dstdir, key, ssh): # Input has to end in /
)
n.show()
upload(args.scrdir, args.dstdir, args.key, args.ssh)
#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")