1 | """This module has various functions for environment specific setings.
|
---|
2 | """
|
---|
3 | __all__ = ["is_casapy", "is_ipython", "setup_env", "get_revision",
|
---|
4 | "is_asap_cli"]
|
---|
5 |
|
---|
6 | import sys
|
---|
7 | import os
|
---|
8 | import shutil
|
---|
9 | import platform
|
---|
10 |
|
---|
11 | def is_casapy():
|
---|
12 | """Are we running inside casapy?"""
|
---|
13 | try:
|
---|
14 | from taskinit import casalog
|
---|
15 | return True
|
---|
16 | except ImportError:
|
---|
17 | return False
|
---|
18 |
|
---|
19 | def is_ipython():
|
---|
20 | """Are we running inside IPython?"""
|
---|
21 | return 'IPython' in sys.modules.keys()
|
---|
22 |
|
---|
23 | def is_asap_cli():
|
---|
24 | """Are we running inside asap ipython (but not casapy)"""
|
---|
25 | return is_ipython() and not is_casapy()
|
---|
26 |
|
---|
27 | def setup_env():
|
---|
28 | """Set-up environment variables for casa and initialise ~/.asap on first
|
---|
29 | use.
|
---|
30 | """
|
---|
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)
|
---|
53 | # set up user space
|
---|
54 | userdir = os.environ["HOME"]+"/.asap"
|
---|
55 | if not os.path.exists(userdir):
|
---|
56 | print 'First time ASAP use. Setting up ~/.asap'
|
---|
57 | os.mkdir(userdir)
|
---|
58 | if not is_casapy():
|
---|
59 | shutil.copyfile(asapdata+"/data/ipy_user_conf.py",
|
---|
60 | userdir+"/ipy_user_conf.py")
|
---|
61 | f = file(userdir+"/asapuserfuncs.py", "w")
|
---|
62 | f.close()
|
---|
63 | f = file(userdir+"/ipythonrc", "w")
|
---|
64 | f.close()
|
---|
65 | else:
|
---|
66 | if not is_casapy():
|
---|
67 | # upgrade to support later ipython versions
|
---|
68 | if not os.path.exists(userdir+"/ipy_user_conf.py"):
|
---|
69 | shutil.copyfile(asapdata+"/data/ipy_user_conf.py",
|
---|
70 | userdir+"/ipy_user_conf.py")
|
---|
71 |
|
---|
72 | def get_revision():
|
---|
73 | """Get the revision of the software. Only useful within casapy."""
|
---|
74 | if not is_casapy:
|
---|
75 | return ' unknown '
|
---|
76 | casapath=os.environ["CASAPATH"].split()
|
---|
77 | versioninfo = sys.version_info
|
---|
78 | pyversion = '%s.%s'%(versioninfo[0],versioninfo[1])
|
---|
79 | if os.path.isdir(casapath[0]+'/'+casapath[1]+'/python/%s/asap'%(pyversion)):
|
---|
80 | # for casa developer environment (linux or darwin)
|
---|
81 | revinfo=casapath[0]+'/'+casapath[1]+'/python/%s/asap/svninfo.txt'%(pyversion)
|
---|
82 | else:
|
---|
83 | # for end-user environments
|
---|
84 | if casapath[1]=='darwin':
|
---|
85 | revinfo=casapath[0]+'/Resources/python/asap/svninfo.txt'
|
---|
86 | else:
|
---|
87 | revinfo=casapath[0]+'/lib/python%s/asap/svninfo.txt'%(pyversion)
|
---|
88 | if os.path.isfile(revinfo):
|
---|
89 | f = file(revinfo)
|
---|
90 | f.readline()
|
---|
91 | revsionno=f.readline()
|
---|
92 | f.close()
|
---|
93 | return revsionno.rstrip()
|
---|
94 | return ' unknown '
|
---|