Last active
January 24, 2018 12:40
-
-
Save smithbr/30060c9781da25532498ba84e313d966 to your computer and use it in GitHub Desktop.
Startup script for my mac. Mount shares and start applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
"""Startup script.""" | |
import os | |
from subprocess import check_output | |
DRIVE_PARENT = '/Users/myusername/Shares' | |
DRIVES = ['Download', | |
'Movies', | |
'TV', | |
'USBDisk1', | |
'USBDisk2', ] | |
SABNZND_PARENT = '/Applications/SABnzbd.app/Contents/MacOS' | |
APPS = [['SABnzbd', 'SABnzbd', ], | |
['CouchPotato', 'CouchPotato'], | |
['Sonarr', 'mono'], | |
['Deluge', 'deluge'], ] | |
def map_drive(this_drive): | |
"""Check if a drive is mounted, and if not mount it.""" | |
drive_abs_path = '%s/%s' % (DRIVE_PARENT, this_drive) | |
if os.path.isdir(drive_abs_path): | |
if os.listdir(drive_abs_path) == []: | |
mount_drive(this_drive, drive_abs_path) | |
print 'Mounted drive %s.' % drive_abs_path | |
else: | |
print '%s is already mounted.' % drive_abs_path | |
else: | |
os.mkdir('%s' % drive_abs_path) | |
mount_drive(this_drive, drive_abs_path) | |
print 'Mounted drive %s.' % drive_abs_path | |
def mount_drive(m_drive, m_drive_abs_path): | |
"""Mount the drive.""" | |
try: | |
os.system('mount_smbfs //admin:brandon@nasa/%s %s' % (m_drive, | |
m_drive_abs_path)) | |
except: | |
raise Exception('Could not mount drives!') | |
def start_app(this_app): | |
"""Start an application.""" | |
app_name = this_app[0] | |
app_pid_name = this_app[1] | |
try: | |
app_pid = check_output('pgrep %s' % app_pid_name, shell=True).strip('\r\n') | |
except: | |
app_pid = False | |
if app_pid: | |
print '%s is already running. PID = %s' % (app_name, app_pid) | |
else: | |
if app_name == 'SABnzbd': # Special circumstance for SABnzbd | |
app_full_name = '%s/%s' % (SABNZND_PARENT, app_name) | |
else: | |
app_full_name = app_name | |
os.system('open -a %s' % app_full_name) | |
print 'Started %s' % app_name | |
def send_email(error_message): | |
"""Send me an email.""" | |
import smtplib | |
fromaddr = 'myusername@gmail.com' | |
toaddrs = fromaddr | |
msg = ("From: %s\r\nTo: %s\r\n\r\n" % (fromaddr, toaddrs)) | |
line = 'Mac Mini mount problem! \n\n%s' % error_message | |
msg = msg + line | |
server = smtplib.SMTP('smtp.gmail.com:587') | |
server.starttls() | |
server.login('myusername@gmail.com', 'password') | |
server.sendmail(fromaddr, toaddrs, msg) | |
server.quit() | |
print 'Something went wrong. Email sent.' | |
if __name__ == '__main__': | |
try: | |
for drive in DRIVES: | |
map_drive(drive) | |
for app in APPS: | |
start_app(app) | |
except Exception, e: | |
send_email(str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment