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