source: branches/newfiller/python/env.py@ 2803

Last change on this file since 2803 was 1797, checked in by Malte Marquarding, 14 years ago

Ticket #193: added new logging class. Also factored out environment based settings from init.py into env.py. This hasn't been enabled/isn't used yet

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