casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_sdflagmanager.py
Go to the documentation of this file.
00001 import os
00002 from taskinit import *
00003 
00004 import asap as sd
00005 import task_flagmanager
00006 import sdutil
00007 
00008 def sdflagmanager(infile, mode, versionname, oldname, comment, merge):
00009 
00010     casalog.origin('sdflagmanager')
00011     
00012     try:
00013         worker = sdflagmanager_worker(**locals())
00014         worker.initialize()
00015         worker.execute()
00016         worker.finalize()
00017     
00018     except Exception, instance:
00019         sdutil.process_exception(instance)
00020         raise Exception, instance
00021 
00022 class sdflagmanager_worker(sdutil.sdtask_template):
00023     def __init__(self, **kwargs):
00024         super(sdflagmanager_worker,self).__init__(**kwargs)
00025         self.infile_abs = sdutil.get_abspath(self.infile)
00026 
00027     def parameter_check(self):
00028         availablemodes = ['list', 'save', 'restore', 'delete', 'rename']
00029         if not (self.mode in availablemodes):
00030             raise Exception, "Unknown mode" + str(mode)
00031 
00032         namer = filenamer('sdflagmanager', self.infile_abs)
00033         self.msfile = namer.configure_name(kind='temp',suffix='ms')
00034         self.sdfverfile = namer.flagversion_name(self.infile_abs)
00035         self.msfverfile = namer.flagversion_name(self.msfile)
00036         self.backupfile = namer.configure_name(kind='backup',suffix='asap')
00037 
00038     def initialize_scan(self):
00039         self.scan = sd.scantable(self.infile_abs, average=False)
00040 
00041         fg.done()
00042 
00043     def execute(self):
00044         if os.path.exists(self.sdfverfile):
00045             move(self.sdfverfile, self.msfverfile)
00046 
00047         sdutil.save(self.scan, self.msfile, 'MS2', False)
00048         task_flagmanager.flagmanager(self.msfile,
00049                                      self.mode,
00050                                      self.versionname,
00051                                      self.oldname,
00052                                      self.comment,
00053                                      self.merge)
00054 
00055         if self.mode=='restore':
00056             # if a directory with the same name as backupinfile exists, rename it for backup
00057             del self.scan
00058             move(self.infile_abs, self.backupfile)
00059             sdutil.save(sd.scantable(self.msfile,False), self.infile_abs, 'ASAP', False)
00060 
00061         move(self.msfverfile, self.sdfverfile)
00062 
00063     def cleanup(self):
00064         remove(self.msfile)
00065 
00066 def domanage(infilename, mode, versionname, oldname, comment, merge):
00067     availablemodes = ['list','save','restore','delete','rename']
00068     if mode in availablemodes:
00069         namer = filenamer('sdflagmanager',infilename)
00070         msfilename = namer.configure_name(kind='temp',suffix='ms')
00071         # if directories 'msfilename' and 'msfilename.flagversions' already exist accidentally,
00072         # rename it for backup (backupms, backupmsfver)
00073         sdfverfile = namer.flagversion_name(infilename)
00074         msfverfile = namer.flagversion_name(msfilename)
00075 
00076         if os.path.exists(sdfverfile):
00077             move(sdfverfile, msfverfile)
00078 
00079         sdutil.save(sd.scantable(infilename,False), msfilename, 'MS2', False)
00080         task_flagmanager.flagmanager(msfilename, mode, versionname, oldname, comment, merge)
00081 
00082         if mode=='restore':
00083             backupinfile = namer.configure_name(kind='backup',suffix='asap')
00084             # if a directory with the same name as backupinfile exists, rename it for backup
00085             move(infilename, backupinfile)
00086             sdutil.save(sd.scantable(msfilename,False), infilename, 'ASAP', False)
00087 
00088         remove(msfilename)
00089         move(msfverfile, sdfverfile)
00090 
00091         # if backupms and backupmsfver exist, rename them back
00092         # to msfilename and msfilename.flagversions, respectively
00093     else:
00094         raise Exception, "Unknown mode" + str(mode)
00095 
00096 
00097 class filenamer(object):
00098     def __init__(self, task, infile):
00099         import datetime
00100         dt = datetime.datetime.today()
00101         timestamp = '%d%02d%02d%02d%02d%02d' % (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
00102         self.tmp = string.Template('%s-%s-${kind}-%s.${suffix}'%(infile,task,timestamp))
00103         self.flagver = 'flagversions'
00104     def configure_name(self,*args,**kwargs):
00105         return self.tmp.safe_substitute(**kwargs)
00106     def flagversion_name(self,filename):
00107         return string.join([filename.rstrip('/'),self.flagver],'.')
00108 
00109 def move(fromfile, tofile):
00110     os.system('mv %s %s'%(fromfile,tofile))
00111 def remove(filename):
00112     os.system('rm -rf %s'%(filename))