#!/usr/bin/env python from sys import argv, exit from os import getcwd, system, getenv from glob import glob from string import split program = 'getvex' version = '0.2' verdate = '20100201' author = 'Walter Brisken' projectdir = '/home/vlbiobs/astronomy' def usage(): print '\n%s ver. %s %s %s' % (program, version, verdate, author) print '\nA program to copy the needed files from a VLBA project for' print 'vex-based software correlation with DiFX' print '\nUsage: %s [options] []' % argv[0] print '\n options can include:' print ' --help' print ' -h print this message and quit\n' print ' --verbose' print ' -v print more diagnostics to screen\n' print ' --force' print ' -f run even if means overwriting exisiting files\n' print ' is the VLBA project code, including segment.' print ' Example: bg160 or bw088r\n' print ' is path in which to create a new subdirectory' print ' called . Default is the current directory.\n' exit(0) def getdir(project): g = glob('%s/*/%s' % (projectdir, project)) if len(g) == 1: return g[0] elif len(g) == 0: print 'Sorry, project %s not in %s.' % (project, projectdir) print '\nNote: project names are case sensitive.\n' exit(0) else: print 'How unusual: project %s is in multiple directories:' % project for d in g: print ' ', d print '' exit(0) def run(cmd, verbose): if verbose: print 'Executing: %s' % cmd system(cmd) # main below here project = None basedir = None force = False verbose = False stop = False for arg in argv[1:]: kv = split(arg, '=') if len(kv) == 2: pass elif len(kv) == 1: if arg[0] == '-': if arg in ['-h', '--help']: usage() elif arg in ['-f', '--force']: force = True elif arg in ['-v', '--verbose']: verbose = True else: print 'Error: unknown command line option: %s' % a stop = True elif project == None: project = arg elif basedir == None: basedir = arg else: print 'Error: extra command line argument: %s' % arg stop = True if project == None: print 'Error: project name not provided on command line' stop = True if stop: print '\nRun with -h for help information.\n' exit(0) if basedir == None: basedir = getcwd() prjdir = getdir(project) newdir = basedir+'/'+project if not force: if len(glob(newdir)) > 0: print "Won't clobber existing directory. Use -f to force." exit(0) cmd = 'mkdir -p %s' % newdir run(cmd, verbose) cmd = 'cp %s/*.oms %s' % (prjdir, newdir) run(cmd, verbose) if len(glob(prjdir + '/*.vex')) > 0: cmd = 'cp %s/*.vex %s' % (prjdir, newdir) run(cmd, verbose) if len(glob(prjdir + '/*.skd')) > 0: cmd = 'cp %s/*.skd %s' % (prjdir, newdir) run(cmd, verbose) cmd = 'cp %s/*log.vlba %s' % (prjdir, newdir) run(cmd, verbose) cmd = 'cd %s ; oms2v2d %s.oms' % (newdir, project) run(cmd, verbose) calfiles = glob('%s/%scal.vlba*' % (prjdir, project)) if len(calfiles) == 0: print 'Warning: %scal.vlba not found in %s' % (project, prjdir) print ' continuing anyway' else: calfile = calfiles[0] cmd = 'cp %s %s' % (calfile, newdir) run(cmd, verbose) if calfile[-3:] == '.gz': cmd = 'gunzip %s/%scal.vlba.gz' % (newdir, project) run(cmd, verbose) cmd = 'cd %s ; vlog %scal.vlba' % (newdir, project) run(cmd, verbose) # set ownership of everything at the end groupId = getenv('DIFX_GROUP_ID') if groupId != None: cmd = 'chown -R :%s %s' % (groupId, newdir) run(cmd, verbose) cmd = 'chmod -R g+w %s' % newdir run(cmd, verbose) cmd = 'chmod +s %s' % newdir run(cmd, verbose) print ''