forked from doug-101/rpCalc-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
executable file
·292 lines (267 loc) · 10.7 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
"""
****************************************************************************
install.py, Linux install script for rpCalc
Copyright (C) 2018, Douglas W. Bell
This is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License, either Version 2 or any later
version. This program is distributed in the hope that it will be useful,
but WITTHOUT ANY WARRANTY. See the included LICENSE file for details.
*****************************************************************************
"""
import sys
import os.path
import getopt
import shutil
import compileall
import py_compile
import glob
import re
import subprocess
prefixDir = '/usr/local'
buildRoot = '/'
progName = 'rpcalc'
docDir = 'share/doc/{0}'.format(progName)
iconDir = 'share/icons/{0}'.format(progName)
def usage(exitCode=2):
"""Display usage info and exit.
Arguments:
exitCode -- the code to retuen when exiting.
"""
global prefixDir
global buildRoot
print('Usage:')
print(' python install.py [-h] [-p dir] [-d dir] [-i dir] '
'[-b dir] [-x]')
print('where:')
print(' -h display this help message')
print(' -p dir install prefix [default: {0}]'.format(prefixDir))
print(' -d dir documentaion dir [default: <prefix>/{0}]'
.format(docDir))
print(' -i dir icon dir [default: <prefix>/{0}]'.format(iconDir))
print(' -b dir temporary build root for packagers [default: {0}]'
.format(buildRoot))
print(' -x skip all dependency checks (risky)')
sys.exit(exitCode)
def cmpVersions(versionStr, reqdTuple):
"""Return True if point-sep values in versionStr are >= reqdTuple.
Arguments:
versionStr -- a string with point-separated version numbers
reqdTuple -- a tuple of version integers for the minimum acceptable
"""
match = re.search(r'[0-9\.]+', versionStr)
if not match:
return False
versionStr = match.group()
versionList = [int(val) for val in versionStr.split('.') if val]
reqdList = list(reqdTuple)
while len(versionList) < len(reqdList):
versionList.append(0)
while len(reqdList) < len(versionList):
reqdList.append(0)
if versionList >= reqdList:
return True
return False
def copyDir(srcDir, dstDir):
"""Copy all regular files from srcDir to dstDir.
dstDir is created if necessary.
Arguments:
srcDir -- the source dir path
dstDir -- the destination dir path
"""
try:
if not os.path.isdir(dstDir):
os.makedirs(dstDir)
names = os.listdir(srcDir)
for name in names:
srcPath = os.path.join(srcDir, name)
if os.path.isfile(srcPath):
shutil.copy2(srcPath, os.path.join(dstDir, name))
except (IOError, OSError) as e:
if str(e).find('Permission denied') >= 0:
print('Error - must be root to install files')
cleanSource()
sys.exit(4)
raise
def createWrapper(execDir, execName):
"""Create a wrapper executable file for a python script in execDir.
Arguments:
execDir -- the path where the executable is placed
execName -- the name for the executable file
"""
text = '#!/bin/sh\n\nexec {0} {1}/{2}.py "$@"'.format(sys.executable,
execDir,
execName)
with open(execName, 'w') as f:
f.write(text)
os.chmod(execName, 0o755)
def replaceLine(path, origLineStart, newLine):
"""Replaces lines with origLineStart with newLine and rewrites the file.
Arguments:
path -- the file to modify
origLineStart -- the beginning of the line to be replaced
newLine -- the replacement line
"""
with open(path, 'r') as f:
lines = f.readlines()
with open(path, 'w') as f:
for line in lines:
if line.startswith(origLineStart):
f.write(newLine)
else:
f.write(line)
def cleanSource():
"""Remove any temporary files added to untarred dirs.
"""
for name in glob.glob(os.path.join('source', '*.py[co]')):
os.remove(name)
removeDir(os.path.join('source', '__pycache__'))
global progName
if os.path.isfile(progName):
os.remove(progName)
def removeDir(dir):
"""Remove dir and all files under it, ignore errors.
Arguments:
dir -- the directory to remove
"""
try:
shutil.rmtree(dir, 1)
except: # shouldn't be needed with ignore error param, but
pass # some python versions have a bug
def main():
"""Main installer function.
"""
optLetters = 'hp:d:i:b:x'
try:
opts, args = getopt.getopt(sys.argv[1:], optLetters)
except getopt.GetoptError:
usage(2)
global prefixDir
global docDir
global iconDir
global buildRoot
global progName
depCheck = True
for opt, val in opts:
if opt == '-h':
usage(0)
elif opt == '-p':
prefixDir = os.path.abspath(val)
elif opt == '-d':
docDir = val
elif opt == '-i':
iconDir = val
elif opt == '-b':
buildRoot = val
elif opt == '-x':
depCheck = False
if not os.path.isfile('install.py'):
print('Error - {0} files not found'.format(progName))
print('The directory containing "install.py" must be current')
sys.exit(4)
if (os.path.isdir('source') and
not os.path.isfile('source/{0}.py'.format(progName))):
print('Error - source files not found')
print('Retry the extraction from the tar archive')
sys.exit(4)
if depCheck:
print('Checking dependencies...')
pyVersion = sys.version_info[:3]
pyVersion = '.'.join([str(num) for num in pyVersion])
if cmpVersions(pyVersion, (3, 4)):
print(' Python Version {0} -> OK'.format(pyVersion))
else:
print(' Python Version {0} -> Sorry, 3.4 or higher is required'
.format(pyVersion))
sys.exit(3)
try:
from PyQt5 import QtCore, QtWidgets
except:
print(' PyQt not found -> Sorry, PyQt 5.4 or higher is required'
' and must be built for Python 3')
sys.exit(3)
qtVersion = QtCore.qVersion()
if cmpVersions(qtVersion, (5, 4)):
print(' Qt Version {0} -> OK'.format(qtVersion))
else:
print(' Qt Version {0} -> Sorry, 5.4 or higher is required'
.format(qtVersion))
sys.exit(3)
pyqtVersion = QtCore.PYQT_VERSION_STR
if cmpVersions(pyqtVersion, (5, 4)):
print(' PyQt Version {0} -> OK'.format(pyqtVersion))
else:
print(' PyQt Version {0} -> Sorry, 5.4 or higher is required'
.format(pyqtVersion))
sys.exit(3)
pythonPrefixDir = os.path.join(prefixDir, 'share', progName)
pythonBuildDir = os.path.join(buildRoot, pythonPrefixDir[1:])
if os.path.isdir('source'):
print('Installing files...')
print(' Copying python files to {0}'.format(pythonBuildDir))
removeDir(pythonBuildDir) # remove old?
copyDir('source', pythonBuildDir)
if os.path.isdir('doc'):
docPrefixDir = docDir.replace('<prefix>/', '')
if not os.path.isabs(docPrefixDir):
docPrefixDir = os.path.join(prefixDir, docPrefixDir)
docBuildDir = os.path.join(buildRoot, docPrefixDir[1:])
print(' Copying documentation files to {0}'.format(docBuildDir))
copyDir('doc', docBuildDir)
# update help file location in main python script
replaceLine(os.path.join(pythonBuildDir, '{0}.py'.format(progName)),
'helpFilePath = None',
'helpFilePath = \'{0}\' # modified by install script\n'
.format(docPrefixDir))
if os.path.isdir('data'):
dataPrefixDir = os.path.join(prefixDir, 'share', progName, 'data')
dataBuildDir = os.path.join(buildRoot, dataPrefixDir[1:])
print(' Copying data files to {0}'.format(dataBuildDir))
removeDir(dataBuildDir) # remove old?
copyDir('data', dataBuildDir)
# update data file location in main python script
replaceLine(os.path.join(pythonBuildDir, '{0}.py'.format(progName)),
'dataFilePath = None',
'dataFilePath = \'{0}\' # modified by install script\n'
.format(dataPrefixDir))
if os.path.isdir('icons'):
iconPrefixDir = iconDir.replace('<prefix>/', '')
if not os.path.isabs(iconPrefixDir):
iconPrefixDir = os.path.join(prefixDir, iconPrefixDir)
iconBuildDir = os.path.join(buildRoot, iconPrefixDir[1:])
print(' Copying icon files to {0}'.format(iconBuildDir))
copyDir('icons', iconBuildDir)
# update icon location in main python script
replaceLine(os.path.join(pythonBuildDir, '{0}.py'.format(progName)),
'iconPath = None',
'iconPath = \'{0}\' # modified by install script\n'
.format(iconPrefixDir))
if os.path.isfile(os.path.join('icons', progName + '-icon.svg')):
svgIconPrefixDir = os.path.join(prefixDir, 'share', 'icons',
'hicolor', 'scalable', 'apps')
svgIconBuildDir = os.path.join(buildRoot, svgIconPrefixDir[1:])
print(' Copying app icon files to {0}'.format(svgIconBuildDir))
if not os.path.isdir(svgIconBuildDir):
os.makedirs(svgIconBuildDir)
shutil.copy2(os.path.join('icons', progName + '-icon.svg'),
svgIconBuildDir)
if os.path.isfile(progName + '.desktop'):
desktopPrefixDir = os.path.join(prefixDir, 'share', 'applications')
desktopBuildDir = os.path.join(buildRoot, desktopPrefixDir[1:])
print(' Copying desktop file to {0}'.format(desktopBuildDir))
if not os.path.isdir(desktopBuildDir):
os.makedirs(desktopBuildDir)
shutil.copy2(progName + '.desktop', desktopBuildDir)
if os.path.isdir('source'):
createWrapper(pythonPrefixDir, progName)
binBuildDir = os.path.join(buildRoot, prefixDir[1:], 'bin')
print(' Copying executable file "{0}" to {1}'
.format(progName, binBuildDir))
if not os.path.isdir(binBuildDir):
os.makedirs(binBuildDir)
shutil.copy2(progName, binBuildDir)
compileall.compile_dir(pythonBuildDir, ddir=prefixDir)
cleanSource()
print('Install complete.')
if __name__ == '__main__':
main()