-
Notifications
You must be signed in to change notification settings - Fork 4
/
scriptRunner.py
executable file
·74 lines (62 loc) · 2.52 KB
/
scriptRunner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
# scriptRunner will run all scripts within a folder that you specify.
# Useful for LaunchAgents and running multiple scripts at user login.
# Thanks to Greg Neagle for an example of how to do this
import optparse
import os
import subprocess
import plistlib
import datetime
import sys
import stat
def main():
"""Main"""
p = optparse.OptionParser()
p.set_usage("""Usage: %prog [options]""")
p.add_option('--once', '-o', dest='runOnce',
help="""Directory of scripts to run only once.""")
p.add_option('--every', '-e', dest='runEvery',
help="""Directory of scripts to run every time.""")
options, arguments = p.parse_args()
# Check to see if passed options are a directory or not
for path in (options.runOnce, options.runEvery):
if path is not None:
if not os.path.isdir(path):
sys.exit(path + " is not a directory")
runOncePlist = os.path.expanduser("~/Library/Preferences/") +\
"com.company.scriptrunner.plist"
try:
runOncePlistValues = plistlib.readPlist(runOncePlist)
except IOError:
runOncePlistValues = {}
if options.runEvery:
for script in os.listdir(options.runEvery):
st = os.stat(os.path.join(options.runEvery, script))
mode = st.st_mode
if not mode & stat.S_IWOTH:
try:
subprocess.call(os.path.join(options.runEvery, script),
stdin=None)
except OSError:
print "Something went wrong!"
else:
print script + " is not executable or has bad permissions"
if options.runOnce:
for script in os.listdir(options.runOnce):
if script in runOncePlistValues:
print os.path.join(options.runOnce, script) + " already run!"
else:
st = os.stat(os.path.join(options.runOnce, script))
mode = st.st_mode
if not mode & stat.S_IWOTH:
try:
subprocess.call(os.path.join(options.runOnce, script),
stdin=None)
runOncePlistValues[script] = datetime.datetime.now()
except OSError:
print "Uh oh"
else:
print script + " is not executable or has bad permissions"
plistlib.writePlist(runOncePlistValues, runOncePlist)
if __name__ == '__main__':
main()