| 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 = "/usr/bin/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 | if sys.platform == "linux2": | 
|---|
| 30 | for l in p.readlines(): | 
|---|
| 31 | lsp = l.split() | 
|---|
| 32 | # test this to avoid fails on linux_gate, which has no library | 
|---|
| 33 | if len(lsp) >= 3: | 
|---|
| 34 | if lsp[2] == "not": | 
|---|
| 35 | unresolved.append(lsp[0]) | 
|---|
| 36 | else: | 
|---|
| 37 | resolved.append(lsp[2]) | 
|---|
| 38 | elif sys.platform == "darwin": | 
|---|
| 39 | for l in p.readlines(): | 
|---|
| 40 | l = l.strip() | 
|---|
| 41 | if l.endswith(":"): | 
|---|
| 42 | continue | 
|---|
| 43 | l = l.split()[0] | 
|---|
| 44 | if not os.path.exists(l): | 
|---|
| 45 | unresolved.append(l) | 
|---|
| 46 | else: | 
|---|
| 47 | resolved.append(l) | 
|---|
| 48 | return resolved, unresolved | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | try: | 
|---|
| 52 | opts, args = getopt.getopt(sys.argv[1:], "p:m:h", | 
|---|
| 53 | ["exec-prefix=", "module-dir=", "help"]) | 
|---|
| 54 |  | 
|---|
| 55 | except getopt.GetoptError: | 
|---|
| 56 | usage() | 
|---|
| 57 | sys.exit(2) | 
|---|
| 58 |  | 
|---|
| 59 | execprefix = dist.EXEC_PREFIX | 
|---|
| 60 | moduledir = dist.get_python_lib() | 
|---|
| 61 | sysmoduledir = moduledir | 
|---|
| 62 | for k, v in opts: | 
|---|
| 63 | if k in ("-h", "--help"): | 
|---|
| 64 | usage() | 
|---|
| 65 | sys.exit() | 
|---|
| 66 | if k in ("-p", "--exec-prefix"): | 
|---|
| 67 | v = os.path.expanduser(v) | 
|---|
| 68 | v = os.path.expandvars(v) | 
|---|
| 69 | if not os.path.exists(v): | 
|---|
| 70 | print "The specified exec directory %s doesn't exist" % v | 
|---|
| 71 | sys.exit(1) | 
|---|
| 72 | p = os.path.join(v,"bin") | 
|---|
| 73 | if not os.path.exists(p): | 
|---|
| 74 | print "The specified exec directory %s doesn't exist" % p | 
|---|
| 75 | sys.exit(1) | 
|---|
| 76 | execprefix = v | 
|---|
| 77 | if k in ("-m", "--module-dir"): | 
|---|
| 78 | v = os.path.expanduser(v) | 
|---|
| 79 | v = os.path.expandvars(v) | 
|---|
| 80 | if not os.path.exists(v): | 
|---|
| 81 | print "The specified module directory %s doesn't exist" % v | 
|---|
| 82 | sys.exit(1) | 
|---|
| 83 | moduledir = v | 
|---|
| 84 |  | 
|---|
| 85 | print "Looking fo all required libraries..." | 
|---|
| 86 | try: | 
|---|
| 87 | libs, missing = get_libs(lib="asap/_asap.so") | 
|---|
| 88 | if len(missing): | 
|---|
| 89 | print " The following libraries are missing:" | 
|---|
| 90 | print missing | 
|---|
| 91 | sys.exit(1) | 
|---|
| 92 | else: | 
|---|
| 93 | print "   All required libraries are present." | 
|---|
| 94 | except IOError,msg: | 
|---|
| 95 | print "Skipping test. (%s)" % msg | 
|---|
| 96 |  | 
|---|
| 97 | print 'Looking for dependent python modules...' | 
|---|
| 98 | try: | 
|---|
| 99 | import matplotlib | 
|---|
| 100 | except ImportError: | 
|---|
| 101 | print "Matplotlib not found" | 
|---|
| 102 | sys.exit(1) | 
|---|
| 103 | if int(matplotlib.__version__.split(".")[1]) < 87: | 
|---|
| 104 | print "Warning: matplotlib version < 0.87." \ | 
|---|
| 105 | "This might cause errors. Please upgrade." | 
|---|
| 106 | try: | 
|---|
| 107 | import matplotlib.backends.backend_tkagg | 
|---|
| 108 | except ImportError: | 
|---|
| 109 | print "Matplotlib doesn't have Tk support" | 
|---|
| 110 | sys.exit(1) | 
|---|
| 111 | try: | 
|---|
| 112 | import numpy | 
|---|
| 113 | except ImportError: | 
|---|
| 114 | try: | 
|---|
| 115 | import numarray | 
|---|
| 116 | except ImportError: | 
|---|
| 117 | print "You need to have either 'numpy' or 'numarray' installed." | 
|---|
| 118 | sys.exit(1) | 
|---|
| 119 | try: | 
|---|
| 120 | import IPython | 
|---|
| 121 | except ImportError: | 
|---|
| 122 | print "IPython should be installed." | 
|---|
| 123 | sys.exit(1) | 
|---|
| 124 |  | 
|---|
| 125 | print "   All required modules were found." | 
|---|
| 126 | moddir = os.path.join(moduledir, "asap") | 
|---|
| 127 | bindir =  os.path.join(execprefix, "bin") | 
|---|
| 128 | try: | 
|---|
| 129 | if os.path.exists(moddir): | 
|---|
| 130 | print "Found previous installation of ASAP. Removing..." | 
|---|
| 131 | shutil.rmtree(moddir) | 
|---|
| 132 | print "Installing asap module in %s" % moddir | 
|---|
| 133 | shutil.copytree("asap", moddir) | 
|---|
| 134 | print "Installing asap  scripts in %s" % bindir | 
|---|
| 135 | if moduledir != sysmoduledir: | 
|---|
| 136 | import re | 
|---|
| 137 | print "Changing asap startup script to use custom PYTHONPATH" | 
|---|
| 138 | inf = file("bin/asap") | 
|---|
| 139 | outf = file(os.path.join(bindir,"asap"), "w") | 
|---|
| 140 | outline = "export PYTHONPATH=%s" % moduledir | 
|---|
| 141 | regx = sre.compile("\*\*PYTHONPATH\*\*") | 
|---|
| 142 | for line in inf.readlines(): | 
|---|
| 143 | if regx.search(line): | 
|---|
| 144 | line = outline | 
|---|
| 145 | outf.write(line) | 
|---|
| 146 | outf.close() | 
|---|
| 147 | os.chmod(os.path.join(bindir,"asap"), 0755) | 
|---|
| 148 | else: | 
|---|
| 149 | shutil.copy2("bin/asap", bindir) | 
|---|
| 150 | shutil.copy2("bin/asap_update_data", bindir) | 
|---|
| 151 | shutil.copy2("bin/asap2to3", bindir) | 
|---|
| 152 | if not os.path.exists("asap/data/ephemerides"): | 
|---|
| 153 | print "Warning - no data directory present" | 
|---|
| 154 | print "Please run asap_update_data after the installation is completed." | 
|---|
| 155 | print "Installation completed." | 
|---|
| 156 | except OSError, oe: | 
|---|
| 157 | print oe | 
|---|