-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
executable file
·213 lines (190 loc) · 7.69 KB
/
install.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
import os, sys, shutil, argparse
from argparse import Namespace
try:
WindowsError
except NameError:
WindowsError = None
class Installer():
def setup(self,args):
parser = argparse.ArgumentParser(description='Install DofColumns')
parser.add_argument('--src-dir', help='Location of DofColumns source', default=os.environ.get('DOFCOLUMNS_DIR',os.getcwd()))
parser.add_argument('--arch', help='Name of this configuration', default=os.environ.get('PETSC_ARCH','build'))
parser.add_argument('--prefix', help='The installation directory', default='')
parser.add_argument('--ranlib', help='The ranlib utility', default=os.environ.get('RANLIB',''))
parser.add_argument('--ar-lib-suffix', help='The archive library suffix', default=os.environ.get('AR_LIB_SUFFIX',''))
parser.parse_args(namespace=args)
if not args.prefix:
print '===================================================================='
print 'No installation directory: maybe a --prefix option was not given to ./configure'
print '===================================================================='
sys.exit(0)
return
def setupDirectories(self,args):
self.rootDir = args.src_dir
self.destDir = args.prefix
self.arch = args.arch
self.copies = []
self.ar_lib_suffix = args.ar_lib_suffix
self.ranlib = args.ranlib
self.archLibDir = os.path.join(self.rootDir, self.arch, 'lib')
self.destIncludeDir = os.path.join(self.destDir, 'include')
self.destLibDir = os.path.join(self.destDir, 'lib')
self.destConfDir = os.path.join(self.destLibDir, 'dofcolumns-conf')
return
def checkPrefix(self):
if not self.destDir:
print '********************************************************************'
print 'No installation directory: maybe a --prefix option was not given to ./configure'
print '********************************************************************'
sys.exit(1)
return
def checkDestdir(self):
if os.path.exists(self.destDir):
if os.path.samefile(self.destDir, self.rootDir):
print '********************************************************************'
print 'Incorrect prefix usage. Specified prefix same as DofColumns source directory'
print '********************************************************************'
sys.exit(1)
if os.path.samefile(self.destDir, os.path.join(self.rootDir,self.arch)):
print '********************************************************************'
print 'Incorrect prefix usage. Specified destDir same as DofColumns build directory'
print '********************************************************************'
sys.exit(1)
return
def mkdirp(self,dst):
if not os.path.exists(dst):
os.makedirs(dst)
elif not os.path.isdir(dst):
raise shutil.Error, 'Destination is not a directory'
def copyfile(self, src, dst, symlinks = False, copyFunc = shutil.copy2):
dirname = os.path.dirname(dst)
self.mkdirp(dirname)
copyFunc(src, dst)
return [(src,dst)]
def copytree(self, src, dst, symlinks = False, copyFunc = shutil.copy2):
"""Recursively copy a directory tree using copyFunc, which defaults to shutil.copy2().
The destination directory must not already exist.
If exception(s) occur, an shutil.Error is raised with a list of reasons.
If the optional symlinks flag is true, symbolic links in the
source tree result in symbolic links in the destination tree; if
it is false, the contents of the files pointed to by symbolic
links are copied.
"""
copies = []
names = os.listdir(src)
self.mkdirp(dst)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copies.extend(self.copytree(srcname, dstname, symlinks))
else:
copyFunc(srcname, dstname)
copies.append((srcname, dstname))
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except shutil.Error, err:
errors.extend((srcname,dstname,str(err.args[0])))
try:
shutil.copystat(src, dst)
except OSError, e:
if WindowsError is not None and isinstance(e, WindowsError):
# Copying file access times may fail on Windows
pass
else:
errors.extend((src, dst, str(e)))
if errors:
raise shutil.Error, errors
return copies
def createUninstaller(self):
uninstallscript = os.path.join(self.destConfDir, 'uninstall_DofColumns.py')
self.mkdirp(self.destConfDir)
f = open(uninstallscript, 'w')
# Could use the Python AST to do this
f.write('#!'+sys.executable+'\n')
f.write('import os\n')
f.write('copies = '+repr(self.copies))
f.write('''
for src, dst in copies:
if os.path.lexists(dst):
os.remove(dst)
''')
f.close()
os.chmod(uninstallscript,0744)
return
def installIncludes(self):
self.copies.extend(self.copyfile(os.path.join(self.rootDir,'DofColumns.h'),os.path.join(self.destIncludeDir,'DofColumns.h')))
return
def copyLib(self, src, dst):
'''Run ranlib on the destination library if it is an archive. Also run install_name_tool on dylib on Mac'''
import subprocess
# Symlinks (assumed local) are recreated at dst
if os.path.islink(src):
linkto = os.readlink(src)
try:
os.remove(dst) # In case it already exists
except OSError:
pass
os.symlink(linkto, dst)
return
# Do not install object files
if not os.path.splitext(src)[1] == '.o':
shutil.copy2(src, dst)
if os.path.splitext(dst)[1] == '.'+self.ar_lib_suffix:
subprocess.check_call([self.ranlib,dst])
if os.path.splitext(dst)[1] == '.dylib' and os.path.isfile('/usr/bin/install_name_tool'):
subprocess.check_call(['/usr/bin/install_name_tool','-id',dst,dst])
# preserve the original timestamps - so that the .a vs .so time order is preserved
shutil.copystat(src,dst)
return
def installLib(self):
self.copies.extend(self.copytree(self.archLibDir, self.destLibDir, copyFunc = self.copyLib))
return
def outputDestDirDone(self):
print '''\
====================================
Installation in %s is now complete.
Now to check if the library is working do (in current directory):
make DOFCOLUMNS_DIR=%s PETSC_ARCH="" test
====================================\
''' % (self.destDir,self.destDir)
return
def runsetup(self,args):
self.setup(args)
self.setupDirectories(args)
self.checkPrefix()
self.checkDestdir()
return
def runcopy(self):
print '*** Installing DofColumns at location:',self.destDir, ' ***'
if not os.path.exists(self.destDir):
try:
os.makedirs(self.destDir)
except:
print '********************************************************************'
print 'Unable to create', self.destDir, 'Perhaps you need to do "sudo make install"'
print '********************************************************************'
sys.exit(1)
self.installIncludes()
self.installLib()
return
def rundone(self):
self.createUninstaller()
self.outputDestDirDone()
return
def run(self,args):
self.runsetup(args)
self.runcopy()
self.rundone()
return
if __name__ == '__main__':
Installer().run(Namespace())