| 1 |
# @HEADER |
| 2 |
# ***************************************************************************** |
| 3 |
# TriBITS: Tribal Build, Integrate, and Test System |
| 4 |
# |
| 5 |
# Copyright 2013-2016 NTESS and the TriBITS contributors. |
| 6 |
# SPDX-License-Identifier: BSD-3-Clause |
| 7 |
# ***************************************************************************** |
| 8 |
# @HEADER |
| 9 |
|
| 10 |
from FindGeneralScriptSupport import * |
| 11 |
from gitdist import addOptionParserChoiceOption |
| 12 |
|
| 13 |
from optparse import OptionParser |
| 14 |
|
| 15 |
|
| 16 |
# |
| 17 |
# Implement behavior of an tool install |
| 18 |
# |
| 19 |
class InstallProgramDriver: |
| 20 |
|
| 21 |
|
| 22 |
def __init__(self, installObj): |
| 23 |
self.installObj = installObj |
| 24 |
self.productVersion = self.installObj.getProductDefaultVersion() |
| 25 |
|
| 26 |
|
| 27 |
def getProductVersion(self): |
| 28 |
return self.productVersion |
| 29 |
|
| 30 |
|
| 31 |
def runDriver(self): |
| 32 |
|
| 33 |
# Get the production version out of command-line |
| 34 |
|
| 35 |
productBaseName = self.installObj.getProductBaseName() |
| 36 |
|
| 37 |
versionCmndArgName = "--"+productBaseName+"-version" |
| 38 |
for arg in sys.argv[1:]: |
| 39 |
#print("arg = '"+arg+"'") |
| 40 |
arg_and_value = arg.split("=") |
| 41 |
if len(arg_and_value) and arg_and_value[0] == versionCmndArgName: |
| 42 |
self.productVersion = arg_and_value[1].strip() |
| 43 |
|
| 44 |
# Get basic info after knowing the version |
| 45 |
|
| 46 |
productName = self.installObj.getProductName(self.productVersion) |
| 47 |
baseDirName = self.installObj.getBaseDirName(self.productVersion) |
| 48 |
|
| 49 |
scriptName = self.installObj.getScriptName(); |
| 50 |
|
| 51 |
|
| 52 |
# |
| 53 |
# 1) Set up the help text |
| 54 |
# |
| 55 |
|
| 56 |
productSupportedVersions = self.installObj.getProductSupportedVersions() |
| 57 |
if productSupportedVersions: |
| 58 |
supportedVersionsTxt = "Versions supported include:\n "+\ |
| 59 |
str(self.installObj.getProductSupportedVersions()) |
| 60 |
else: |
| 61 |
supportedVersionsTxt = "Arbitrary versions are supported." |
| 62 |
|
| 63 |
usageHelp = scriptName+\ |
| 64 |
r""" [OPTIONS] [--install-dir=<install-dir> ...] |
| 65 |
|
| 66 |
This script checks out source, untars, configures, builds, and installs |
| 67 |
"""+productName+r""" in one shot. """+supportedVersionsTxt+r""" |
| 68 |
|
| 69 |
The version to install is set with --"""+productBaseName+r"""-version=<version>. |
| 70 |
|
| 71 |
By default, if you just type: |
| 72 |
|
| 73 |
$ """+scriptName+r""" --install-dir=<install-dir> --parallel=<num-procs> --do-all |
| 74 |
|
| 75 |
then the directory """+baseDirName+r""" will be created in the local working directory |
| 76 |
and it will contain the source for """+productName+r""" and the build files. NOTE: This |
| 77 |
requires that you not run as root or your userid on the download computer |
| 78 |
will not be correct. If you want to install as root, see below. |
| 79 |
|
| 80 |
You can control various parts of the process with various options (see below). |
| 81 |
|
| 82 |
If you do not install as root then you must override the option --install-dir |
| 83 |
which is set to /usr/local/bin by default. For example, you might just type: |
| 84 |
|
| 85 |
$ """+scriptName+r""" --install-dir=$HOME/install --parallel=8 --do-all |
| 86 |
|
| 87 |
and then it would install """+productName+r""" and the other executables in $HOME/install/bin. |
| 88 |
NOTE: You will have to update your PATH variable to include whatever directory |
| 89 |
you choose to install """+productName+r""" in. |
| 90 |
|
| 91 |
NOTE: If you need to use sudo to install in /usr/local/bin or some other place |
| 92 |
that needs root privileges, do: |
| 93 |
|
| 94 |
$ """+scriptName+r""" --install-dir=$HOME/install --parallel=8 \ |
| 95 |
--download --untar --configure --build |
| 96 |
$ sudo """+scriptName+r""" --install-dir=$HOME/install --parallel=8 \ |
| 97 |
--install-owner=<owner> --install-group=<group> [--install-for-all] \ |
| 98 |
--install |
| 99 |
|
| 100 |
This appears to work on most systems. |
| 101 |
|
| 102 |
After you have done a successful install, you might want to do: |
| 103 |
|
| 104 |
$ rm -r """+baseDirName+r""" |
| 105 |
|
| 106 |
in order to remove the intermediate source and build files. |
| 107 |
""" + self.installObj.getExtraHelpStr(self.productVersion) |
| 108 |
|
| 109 |
# |
| 110 |
# 2) Parse the command-line |
| 111 |
# |
| 112 |
|
| 113 |
clp = OptionParser(usage=usageHelp) |
| 114 |
|
| 115 |
supportedVersions = self.installObj.getProductSupportedVersions() |
| 116 |
defaultVersion = self.installObj.getProductDefaultVersion() |
| 117 |
defaultVersionIdx = findInSequence(supportedVersions, defaultVersion) |
| 118 |
|
| 119 |
if supportedVersions: |
| 120 |
addOptionParserChoiceOption( |
| 121 |
versionCmndArgName, "version", supportedVersions, defaultVersionIdx, |
| 122 |
"Version to install for "+productName+".", clp) |
| 123 |
else: |
| 124 |
clp.add_option( |
| 125 |
versionCmndArgName, dest="version", type="string", |
| 126 |
default=defaultVersion, |
| 127 |
help="Version to install for "+productName+" (list of versions open-ended).") |
| 128 |
|
| 129 |
clp.add_option( |
| 130 |
"--install-dir", dest="installDir", type="string", |
| 131 |
default="", |
| 132 |
help="The install directory <install-dir> for "+productName+ \ |
| 133 |
" (default = ''). This can be a relative or absolute path, it can" \ |
| 134 |
" start with ~/, etc." ) |
| 135 |
|
| 136 |
clp.add_option( |
| 137 |
"--install-dir-base", dest="installDirBase", type="string", |
| 138 |
default="", |
| 139 |
help="The base install directory <install-dir> for "+productName+ \ |
| 140 |
" (default = ''). In this case the subdir "+productName+\ |
| 141 |
" will be created under this directory for the install prefix." ) |
| 142 |
|
| 143 |
insertInstallPermissionsOptions(clp) |
| 144 |
|
| 145 |
clp.add_option( |
| 146 |
"--parallel", dest="parallel", type="int", \ |
| 147 |
default=0, \ |
| 148 |
help="Uses parallelism in build if set to > 0." ) |
| 149 |
|
| 150 |
clp.add_option( |
| 151 |
"--make-options", dest="makeOptions", type="string", |
| 152 |
default="", |
| 153 |
help="The options to pass to make for "+productName+"." ) |
| 154 |
|
| 155 |
self.installObj.injectExtraCmndLineOptions(clp, self.productVersion) |
| 156 |
|
| 157 |
clp.add_option( |
| 158 |
"--show-defaults", dest="showDefaults", action="store_true", default=False, |
| 159 |
help="[ACTION] Show the defaults and exit." ) |
| 160 |
|
| 161 |
clp.add_option( |
| 162 |
"--download", dest="download", action="store_true", default=False, |
| 163 |
help="[ACTION] Do the download of the tarball" ) |
| 164 |
|
| 165 |
clp.add_option( |
| 166 |
"--untar", dest="untar", action="store_true", default=False, |
| 167 |
help="[ACTION] Do the untar of the "+productName+" sources" ) |
| 168 |
|
| 169 |
clp.add_option( |
| 170 |
"--configure", dest="configure", action="store_true", default=False, |
| 171 |
help="[ACTION] Configure "+productName+" to build" ) |
| 172 |
|
| 173 |
clp.add_option( |
| 174 |
"--build", dest="build", action="store_true", default=False, |
| 175 |
help="[Action] Build "+productName+" and related executables" ) |
| 176 |
|
| 177 |
clp.add_option( |
| 178 |
"--install", dest="install", action="store_true", default=False, |
| 179 |
help="[ACTION] Install "+productName ) |
| 180 |
|
| 181 |
clp.add_option( |
| 182 |
"--show-final-instructions", dest="showFinalInstructions", action="store_true", |
| 183 |
default=False, |
| 184 |
help="[ACTION] Show final instructions for using "+productName ) |
| 185 |
|
| 186 |
clp.add_option( |
| 187 |
"--do-all", dest="doAll", action="store_true", default=False, |
| 188 |
help="[AGGR ACTION] Same as --download --untar --configure --build --install" \ |
| 189 |
+" --show-final-instructions") |
| 190 |
|
| 191 |
(options, args) = clp.parse_args() |
| 192 |
|
| 193 |
|
| 194 |
# |
| 195 |
# 3) Echo the command-line options |
| 196 |
# |
| 197 |
|
| 198 |
cmndLine = \ |
| 199 |
"******************************************************************************\n" |
| 200 |
cmndLine += scriptName + " \\\n" |
| 201 |
cmndLine += " "+versionCmndArgName + "='"+options.version+"' \\\n" |
| 202 |
cmndLine += " --install-dir='" + options.installDir + "' \\\n" |
| 203 |
cmndLine += " --install-dir-base='" + options.installDirBase + "' \\\n" |
| 204 |
cmndLine += echoInsertPermissionsOptions(options) |
| 205 |
cmndLine += " --parallel='" + str(options.parallel) + "' \\\n" |
| 206 |
cmndLine += " --make-options='" + options.makeOptions + "'\\\n" |
| 207 |
cmndLine += self.installObj.echoExtraCmndLineOptions(options) |
| 208 |
if options.download: |
| 209 |
cmndLine += " --download \\\n" |
| 210 |
if options.untar: |
| 211 |
cmndLine += " --untar \\\n" |
| 212 |
if options.configure: |
| 213 |
cmndLine += " --configure \\\n" |
| 214 |
if options.build: |
| 215 |
cmndLine += " --build \\\n" |
| 216 |
if options.install: |
| 217 |
cmndLine += " --install \\\n" |
| 218 |
if options.showFinalInstructions: |
| 219 |
cmndLine += " --show-final-instructions \\\n" |
| 220 |
if options.doAll: |
| 221 |
cmndLine += " --do-all \\\n" |
| 222 |
|
| 223 |
print(cmndLine) |
| 224 |
|
| 225 |
if options.showDefaults: |
| 226 |
return 0; |
| 227 |
|
| 228 |
# Check the options |
| 229 |
|
| 230 |
if options.installDirBase != "" and options.installDir == "": |
| 231 |
options.installDir=options.installDirBase+"/"+productName |
| 232 |
|
| 233 |
elif options.installDir == "": |
| 234 |
raise Exception("Error, --install-dir=<install-dir> can't be empty!") |
| 235 |
options.installDir = os.path.abspath(os.path.expanduser(options.installDir)) |
| 236 |
|
| 237 |
# |
| 238 |
# 4) Execute the commands |
| 239 |
# |
| 240 |
|
| 241 |
if options.doAll: |
| 242 |
options.download = True |
| 243 |
options.untar = True |
| 244 |
options.configure = True |
| 245 |
options.build = True |
| 246 |
options.install = True |
| 247 |
options.showFinalInstructions = True |
| 248 |
|
| 249 |
baseDir = os.getcwd() |
| 250 |
|
| 251 |
productBaseDir = baseDir+"/"+baseDirName |
| 252 |
|
| 253 |
self.installObj.setup(options) |
| 254 |
|
| 255 |
print("") |
| 256 |
print("A) Download the source for "+productName+" ...") |
| 257 |
print("") |
| 258 |
|
| 259 |
if options.download: |
| 260 |
self.installObj.doDownload() |
| 261 |
else: |
| 262 |
print("Skipping on request ...") |
| 263 |
|
| 264 |
print("") |
| 265 |
print("B) Untar the tarball(s) and set up ready to configure ...") |
| 266 |
print("") |
| 267 |
|
| 268 |
if options.untar: |
| 269 |
self.installObj.doUntar() |
| 270 |
else: |
| 271 |
print("Skipping on request ...") |
| 272 |
|
| 273 |
print("") |
| 274 |
print("C) Configure "+productName+" ...") |
| 275 |
print("") |
| 276 |
|
| 277 |
if options.configure: |
| 278 |
self.installObj.doConfigure() |
| 279 |
else: |
| 280 |
print("Skipping on request ...") |
| 281 |
|
| 282 |
print("") |
| 283 |
print("D) Build "+productName+" ...") |
| 284 |
print("") |
| 285 |
|
| 286 |
if options.build: |
| 287 |
self.installObj.doBuild() |
| 288 |
else: |
| 289 |
print("Skipping on request ...") |
| 290 |
|
| 291 |
print("") |
| 292 |
print("E) Install "+productName+" ...") |
| 293 |
print("") |
| 294 |
|
| 295 |
if options.install: |
| 296 |
self.installObj.doInstall() |
| 297 |
fixupInstallPermissions(options, options.installDir) |
| 298 |
else: |
| 299 |
print("Skipping on request ...") |
| 300 |
|
| 301 |
|
| 302 |
print("") |
| 303 |
print("D) Final instructions for using "+productName+" ...") |
| 304 |
print("") |
| 305 |
|
| 306 |
if options.showFinalInstructions: |
| 307 |
print(self.installObj.getFinalInstructions()) |
| 308 |
else: |
| 309 |
print("Skipping on request ...") |
| 310 |
|
| 311 |
print("\n[End]") |
| 312 |
|
| 313 |
|
| 314 |
# |
| 315 |
# Insert the standard --download-cmnd option |
| 316 |
# |
| 317 |
|
| 318 |
def setStdDownloadCmndOption(installObj, clp, version): |
| 319 |
productName = installObj.getProductBaseName()+"-"+version |
| 320 |
productBaseDirName = productName+"-base" |
| 321 |
defaultDownloadCmnd = \ |
| 322 |
"git clone https://github.com/tribitsdevtools/"+productBaseDirName |
| 323 |
clp.add_option( |
| 324 |
"--download-cmnd", dest="downloadCmnd", type="string", |
| 325 |
default=defaultDownloadCmnd, |
| 326 |
help="Command used to download source for "+productName+"." \ |
| 327 |
+" (Default ='"+defaultDownloadCmnd+"') WARNING: This will delete" \ |
| 328 |
+" an existing directory '"+productBaseDirName+"' if it already exists!") |
| 329 |
|
| 330 |
def setStdGithubDownloadCmndOption(installObj, githubOrg, githubRepo, clp, version): |
| 331 |
productName = installObj.getProductBaseName()+"-"+version |
| 332 |
productBaseDirName = productName+"-base" |
| 333 |
defaultDownloadCmnd = \ |
| 334 |
"wget -O "+productName+".tar.gz https://github.com/"+githubOrg+"/"+githubRepo+"/tarball/v"+version |
| 335 |
clp.add_option( |
| 336 |
"--download-cmnd", dest="downloadCmnd", type="string", |
| 337 |
default=defaultDownloadCmnd, |
| 338 |
help="Command used to download source tarball for "+productName+"." \ |
| 339 |
+" (Default ='"+defaultDownloadCmnd+"') WARNING: This will delete" \ |
| 340 |
+" an existing directory '"+productBaseDirName+"' if it already exists!") |
| 341 |
|
| 342 |
# |
| 343 |
# Get the parallel option |
| 344 |
# |
| 345 |
|
| 346 |
def getParallelOpt(inOptions, optName): |
| 347 |
if inOptions.parallel > 0: |
| 348 |
return " "+optName+str(inOptions.parallel) |
| 349 |
return " " |
| 350 |
|
| 351 |
|
| 352 |
# |
| 353 |
# Insert options for fixing owner, group, permissions |
| 354 |
# |
| 355 |
|
| 356 |
def insertInstallPermissionsOptions(clp): |
| 357 |
|
| 358 |
clp.add_option( |
| 359 |
"--install-owner", dest="installOwner", type="string", default="", |
| 360 |
help="If set, then 'chown -R <install-owner> <install-dir>' will be run after install." \ |
| 361 |
" Note that you can only change the owner when running this script as sudo." ) |
| 362 |
|
| 363 |
clp.add_option( |
| 364 |
"--install-group", dest="installGroup", type="string", default="", |
| 365 |
help="If set, then 'chgrp -R <install-group> <install-dir>' and " \ |
| 366 |
"'chmod -R g+rX <install-dir> will be run after install." \ |
| 367 |
" Note that you can only change a to a group that the owner is" \ |
| 368 |
" a member of." ) |
| 369 |
|
| 370 |
clp.add_option( |
| 371 |
"--install-for-all", dest="installForAll", action="store_true", |
| 372 |
help="If set, then 'chmod -R a+rX <install-dir>' will be run after install.") |
| 373 |
clp.add_option( |
| 374 |
"--no-install-for-all", dest="installForAll", action="store_false", default=False, |
| 375 |
help="If set, then <install-dir> is not opened up to everyone." ) |
| 376 |
|
| 377 |
|
| 378 |
def echoInsertPermissionsOptions(inOptions): |
| 379 |
cmndLine = "" |
| 380 |
cmndLine += " --install-owner='" + inOptions.installOwner + "' \\\n" |
| 381 |
cmndLine += " --install-group='" + inOptions.installGroup + "' \\\n" |
| 382 |
if inOptions.installForAll: |
| 383 |
cmndLine += " --install-for-all \\\n" |
| 384 |
else: |
| 385 |
cmndLine += " --no-install-for-all \\\n" |
| 386 |
return cmndLine |
| 387 |
|
| 388 |
|
| 389 |
def fixupInstallPermissions(inOptions, installDir): |
| 390 |
if inOptions.installOwner: |
| 391 |
print("\n*** Changing owner to '" + inOptions.installOwner + "':") |
| 392 |
echoRunSysCmnd("chown -R " + inOptions.installOwner + " " + installDir) |
| 393 |
if inOptions.installGroup: |
| 394 |
print("\n*** Changing group to '" + inOptions.installGroup + "' and giving " |
| 395 |
"group read/execute:") |
| 396 |
echoRunSysCmnd("chgrp -R " + inOptions.installGroup + " " + installDir) |
| 397 |
echoRunSysCmnd("chmod -R g+rX " + installDir) |
| 398 |
if inOptions.installForAll: |
| 399 |
print("\n*** Allowing everyone to read/execute:") |
| 400 |
echoRunSysCmnd("chmod -R a+rX " + installDir) |