root/trunk/python/seatree/install/install.py

Revision 470, 6.5 kB (checked in by kmilner, 1 month ago)

installation and bug fixes

  • Property svn:executable set to *
Line 
1 #! /usr/bin/env python
2
3 import os, sys, xml.dom.minidom, subprocess, shutil, traceback
4
5 # path to python/ folder
6 path = os.path.abspath(os.path.dirname(sys.argv[0]) + os.sep + ".." + os.sep + ".." + os.sep)
7
8 try:
9         import seatree.xml.confMaker as confMaker
10 except:
11         sys.path.append(path)
12         import seatree.xml.confMaker as confMaker
13
14 class SEATREEInstaller:
15        
16         def __init__(self, path):
17                 self.path = path
18                 self.confDir = self.path + os.sep + "conf" + os.sep + "install"
19                
20                 self.arch = ""
21                
22                 # check GMT
23                 self.setupGMT()
24                
25                 # check ImageMagick Convert
26                 self.checkConvert()
27                
28                 # load modules from default conf files
29                 modules = self.loadModules()
30                
31                 # install modules
32                 for mod in modules:
33                         modPath = os.path.abspath(self.path + os.sep + "seatree" + os.sep + "modules" + os.sep + mod.directory)
34                         mod.directory = modPath
35                         self.installModule(mod)
36                
37                 conf = confMaker.ConfMaker(modules, self.path + os.sep + \
38                                                    "conf" + os.sep + "conf.xml", self.gmtPath, self.convertPath)
39                
40                 self.createExecutable()
41        
42         def getPythonRootDir(self):
43                 """
44                 Returns the path to the python directory within seatree
45                 """
46                 return self.path
47        
48         def loadModules(self):
49                 print "Loading Modules..."
50                 modules = []
51                 files = os.listdir(self.confDir)
52                 files.sort()
53                 for file in files:
54                         file = self.confDir + os.sep + file
55                         if (not os.path.isdir(file) and not file[0] == '.'):
56                                 print "Parsing " + file
57                                 for mod in self.loadConfFile(file):
58                                         modules.append(mod)
59                 print ""
60                 return modules
61        
62         def loadConfFile(self, name):
63                 modules = []
64                 try:
65                         self.doc = xml.dom.minidom.parse(name)
66                         modulesNode = self.doc.getElementsByTagName("modules")
67                         for node in modulesNode.item(0).childNodes: # for each module
68                                 if (node.nodeName == "module"): # if it actually is a module
69                                         module = LoadedModule()
70                                         for value in node.childNodes: # for each value in module
71                                                 if (value.nodeName == "importName"):
72                                                         module.importName = value.firstChild.nodeValue.strip()
73                                                 elif (value.nodeName == "className"):
74                                                         module.className = value.firstChild.nodeValue.strip()
75                                                 elif (value.nodeName == "directory"):
76                                                         module.directory = value.firstChild.nodeValue.strip()
77                                                 elif (value.nodeName == "installImportName"):
78                                                         module.installImportName = value.firstChild.nodeValue.strip()
79                                                 elif (value.nodeName == "installClassName"):
80                                                         module.installClassName = value.firstChild.nodeValue.strip()
81                                         modules.append(module)
82                                         print "Found " + module.className
83                 except:
84                         print "Could not parse " + name + ", skipping..."
85                 return modules
86        
87         def installModule(self, mod):
88                 print "Installing Module: " + mod.className
89                
90                 try:
91                         sys.path.append(mod.directory)
92                         nameSplit = mod.importName.split(".")
93                         name = nameSplit[len(nameSplit)-1]
94                         execstr = name + "Install" + ' = __import__("' + mod.installImportName + '")'
95                         exec(execstr)
96                         execstr = 'module = getattr(' + name + "Install" + ', "' + mod.installClassName + '")()'
97                         exec(execstr)
98                 except:
99                         traceback.print_exception(*sys.exc_info())
100                         print mod.className + " installer missing or not needed"
101                         return
102                
103                 module.directory = mod.directory
104                 module.importname = mod.importName
105                 module.classname = mod.className
106                 module.gmtPath = self.gmtPath
107                
108                 module.install(self)
109                 print ""
110        
111         def setupGMT(self):
112                 print "Setting up GMT"
113                 self.gmtPath = ""
114                 command = "gmtdefaults -L"
115                 if (self.testCommand("", command) == 0):
116                         print"GMT appears to be in your system path already..."
117                         response = raw_input("Specify another path (y/n default: n)? ")
118                         if (not (response.find("y") >= 0) and not (response.find("Y") >= 0)):
119                                 return
120                
121                 self.gmtPath = "/usr/lib/gmt/bin"
122                 var = self.sys_var("GMTHOME")
123                 if (var and os.path.isdir(var + os.sep + "bin")):
124                         self.gmtPath = var + os.sep + "bin"
125                
126                 while True:
127                         response = raw_input("Path to GMT binaries (default: " + self.gmtPath + "): ")
128                        
129                         if (not response.strip()):
130                                 return
131                        
132                         path = os.path.abspath(response)
133                        
134                         if (os.path.exists(path) and os.path.isdir(path) and self.testCommand(path, command) == 0):
135                                 self.gmtPath = path
136                                 print "GMT path set to " + path
137                                 return
138                         else:
139                                 print "Error with GMT path (invalid path or GMT not found)...try again!"
140        
141         def checkConvert(self):
142                 self.convertPath = ""
143                 command = "convert -version"
144                 if (self.testCommand("", command) in (0,1)):
145                         return
146                
147                 print"ImageMagick's 'convert' tool does not appear to be in your system path..."
148                 self.convertPath = "/usr/bin"
149                
150                 while True:
151                         response = raw_input("Path to convert (default: " + self.convertPath + "): ")
152                        
153                         if (not response.strip()):
154                                 return
155                        
156                         path = os.path.abspath(response)
157                        
158                         if (os.path.exists(path) and os.path.isdir(path) and self.testCommand(path, command) in (0,1)):
159                                 self.convertPath = path
160                                 print "convert path set to " + path
161                                 return
162                         else:
163                                 print "Invalid convert path...try again!"
164        
165         def testCommand(self, path, command):
166                 if (path):
167                         command = path + os.sep + command
168                 proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
169                
170                 proc.communicate()
171                 ret = proc.returncode
172                 return ret
173        
174         def createExecutable(self):
175                 filename = self.path + os.sep + ".." + os.sep + "seatree"
176                
177                 exPath = os.path.abspath(self.path + os.sep + "seatree" + os.sep + "gui" + os.sep + "SEATREE.py")
178                
179                 executable = []
180                 executable.append("#!/bin/sh\n")
181                 executable.append("\n")
182                 executable.append(exPath + "\n")
183                
184                 f = open(filename, 'w')
185                 f.writelines(executable)
186                 f.close()
187                
188                 os.chmod(filename, 0755);
189                
190                 response = raw_input("Link to 'seatree' executable from /usr/bin/seatree? (y/n default: y)? ")
191                 if (not (response.find("n") >= 0) and not (response.find("n") >= 0)):
192                        
193                         if (os.path.exists(os.sep + "usr" + os.sep + "bin" + os.sep + "seatree")):
194                                 print "Warning: Link not created because /usr/bin/seatree exists"
195                                 return
196                        
197                         command = "ln -s " + filename + " " + os.sep + "usr" + os.sep + "bin" + os.sep + "seatree"
198                         proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
199                        
200                         output = proc.communicate()
201                         err = output[1]
202                         ret = proc.returncode
203                        
204                         if (ret > 0):
205                                 if (err.upper().find("PERMISSION")):
206                                         print "ERROR: You do not have write priviledges to /usr/bin...run as a System Administrator!"
207                                 else:
208                                         print "ERROR creating link!"
209        
210         def sys_var(self, name):
211                 return os.popen("echo $"+name).readline()[:-1]
212
213 class LoadedModule:
214        
215         def __init__(self):
216                 self.importName = ""
217                 self.className = ""
218                 self.directory = ""
219                 self.storeDir = ""
220
221 if (__name__ == '__main__'): # is being run from commmand line
222         install = SEATREEInstaller(path)
Note: See TracBrowser for help on using the browser.