1#!/usr/bin/env python 2 3# Install.py tool to download, unpack, and point to the Eigen library 4# used to automate the steps described in the README file in this dir 5 6from __future__ import print_function 7import sys,os,re,glob,subprocess 8 9# help message 10 11help = """ 12Syntax from src dir: make lib-smd args="-b" 13 or: make lib-smd args="-p /usr/include/eigen3" 14 15Syntax from lib dir: python Install.py -b 16 or: python Install.py -p /usr/include/eigen3" 17 or: python Install.py -v 3.3.4 -b 18 19specify one or more options, order does not matter 20 21 -b = download and unpack/configure the Eigen library 22 -p = specify folder holding an existing installation of Eigen 23 -v = set version of Eigen library to download and set up (default = 3.3.4) 24 25 26Example: 27 28make lib-smd args="-b" # download/build in default lib/smd/eigen-eigen-* 29make lib-smd args="-p /usr/include/eigen3" # use existing Eigen installation in /usr/include/eigen3 30""" 31 32# settings 33 34version = '3.3.4' 35tarball = "eigen.tar.gz" 36 37# print error message or help 38 39def error(str=None): 40 if not str: print(help) 41 else: print("ERROR",str) 42 sys.exit() 43 44# expand to full path name 45# process leading '~' or relative path 46 47def fullpath(path): 48 return os.path.abspath(os.path.expanduser(path)) 49 50def which(program): 51 def is_exe(fpath): 52 return os.path.isfile(fpath) and os.access(fpath, os.X_OK) 53 54 fpath, fname = os.path.split(program) 55 if fpath: 56 if is_exe(program): 57 return program 58 else: 59 for path in os.environ["PATH"].split(os.pathsep): 60 path = path.strip('"') 61 exe_file = os.path.join(path, program) 62 if is_exe(exe_file): 63 return exe_file 64 65 return None 66 67def geturl(url,fname): 68 success = False 69 70 if which('curl') != None: 71 cmd = 'curl -L -o "%s" %s' % (fname,url) 72 try: 73 subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True) 74 success = True 75 except subprocess.CalledProcessError as e: 76 print("Calling curl failed with: %s" % e.output.decode('UTF-8')) 77 78 if not success and which('wget') != None: 79 cmd = 'wget -O "%s" %s' % (fname,url) 80 try: 81 subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True) 82 success = True 83 except subprocess.CalledProcessError as e: 84 print("Calling wget failed with: %s" % e.output.decode('UTF-8')) 85 86 if not success: 87 error("Failed to download source code with 'curl' or 'wget'") 88 return 89 90# parse args 91 92args = sys.argv[1:] 93nargs = len(args) 94if nargs == 0: error() 95 96homepath = "." 97homedir = "eigen3" 98 99buildflag = False 100pathflag = False 101linkflag = True 102 103iarg = 0 104while iarg < nargs: 105 if args[iarg] == "-v": 106 if iarg+2 > nargs: error() 107 version = args[iarg+1] 108 iarg += 2 109 elif args[iarg] == "-p": 110 if iarg+2 > nargs: error() 111 eigenpath = fullpath(args[iarg+1]) 112 pathflag = True 113 iarg += 2 114 elif args[iarg] == "-b": 115 buildflag = True 116 iarg += 1 117 else: error() 118 119homepath = fullpath(homepath) 120 121if (pathflag): 122 if not os.path.isdir(eigenpath): error("Eigen path does not exist") 123 124if (buildflag and pathflag): 125 error("Cannot use -b and -p flag at the same time") 126 127if (not buildflag and not pathflag): 128 error("Have to use either -b or -p flag") 129 130# download and unpack Eigen tarball 131# use glob to find name of dir it unpacks to 132 133if buildflag: 134 print("Downloading Eigen ...") 135 url = "http://bitbucket.org/eigen/eigen/get/%s.tar.gz" % version 136 geturl(url,"%s/%s" % (homepath,tarball)) 137 138 print("Unpacking Eigen tarball ...") 139 edir = glob.glob("%s/eigen-eigen-*" % homepath) 140 for one in edir: 141 if os.path.isdir(one): 142 subprocess.check_output('rm -rf "%s"' % one,stderr=subprocess.STDOUT,shell=True) 143 cmd = 'cd "%s"; tar -xzvf %s' % (homepath,tarball) 144 subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True) 145 edir = glob.glob("%s/eigen-eigen-*" % homepath) 146 os.rename(edir[0],"%s/%s" % (homepath,homedir)) 147 os.remove(tarball) 148 149# create link in lib/smd to Eigen src dir 150 151if linkflag: 152 print("Creating link to Eigen files") 153 if os.path.isfile("includelink") or os.path.islink("includelink"): 154 os.remove("includelink") 155 if pathflag: linkdir = eigenpath 156 else: linkdir = "%s/%s" % (homepath,homedir) 157 cmd = 'ln -s "%s" includelink' % linkdir 158 subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True) 159