Skip to content

Instantly share code, notes, and snippets.

@mythmon
Created January 11, 2011 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mythmon/775368 to your computer and use it in GitHub Desktop.
Save mythmon/775368 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os
import json
import subprocess
def main():
BASEPATH = "/home/mythmon/code/better-vacation"
USERPATH = os.path.join(BASEPATH, "users")
HOMEPATH = "/home"
for root, dirs, files in os.walk(USERPATH):
for name in files:
# Only parse JSON files
if name[-4:] != 'json':
continue
# Read the JSON file.
try:
json_path = os.path.join(root, name)
json_file = open(json_path)
json_data = json.load(json_file)
finally:
json_file.close()
# Update the message file, but only if the json was modifed since
# the last time the message has been updated.
message_path = os.path.join(HOMEPATH, json_data['user'], '.vacation.msg')
try:
mtime_message = os.stat(message_path).st_mtime
except OSError:
# file doesn't exist, create it.
mtime_message = -1
try:
mtime_json = os.stat(json_path).st_mtime
except OSError:
print("JSON File '{0}' mysteriously vanished. Skipping...".format(name))
if mtime_json > mtime_message:
try:
message_file = open(message_path, 'w')
message_file.write(json_data['message'])
finally:
message_file.close()
if json_data['prev'] == 'off' and json_data['cur'] == 'on':
# Turn vacation responder on
p = subprocess.Popen(['su', '-c', '/usb/bin/vacation -I', json_data['user'])
elif json_data['prev'] == 'on' and json_data['cur'] == 'off':
# Turn vacation responder off
# delete the .db file
db_path = os.path.join(HOMEPATH, json_data['user'], '.vacation.db')
try:
os.remove(db_path)
except:
pass
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment