source: trunk/python/env.py @ 2621

Last change on this file since 2621 was 2621, checked in by Malte Marquarding, 12 years ago

allow for ipython >=0.10 (both 0.10 and 0.11)

File size: 3.8 KB
Line 
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
6import sys
7import os
8import shutil
9import platform
10
11def 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
19def is_ipython():
20    """Are we running inside IPython?"""
21    return 'IPython' in sys.modules.keys()
22
23def is_asap_cli():
24    """Are we running inside asap ipython (but not casapy)"""
25    return is_ipython() and not is_casapy()
26
27def 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
54    # set up user space
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):
67        print 'First time ASAP use. Setting up ~/.asap'
68#        os.makedirs(os.path.join(userdir, "profile_default"))
69        if not is_casapy():
70            shutil.copyfile(asapdata+"/data/ipy_user_conf.py",
71                            userdir+"/ipy_user_conf.py")
72#            shutil.copyfile(asapdata+"/data/ipython_config.py",
73#                            userdir+"/profile_default/ipython_config.py")
74        f = file(userdir+"/asapuserfuncs.py", "w")
75        f.close()
76        f = file(userdir+"/ipythonrc", "w")
77        f.close()
78    else:
79        if not is_casapy():
80            # upgrade to support later ipython versions
81           
82             if not os.path.exists(userdir+"/ipy_user_conf.py"):
83                shutil.copyfile(asapdata+"/data/ipy_user_conf.py",
84                                userdir+"/ipy_user_conf.py")
85#             if not os.path.exists(userdir+"/profile_default/ipython_config.py"):
86#                shutil.copyfile(asapdata+"/data/ipython_config.py",
87#                                userdir+"/profile_default/ipython_config.py")
88
89
90def get_revision():
91    """Get the revision of the software. Only useful within casapy."""
92    if not is_casapy:
93        return ' unknown '
94    casapath=os.environ["CASAPATH"].split()
95    versioninfo = sys.version_info
96    pyversion = '%s.%s'%(versioninfo[0],versioninfo[1])
97    if os.path.isdir(casapath[0]+'/'+casapath[1]+'/python/%s/asap'%(pyversion)):
98        # for casa developer environment (linux or darwin)
99        revinfo=casapath[0]+'/'+casapath[1]+'/python/%s/asap/svninfo.txt'%(pyversion)
100    else:
101        # for end-user environments
102        if casapath[1]=='darwin':
103            revinfo=casapath[0]+'/Resources/python/asap/svninfo.txt'
104        else:
105            revinfo=casapath[0]+'/lib/python%s/asap/svninfo.txt'%(pyversion)
106    if os.path.isfile(revinfo):
107        f = file(revinfo)
108        f.readline()
109        revsionno=f.readline()
110        f.close()
111        return revsionno.rstrip()
112    return ' unknown '
Note: See TracBrowser for help on using the repository browser.