1 | #!/usr/bin/env python
|
---|
2 |
|
---|
3 | import distutils.sysconfig as dist
|
---|
4 | import shutil
|
---|
5 | import os
|
---|
6 | import sys
|
---|
7 | import getopt
|
---|
8 |
|
---|
9 | def usage():
|
---|
10 | print """Usage:
|
---|
11 | ./install [-p <prefix for asap binary>]
|
---|
12 | [-m <where to install python module>]
|
---|
13 | [-h] this message
|
---|
14 | """
|
---|
15 |
|
---|
16 | def get_libs(lib='_asap.so'):
|
---|
17 | lddcommand = "/usr/bin/ldd"
|
---|
18 | lddopts = ""
|
---|
19 | if (sys.platform == "darwin"):
|
---|
20 | lddcommand = "otool"
|
---|
21 | lddopts = "-L"
|
---|
22 | if not os.path.isfile(lddcommand):
|
---|
23 | raise IOError("ldd/otool is not available")
|
---|
24 | if not os.path.exists(lib):
|
---|
25 | raise IOError("File %s not found" %lib)
|
---|
26 | p = os.popen(lddcommand+" "+lddopts+" "+lib)
|
---|
27 | resolved = []
|
---|
28 | unresolved = []
|
---|
29 | for l in p.readlines():
|
---|
30 | lsp = l.split()
|
---|
31 | if lsp[2] == "not":
|
---|
32 | unresolved.append(lsp[0])
|
---|
33 | else:
|
---|
34 | resolved.append(lsp[2])
|
---|
35 | return resolved, unresolved
|
---|
36 |
|
---|
37 | try:
|
---|
38 | opts, args = getopt.getopt(sys.argv[1:], "p:m:h",
|
---|
39 | ["exec-prefix=", "module-dir=", "help"])
|
---|
40 |
|
---|
41 | except getopt.GetoptError:
|
---|
42 | usage()
|
---|
43 | sys.exit(2)
|
---|
44 |
|
---|
45 | execprefix = dist.EXEC_PREFIX
|
---|
46 | moduledir = dist.get_python_lib()
|
---|
47 | sysmoduledir = moduledir
|
---|
48 | for k, v in opts:
|
---|
49 | if k in ("-h", "--help"):
|
---|
50 | usage()
|
---|
51 | sys.exit()
|
---|
52 | if k in ("-p", "--exec-prefix"):
|
---|
53 | v = os.path.expanduser(v)
|
---|
54 | v = os.path.expandvars(v)
|
---|
55 | if not os.path.exists(v):
|
---|
56 | print "The specified exec directory %s doesn't exist" % v
|
---|
57 | sys.exit(1)
|
---|
58 | p = os.path.join(v,"bin")
|
---|
59 | if not os.path.exists(p):
|
---|
60 | print "The specified exec directory %s doesn't exist" % p
|
---|
61 | sys.exit(1)
|
---|
62 | execprefix = v
|
---|
63 | if k in ("-m", "--module-dir"):
|
---|
64 | v = os.path.expanduser(v)
|
---|
65 | v = os.path.expandvars(v)
|
---|
66 | if not os.path.exists(v):
|
---|
67 | print "The specified module directory %s doesn't exist" % v
|
---|
68 | sys.exit(1)
|
---|
69 | moduledir = v
|
---|
70 |
|
---|
71 | print "Looking fo all required libraries..."
|
---|
72 | try:
|
---|
73 | libs, missing = get_libs(lib="asap/_asap.so")
|
---|
74 | if len(missing):
|
---|
75 | print " The following libraries are missing:"
|
---|
76 | print missing
|
---|
77 | sys.exit(1)
|
---|
78 | else:
|
---|
79 | print " All required libraries are present."
|
---|
80 | except IOError,msg:
|
---|
81 | print msg+" Skipping test."
|
---|
82 |
|
---|
83 | print 'Looking for dependent python modules...'
|
---|
84 | try:
|
---|
85 | import matplotlib
|
---|
86 | except ImportError:
|
---|
87 | print "Matplotlib not found"
|
---|
88 | sys.exit(1)
|
---|
89 | if int(matplotlib.__version__.split(".")[1]) < 87:
|
---|
90 | print "Warning: matplotlib version < 0.87." \
|
---|
91 | "This might cause errors. Please upgrade."
|
---|
92 | try:
|
---|
93 | import matplotlib.backends.backend_tkagg
|
---|
94 | except ImportError:
|
---|
95 | print "Matplotlib doesn't have Tk support"
|
---|
96 | sys.exit(1)
|
---|
97 | try:
|
---|
98 | import numpy
|
---|
99 | except ImportError:
|
---|
100 | try:
|
---|
101 | import numarray
|
---|
102 | except ImportError:
|
---|
103 | print "You need to have either 'numpy' or 'numarray' installed."
|
---|
104 | sys.exit(1)
|
---|
105 | try:
|
---|
106 | import IPython
|
---|
107 | except ImportError:
|
---|
108 | print "IPython should be installed."
|
---|
109 | sys.exit(1)
|
---|
110 |
|
---|
111 | print " All required modules were found."
|
---|
112 | moddir = os.path.join(moduledir, "asap")
|
---|
113 | bindir = os.path.join(execprefix, "bin")
|
---|
114 | try:
|
---|
115 | if os.path.exists(moddir):
|
---|
116 | print "Found previous installation of ASAP. Removing..."
|
---|
117 | shutil.rmtree(moddir)
|
---|
118 | print "Installing asap module in %s" % moddir
|
---|
119 | shutil.copytree("asap", moddir)
|
---|
120 | print "Installing asap scripts in %s" % bindir
|
---|
121 | if moduledir != sysmoduledir:
|
---|
122 | import sre
|
---|
123 | print "Changing asap startup script to use custom PYTHONPATH"
|
---|
124 | inf = file("bin/asap")
|
---|
125 | outf = file(os.path.join(bindir,"asap"), "w")
|
---|
126 | outline = "export PYTHONPATH=%s" % moduledir
|
---|
127 | regx = sre.compile("\*\*PYTHONPATH\*\*")
|
---|
128 | for line in inf.readlines():
|
---|
129 | if regx.search(line):
|
---|
130 | line = outline
|
---|
131 | outf.write(line)
|
---|
132 | outf.close()
|
---|
133 | os.chmod(os.path.join(bindir,"asap"), 0755)
|
---|
134 | else:
|
---|
135 | shutil.copy2("bin/asap", bindir)
|
---|
136 | shutil.copy2("bin/asap_update_data", bindir)
|
---|
137 | if not os.path.exists("asap/data/ephemerides"):
|
---|
138 | print "Warning - no data directory present"
|
---|
139 | print "Please run asap_update_data after the installation is completed."
|
---|
140 | print "Installation completed."
|
---|
141 | except OSError, oe:
|
---|
142 | print oe
|
---|