#!/usr/bin/env python from sys import exit, argv, stdout, stdin from os import getenv, system from string import upper, strip from os.path import isdir import cx_Oracle program = 'difxclean' version = '0.1' author = 'Walter Brisken' verdate = '20090626' def usage(prog): print '\n%s ver %s %s %s' % (program, version, author, verdate) print '\nA program to free disk space after a project is correlated.' print '\nUsage: %s [options] ' % prog print """ Options can include: --help -h Print this help information and quit --pretend -p Pretend to operate is the name of a project, e.g., mt912 or bc120ac """ exit(0) # Splits a combined obs code into its proposal and segment portions def splitobscode(exper): obsSeg = '' proposal = exper[:] if len(proposal) > 3: if proposal[0].isalpha() and proposal[1].isalpha() and proposal[2].isdigit(): for i in range(3, len(proposal)): if proposal[i].isalpha(): obsSeg = proposal[i:] proposal = proposal[0:i] break if proposal[0].isalpha() and proposal[1].isdigit(): for i in range(2, len(proposal)): if proposal[i].isalpha(): obsSeg = proposal[i:] proposal = proposal[0:i] break return proposal, obsSeg project = None; pretend = False for a in argv[1:]: if a[0] == '-': if a in ['-h', '--help']: usage(argv[0]) elif a in ['-p', '--pretend']: pretend = True else: if project == None: project = a else: print 'Only one project can be specified' exit(0) print '' if project == None: print 'No projects specified. Run with -h for help\n' exit(0) difxQueueBase = getenv('DIFX_QUEUE_BASE') if difxQueueBase == None: print 'Error: env var DIFX_QUEUE_BASE is not set. Cannot proceed.\n' exit(0) if len(difxQueueBase) < 4: print 'Suspicious value of difxQueueBase (%s)\nQuitting.\n' % \ difxQueueBase exit(0) dbname = getenv("VLBA_DB") if dbname == None: print 'Error: env var VLBA_DB is not set. Cannot proceed.\n' exit(0) prjDir = difxQueueBase + '/' + upper(project) if not isdir(prjDir): print 'Nothing to clean for project %s\nDir %s does not exist\n' % \ (project, prjDir) exit(0) if pretend: print '\nPRETEND MODE IS ON; NO DATA WILL BE DELETED\n' print 'The queue directory for project %s is %s' % (project, prjDir) print '\nContinuing will delete this directory, including any unarchived\ncorrelator output.' print '\nAre you sure you ant to continue? [y/N]' a = stdin.readline() if strip(a) in ['y', 'Y']: print 'OK, proceeding\n' else: print 'Not continuing.\n' exit(0) exper,seg = splitobscode(upper(project)) if len(seg) > 0: segstr = "and SEGMENT='%s' " % seg else: segstr = "" cmd = 'rm -rf %s' % prjDir dbcmd = "delete from DIFXQUEUE where PROPOSAL='%s' %sand STATUS!='COMPLETE'" % (exper, segstr) if pretend: print 'Pretending to execute: %s' % cmd print 'Pretending to execute: %s' % dbcmd else: database = cx_Oracle.connect(dbname) print 'Executing: %s' % cmd system(cmd) print 'Executing: %s' % dbcmd cursor = database.cursor() cursor.execute(dbcmd) database.commit()