[1858] | 1 | """This module has various functions for environment specific setings.
|
---|
| 2 | """
|
---|
[1859] | 3 | __all__ = ["is_casapy", "is_ipython", "setup_env", "get_revision",
|
---|
| 4 | "is_asap_cli"]
|
---|
[1797] | 5 |
|
---|
| 6 | import sys
|
---|
| 7 | import os
|
---|
| 8 | import shutil
|
---|
| 9 | import platform
|
---|
| 10 |
|
---|
| 11 | def is_casapy():
|
---|
[1858] | 12 | """Are we running inside casapy?"""
|
---|
[1797] | 13 | try:
|
---|
[1848] | 14 | from taskinit import casalog
|
---|
[1797] | 15 | return True
|
---|
| 16 | except ImportError:
|
---|
| 17 | return False
|
---|
| 18 |
|
---|
| 19 | def is_ipython():
|
---|
[1858] | 20 | """Are we running inside IPython?"""
|
---|
[1797] | 21 | return 'IPython' in sys.modules.keys()
|
---|
| 22 |
|
---|
[1859] | 23 | def is_asap_cli():
|
---|
| 24 | """Are we running inside asap ipython (but not casapy)"""
|
---|
| 25 | return is_ipython() and not is_casapy()
|
---|
| 26 |
|
---|
[1797] | 27 | def setup_env():
|
---|
[1858] | 28 | """Set-up environment variables for casa and initialise ~/.asap on first
|
---|
| 29 | use.
|
---|
| 30 | """
|
---|
[1797] | 31 | # Set up CASAPATH and first time use of asap i.e. ~/.asap/*
|
---|
| 32 | plf = None
|
---|
| 33 | if sys.platform == "linux2":
|
---|
| 34 | if platform.architecture()[0] == '64bit':
|
---|
| 35 | plf = 'linux_64b'
|
---|
| 36 | else:
|
---|
| 37 | plf = 'linux_gnu'
|
---|
| 38 | elif sys.platform == 'darwin':
|
---|
| 39 | plf = 'darwin'
|
---|
| 40 | else:
|
---|
| 41 | # Shouldn't happen - default to linux
|
---|
| 42 | plf = 'linux'
|
---|
| 43 | asapdata = os.path.split(__file__)[0]
|
---|
| 44 |
|
---|
| 45 | # Allow user defined data location
|
---|
| 46 | if os.environ.has_key("ASAPDATA"):
|
---|
| 47 | if os.path.exists(os.environ["ASAPDATA"]):
|
---|
| 48 | asapdata = os.environ["ASAPDATA"]
|
---|
| 49 | # use CASAPATH if defined and "data" dir present
|
---|
| 50 | if not os.environ.has_key("CASAPATH") or \
|
---|
| 51 | not os.path.exists(os.environ["CASAPATH"].split()[0]+"/data"):
|
---|
| 52 | os.environ["CASAPATH"] = "%s %s somwhere" % ( asapdata, plf)
|
---|
[2499] | 53 |
|
---|
[1797] | 54 | # set up user space
|
---|
[2621] | 55 | if os.environ["USER"] == 'root':
|
---|
| 56 | return
|
---|
| 57 | try:
|
---|
| 58 | import IPython
|
---|
| 59 | # 0.11+ doesn't use user dir
|
---|
| 60 | if int(IPython.__version__.split(".")[1]) >= 11:
|
---|
| 61 | return
|
---|
| 62 | except:
|
---|
| 63 | # no ipython so we don't need user dir
|
---|
| 64 | return
|
---|
| 65 | userdir = os.environ["HOME"]+"/.asap"
|
---|
| 66 | if not os.path.exists(userdir):
|
---|
[2622] | 67 | os.mkdir(userdir)
|
---|
[2621] | 68 | print 'First time ASAP use. Setting up ~/.asap'
|
---|
| 69 | # os.makedirs(os.path.join(userdir, "profile_default"))
|
---|
| 70 | if not is_casapy():
|
---|
| 71 | shutil.copyfile(asapdata+"/data/ipy_user_conf.py",
|
---|
| 72 | userdir+"/ipy_user_conf.py")
|
---|
| 73 | # shutil.copyfile(asapdata+"/data/ipython_config.py",
|
---|
| 74 | # userdir+"/profile_default/ipython_config.py")
|
---|
| 75 | f = file(userdir+"/asapuserfuncs.py", "w")
|
---|
| 76 | f.close()
|
---|
| 77 | f = file(userdir+"/ipythonrc", "w")
|
---|
| 78 | f.close()
|
---|
| 79 | else:
|
---|
| 80 | if not is_casapy():
|
---|
| 81 | # upgrade to support later ipython versions
|
---|
[2499] | 82 |
|
---|
[2621] | 83 | if not os.path.exists(userdir+"/ipy_user_conf.py"):
|
---|
| 84 | shutil.copyfile(asapdata+"/data/ipy_user_conf.py",
|
---|
| 85 | userdir+"/ipy_user_conf.py")
|
---|
[2499] | 86 | # if not os.path.exists(userdir+"/profile_default/ipython_config.py"):
|
---|
| 87 | # shutil.copyfile(asapdata+"/data/ipython_config.py",
|
---|
| 88 | # userdir+"/profile_default/ipython_config.py")
|
---|
[1797] | 89 |
|
---|
[2499] | 90 |
|
---|
[1797] | 91 | def get_revision():
|
---|
[1858] | 92 | """Get the revision of the software. Only useful within casapy."""
|
---|
[1797] | 93 | if not is_casapy:
|
---|
| 94 | return ' unknown '
|
---|
| 95 | casapath=os.environ["CASAPATH"].split()
|
---|
[2090] | 96 | versioninfo = sys.version_info
|
---|
| 97 | pyversion = '%s.%s'%(versioninfo[0],versioninfo[1])
|
---|
| 98 | if os.path.isdir(casapath[0]+'/'+casapath[1]+'/python/%s/asap'%(pyversion)):
|
---|
[1797] | 99 | # for casa developer environment (linux or darwin)
|
---|
[2090] | 100 | revinfo=casapath[0]+'/'+casapath[1]+'/python/%s/asap/svninfo.txt'%(pyversion)
|
---|
[1797] | 101 | else:
|
---|
| 102 | # for end-user environments
|
---|
| 103 | if casapath[1]=='darwin':
|
---|
| 104 | revinfo=casapath[0]+'/Resources/python/asap/svninfo.txt'
|
---|
| 105 | else:
|
---|
[2090] | 106 | revinfo=casapath[0]+'/lib/python%s/asap/svninfo.txt'%(pyversion)
|
---|
[1797] | 107 | if os.path.isfile(revinfo):
|
---|
| 108 | f = file(revinfo)
|
---|
| 109 | f.readline()
|
---|
| 110 | revsionno=f.readline()
|
---|
| 111 | f.close()
|
---|
| 112 | return revsionno.rstrip()
|
---|
| 113 | return ' unknown '
|
---|