casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
locatescript.py
Go to the documentation of this file.
00001 import sys
00002 import os
00003 import string
00004 import inspect
00005 import shutil
00006 
00007 a=inspect.stack()
00008 stacklevel=0
00009 for k in range(len(a)):
00010     if (string.find(a[k][1], 'ipython console') > 0):
00011         stacklevel=k
00012         break
00013 gl=sys._getframe(stacklevel).f_globals
00014 
00015 PYVER = str(sys.version_info[0]) + "." + str(sys.version_info[1])
00016 
00017 SCRIPT_REPOS = os.environ["CASAPATH"].split()[0] + '/' + os.environ["CASAPATH"].split()[1] + '/python/' + PYVER
00018 
00019 # To support casapy-test releases which have different structure
00020 SCRIPT_REPOS2 = os.environ["CASAPATH"].split()[0]
00021 SCRIPT_REPOS2 += "/lib64/python" + PYVER
00022 
00023 SCRIPT_REPOS3 = os.environ["CASAPATH"].split()[0]
00024 SCRIPT_REPOS3 += "/lib/python" + PYVER
00025 
00026 SCRIPT_REPOS4 = os.environ["CASAPATH"].split()[0]
00027 SCRIPT_REPOS4 += "/Resources/python"
00028 
00029 def locatescript(lescript=''):
00030     a=os.popen("which casapy", "r")
00031     lepath=string.split(a.read(),"/bin")[0]
00032     if (os.path.exists(SCRIPT_REPOS+"/regressions/"+lescript)):
00033         lepath = SCRIPT_REPOS+"/regressions/"+lescript
00034     elif (os.path.exists(SCRIPT_REPOS2+"/regressions/"+lescript)):
00035         lepath = SCRIPT_REPOS2+"/regressions/"+lescript
00036     elif (os.path.exists(SCRIPT_REPOS3+"/regressions/"+lescript)):
00037         lepath = SCRIPT_REPOS3+"/regressions/"+lescript
00038     elif (os.path.exists(SCRIPT_REPOS4+"/regressions/"+lescript)):
00039         lepath = SCRIPT_REPOS4+"/regressions/"+lescript
00040     elif (os.path.exists(SCRIPT_REPOS+"/demos/"+lescript)):
00041         lepath = SCRIPT_REPOS+"/demos/"+lescript
00042     elif (os.path.exists(SCRIPT_REPOS+"/"+lescript)):
00043         lepath = SCRIPT_REPOS+"/"+lescript
00044     elif(os.path.exists(lepath+"/python/"+PYVER+"/"+lescript)):
00045         lepath=lepath+"/python/"+PYVER+"/"+lescript
00046     elif(os.path.exists(lepath+"/lib/python"+PYVER+"/"+lescript)):
00047         lepath=lepath+"/lib/python"+PYVER+"/"+lescript
00048     elif(os.path.exists('/usr/bin/casapyinfo')):         
00049         #locate the /usr/bin one
00050         a=os.popen("/usr/bin/casapyinfo --environ | grep CASAPATH", "r")
00051         x=a.read()
00052         lepath=string.split(string.split(x,'="')[1], " ")[0]+"/lib/python"+PYVER+"/"+lescript
00053     else:
00054         raise Exception, "Regression script %s not found "%(lescript)
00055     return lepath
00056 
00057 def copydata(name, destdir):
00058     destdir = str(destdir)
00059     ok = False
00060     if not os.path.isdir(destdir):
00061         raise RuntimeError('destination directory (' + destdir + ') must exist...')
00062     for root, dirs, files in os.walk(gl['casa']['dirs']['data'] + "/regression"):
00063         if name in dirs:
00064             full_path = root + "/" + name
00065             shutil.copytree(full_path,destdir + "/" + name)
00066             ok = True
00067             break
00068         elif name in files:
00069             full_path = root + "/" + name
00070             shutil.copy(full_path,destdir)
00071             ok = True
00072             break
00073 
00074     if not ok:
00075         for root, dirs, files in os.walk(gl['casa']['dirs']['data']):
00076             if name in dirs:
00077                 full_path = root + "/" + name
00078                 shutil.copytree(full_path,destdir + "/" + name)
00079                 ok = True
00080                 break
00081             elif name in files:
00082                 full_path = root + "/" + name
00083                 shutil.copy(full_path,destdir)
00084                 ok = True
00085                 break
00086 
00087     if not ok:
00088         raise RuntimeError( 'failed to find "' + name + '" in ' + gl['casa']['dirs']['data'] )