Lovely broken more portable code. More PHW exercises.

This commit is contained in:
Josh Mudge 2018-10-20 13:34:37 -06:00
parent 091bd47256
commit c75ec75627
5 changed files with 87 additions and 28 deletions

View File

@ -1,4 +1,4 @@
# Sermon Upload v1.1 Alpha
# Sermon Upload v1.2 Alpha
from subprocess import call # We can do BASH in Python if needed.
import os # Lots of system stuff
@ -7,27 +7,33 @@ import glob # Import glob tools for file listings.
import notify2 # Notification utility.
import time # Import time into the galaxy
time.sleep(10) # Wait 10 seconds.
os.chdir("/mnt/52265C99265C8041/Users/Berean-Sound/sermons") # Go to sermon directory.
list = sorted(glob.glob('*.mp3')) # Sort files alphabetically.
mostrecent = list[-1] # Find most recent sermon
secondrecent = list[-2] # Find second most recent sermon
def upload(sermonsrc, sermondst, key): # Input has to end in /
sermonsrc = "/mnt/52265C99265C8041/Users/Berean-Sound/sermons/" + mostrecent # Grab the most recent sermon to copy.
sermondst = "/home/berean/sermons/2018/" + mostrecent # Destination for most recent sermon.
time.sleep(10) # Wait 10 seconds.
os.chdir(sermonsrc) # Go to sermon directory.
list = sorted(glob.glob('*.mp3')) # Sort files alphabetically.
mostrecent = list[-1] # Find most recent sermon
secondrecent = list[-2] # Find second most recent sermon
copyfile(sermonsrc, sermondst) # Copy the sermon
sermonsrc = sermonsrc + mostrecent # Grab the most recent sermon to copy.
sermondst = sermondst + mostrecent # Destination for most recent sermon.
os.chdir("/home/berean/sermons/2018/") # Go to the directory where the sermon has been copied to
os.system("rsync --update --progress -e 'ssh -i /home/berean/sermons/.tempssh/temp' *.mp3 sermons@bereanbibleutah.org:/home/josh/sermons/2018/") # Upload the sermon.
copyfile(sermonsrc, sermondst) # Copy the sermon
call(sermons=$("ssh -i /home/berean/sermons/.tempssh/temp sermons@bereanbibleutah.org 'ls /home/josh/sermons/2018'") # Grab a list of uploaded sermons.
call("export sermons") # Export the list to Python
recentsermon = os.environ["sermons"] # Set it to a variable
recentsermon = recentsermon[-1] # Try to grab most recent sermon.
print(recentsermon) # Print most recent sermon.
os.chdir(sermondst) # Go to the directory where the sermon has been copied to
call(sermondst=sermondst)
call("export sermondst")
print(call("echo 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.
call("findsermon='ls $sermondst'")
call("export findsermon")
call('sermons=$("ssh -i $key sermons@bereanbibleutah.org $findsermon")')
call("export sermons") # Export the list to Python
recentsermon = os.environ["sermons"] # Set it to a variable
recentsermon = recentsermon[-1] # Try to grab most recent sermon.
print(recentsermon) # Print most recent sermon.
if mostrecent == recentsermon: # If the most recent sermon uploaded matches the most recent sermon locally, we're good.
if mostrecent == recentsermon: # If the most recent sermon uploaded matches the most recent sermon locally, we're good.
notify2.init('Sermon Script')
n = notify2.Notification("Sermon Uploaded",
@ -35,10 +41,12 @@ if mostrecent == recentsermon: # If the most recent sermon uploaded matches the
)
n.show()
else: # Otherwise, something went wrong.
else: # Otherwise, something went wrong.
notify2.init('Sermon Script')
n = notify2.Notification("Sermon Upload Failed",
mostrecent
)
n.show()
upload("/mnt/52265C99265C8041/Users/Berean-Sound/sermons", "/home/josh/sermons/2018", "/home/berean/sermons/.tempssh/temp")

27
ex17.py Normal file
View File

@ -0,0 +1,27 @@
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print(f"Copying from {from_file} to {to_file}")
# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read()
#print(f"The input file is {len(indata)} bytes long")
#print(f"Does the output file exist? {exists(to_file)}")
#print("Ready, hit RETURN to continue, CRTL-C to abort.")
#input()
out_file = open(to_file, 'w')
out_file.write(indata)
print("Alright, all done.")
out_file.close() # Close file to save resources.
in_file.close()
# 1, 3, 4, 5 Done.
# 2. nah

22
ex18.py Normal file
View File

@ -0,0 +1,22 @@
# this one is like your scripts with argv
def print_two(*args):
arg1, arg2 = args
print(f"arg1: {arg1}, arg2: {arg2}")
# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
print(f"arg1: {arg1}, arg2: {arg2}")
# this just takes one argument
def print_one(arg1):
print(f"arg1: {arg1}")
# this one takes no arguments
def print_none():
print("I got nothin'.")
print_two("Zed", "Shaw")
print_two_again("Zed", "Shaw")
print_one("First!")
print_none()

0
ex19.py Normal file
View File

View File

@ -4,3 +4,5 @@
pip install notify2
sudo apt-get install libgtk2.0-dev libdbus-1-dev
pip install dbus-python
#Put pip in $PATH by editing .bashrc and adding $PATH=PATH:/opt/python3.6/bin/