Monday, June 29, 2009

Work session start/end script

There is a set of repetitive tasks I do when I develop something, like check-in, off-site backup, archive integrity checking, etc. To automate this, I wrote a little script that I invoke when I start and stop to work on something for this purpose. The start and end functions should be adapted to personal needs, this one is a bit bare-bone.

Here is what it looks like (syntax highlighted version here):

#! /usr/bin/env python3

"""
Work session start/end script.

This script contains the routines that should be performed before and after a
work session and log it. Currently these are git repository integrity checking
for start up. Change commiting and backup for end of session.

Uses 'rsync' for backup and 'git' for revision control, make sure they are in
place.

The BACKUP_PATH has to be adapted to the syncronization target.

This script is quite safe to use.
"""

import os, sys, subprocess, time, datetime

BACKUP_PATH = '/media/disk'
LOGFILE = 'session.log'

def log(message, logfile=LOGFILE):
timestamp = str(int(time.mktime(datetime.datetime.now().timetuple())))
name = os.path.basename(__file__)
with open(logfile, 'a') as f:
f.write('[' + timestamp + '] ' + name + ': ' + message + '\n')

def start():
status = subprocess.call(['git', 'gc'])
if status != 0:
print ('!! something went wrong with the repository checking!')
print ('(recheck manually with git fsck and restore integrity)')
sys.exit(1)

log('session started')

def end():
os.system('git add .')
os.system('git gui')

if os.path.isdir(BACKUP_PATH):
os.system('rsync -a . ' + BACKUP_PATH)
else:
print('backup path not existent, backup aborted..')

log('session ended')

if __name__ == '__main__':
usage = ('Usage: session.py start|end\n' +
' start: start session\n' +
' end: end session\n')

os.chdir(os.path.abspath(os.path.dirname(__file__)))

if len(sys.argv) == 1 or (
sys.argv[1] != 'start' and sys.argv[1] != 'end'):
print(usage)
sys.exit()

if sys.argv[1] == 'start':
start()
elif sys.argv[1] == 'end':
end()

No comments:

Post a Comment