buildpyΒΆ

Back to Bash Module Index

   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
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
#!/usr/bin/env bash

#
#   Summary:
#       - Script to retrieve, compile, and install specific python3 interpreter
#       - Compile and install Python3 versions: 3.0 thru 3.7+
#       - Binaries installed at OS level enable ipython, sqlite, other useful
#         packages during python development
#
#
#   Copyright 2017-2018 Blake Huber.  All rights reserved.
#


# --- global vars ---------------------------------------------------------------------------------


pkg=$(basename $0)                                  # pkg (script) full name
pkg_root=$(echo $pkg | awk -F '.' '{print $1}')     # pkg without file extention
pkg_path=$(cd $(dirname $0); pwd -P)                # location of pkg
pkg_lib="$pkg_path/core"
TMPDIR='/tmp'
workdir="$HOME/Downloads"
NOW=$(date +'%Y%m%d')
LOG_DIR="$HOME/logs"
LOG_FILE="$LOG_DIR/$pkg_root.log"
LOG_CONSOLE="$LOG_DIR/console.log"

# source colors, exitcodes, version file, and  std_function defs | NOTE: source order dependent
source $pkg_lib/colors.sh
source $pkg_lib/exitcodes.sh
source $pkg_lib/version.py
source $pkg_lib/std_functions.sh

# formatting
bbc=$(echo -e ${bold}${a_brightcyan})               # bold bright cyan highlight
bbw=$(echo -e ${bold}${a_brightwhite})              # bold, bright white highlight
title=$(echo -e ${bold}${a_brightwhite})            # title color, white + bold
hic=$(echo -e ${bold}${a_brightyellowgreen})        # help menu accent 1
bin=$(echo -e ${bold}${a_orange})                   # help menu binary accent
ul=$(echo -e ${underline})                          # std underline
bd=$(echo -e ${bold})                               # std bold
wt=$(echo -e ${a_brightwhite})                      # help menu accent 2
fs=$(echo -e ${yellow})                             # file path color
btext=${reset}                                      # clear accents; rtn to native term colors

# optimizations increase performance, but lengthen
# build time (see -o, --optimizations parameter)
OPTIMIZATIONS="false"
PARALLEL_JOB_CT='4'
DEFAULT_MAJOR_VERSION='3.6'


# --- declarations ---------------------------------------------------------------------------------


function help_menu(){
    cat <<EOM

                    ${title}Build ${hic}Python3 ${title}from Source | ${hic}Linux${btext}

  ${title}DESCRIPTION${btext}

        Utility to compile and install the latest versions of python3
        official source binaries download from ${url}https://www.python.org${btext}

  ${title}SYNOPSIS${btext}

           $  ${bin}$pkg${reset}   --install  ${bbc}|${btext}  --uninstall  ${bbc}|${btext}  --show

                          -I | --install <value>
                          -d | --download <value>
                         [-c | --clean <value> ]
                         [-o | --optimization  ]
                          -s | --show  <value>
                         [-i | --info  ]
                         [-q | --quiet ]
                         [-t | --os-detect  ]
                         [-b | --backup-pip ]
                         [-V | --version  ]
                          -U | --uninstall  <value>

                 ${title}Note:${btext}  Requires sudo or root privileges

  ${title}OPTIONS${btext}
        ${title}-b${btext}, ${title}--backup-pip${btext}: Create local Backup copy of installed python
            packages (single operation only)

        ${title}-c${btext}, ${title}--clean${btext}: Remove all build and installation artifacts. Can
            optionally be invoked with a Python major revision number
            provided as a parameter to clean specific build artifacts

        ${title}-d${btext}, ${title}--download${btext}:  Download Python build artifacts, but take no
            other action. Optionally can be invoked with Python major
            revision number to download a specific Python binary set

        ${title}-I${btext}, ${title}--install${btext} <value>: Build, compile, and install Python bin-
            aries  on  local  machine  for  all  system users.  Value
            indicates the latest Python minor version for the release
            specified by <value>:

                      $  sudo  ${bin}$pkg${reset}  --install  3.7

        ${title}-o${btext}, ${title}--optimizations${btext}:  Compile with configure option --enable-
            optimizations  turned-on.  Increases duration of comnpile
            time,  but results in increased execution speed of the C-
            Python compiler.  Recommended if abundant cpu resources.

        ${title}-i${btext}, ${title}--info${btext}:  Display detailed information on the $pkg
            prgram and functions it contains

        ${title}-q${btext}, ${title}--quiet${btext}:  (Optional) Supress output to stdout.  Option is
            invoked for scripted or unattended compile and install.

        ${title}-s${btext}, ${title}--show${btext}: (Optional) Show latest Python minor version avail-
            able for named Python major version (DEFAULT: 3.6)

        ${title}-t${btext}, ${title}--os-detect${btext}: Detect Operating System type and exit (single
            operation only)

        ${title}-U${btext}, ${title}--uninstall${btext}:  Uninstall  the Linux system Python3 binaries
            and related libraries and support artifacts from the local
            file system for Python major revision given as a parameter

        ${title}-V${btext}, ${title}--version${btext}: Display $pkg version and license information

  ${title}EXAMPLES${btext}
            ${title}Compile and install latest Python3.7 with Optimizations${btext}
                $ sudo $pkg  --install 3.7 --optimizations

            ${title}Show which os-package dependencies are available${btext}
                $ sudo $pkg  --show os-packages

            ${title}Show most recent Python 3.4 source binary available${btext}
                $ $pkg  --show Python-3.4
  _________________________________________________________________________

            ${btext}Documentation:  ${url}https://buildpy.readthedocs.io${btext}
  _________________________________________________________________________
${reset}
EOM
    #
    # <-- end function put_rule_help -->
}


function create_global_symlink(){
    ##
    ##  Creates python3 symlink to python executable if missing
    ##
    local major_version="$1"
    local log_file="$2"

    if [ ! "$(command -v python3 2> /dev/null )" ]; then
        ln -s /usr/local/bin/"$major_version" /usr/bin/python3
        std_message "Created global symlink to Python $major_version binary" "INFO" "$log_file"
    else
        std_message "Found global symlink to Python $major_version. Skip creation." "INFO" "$log_file"
    fi
    #
    #<-- end function create_global_symlink -->
}


function install_subcommand_help(){
    ##
    ##   Display help menu for install subcommands
    ##
    cat <<EOM

                       ${title}install${btext} options help

  ${title}DESCRIPTION${btext}

        Optional parameters for use when installing Python3 binaries
        Complementary options to the --install command-line option.

  ${title}SYNOPSIS${btext}

            $ ${bin}$pkg${reset}  --install  <${bbc}value${btext}>   [ OPTIONS ]

                        [ --optimizations  ]
                        [ --parallel-processes  ]
                        [ --quiet  ]

  ${title}OPTIONS${btext}

        ${title}-I${btext}, ${title}--install${btext} <value>: Build, compile, and install Python bin-
            aries  on  local  machine  for  all  system users.  Value
            indicates the latest Python minor version for the release
            specified by <value>:

                    $  sudo  ${bin}$pkg${reset}  --install  3.7

        ${title}-p, --parallel-processes${btext} <value>: Number of parallel processes
            (1-9) to use during compile with make.  Setting to a value
            of one (1) equals no parallelism, Default = 4.  Use of low
            setting on systems with low amount of free resources rec-
            commended.

        ${title}-o${btext}, ${title}--optimizations${btext}:  Compile with configure option --enable-
            optimizations  turned-on.  Increases duration of comnpile
            time,  but results in an increased execution speed of the
            C-Python compiler. Recommended if abundant cpu resources.

        ${title}-q${btext}, ${title}--quiet${btext}:  (Optional) Supress output to stdout.  Option is
            invoked for scripted or unattended compile and install.

        ${title}help${btext}:  Option to display the commit-log help menu contents

                $ ${BOLD}$pkg${reset}  --install  ${cyan}help${btext}
        ____________________________________________________________
    ${reset}
EOM
    #
    # <-- end function commit_log_help -->
}


function is_float(){
    local num="$1"
    local regex='^[0-9]+[.][0-9]+?$'
    local regex2='^[0-9]+[.][0-9][0-9]+?$'
    #
    if [[ $num =~ $regex ]] || [[ $num =~ $regex2 ]]; then
        # int or float
        return 0
    fi
    # not a number
    return 1
}


function parse_parameters() {
    ##
    ##  Parse all command-line parameters
    ##

    local var parameter major

    if [[ ! $@ ]]; then
        help_menu | more
        exit 0
    else
        while [ $# -gt 0 ]; do
            case $1 in
                -h | --help)
                    help_menu | more
                    shift 1
                    exit 0
                    ;;

                -b | --backup-pip)
                    PIP_BACKUP="true"
                    shift 1
                    ;;

                -c | --clean)
                    CLEAN_UP="true"
                    if [ "$2" ]; then
                        case "$2" in
                            'ALL' | 'all' | 'everything')
                                CLEAN_VERSION="ALL"
                                shift 2
                                ;;
                            *)
                                var="$2"
                                parameter=${var#Python-}
                                if is_float "$parameter"; then
                                    MAJOR_VERSION="$parameter"
                                    CLEAN_VERSION="$parameter"
                                    shift 2
                                else
                                    std_error_exit "You must provide Python major version (Example: 3.7)"
                                fi
                                ;;
                        esac

                    else
                        CLEAN_VERSION="ALL"
                        shift 1
                    fi
                    shift 1
                    ;;

                '-P' | '--purge')
                    # uninstall option, full system expunge
                    WIPE_CLEAN=true     # clean invoked with --uninstall option
                    shift 1
                    ;;

                -d | --download)
                    DOWNLOAD_ONLY="true"
                    if [ "$2" ]; then
                        var="$2";
                        parameter=${var#Python-}
                        if is_float "$parameter"; then
                            MAJOR_VERSION="$parameter"
                            shift 2
                        else
                            std_error_exit "You must provide Python major version (Example: 3.7)"
                        fi
                    else
                        MAJOR_VERSION="$DEFAULT_MAJOR_VERSION"
                        std_message "Defaulting to Python $MAJOR_VERSION" "INFO" $LOG_FILE
                        shift 1
                    fi
                    shift 1
                    ;;

                -I | --install)
                        INSTALL="true"

                        if [ "$2" ] && [ "$2" = "help" ]; then
                            install_subcommand_help
                            exit 0

                        elif [ "$2" ] && [ "$2" = "os-packages" ]; then
                            OPERATION="INSTALL_OS_DEPS"     # install os dependencies
                            INSTALL_OS_DEPS="true"
                            shift 2

                        elif [ "$2" ]; then
                            var="$2"
                            parameter=${var#Python-}
                            # stdout allowed
                            case "$parameter" in
                                [0-9].[0-9] | [0-9].[0-9][0-9]  | [0-9].[0-9].[0-9] | [0-9].[0-9][0-9].[0-9])
                                        # floating point - Python major version number
                                        MAJOR_VERSION="$parameter"
                                        shift 2
                                        ;;
                                "os" | "packages" | "deps" | "os_deps" | "os_packages")
                                        OPERATION="INSTALL_OS_DEPS"     # install os dependencies
                                        INSTALL_OS_DEPS="true"
                                        shift 2
                                        ;;
                                * )
                                        tab10='          '
                                        msg2="${tab10}or 'os-packages' to install operating system depedencies."
                                        std_message "You must provide either a Python Major Version (Example: 3.6),\n${msg2}" "INFO" $LOG_FILE
                                        exit $E_DEPENDENCY
                                        ;;
                            esac
                        else
                            std_error_exit "You must provide Python major version (Example: 3.7) or 'os_deps'"
                        fi
                    ;;

                -t | --os-detect)
                    OS_DETECT="true"
                    shift 1
                    ;;

                -o | --optimization | --optimizations | --optimize)
                    OPTIMIZATIONS="true"
                    shift 1
                    ;;

                -i | --info)
                    OPERATION="PKG_INFO"
                    PKG_INFO="true"
                    shift 1
                    ;;

                -p | --parallel-processes)
                    if [ "$2" ] && [ "$2" = "help" ]; then
                        install_subcommand_help
                        exit 0
                    elif [ "$2" ]; then
                        var="$2"
                        PARALLEL_JOB_CT=${var%\-*}
                        shift 2
                    else
                        std_error_exit "You must provide a parallel process count (0-9) when using --parallel-processes option" $E_DEPENDENCY
                    fi
                    ;;

                -q | --quiet)
                    QUIET="true"
                    shift 1
                    ;;

                -s | --show)
                    if [ "$2" ]; then
                        OPERATION='SHOW'
                        SUBCMD="$2"
                        if [ $3 ]; then
                            alternate_os="$3"
                            shift 3
                        else
                            shift 2
                        fi
                    else
                        shift 1
                    fi
                    ;;

                -U | --uninstall)
                    UNINSTALL="true"
                    if [ "$2" ]; then
                        if [ "$(echo "$2" | grep Python)" ]; then
                            # convert to major version number
                            var="$2"; parameter=${var#Python-}
                        else
                            parameter="$2"
                        fi
                        if is_float "$parameter"; then
                            MAJOR_VERSION="$parameter"
                            UNINSTALL_VERSION="$parameter"
                        else
                            std_error_exit "You must provide Python major version (Example: 3.7)"
                        fi
                        shift 2
                    fi
                    ;;

                -V | --version)
                    DISPLAY_VERSION="true"
                    shift 1
                    ;;

                *)
                    echo "unknown parameter. Exiting"
                    exit 1
                    ;;
            esac
        done
    fi

    # defaults
    if [ ! $MAJOR_VERSION ]; then

        # Python Major version
        MAJOR_VERSION="$DEFAULT_MAJOR_VERSION"
        std_logger "Defaulting to Python $MAJOR_VERSION" "INFO" $LOG_FILE

    else

        # major version supplied with command options
        std_message "Python Major Version $MAJOR_VERSION provided via parameter" "INFO" $LOG_FILE

    fi

    # eliminate tty output if quiet=true
    if [ $QUIET ]; then tty="/dev/null"; else tty="/dev/tty"; fi
    #
    # <-- end function parse_parameters -->
}


function binary_depcheck(){
    ##
    ## exit if binary dependencies not installed ##
    ##
    local check_list=( "$@" )
    local msg

    for prog in "${check_list[@]}"; do

        if ! type "$prog" > /dev/null 2>&1; then
            # print error msg
            msg="${title}$prog${reset} is required and not found in the PATH (code $E_DEPENDENCY)"
            std_warn "$msg" $LOG_FILE
            # print pkg description info
            printf -- '%s\n' "$(cat $pkg_lib/$prog.pkg)"
            exit $E_DEPENDENCY
        fi

    done
    #
    # <<-- end function binary_depcheck -->>
}


function reset_rc_version(){
    ##
    ##  Reset VERSION global variable in case binary set to be
    ##  compiled is a release candidate, alpha, or beta code.
    ##
    local version="$1"

    if [[ $(ls $TMPDIR | grep "Python-$version.tar.xz") ]]; then
        echo "$version"
        std_logger "VERSION retained as ${fs}$VERSION${reset} -- no refactor required" "INFO" $LOG_FILE

    elif [[ $(ls $TMPDIR | grep "Python-${version}rc[1-4].tar.xz") ]]; then
        bin=$(ls $TMPDIR | grep "Python-${version}rc[1-4].tar.xz")
        echo "$(echo $bin | awk -F '-' '{print $2}')" | awk -F 'tar' '{print $1}' | rev | cut -c 2-20 | rev
        std_logger "Reset VERSION to ${fs}$VERSION${reset}" "INFO" $LOG_FILE

    elif [[ $(ls $TMPDIR | grep "Python-$version[a-b][1-4].tar.xz") ]]; then
        bin=$(ls $TMPDIR | grep "Python-${version}[a-b][1-4].tar.xz")
        echo "$(echo $bin | awk -F '-' '{print $2}')" | awk -F 'tar' '{print $1}' | rev | cut -c 2-20 | rev
        std_logger "Reset VERSION to ${fs}$VERSION${reset}" "INFO" $LOG_FILE
    fi
}


function binary_installed_bool(){
    ##
    ## return boolean value if binary dependencies installed ##
    ##
    local check_list=( "$@" )
    #
    for prog in "${check_list[@]}"; do
        if ! type "$prog" > /dev/null 2>&1; then
            return 1
        fi
    done
    return 0
    #
    # <<-- end function binary_installed_bool -->>
}


function configure_python(){
    ##
    ## function runs configure stage with appropriate options ##
    ##
    local optimizations="$1"
    local quiet="$2"
    #
    if [ "$optimizations" = "true" ] && [ ! $quiet ]; then

        ./configure --enable-optimizations  | tee /dev/tty >> $LOG_CONSOLE

    elif [ "$optimizations" = "true" ] && [ $quiet ]; then

        ./configure --enable-optimizations  >> $LOG_CONSOLE

    elif [ "$optimizations" = "false" ] && [ ! $quiet ]; then

        ./configure  | tee /dev/tty >> $LOG_CONSOLE

    elif [ "$optimizations" = "false" ] && [ $quiet ]; then

        ./configure  >> $LOG_CONSOLE

    fi

    return 0
    #
    # <-- end function configure_python -->
}


function continue_on(){
    ##
    ## user prompts to continue path ##
    ##
    local msg="$1"
    local choice

    if [ "$msg" ]; then
        std_message "$msg" "INFO" $LOG_FILE
    fi

    read -p "$(printf -- '\t\tContinue? [quit]:   \n\n' )" choice

    if [ -z $choice ] || [ "$choice" = "q" ] || [ "$choice" = "quit" ]; then
        return 1
    else
        case $choice in
            'Yes' | 'yes' | 'Y' | 'y' | true)
                return 0
                ;;
            *)
                return 1
        esac
    fi
    #
    # <-- end function continue_on -->
}


function _current_downloads(){
    ##
    ##  Examines local fs for downloaded artifacts
    ##
    ##      - returns entry for each python binary set downloaded to /tmp
    ##
    local index="0"
    local IFS
    declare -a arr_targets xz tgz sorted_targets

    # populate tar.xz, tgz arrays
    mapfile -t xz < <(find /tmp -name \*.tar.xz 2>/dev/null)
    mapfile -t tgz < <(find /tmp -name \*.tgz 2>/dev/null)

    for i in "${xz[@]}"; do
        temp="$(echo $i | awk -F '.tar' '{print $1}' | awk -F '.' '{print $1"."$2}')"
        xz[$index]=$(echo $temp | awk -F '/' '{print $NF}')
        (( index++ ))
    done

    index="0"

    for i in "${tgz[@]}"; do
        temp="$(echo $i | awk -F '.tgz' '{print $1}' | awk -F '.' '{print $1"."$2}')"
        tgz[$index]=$(echo $temp | awk -F '/' '{print $NF}')
        (( index++ ))
    done

    arr_targets=( "$(echo "${tgz[@]}")"  "$(echo "${xz[@]}")" )
    # sort
    IFS=$'\n' sorted_targets=($(sort <<<"${arr_targets[*]}"))
    unset IFS

    echo "${sorted_targets[@]}"
    #
    # <--- end function _clean_subcommands --->
}


function depcheck(){
    ##
    ## validate cis report dependencies ##
    ##
    local log_dir="$1"
    local log_file="$2"
    local console_log="$3"
    local msg

    ## test default shell ##
    if [ ! -n "$BASH" ]; then
        # shell other than bash
        msg="Default shell appears to be something other than bash. Please rerun with bash. Aborting (code $E_BADSHELL)"
        printf -- "\n%s\n" "$msg"
    fi

    ## logging prerequisites  ##
    if [[ ! -d "$log_dir" ]]; then

        if ! mkdir -p "$log_dir"; then
            printf -- "\n%s: failed to make log directory: %s. Exit\n" % $pkg $log_dir
        fi

    elif [ ! -f $log_file ]; then

        if ! touch $log_file 2>/dev/null; then
            printf -- "\n%s: failed to seed log file: %s. Exit\n" % $pkg $log_file
        fi

    elif [ ! -f $console_log ]; then

        if ! touch $console_log 2>/dev/null; then
            printf -- "\n%s: failed to seed console log file: %s. Exit\n" % $pkg $console_log
        fi

    fi

    ## working directory for user
    if [[ ! -d "$workdir" ]]; then

        if ! mkdir -p "$workdir"; then
            std_error_exit "$pkg: failed to make log directory: $workdir. Exit" $E_DEPENDENCY
        fi

    fi

    ## check for required cli tools ##
    binary_depcheck awk bc curl

    # success
    std_logger "$pkg: dependency check satisfied." "INFO" $log_file

    # set path
    PATH=$PATH:/usr/local/bin
    return 0
    #
    # <<-- end function depcheck -->>
}


function display_program_version(){
    ##
    ## output script version info, license
    ##
    local _version=$__version__
    local _hic=$(echo -e ${a_brightblue})
    local _year=$(date +%G)
    local _bashver="$(
                bash --version | head -n1 | awk -F 'version' '{print $2}' \
                    | awk '{print $1}' | awk -F '(' '{print $1}'
            )"
    #
    cat <<EOM
    ______________________________________________________________________





              ${_hic}$pkg${reset} version: ${title}$_version${reset}   |   GNU Bash $_bashver




    ______________________________________________________________________

      Copyright 2017-$_year, Blake Huber.  This program distributed under
      GPL v3.  This copyright notice must remain with derivative works.
    ______________________________________________________________________

EOM
}


function download_progress(){
    ##
    ## display progress for downloading large files, wget only
    ##
    local url="$1"

    wget --progress=dot $url 2>&1 | grep --line-buffered "%" | \
    sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
    echo -ne "\b\b\b\b"
    echo " Download Complete"
    return 0
}


function download_binary(){
    ##
    ## download python components ##
    ##
    local version="$1"
    local major="$2"
    local minor="$(echo $version | awk -F '.' '{print $NF}')"
    local bin_file="Python-$version.tar.xz"
    local rc_file="Python-${version}rc[1-4].tar.xz"
    local beta_file="Python-$version[a-b][1-4].tar.xz"
    local bin_dcr                                 # N-1 minor release
    local bin_alt="Python-$version.tgz"
    local bin                                     # loop binfile
    local url
    local cached_download
    declare -a arr_versions

    function download_rc(){
        ## Download release candidate binary set
        local bin

        cd $TMPDIR || exit $E_OSERROR

        for rtype in rc b a; do
            for i in 4 3 2 1; do
                bin="Python-${version}${rtype}${i}.tar.xz"

                # check if pre-existing; remove
                if [ -f "$TMPDIR/$bin" ]; then
                    rm -f "$TMPDIR/$bin"
                    std_logger "Found previously downloaded binary ($TMPDIR/$bin). Removed"  "INFO" $LOG_FILE
                fi

                # download binary set
                url="https://www.python.org/ftp/python/$version/$bin"
                wget "$url" >/dev/null 2>&1

                # check if downloaded, break
                if [ -f "$TMPDIR/$bin" ]; then
                    echo "$bin"
                    break
                fi
            done
            # check if downloaded, break
            if [ -f "$TMPDIR/$bin" ]; then
                break
            fi
        done
        return 0
    }

    nminus1="$major.$(( $minor - 1 ))"
    bin_dcr="Python-${nminus1}.tar.xz"

    # for Python < version 2.6, only tgz archive available; download alternate bin format
    if (( $(echo "$major < 2.6" | bc -l) )); then  bin_file=$bin_alt; fi

    arr_versions=( "$bin_file" )          # array of download candidates

    cd $TMPDIR || exit $E_OSERROR

    for bin in "${arr_versions[@]}"; do

        # check for pre-existing file
        if [ -f "$TMPDIR/$bin" ]; then
            cached_download=true
            std_message "Found previously downloaded binary ($bin). Using..." "OK"  $LOG_FILE
            break
            #rm "$TMPDIR/$bin"
        fi

        std_message "Downloading $bin ..." "INFO"  $LOG_FILE

        if [ "$(type wget 2>/dev/null)" ]; then

            url="https://www.python.org/ftp/python/$version/$bin"
            wget "$url" >/dev/null 2>&1 &
            progress_dots --fast "false"

        elif [ "$(type curl 2>/dev/null)" ]; then

            curl -O "https://www.python.org/ftp/python/$version/$bin" >/dev/null 2>&1 &
            progress_dots --fast "false"

        else
            std_error_exit "Either wget or curl binary not found. Cannot retrieve python source files" $E_DEPENDENCY
        fi

        # check if downloaded, break
        if [ -f "$TMPDIR/$bin" ]; then
            break
        else
            std_warn "$bin not downloaded, version $version in alpha, beta, or release candidate stage." $LOG_FILE
            std_message "Downloading latest available alpha, beta, or release candidate binary" "INFO"
            bin=''

            # attempt to download release canidate
            bin="$(download_rc)"

            if [ ! -f "$TMPDIR/$bin" ]; then
                std_warn "Release candidate version not found. Exit" $LOG_FILE
                exit $E_OSERROR
            fi
        fi

    done

    # user status msg
    if file_check "$TMPDIR/$bin"; then

        if [[ $(echo $bin | grep "Python-$version.tar.xz") ]] && [[ "$cached_download" ]]; then
            std_message "${yellow}Python${btext} ${bd}$version${btext} cached source successfully located in ${fs}$TMPDIR/$bin${reset}" "INFO" $LOG_FILE

        elif [[ $(echo $bin | grep "Python-$version.tar.xz") ]]; then
            std_message "${yellow}Python${btext} ${bd}$version${btext} source downloaded successfully to ${fs}$TMPDIR/$bin${reset}" "INFO" $LOG_FILE

        elif [[ $(echo $bin | grep "Python-${version}rc[1-4].tar.xz") ]]; then
            std_message "${yellow}Python${btext} ${bd}$version${btext} release candidate source downloaded successfully to ${fs}$TMPDIR/$bin${reset}" "INFO" $LOG_FILE

        elif [[ $(echo $bin | grep "Python-$version[a-b][1-4].tar.xz") ]]; then
            std_message "${yellow}Python${btext} ${bd}$version${btext} alpha/beta source downloaded successfully to ${fs}$TMPDIR/$bin${reset}" "INFO" $LOG_FILE
        fi

    else
        # download filed
        std_warn "Problem downloading ${yellow}Python${btext} ${bd}$version${btext} source binary to ${fs}$TMPDIR/$bin${btext}" $LOG_FILE
        std_warn "${fs}$TMPDIR/$bin${btext} Not saved to local filesystem" $LOG_FILE
    fi
    #
    # <<-- end function download_binary -->>
}


function file_check(){
    ##
    ##  Validates existence of file object on local fs
    ##
    ##  Returns:
    ##      - Success | Failure, TYPE: bool
    ##

    local file_path="$1"

    if [ -f "$file_path" ]; then

        return 0

    else

        return 1      # file not found

    fi
    #
    # <<-- end function file_check -->>
}


function pip_backup(){
    ##
    ## create backup of local python package repo ##
    ##
    local pip                       # pip package mgr binary path
    local pip3                      # pip3 package mgr binary path
    local pip_r
    local pip3_backup

    pip3=$(which pip3 2>/dev/null)
    pip3_backup="pip3-requirements-$NOW.txt"

    pip=$(which pip 2>/dev/null)
    pip_r="pip-requirements-$NOW.txt"
    #
    msg="Creating backups of local python package repositories in $workdir"
    std_message "$msg" "INFO"
    std_logger "Backup of local python package repositories - START" "INFO" $LOG_FILE

    # pip backup
    if [ "$pip" ]; then
        $pip --no-cache-dir freeze > $workdir/$pip_r
        std_logger "Created pip backup: $workdir/$pip_r" "INFO" $LOG_FILE
    fi

    # pip3 backup
    if [ "$pip3" ]; then
        $pip3 --no-cache-dir freeze > $workdir/$pip3_backup
        std_logger "Created pip3 backup: $workdir/$pip3_backup" "INFO" $LOG_FILE
    fi

    if [ -f "$workdir/$pip3_backup" ] && [ -f "$workdir/$pip_r" ]; then

        if [[ $(diff $workdir/$pip_r $workdir/$pip3_backup) ]]; then
            # pip backup and pip3 files are different
            msg="Created backups of local python package repositories in $workdir:\n
            \tPip: \t$pip_r \n\t\tPip3: \t$pip3_backup\n"
            std_message "$msg" "INFO" $LOG_FILE

        else
            # pip backup & pip3 backup file same
            rm -f $workdir/$pip3_backup
            std_logger "Deleting pip3 backup file ($pip3_backup) - Identical to $pip_r" "INFO" $LOG_FILE
            std_logger "Retained single backup file $workdir/$pip_r" "INFO" $LOG_FILE

            msg="Created backup of python package repositories in $workdir:\n\n\t\tpip: \t$pip_r"
            std_message "$msg" "INFO"
        fi

    elif [ $pip3_backup ]; then
        # pip3 backup only
        msg="Created backup of python package repositories in $workdir:\n\n\t\tpip: \t$pip3_backup"
        std_message "$msg" "INFO"
        std_logger "Created backup file in $workdir:  $pip3_backup" "INFO" $LOG_FILE

    else
        # pip backup only
        msg="Created backup of python package repositories in $workdir:\n\n\t\tpip: \t$pip_r"
        std_message "$msg" "INFO"
        std_logger "Created backup file in $workdir:  $pip_r" "INFO" $LOG_FILE
    fi

    std_logger "Backup of local python package repositories - END" "INFO" $LOG_FILE
    return 0

    #
    # <<-- end function pip_backup -->>
}


function latest_version_available(){
    ##
    ## retrieve latest minor version of python to install ##
    ##
    local major_version="$1"                # 3.7, etc
    local minor=20                          # starting count to decrement 3.7.minor
    local act="$(echo -e ${cyan})"          # color code
    local rst="$(echo -e ${resetansi})"     # reset color code
    #
    # Guarding Clause:
    # Return if VERSION during installs already set; else continue
    if [ "$VERSION" ] && [ "$INSTALL" ]; then return 0; fi

    if [ "$(type wget 2>/dev/null)" ]; then
        wget -O "$TMPDIR/index.html" 'https://www.python.org/ftp/python' 2>/dev/null
    else
        curl -L -o "$TMPDIR/index.html" 'https://www.python.org/ftp/python' 2>/dev/null
    fi

    # find highest (most recent) minor version
    while (( $minor >= 0 )); do

        try="$(grep $major_version.$minor $TMPDIR/index.html)"

        if [ "$try" ]; then
            # set full version identifer to global var
            VERSION=$major_version.$minor

            # date of release
            rdate="$(echo $try | awk '{print $3}')"
            fdate="${act}$(date --date=${rdate} +"%a %b %d %Y")${rst}"

            std_message "Found most recent binaries available for Python Version ${major_version}" "OK" $LOG_FILE
            std_message "${yellow}Python ${title}$VERSION${rst}, released on $fdate" "INFO" $LOG_FILE

            found="true"
            break
        fi
        minor=$(( $minor - 1 ))
    done

    if [ ! "$found" ]; then
        std_warn "No binary version found for Python $major_version. Exit" $LOG_FILE
        rm $TMPDIR/index.html
        return 1    # minor version not found; return false
    fi

    # rm version artifacts
    rm $TMPDIR/index.html
    return 0        # minor version identified; return true
}


function logfile_owner(){
    ##
    ## ensures log file owned by normal user ##
    ##
    local log_file="$1"
    local console_log="$2"
    local yl=${yellow}
    local rst=${reset}
    #
    # update owner of log file if NOT located in /var/log
    if [ "$SUDO_USER" ] && [ ! "$(echo $log_file | grep \/var)" ] && \
       [ "$(ls -l $log_file | awk '{print $3}')" != "root" ]; then

           chown $SUDO_USER:$SUDO_USER $log_file
           std_logger "Successfully updated file owner to root for ${yl}$log_file${rst}" "INFO" $log_file

    fi
    # update owner of console log file if NOT located in /var/log
    if [ "$SUDO_USER" ] && [ ! "$(echo $console_log | grep \/var)" ] && \
       [ "$(ls -l $console_log | awk '{print $3}')" != "root" ]; then

            chown $SUDO_USER:$SUDO_USER $console_log
            std_logger "Successfully updated file owner to root for ${yl}$console_log${rst}" "INFO" $log_file

    fi
    # update permissions on log files if not correct
    if [ "$SUDO_USER" ] || [ "$EUID" = "0" ]; then
        chmod 0666 $log_file $console_log

    fi
}


function mlocate_installation(){
    ##
    ## installs os-specfic installation of mlocate; runs db update
    ##
    ##   This should ideally work via the following:
    ##      1. install mlocate
    ##      2. after updatedb -v run, find all python3.X locations
    ##      3. Using this list, delete each path in the list
    ##
    ##  Installing mlocate is OS-specific
    ##
    local clearscn=$(which clear 2>/dev/null)
    local epel_url='https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm'
    #
    os_type     # linux distro environment; sets globals: family, release
    #
    std_message "$family Linux:  Installing mlocate binaries in preparation for system Python3 removal" "INFO" $LOG_FILE

    if [ "$family" = "AmazonLinux" ]; then

        $clearscn    # clear screen
        std_message "$family Linux:  Update OS pkgs" "INFO" $LOG_FILE
        yum -y update >> $LOG_CONSOLE &
        progress_dots --fast "false"
        std_message "$family Linux:  Install mlocate and dependencies" "INFO" $LOG_FILE
        yum -y install mlocate
        std_message "$family Linux:  First db filesystem discovery run..." "INFO" $LOG_FILE
        updatedb -v >/dev/null &
        progress_dots --fast "false" --text "Searching for Python filesystem locations"
        return 0

    elif [ "$family" = "Redhat" ] || [ "$family" = "CentOS" ]; then

        $clearscn    # clear screen
        std_message "$family Linux:  Update OS pkgs" "INFO" $LOG_FILE
        yum -y update >> $LOG_CONSOLE &
        progress_dots --fast "false"
        # install epel
        std_message "$family Linux:  Installing epel package repository" "INFO" $LOG_FILE
        curl -o epel.rpm $epel_url
        yum -y install epel.rpm
        std_message "$family Linux:  Install mlocate and dependencies" "INFO" $LOG_FILE
        yum -y update
        yum -y install mlocate
        std_message "$family Linux:  First db filesystem discovery run..." "INFO" $LOG_FILE
        updatedb -v >/dev/null &
        progress_dots --fast "false" --text "Searching for Python filesystem locations"
        return 0

    elif [ "$family" = "Ubuntu" ] || [ "$family" = "Mint" ]; then

        $clearscn    # clear screen
        std_message "$family Linux:  Update OS pkgs" "INFO" $LOG_FILE
        apt update -y >> $LOG_CONSOLE &
        progress_dots --fast "false"
        std_message "$family Linux:  Install mlocate and dependencies" "INFO" $LOG_FILE
        apt install -y mlocate
        std_message "$family Linux:  First db filesystem discovery run..." "INFO" $LOG_FILE
        updatedb -v >/dev/null &
        progress_dots --fast "false" --text "Searching for Python filesystem locations"
        return 0

    fi

    return 1    # mlocate installation fail or not possible
    #
    # <-- end function mlocate_installation -->
}


function os_type(){
    ##
    ## determine linux os distribution ##
    ##
    function determine_osrelease(){
        # interrogate /etc/os-release directly
        # to determine release
        id=$(grep VERSION_ID /etc/os-release | awk -F '=' '{print $2}')
        clean_id=$(echo $id | cut -c 2-20 | rev | cut -c 2-20 | rev)
        printf -- '%s\n' "$clean_id"
    }

    OS_INFO=$(source $pkg_lib/os_distro.sh 2>/dev/null)


    if [ "$OS_INFO" ]; then

        family=$(echo $OS_INFO | awk '{print $1}')
        release=$(echo $OS_INFO | awk '{print $2}')
        codename=$(echo $OS_INFO | awk '{print $3}')

        case "$family" in
            'Ubuntu')
                act=${a_magenta}
                ;;
            'Redhat' | 'CentOS')
                act=${red}
                ;;
            'AmazonLinux')
                act=${a_orange}
                ;;
        esac

        if [ ! "$release" ] || [ -z "$release" ]; then
            release=$(determine_osrelease)
        fi

        std_message "Operating System Info detected:
        \n\t\tFamily:\t\t${act}$family${btext}
        \tRelease:\t${bd}$release${btext}
        \tCodename:\t$codename" "INFO"
        std_logger "OS Info Detected: Family: $family, Release: $release, Codename: $codename" "INFO" $LOG_FILE

    else

        std_warn "Failed to determine Linux Distribution Type.  Fall back detection" $LOG_FILE
        FALLBACK_DETECT="true"

    fi
    #
    # <-- end function os_type -->
}


function ospackage_installed(){
    ##
    ##  os specific test to determine if an operating system
    ##  package is installed
    ##
    local packagemgr="$1"       # packagemgr; redhat (.rpm) or debian (.deb)
    local ospkg="$2"            # os package tested for installation status

    case $packagemgr in
        'rpm' | '.rpm' | 'redhat')
            if [ ! "$(rpm -q $ospkg | grep 'not installed')" ]; then
                return 0    # is installed
            fi
            ;;
        'deb' | '.deb' | 'debian')
            if [ "$(dpkg -s $ospkg 2>/dev/null | grep Status\:)" ]; then
                return 0    # is installed
            fi
            ;;
    esac

    return 1    # not installed
    #
    # < --- end function ospackage_installed --->
}


function ospackage_available(){
    ##
    ##  os specific test to determine if an operating system
    ##  package is present in local package repositories
    ##
    local packagemgr="$1"       # packagemgr; redhat (.rpm) or debian (.deb)
    local ospkg="$2"            # os package tested for installation status

    case $packagemgr in
        'rpm' | '.rpm' | 'redhat')
            if [ "$(yum list $ospkg 2>/dev/null | grep -i 'Available')" ]; then
                return 0    # is available, but not installed
            fi
            ;;
        'deb' | '.deb' | 'debian')
            if [ "$(apt-cache search $ospkg)" ]; then
                return 0    # is available, but not installed
            fi
            ;;
    esac

    # not installed
    return 1
    #
    # < --- end function ospackage_available --->
}


function ospackage_status(){
    ##
    ##  displays os package summary info
    ##
    local packagemgr="$1"
    local ospkg="$2"
    local logfile="$3"
    local prefix

    case $packagemgr in

        'deb')
            if ospackage_installed $packagemgr $ospkg; then
                # available and installed
                summary=$(apt-cache show $ospkg | grep Description | head -n 1 | awk -F ':' '{print $2}')
                prefix=" INSTALLED"

            elif ospackage_available $packagemgr $ospkg; then
                # available but not installed
                if [ "$(apt-cache show $ospkg 2>/dev/null)" ]; then
                    summary=$(apt-cache show $ospkg | grep Description | head -n 1 | awk -F ':' '{print $2}')
                else
                    summary=$(apt-cache search $ospkg 2>/dev/null | awk '{ print substr(" "$0, index($0,$3)) }')
                fi
                prefix=" AVAILABLE"

            elif ! ospackage_available $packagemgr $ospkg; then
                # not available
                prefix=" NOT-FOUND"
            fi
            ;;

        'rpm')
            if ospackage_installed $packagemgr $ospkg; then
                # available and installed
                summary=$(rpm -qi $ospkg | grep 'Summary' | awk -F ':' '{print $2}')
                prefix=" INSTALLED"

            elif ospackage_available $packagemgr $ospkg; then
                # available but not installed
                summary=$(yum info $ospkg 2>/dev/null | grep 'Summary' | head -n1 | awk -F ':' '{print $2}')
                prefix=" AVAILABLE"

            elif ! ospackage_available $packagemgr $ospkg; then
                # not available
                prefix=" NOT-FOUND"
            fi
            ;;

        'alternate_rpm')
            if ospackage_available 'rpm' $ospkg; then
                # available but not installed
                summary=$(yum info $ospkg 2>/dev/null | grep 'Summary' | head -n1 | awk -F ':' '{print $2}')
            else
                summary='no available package description in current os'
            fi
            prefix="ESTIMATE"
            ;;

        'alternate_deb')
            if ospackage_available 'deb' $ospkg; then
                # available but not installed
                if [ "$(apt-cache show $ospkg 2>/dev/null)" ]; then
                    summary=$(apt-cache show $ospkg | grep Description | head -n 1 | awk -F ':' '{print $2}')
                else
                    summary=$(apt-cache search $ospkg 2>/dev/null | awk '{ print substr(" "$0, index($0,$3)) }')
                fi
            else
                summary='no available package description in current os'
            fi
            prefix="ESTIMATE"
            ;;

        'alternate')
            printf -- ' %s\n' "$ospkg"
            ;;
    esac

    if [ -z "$summary" ] || [ "$summary" = "" ]; then
        summary=' description not found'
    fi

    case $summary in
        ' description not found')
            std_logger "$ospkg: Description not found for os-package $ospkg" "$prefix" $logfile
            ;;

        *)
            case ${#ospkg} in
                [0-5])
                    std_message "$ospkg\t\t\t|  $summary" $prefix $logfile 'noblanklines'
                    ;;
                [6-9] | 10 | 11 | 12 | 13)
                    std_message "$ospkg\t\t|  $summary" $prefix $logfile 'noblanklines'
                    ;;
                *)
                    std_message "$ospkg\t|  $summary" $prefix $logfile 'noblanklines'
                    ;;
            esac
            ;;
    esac
    #
    # <--- end function ospackage_status --- >
}


function install_deb_packages(){
    ##
    ##  installs packages supplied as an array
    ##
    ##  Usage:
    ##
    ##      install_deb_packages  -o "install"  --packages pkgs[@]
    ##
    ##  Where:
    ##      --operation:  update | upgrade | install
    ##      --packages:  associative array
    ##
    ##  Note:
    ##      Mint Linux machines not upgraded via apt for reliability
    ##
    declare -a pkg_list
    local operation
    local logfile=$LOG_FILE
    local consolelog=$LOG_CONSOLE
    local show_packages=$SHOW_OSPACKAGES

    while [ $# -gt 0 ]; do
        case $1 in
            -a | --alt_os)
                alternate_os="$2"
                shift 2
                ;;

            -l | --log)
                logfile="$2"; shift 2
                ;;

            -o | --operation)
                case $2 in
                    update | upgrade | install)
                        operation="$2"
                        shift 2
                        ;;
                    *)
                        std_error_exit "Operation must be either update, upgrade, or install. Exit"
                        ;;
                esac
                ;;

            -p | --packages)
                pkg_list=("${!2}"); shift 2
                ;;
        esac
    done

    ##  display ospackage status instead of updating   ##

    if [ $show_packages ] && [ $alternate_os ]; then
        case $alternate_os in
            redhat* | centos* | amazonlinux*)
                ospackage_status "alternate_rpm" "$ospkg" "$logfile"
                ;;
            *)
                ospackage_status "alternate_deb" "$ospkg" "$logfile"
                ;;
        esac

    elif [ $show_packages ]; then
        if [ -z $operation ] || [ "$operation" = "install" ]; then
            for ospkg in "${pkg_list[@]}"; do

                ospackage_status "deb" "$ospkg" "$logfile"

            done
        fi
        return 0
    fi

    ##    perform actual update of system ospackages   ##

    if [ "$operation" = "update" ]; then
        std_message "Refreshing os package repository caches" "INFO" $logfile
        apt update -y 2>/dev/null | tee $tty >> $consolelog
        std_message "Package repository cache refresh complete" "OK" $logfile
        return 0

    elif [ "$operation" = "upgrade" ] && [ "$family" = "Ubuntu" ]; then
        std_message "Upgrading Operating System Packages" "INFO" $logfile
        apt upgrade -y 2>/dev/null  | tee $tty >> $consolelog
        std_message "Operating System update complete" "OK" $logfile
        return 0

    elif [ -z $operation ] || [ "$operation" = "install" ]; then
        for ospkg in "${pkg_list[@]}"; do

            # validate install
            if ospackage_installed 'deb' $ospkg; then
                std_message "Package $ospkg already installed" "OK" $logfile
            else
                std_message "Installing Debian package $ospkg" "INFO" $logfile
                apt install -y $ospkg  2>/dev/null | tee $tty >> $consolelog

                # verify install
                if ospackage_installed 'deb' $ospkg; then
                    std_message "Package $ospkg installed successfully" "OK" $logfile
                else
                    std_warn "Package $ospkg failed to install. Possibly missing from os package repository" $logfile
                fi
            fi

        done
        return 0
    fi

    return 1
    #
    # <<--- end function install_deb_packages --->>
}


function install_yum_packages(){
    ##
    ##  installs packages supplied as an array
    ##
    ##  Usage:
    ##
    ##      install_yum_packages  -o "install"  --packages pkgs[@]
    ##
    ##  Where:
    ##      --operation:  update | install
    ##      --packages:  associative array
    ##
    declare -a pkg_list
    local operation
    local pkgmanager
    local logfile=$LOG_FILE
    local consolelog=$LOG_CONSOLE
    local show_packages=$SHOW_OSPACKAGES

    if [ "$(which dnf 2>/dev/null)" ]; then
        pkgmanager=$(which dnf 2>/dev/null)

    elif [ "$(which yum 2>/dev/null)" ]; then
        pkgmanager=$(which yum 2>/dev/null)

    else
        std_warn "No package manager found. Abort os package install" $logfile
    fi

    while [ $# -gt 0 ]; do
        case $1 in
            -a | --alt_os)
                alternate_os="$2"
                shift 2
                ;;

            -l | --log)
                logfile="$2"
                shift 2
                ;;

            -o | --operation)
                case $2 in
                    update | install | groupinstall)
                        operation="$2"
                        shift 2
                        ;;
                    *)
                        std_error_exit "Operation must be either update, install, or groupinstall. Exit"
                    ;;
                esac
                ;;

            -p | --packages)
                pkg_list=("${!2}")
                shift 2
                ;;
        esac
    done

    ##  display ospackage status instead of updating   ##

    if [ $show_packages ] && [ $alternate_os ]; then
        ospackage_status "alternate" "$ospkg" "$logfile"

    elif [ $show_packages ]; then
        if [ -z $operation ] || [ "$operation" = "install" ] || [ "$operation" = "groupinstall" ]; then

            for ospkg in "${pkg_list[@]}"; do
                ospackage_status "rpm" "$ospkg" "$logfile"
            done

        fi
        return 0
    fi

    ##    perform actual update of system ospackages   ##

    if [ "$operation" = "update" ]; then
        std_message "Refreshing os package repository caches" "INFO" $logfile
        $pkgmanager -y update 2>/dev/null | tee $tty >> $consolelog
        std_message "Package repository cache refresh complete" "OK" $logfile
        return 0

    elif [ "$operation" = "groupinstall" ]; then
        for osgroup in "${pkg_list[@]}"; do
            std_message "Installing operating system package group" "INFO" $logfile
            $pkgmanager -y groupinstall $osgroup 2>/dev/null  | tee $tty >> $consolelog
        done
        std_message "Operating System Upgrade Complete" "OK" $logfile
        return 0

    elif [ -z $operation ] || [ "$operation" = "install" ]; then
        for ospkg in "${pkg_list[@]}"; do

            # validate install
            if ospackage_installed 'rpm' $ospkg; then
                std_message "Package $ospkg already installed - nothing to do" "INFO" $logfile
            else
                std_message "Installing package $ospkg" "INFO" $logfile
                $pkgmanager -y install $ospkg  2>/dev/null | tee $tty >> $consolelog

                # verify install
                if ospackage_installed 'rpm' $ospkg; then
                    std_message "Package $ospkg installed successfully" "OK" $logfile
                else
                    std_warn "Package $ospkg failed to install. Possibly missing from os package repository" $logfile
                fi
            fi

        done
        return 0
    fi

    return 1
    #
    # <<--- end function install_yum_packages --->>
}


function print_header(){
    ##
    ## print formatted report header ##
    ##
    local width="$1"
    local color="$2"
    #
    echo -e "${color}"
    printf '%*s' "$width" '' | tr ' ' -  | indent04
    echo -e "${btext}"
    printf -- '      %s   %s\t%s\t\t%s\t\t\t%s' "${BOLD}Status" '|' "Package" '|' "Description${UNBOLD}"
    echo -e "${color}"
    printf '%*s' "$width" '' | tr ' ' - | indent04
    echo -e "${btext}"
    #
    # <<--- end function print_header -->>
}


function os_packages(){
    ##
    ## install os prerequisites depending on os type ##
    ##
    local version
    declare -a packages

    version="$1"

    if [ $SHOW_OSPACKAGES ]; then
        if [ ! $family ]; then std_error_exit "Unknown operating" $E_DEPENDENCY; fi
        # only show which os packages to be installed
        print_header "100" "${a_wgray}"
    else
        std_message "Installing OS-specific packages for Python-$version..." "INFO" $LOG_FILE
    fi

    # AMAZON Linux, Version 1

        if [ "$family" = "AmazonLinux" ] && [ "$release" = "1" ]; then
            install_yum_packages --operation update

            packages=( "Development Libraries" "Development tools" )
            install_yum_packages --operation groupinstall --packages packages[@]

            packages=( "gcc" "make" "wget" "which" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "openssl-devel" "bzip2-devel" "expat-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses' 'ncurses-devel' 'ncurses-libs' 'ncurses-static')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses-libs' 'ncurses-term' 'uuid-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( "gdb" "gdbm-devel" "readline-devel" "sqlite-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'zlib' 'zlib-devel' 'zlib-static' 'libffi-devel')
            install_yum_packages --operation install --packages packages[@]
            return 0

    # AMAZON Linux, Version 2

        elif [ "$family" = "AmazonLinux" ] && [ "$release" = "2" ]; then
            install_yum_packages --operation update

            packages=( "Development tools" )
            install_yum_packages --operation groupinstall --packages packages[@]

            packages=( "gcc" "make" "wget" "which" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "gdb" "gdbm-devel" "readline-devel" "sqlite-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "openssl-devel" "bzip2-devel" "expat-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses' 'ncurses-devel' 'ncurses-libs' 'ncurses-static')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses-libs' 'ncurses-term' 'uuid-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'python-magic' 'libuuid-devel' 'tk-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'xz' 'xz-devel' 'xz-libs')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'zlib' 'zlib-devel' 'libffi-devel')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'lzma-sdk' 'lzma-sdk-devel' 'xz-devel' 'gdbm-devel' )
            install_yum_packages --operation install --packages packages[@]
            return 0

        elif [ $FALLBACK_DETECT ] && [ "$(grep -i amazon /etc/os-release  | head -n 1)" ]; then
            install_yum_packages --operation update

            packages=( "Development Libraries" "Development tools" )
            install_yum_packages --operation groupinstall --packages packages[@]

            packages=( "gcc" "make" "wget" "which" 'uuid-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( "openssl-devel" "bzip2-devel" "expat-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "gdb" "gdbm-devel" "readline-devel" "sqlite-devel" )
            install_yum_packages --operation install --packages packages[@]
            return 0

    # REDHAT Enterprise Linux 7

        elif [ "$family" = "Redhat" ] && { [ "$release" = "7.3" ] || [ "$release" = "7.4" ]; }; then
            install_yum_packages --operation update

            packages=( "Development tools" )
            install_yum_packages --operation groupinstall --packages packages[@]

            packages=( "gcc" "make" "wget" "which" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "gdb" "gdbm-devel" "readline-devel" "sqlite-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses' 'ncurses-devel' 'ncurses-libs' 'ncurses-static')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses-libs' 'ncurses-term' 'uuid-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( "openssl-devel" "bzip2-devel" "expat-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'xz' 'xz-devel' 'xz-libs' 'libuuid-devel' 'tk-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'zlib' 'zlib-devel' 'libffi-devel')
            install_yum_packages --operation install --packages packages[@]
            return 0

        elif [ "$family" = "Redhat" ] && { [ "$release" = "7.5" ] || [ "$release" = "7.6" ]; }; then
            install_yum_packages --operation update

            packages=( "Development tools" )
            install_yum_packages --operation groupinstall --packages packages[@]

            packages=( "gcc" "make" "wget" "which" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "gdb" "gdbm-devel" "readline-devel" "sqlite-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses' 'ncurses-devel' 'ncurses-libs' 'ncurses-static')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses-libs' 'ncurses-term' 'uuid-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( "openssl-devel" "bzip2-devel" "expat-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'xz' 'xz-devel' 'xz-libs' 'libuuid-devel' 'tk-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'zlib' 'zlib-devel' 'libffi-devel')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'lzma-sdk' 'lzma-sdk-devel' 'xz-devel' 'gdbm-devel' )
            install_yum_packages --operation install --packages packages[@]
            return 0

    # CentOS Linux (Redhat binary compatible)

        elif [ "$family" = "CentOS" ] && { [ "$release" = "7" ] || [ "$release" = "8" ]; }; then
            install_yum_packages --operation update

            packages=( "Development tools" )
            install_yum_packages --operation groupinstall --packages packages[@]

            packages=( "gcc" "make" "wget" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "openssl-devel" "bzip2-devel" "expat-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "gdb" "gdbm-devel" "readline-devel" "sqlite-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses' 'ncurses-devel' 'ncurses-libs' )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses-base' 'ncurses-term')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'xz' 'xz-devel' 'xz-libs')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'zlib' 'zlib-devel' 'libffi-devel')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'xz-devel' 'gdbm-devel' 'libuuid-devel' 'tk-devel' )
            install_yum_packages --operation install --packages packages[@]

            if [ "$release" = "7" ]; then
                packages=( 'ncurses-static' 'yum-utils' 'python36-pyOpenSSL' )
                install_yum_packages --operation install --packages packages[@]

                packages=( 'lzma-sdk' 'lzma-sdk-devel' 'uuid-devel' )
                install_yum_packages --operation install --packages packages[@]
            else
                packages=( 'python3-pyOpenSSL' 'libuuid-devel' )
                install_yum_packages --operation install --packages packages[@]
            fi
            return 0

    # Fedora Linux

        elif [ "$family" = "Fedora" ] && (( "$release" >= "26" )); then
            install_yum_packages --operation update

            packages=( "Development tools" )
            install_yum_packages --operation groupinstall --packages packages[@]

            packages=( "yum-utils" "gcc" "make" "wget" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "gdb" "gdbm-devel" "readline-devel" "sqlite-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( "openssl-devel" "bzip2-devel" "expat-devel" )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses' 'ncurses-devel' 'ncurses-libs' 'ncurses-static')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'ncurses-libs' 'ncurses-term' 'uuid-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'python3-magic' 'libuuid-devel' 'tk-devel' )
            install_yum_packages --operation install --packages packages[@]

            packages=( 'xz' 'xz-devel' 'xz-libs')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'zlib' 'zlib-devel' 'libffi-devel')
            install_yum_packages --operation install --packages packages[@]

            packages=( 'lzma-sdk' 'lzma-sdk-devel' 'xz-devel' 'gdbm-devel' )
            install_yum_packages --operation install --packages packages[@]
            return 0

    # UBUNTU, Ubuntu variants, Linux Mint

        elif { [ "$family" = "Ubuntu" ] && [ "$(echo "$release" | grep 14)" ]; } || \
             { [ "$family" = "Mint" ] && [ "$(echo "$release" | grep 17)" ]; }; then

            install_deb_packages --operation update
            install_deb_packages --operation upgrade

            packages=( "build-essential" "checkinstall" )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'gcc' 'make' 'wget' 'python3-openssl' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libreadline-dev' 'libreadline6-dev' 'libssl-dev' 'libffi-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libbz2-dev' 'libgdbm-dev' 'zlib1g-dev' 'uuid-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( "libncursesw5-dev" "tk-dev" "libc6-dev" "libbz2-dev" )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'sqlite3' 'sqlite3-doc' 'libsqlite3-dev' )
            install_deb_packages --operation install --packages packages[@]
            return 0

        elif { [ "$family" = "Ubuntu" ] && [ "$(echo "$release" | grep '16')" ]; } || \
             { [ "$family" = "Mint" ] && [ "$(echo "$release" | grep '18')" ]; }; then

            install_deb_packages --operation update
            install_deb_packages --operation upgrade

            packages=( 'build-essential' 'checkinstall' 'llvm')
            install_deb_packages --operation install --packages packages[@]

            packages=( 'gcc' 'make' 'wget' 'python3-openssl' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'zlib1g-dev' 'libssl-dev' 'libffi-dev')
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libbz2-dev' 'libgdbm-dev' 'libsqlite3-dev' 'libc6-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libncursesw5-dev' 'libncurses5-dev' 'lib64ncurses5' 'lib64ncurses5-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libreadline6' 'libreadline6-dev' 'libreadline-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'ncurses-term' 'ncurses-examples' 'ncurses-base' 'ncurses-doc')
            install_deb_packages --operation install --packages packages[@]

            packages=( 'readline-doc' 'gdb' 'gdb-doc' 'uuid-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'sqlite3' 'sqlite3-doc' 'libsqlite3-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'tk-dev' 'python3-tk' 'python3-magic' 'xz-utils' )
            install_deb_packages --operation install --packages packages[@]

            if [ $EXTRA_PACKAGES ]; then
                packages=(
                    binutils-doc gettext cpp-doc gcc-5-locales debian-keyring
                    g++-multilib g++-5-multilib gcc-5-doc libstdc++6-5-dbg gcc-multilib
                    autoconf automake libtool flex bison gcc-doc gcc-5-multilib
                    libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan2-dbg
                    liblsan0-dbg libtsan0-dbg libubsan0-dbg libcilkrts5-dbg
                    libmpx0-dbg libquadmath0-dbg glibc-doc libstdc++-5-doc make-doc
                )
                install_deb_packages --operation install --packages packages[@]
            fi
            return 0

        elif { [ "$family" = "Ubuntu" ] && [ "$(echo "$release" | grep '18')" ]; } || \
             { [ "$family" = "Mint" ] && [ "$(echo "$release" | grep '19')" ]; }; then

            install_deb_packages --operation update
            install_deb_packages --operation upgrade

            packages=( 'build-essential' 'checkinstall' 'llvm')
            install_deb_packages --operation install --packages packages[@]

            packages=( 'gcc' 'make' 'wget' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'zlib1g-dev' 'libreadline-dev' 'libssl-dev' 'libffi-dev')
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libbz2-dev' 'libgdbm-dev' 'libsqlite3-dev' 'libc6-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libreadline6' 'libreadline6-dev' 'libreadline-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libreadline7' 'python3-urwid' 'python3-btrees' 'python3-openssl' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'ncurses-term' 'ncurses-examples' 'ncurses-base' 'ncurses-doc')
            install_deb_packages --operation install --packages packages[@]

            packages=( 'readline-doc' 'gdb' 'gdb-doc' 'uuid-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'sqlite3' 'sqlite3-doc' 'libsqlite3-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'tk-dev' 'python3-tk' 'python3-magic' 'xz-utils' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'liblzma-dev' 'liblz-dev' 'lzma-dev' 'libqdbm-dev' )
            install_deb_packages --operation install --packages packages[@]

            if [ "$(echo "$release" | grep '18.10')" ]; then
                packages=( 'libncurses6' 'libncurses6-dbg' 'libncursesw6' 'libncursesw6-dbg' )
                install_deb_packages --operation install --packages packages[@]

            elif [ "$(echo "$release" | grep '18.04')" ]; then
                packages=( 'libncursesw5-dev' 'libncurses5-dev' )
                install_deb_packages --operation install --packages packages[@]
            fi
            return 0

        elif { [ "$family" = "Ubuntu" ] && [ "$(echo "$release" | grep '20')" ]; } || \
             { [ "$family" = "Mint" ] && [ "$(echo "$release" | grep '20')" ]; }; then

            install_deb_packages --operation update
            install_deb_packages --operation upgrade

            packages=( 'build-essential' 'checkinstall' 'llvm')
            install_deb_packages --operation install --packages packages[@]

            packages=( 'gcc' 'make' 'wget' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'zlib1g-dev' 'libreadline-dev' 'libssl-dev' 'libffi-dev')
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libbz2-dev' 'libgdbm-dev' 'libsqlite3-dev' 'libc6-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libreadline8' 'libreadline-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'python3-urwid' 'python3-btrees' 'python3-openssl' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'ncurses-term' 'ncurses-examples' 'ncurses-base' 'ncurses-doc')
            install_deb_packages --operation install --packages packages[@]

            packages=( 'readline-doc' 'gdb' 'gdb-doc' 'uuid-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'sqlite3' 'sqlite3-doc' 'libsqlite3-dev' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'tk-dev' 'python3-tk' 'python3-magic' 'xz-utils' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'libpython3.8' 'libpython3.8-dev' 'libpython3.8-minimal' 'libpython3.8-stdlib' )
            install_deb_packages --operation install --packages packages[@]

            packages=( 'liblzma-dev' 'libghc-lzma-dev' 'liblz-dev' 'lzma-dev' 'libqdbm-dev' )
            install_deb_packages --operation install --packages packages[@]

            if [ "$(echo "$release" | grep '18.10')" ]; then
                packages=( 'libncurses6' 'libncurses6-dbg' 'libncursesw6' 'libncursesw6-dbg' )
                install_deb_packages --operation install --packages packages[@]
            fi
            return 0

        elif [ "$(grep -i ubuntu /etc/os-release)" ] || [ "$(grep -i mint /etc/os-release)" ]; then

            std_error_exit "Ubuntu-based distribution of unknown family or release. Exit" "$E_OSERROR"

        fi

        std_error_exit "OS package dependencies failed to install correctly" "$E_DEPENDENCY"
        #
        # <-- end function os_packages -->
}


function package_info(){
    ##
    ##  displays information about this library module
    ##
    ##     - dependent module colors.sh is located always adjacent
    ##     - sourcing of dep modules must occur after local var to avoid overwrite
    ##       of variable values in this module
    ##
    local version=$__version__
    local act="$(echo -e ${a_orange})"
    local rst="$(echo -e ${reset})"
    local i path clearscn
    local tmpdir='/tmp'
    local logs
    local col1
    local total_index
    local l_index  r_index

    declare -a arr_modules              # array; all file modules comprising buildpy
    declare -a arr_functions            # array; all function names
    declare -a arr_left                 # array; left-hand side column function names
    declare -a arr_right                # array; right-hand side column function names

    clearscn=$(which clear 2>/dev/null)

    # bash module ecosystem stats
    modules='1'                         # start @ 1 bc of main binary
    logs='0'
    col1='25'                           # lhs column fixed width

    # add main executable to list of bash modules (.modules file)
    printf -- '\n%-12s%s %-37s%s\n' " " "-" "${cyan}$pkg${rst}" "$pkg_path" > $tmpdir/.modules

    for path in $pkg_lib/*; do

        i=${path##*/}

        if [ "${i#*.}" = "log" ] || [ "${i#*.}" = "pkg" ]; then
            continue

        else
            if [ "$(echo $i | wc -c)" -ge "15" ]; then


                width=$(( $col1 - $(echo $i | wc -c) ))

                # req keep columns vertically aligned
                printf -- '%-12s%s %-37s%-'$width's\n' " " "-" "${cyan}$i${rst}" "$pkg_lib" >> $tmpdir/.modules

            else
                printf -- '%-12s%s %-37s%-'$width's\n' " " "-" "${cyan}$i${rst}" "$pkg_lib" >> $tmpdir/.modules
            fi
            # incr modules
            (( modules ++ ))
        fi
    done

    # finally, add bash completion file artifact (last .modules file)
    printf -- '%-12s%s %-37s%s\n' " " "-" "${cyan}buildpy-completion.bash${rst}" "/etc/bash_completion.d" >> $tmpdir/.modules

    # log files,rm path in front
    printf -- '\n' > $tmpdir/.logs

    for i in ${LOG_FILE##*/} ${LOG_CONSOLE##*/}; do

        if [ "$(echo $i | wc -c)" -ge "15" ]; then
            # req keep columns vertically aligned
            printf -- '%-12s%s %-34s\t%s\n' ' ' "-" "${cyan}$i${rst}" "$LOG_DIR" >> $tmpdir/.logs
        else
            printf -- '%-12s%s %-37s%-16s\n' ' ' "-" "${cyan}$i${rst}" "$LOG_DIR" >> $tmpdir/.logs
        fi

        (( logs ++ ))

    done

    # split into 2 columns:
    arr_functions=( )
    for i in $(declare -F | awk '{print $3}'); do
        imod="- ${i}"
        arr_functions=( "${arr_functions[@]}" "${imod}" )
    done

    total_index=${#arr_functions[@]}

    # construct, display program info msg output
    cat <<EOM
    $(printf "%0.s_" {1..65})

        ${title}Build ${hic}Python3 ${title}from Source | ${hic}Linux${btext}

    $(printf -- '%-4s%-12s%-19s%s\n'  ' ' "Module Name:" ' '  "${cyan}$pkg${rst}")
    $(printf -- '%-4s%2s%-16s%s\n' ' ' "Module Version:" " "  "${act}$version${rst}")
    $(printf "%0.s_" {1..65})

    $(printf -- '%-4s%2s%-19s%s\n' ' ' "Bash Modules:" " "  "$modules")
    $(cat $tmpdir/.modules)

    $(printf -- '%-4s%2s%-22s%s\n' ' ' "Log Files:" " "  "$logs")
    $(cat $tmpdir/.logs)
    $(printf "%0.s_" {1..65})

    $(printf -- '\t%2s%-17s%s\n'  "Bash Functions:" " "  "$total_index")

EOM

    l_index=$(( $(( $total_index / 2 )) + 1 ))

    for j in "${arr_functions[@]:0:$l_index}"; do
        j_trunc=$(echo $j | cut -c 1-26)
        arr_left=( "${arr_left[@]}" "$j_trunc" )
    done

    r_index=$(( $l_index + 1 ))

    for k in "${arr_functions[@]:$r_index:$total_index}"; do
        arr_right=( "${arr_right[@]}" "$k" )
    done

    middle_index="$l_index"

    for m in $(seq 0 $middle_index); do

        # calc the width of left-hand column element
        if [[ "${arr_left[$m]}" ]] || [[ "${arr_right[$m]}" ]]; then

            width=$(echo "${arr_left[$m]}" | wc -c)

            # subtract lhs column width - width of element; difference is rhs column offset
            col2=$(( $col1 - $width ))
            printf -- '%-12s%-'$col1's\t%-'$col2's\n' ' ' "${arr_left[$m]}" "${arr_right[$m]}"
        else
            continue
        fi
    done
    printf -- '\n'
    # clean up
    rm $tmpdir/.functions $tmpdir/.modules $tmpdir/.logs 2>/dev/null
    #
    # <<-- end function package_info -->>
}


function post_install(){
    ##
    ## prints ending message and stats ##
    ##
    local endtime="$1"

    if [ ! $endtime ]; then

        START=$(timer)
        std_message "Python3 installation start: ${title}$(date +'%H:%M:%S')${reset}" "INFO" $LOG_FILE
        return 1

    else

        std_message "Python3 installation COMPLETE at $(date +'%H:%M'). Total runtime: ${title}$(timer $endtime)${reset}." "INFO" $LOG_FILE
        std_message "See the following for installation details:\n
        \t* Log file:  ${yellow}$LOG_FILE${reset}\n
        \t* Console Event Log: ${yellow}$LOG_CONSOLE${reset}" "INFO"

    fi

    return 0
    #
    # <-- end function print stats -->
}


function preinstall_validation(){
    ##
    ##  Ensures version to be installed does not already exist
    ##
    local major="$1"
    if which "python${major}" 2>&1 >/dev/null; then
        std_warn "Python $major is already active on this system. Aborting installation" $LOG_FILE
        exit 0
    fi
}


function root_permissions(){
    ##
    ## validates required root privileges ##
    ##
    if [ $EUID -ne 0 ]; then

        std_warn "This command requires root or sudo access to root privileges.\n
        \n\t\tRe-run as root or execute with sudo:
            \n\t\t\t$ sudo  $pkg  <parameters>"
        printf -- "\n\n"
        std_logger "Permission denied. Rerun as root or execute with sudo" "WARN" $LOG_FILE
        exit $E_DEPENDENCY

    elif [ $SUDO_USER ]; then

        SUDO=""
        logfile_owner $LOG_FILE $LOG_CONSOLE

    fi

    return 0

    #
    # <-- end function root_permissions -->
}


function root_permissions_bool(){
    ##
    ## validates required root privileges, return bool only ##
    ##

    if [ $EUID -eq 0 ] || [ $SUDO_USER ]; then

        return 0

    fi

    return 1

    #
    # <-- end function root_permissions -->
}


function source_profile(){
    ##
    ## used to import aliases and other environment elements at runtime ##
    ##

    # source paths
    if [[ -f "$HOME/.bashrc" ]]; then

        source "$HOME/.bashrc" 2>/dev/null

    elif [[ -f "$HOME/.bash_profile" ]]; then

        source "$HOME/.bash_profile"  2>/dev/null

    fi
    return 0
    #
    # <-- end function source_profile -->
}


function test_removal(){
    ##
    ## tests to ensure python binaries and deps removed ##
    ##
    local major="$1"
    local python_ver="python$major"
    #
    std_message "Testing removal of Python $major..." "TEST" $LOG_FILE

    # delay for user read
    sleep 1

    if [ "$($python_ver --version 2>/dev/null)" ]; then

        std_warn "Problem removing Python $major. Identifying remaining file locations:" $LOG_FILE
        updatedb -v
        std_message "$(locate $python_ver)" "INFO" $LOG_FILE

    else

        std_message "Python $major ${green}successfully${reset} removed." "INFO" $LOG_FILE

    fi
    #
    # <-- end function test_removal -->
}


function uninstall_disclaimer(){
    ##
    ##  Display disclaimer msg prior to initiating uninstall
    ##
    cat <<EOM
        ${red}______________________________________________________________________${reset}

                                    ${title} DISCLAIMER ${btext}
        ${red}______________________________________________________________________${reset}

              You are about to remove system-level python $major binaries
              and dependencies.

              Uninstalling a Python version you have installed using this
              program generally works fine. Please note that uninstalling
              a Python version included natively with your operating
              system is ${ul}not recommended${reset}.

              Depending on your Linux distribution, some operating system
              functions may depend upon the Python version(s) included
              natively with the OS.

              Although the uninstall functionality has been successfully
              tested on all supported major Linux distributions,


                              ${red}PROCEED AT YOUR OWN RISK${reset}


EOM
    return 0
    #
    # <--- end function uninstal_disclaimer --->
}


function uninstall(){
    ##
    ## uninstalls system-level python3 ##
    ##
    #
    #   This should ideally work via the following:
    #       1. find /usr -name "python3.X"
    #       2. delete paths that result
    #       3. Install and run mlocate after to find any paths left
    #
    #   Installing mlocate is OS-specific (see function mlocate_installation):
    #      REDHAT (for example, remove python3.6):
    #        sudo yum -y update
    #        # install epel
    #        curl -o epel.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    #        sudo yum -y install epel.rpm
    #        sudo yum -y update
    #        sudo yum -y install mlocate
    #        sudo updatedb
    #
    local major="$1"
    local wipe_clean="$2"
    local choice
    local python_str="Python-$major"
    local python_ver="python$major"
    declare -a arr_paths

    if ! binary_installed_bool "python$major"; then
        msg="You are attempting to uninstall $python_str, which is not installed. Exit"

        if [ $QUIET ]; then
            std_logger "$msg" "WARN" $LOG_FILE
        else
            std_error_exit "$msg" $E_DEPENDENCY
        fi
    fi

    # print disclaimer msg
    msg="Initiate uninstall of Python $major binaries"

    if [ $QUIET ]; then
        std_logger "$msg" "INFO" $LOG_FILE
    else
        uninstall_disclaimer
        # choose
        if continue_on; then
            std_logger "$msg" "INFO" $LOG_FILE
        else
            # abort uninstall
            printf -- '\n'; exit 0
        fi
    fi

    # create backup of pip packages
    pip_backup

    # display paths found
    std_message "Found paths to delete:" "INFO" $LOG_FILE "pprint"

    mapfile -t arr_paths < <(find /usr/local -name "$python_ver" 2>/dev/null)

    for path in "${arr_paths[@]}"; do
        std_message "\t$path" "INFO" $LOG_FILE "pprint"
    done

    std_message 'Please review the uninstall paths' 'INFO' $LOG_FILE
    if ! continue_on; then return 0; fi

    # remove python3 version ("python_ver")
    for path in "${arr_paths[@]}"; do
        std_message "Deleting $path" "INFO" $LOG_FILE
        rm -fr "$path"
        printf -- "\n"
    done

    # check specific paths noted in paths filemap
    for path in $(cat $pkg_lib/uninstall.paths); do
        std_message "Deleting $path" "INFO" $LOG_FILE
        rm -fr "${path:?}/$python_ver*"
        printf -- "\n"
    done

    # locate any residual locations only if WIPE CLEAN explicit option given
    if mlocate_installation && $wipe_clean; then

        for path in $(locate $python_ver); do
            std_message "Deleting path: ${yellow}$path${btext}" "INFO" $LOG_FILE
            rm -fr $path
            printf -- "\n"
        done

    fi

    return 0
    #
    # <-- end function uninstall -->
}


function update_path(){
    ##
    ## Update PATH environment variable:
    ##      - /usr/local/bin
    ##

    # ensure /usr/local/bin for python executables in PATH
    if [ ! "$(echo $PATH | grep '\/usr\/local\/bin')" ]; then

        if [ -f $HOME/.bashrc ]; then

            printf -- '%s\n' "PATH=\$PATH:/usr/local/bin" >> "$HOME/.bashrc"
            printf -- '%s\n' "export PATH" >> "$HOME/.bashrc"

        elif [ -f "$HOME/.bash_profile" ]; then

            printf -- '%s\n' "PATH=\$PATH:/usr/local/bin" >> "$HOME/.bash_profile"
            printf -- '%s\n' "export PATH" >> "$HOME/.bash_profile"

        elif [ -f "$HOME/.profile" ]; then

            printf -- '%s\n' "PATH=\$PATH:/usr/local/bin" >> "$HOME/.profile"
            printf -- '%s\n' "export PATH" >> "$HOME/.profile"

        fi

    fi

    # ensure local bin for python packages in PATH
    if [ ! "$(echo $PATH | grep $HOME\/.local\/bin)" ]; then

        if [ -f $HOME/.bashrc ]; then

            printf -- '%s\n' "PATH=\$PATH:$HOME/.local/bin" >> "$HOME/.bashrc"
            printf -- '%s\n' "export PATH" >> "$HOME/.bashrc"

        elif [ -f "$HOME/.bash_profile" ]; then

            printf -- '%s\n' "PATH=\$PATH:$HOME/.local/bin" >> "$HOME/.bash_profile"
            printf -- '%s\n' "export PATH" >> "$HOME/.bash_profile"

        elif [ -f "$HOME/.profile" ]; then

            printf -- '%s\n' "PATH=\$PATH:$HOME/.local/bin" >> "$HOME/.profile"
            printf -- '%s\n' "export PATH" >> "$HOME/.profile"

        fi

    fi

    source_profile
    return 0
    #
    # <-- end function update_path -->
}


function upgrade_pip(){
    ##
    ##  - upgrades pip3, setuptools
    ##  - sets symlink to pip for python3
    ##  - multiple update cycles to accommodate pip 9.x > pip 10.x > pip 18.x
    ##
    local pip_bin
    local count="0"
    #
    pip_bin="$(which pip3 2>/dev/null)"
    source_profile

    # set path to pip binary
    if [ "$pip_bin" ]; then

        std_message "Found pip3 at path: ${yellow}$pip_bin${reset}" "INFO" $LOG_FILE

    else
        if [ -f "/usr/local/bin/pip3" ]; then
            pip_bin="/usr/local/bin/pip3"

        elif [ "$(which pip 2>/dev/null)" ]; then
            pip_bin="$(which pip)"

        elif [ -f "/usr/local/bin/pip" ]; then
            pip_bin="/usr/local/bin/pip"

        else
            std_warn "Skipping pip / pip3 upgrade -- pip executable could not be found" $LOG_FILE
            return 1
        fi
    fi

    #
    if [ $SUDO_USER ]; then
        # user using sudo
        while (( $count <= 2 )); do

            # check for latest version, pip
            if [ "$($pip_bin list --outdated  2>/dev/null | grep pip)" ]; then

                std_message "Upgrade pip3 to latest, set python3 symlink for pip" "INFO" $LOG_FILE
                $pip_bin install -U pip  >> $LOG_CONSOLE 2>/dev/null &
                progress_dots --text "Upgrading pip3"

            else
                std_message "pip $($pip_bin --version 2>/dev/null | awk '{print $2}') installed - latest" "INFO" $LOG_FILE
                break
            fi

            # check for latest version, setuptools
            if [ "$($pip_bin list --outdated 2>/dev/null | grep setuptools)" ]; then

                std_message "Upgrade setuptools to latest" "INFO" $LOG_FILE
                $pip_bin install -U setuptools >> $LOG_CONSOLE  2>/dev/null &
                progress_dots --text "upgrading setuptools"

            else
                std_message "setuptools $($pip_bin list | grep setuptools | awk '{print $2}') installed - latest" "INFO" $LOG_FILE
            fi

            count=$(( $count + 1 ))

        done

    elif [ "$pip_bin" ]; then
        # root user
        while (( $count <= 2 )); do

            # check for latest version, pip
            if [ "$($pip_bin list --outdated  2>/dev/null | grep pip)" ]; then

                std_message "Upgrade pip3 to latest, set python3 symlink for pip" "INFO" $LOG_FILE
                $pip_bin install -U pip  >> $LOG_CONSOLE 2>/dev/null &
                progress_dots --text "Upgrading pip3"

            else
                std_message "pip $($pip_bin --version 2>/dev/null | awk '{print $2}') installed - latest" "INFO" $LOG_FILE
                break
            fi

            # check for latest version, setuptools
            if [ "$($pip_bin list --outdated 2>/dev/null | grep setuptools)" ]; then

                std_message "Upgrade setuptools to latest" "INFO" $LOG_FILE
                $pip_bin install -U setuptools >> $LOG_CONSOLE  2>/dev/null &
                progress_dots --text "upgrading setuptools"

            else
                std_message "setuptools $($pip_bin list | grep setuptools | awk '{print $2}') installed - latest" "INFO" $LOG_FILE
            fi

            count=$(( $count + 1 ))

        done

    else

        std_warn "Unable to upgrade pip3, setuptools. Please use manual upgrade" $LOG_FILE

    fi
    #
    # <-- end function upgrade_pip -->
}


function verify_rc_status(){
    ##
    ##  Reset VERSION global variable in case binary set to be
    ##  compiled is a release candidate, alpha, or beta code.
    ##
    local version="$1"
    if [[ $(ls $TMPDIR | grep "Python-$version.tar.xz") ]]; then
        return 0

    elif [[ $(ls $TMPDIR | grep "Python-${version}rc[1-4].tar.xz") ]]; then
        bin=$(ls $TMPDIR | grep "Python-${version}rc[1-4].tar.xz")
        std_warn "Release Candidate binary ($bin). Stable version NOT available" $LOG_FILE
        if continue_on "Do you want to install anyway?"; then
            return 0
        else
            exit 0
        fi

    elif [[ $(ls $TMPDIR | grep "Python-$version[a-b][1-4].tar.xz") ]]; then
        bin=$(ls $TMPDIR | grep "Python-${version}[a-b][1-4].tar.xz")
        std_warn "Beta or Alpha code binary ($bin)." $LOG_FILE
        if continue_on "Do you want to install anyway?"; then
            return 0
        else
            exit 0
        fi
    fi
}


function clean_up(){
    ##
    ## remove build artificts ##
    ##
    local version="$1"                  # full version (X.Y.Z) to remove
    local yl=$(echo -e ${yellow})       # yellow link color code
    local clean_status="0"              # return code container
    local tmpdir='/tmp'
    declare -a arr_clean

    ##   remove build artifacts for a specific version   ##
    if [ $version ]; then

        mapfile -t arr_clean < <(find /tmp -name "Python-${version}*" 2>/dev/null)

        std_message "Begin build artifict removal for Python $version only..." "INFO" $LOG_FILE
        for residual in "${arr_clean[@]}"; do

            for artifact in $residual; do
                # .tar, .tar.xz, or dir object
                rm -fr $artifact

                # success | failure status
                if [ -e "$artifact" ] || [ -f "$artifact" ] || [ -d "$artifact" ] ; then
                    std_warn "Unable to remove ${yl}${artifact}${reset}" $LOG_FILE
                    clean_status="1"        # something went wrong
                else
                    std_message "Successfully removed ${yl}${artifact}${reset}" "OK" $LOG_FILE
                fi
            done
        done
        return $clean_status

    else
        ##   remove build artifacts for all versions   ##
        find $tmpdir -name "Python-*" 2>/dev/null > $tmpdir/.s.results

        for i in $(cat $tmpdir/.s.results); do
            arr_clean=(  "${arr_clean[@]}" $i  )
        done

        if [ -z "${arr_clean[*]}" ]; then
            # nothing to clean
            std_warn "No build artifacts found to remove" $LOG_FILE
            return $clean_status
        fi

        std_message "Locating all local build artificts..." "INFO" $LOG_FILE

        for residual in "${arr_clean[@]}"; do

            rm -fr $residual

            # success | failure status
            if [ -e "$residual" ] || [ -f "$residual" ] || [ -d "$residual" ] ; then
                std_warn "Unable to remove ${yl}${residual}${reset}" $LOG_FILE
                clean_status="1"        # something went wrong
            else
                std_message "Successfully removed ${yl}${residual}${reset}" "OK" $LOG_FILE
            fi

        done
        return $clean_status
    fi
    #
    # <-- end function clean_up -->
}


function show_downloads_display(){
    ##
    ##
    ##
    local downloads location major size i
    local tmpfile sorted_tmp twidth

    # set tempfile location
    if [ -d '/dev/shm' ]; then
        tmpfile='/dev/shm/downloads.tmp'
        sorted_tmp='/dev/shm/sorted.tmp'
    else
        tmpfile='/tmp/downloads.tmp'
        sorted_tmp='/tmp/sorted.tmp'
    fi

    # find and sort downloaded python binaries on local filesystem
    downloads=$(_current_downloads)

    if [ ! "$downloads" ]; then
        std_message "No current downloads" "INFO" $LOG_FILE

    else
        twidth='52'
        printf -- '\n\t   %s   %s  %s   %s   %s\n' "Version" '|' "Size" '|' "Filesystem Location"
        printf '\t%*s\n' "$twidth" '' | tr ' ' -

        for i in $downloads; do
            location=$(find /tmp -name "*${i}*" 2>/dev/null)
            major=${i#Python-}
            rsize=$(ls -l --block-size=MB $location | awk '{print $5}')
            size="${rsize%MB} MB"
            printf -- '\t%s %s %s %s %s\n' "${BOLD}Python-${btext}${bbc}$major${btext}" '|' "$size" '|' "$location" >> $tmpfile
        done

        # sort
        cat $tmpfile | sort -t . > $sorted_tmp
        awk  '{ printf "%-8s %-41s %-2s %2s %-3s %-2s %-30s \n", " ", $1, $2, $3, $4, $5, $6}' $sorted_tmp
        printf -- '\n'
        rm  $tmpfile $sorted_tmp || true
    fi
}


function major_version_reconcilliation(){
    ##
    ##  Test if MAJOR_VERSION includes minor version
    ##  If yes, reset MAJOR_VERSION parameter
    ##
    local parameter_version="$1"
    local major

    if [ "$(echo ${#parameter_version})" -ge 4 ]; then
        major=$(echo "$parameter_version" | awk -F '.' '{print $1"."$2}')
        echo "$major"
    else
        echo "$parameter_version"
    fi
    #
    # <-- end function major_version_reconcilliation -->
}

function version_reconcilliation(){
    ##
    ##  Test if VERSION includes minor version
    ##  If yes, reset VERSION parameter
    ##
    local parameter_version="$1"
    local major

    if [ "$(echo ${#parameter_version})" -ge 4 ]; then
        echo "$parameter_version"
    else
        echo ""
    fi
    #
    # <-- end function version_reconcilliation -->
}


# --- main --------------------------------------------------------------------


depcheck "$LOG_DIR" "$LOG_FILE" "$LOG_CONSOLE"

parse_parameters "$@"


if [ $PKG_INFO ]; then

    # display information about this program
    package_info | more

elif [ $DOWNLOAD_ONLY ]; then

    # discover latest python version available
    if latest_version_available $MAJOR_VERSION; then
        # download build assets
        download_binary $VERSION $MAJOR_VERSION
    fi

elif [ "$OPERATION" = "SHOW" ]; then

    case "$SUBCMD" in
        'downloads')
            show_downloads_display
            exit 0
            ;;

        'os-packages')
            os_type
            SHOW_OSPACKAGES="true"
            if [ $alternate_os ]; then
                # match current os to $alternate_os; if match, os_packages '3' & set alternative os=""
                # if different os, set family and release values
                case ${alternate_os#[0-9]*} in
                    $family)
                        release=$(determine_osrelease $alternate_os)
                        alternate_os=""
                        ;;
                    *)
                        release=$(determine_osrelease $alternate_os)
                        ;;
                esac
            fi
            os_packages '3'
            printf -- '\n'
            ;;

        'Python-'[0-9].[0-9] | 'Python-'[0-9].[0-9][0-9] | [0-9].[0-9] | [0-9].[0-9][0-9])
            # convert to major version number
            var="$2"; parameter=${var#Python-}
            if is_float "$parameter"; then
                MAJOR_VERSION="$parameter"
            else
                std_error_exit "You must provide Python major version (Example: 3.7)"
            fi
            # discover latest python version available
            latest_version_available $MAJOR_VERSION
            ;;
    esac

elif [ $OS_DETECT ]; then

    # detect operating system type (test)
    os_type

elif [ "$INSTALL_OS_DEPS" ]; then
    function install_status_display(){
        # show installed status of packages
        SHOW_OSPACKAGES="true"
        os_packages
        printf -- '\n\n'
    }

    clear
    # determine os type
    os_type
    install_status_display
    read -p "    Install all Python 3 operating system package dependencies?  [quit]:  " choice
    case $choice in
        "" | "No" | "no" | "q" | "quit" | "Q*")
            printf -- '\n\n'
            exit 0
            ;;
        "y" | "Y" | "yes" | "Yes")
            # ensure root-level permissions
            root_permissions
            # install python os dependencies
            SHOW_OSPACKAGES=""      # turn off display-only variable to install
            os_packages
            ;;
        *)
            printf -- '\n\n'
            exit 0
            ;;
    esac
    printf -- '\n\t\t\t%s\n' 'FINAL OS PACKAGE INSTALLATION STATUS'
    install_status_display

elif [ $PIP_BACKUP ]; then

    # update pip / pip3 and core deps to latest if root
    if root_permissions_bool; then
        upgrade_pip
    fi

    # create backup of pip packages
    pip_backup

elif [ $CLEAN_UP ]; then

    root_permissions

    if [ "$CLEAN_VERSION" = 'ALL' ]; then
        clean_up

    # discover latest python version available
    elif latest_version_available $MAJOR_VERSION; then
        # rm build artifacts
        clean_up $VERSION
    fi

elif [ $INSTALL ]; then

    # start timer
    START=$(timer)

    # validate root privileges
    root_permissions

    # reset version vars if MAJOR_VERION contains full version (major.minor)
    VERSION=$(version_reconcilliation $MAJOR_VERSION)
    MAJOR_VERSION=$(major_version_reconcilliation $MAJOR_VERSION)

    # ensure requested python binaries not currently installed
    preinstall_validation $MAJOR_VERSION

    # discover latest python version available
    latest_version_available $MAJOR_VERSION || exit $E_DEPENDENCY

    std_message "Major Python version marked for installation:  ${pv_blue}$MAJOR_VERSION${reset}\n \
         Full Python version marked for installation: ${bbw}$VERSION${reset}" "INFO"
    sleep 3

    # create backup of pip packages
    pip_backup

    # determine os type
    os_type

    # download build assets
    download_binary $VERSION $MAJOR_VERSION
    verify_rc_status $VERSION

    # install required operating system packages
    os_packages $VERSION

    ## --- unpack --- ##
    cd $TMPDIR || exit $E_OSERROR
    std_message "Decompressing binary... " "INFO"  $LOG_FILE
    VERSION="$(reset_rc_version $VERSION)"
    unxz Python-$VERSION.tar.xz  2>&1 | tee $tty >> $LOG_CONSOLE
    tar -xvf Python-$VERSION.tar  2>&1 | tee $tty >> $LOG_CONSOLE

    ## --- pre-build --- ##
    std_message "Begin Python-$VERSION Configure Stage..." "INFO" $LOG_FILE
    cd $TMPDIR/Python-$VERSION || exit $E_OSERROR
    configure_python "$OPTIMIZATIONS" "$QUIET"
    std_message "End Python-$VERSION Configure Stage..." "OK" $LOG_FILE

    ## --- build, install --- ##
    std_message "Begin Python-$VERSION compile with make..." "INFO" $LOG_FILE
    make -j $PARALLEL_JOB_CT   2>&1  |  tee $tty >> $LOG_CONSOLE
    std_message "Compilation stage completed successfully" "OK" $LOG_FILE
    std_message "Begin Python-$VERSION install..." "INFO" $LOG_FILE
    make install  2>&1 | tee $tty >> $LOG_CONSOLE
    std_message "Python-$VERSION Installation completed successfully..." "OK" $LOG_FILE

    # clean up build artificts
    std_message "Clean up -- remove build artifacts" "INFO" $LOG_FILE
    clean_up

    # If missing, create python3 global symlink
    create_global_symlink "$MAJOR_VERSION" "$LOG_FILE"

    # update pip3, install symlink to pip fro python3
    upgrade_pip

    # print ending runtime stats
    post_install $START

elif [ $UNINSTALL ]; then

    # uninstall Python 3.X Version
    if [ ! $UNINSTALL_VERSION ]; then

        std_error_exit "You must provide a Python3 major version number to remove\n
        \tExample: \n\n\t\t    $ sudo ./buildpy --uninstall ${title}3.6${reset}"

    else

        # validate root privileges
        root_permissions

        # uninstall binaries
        uninstall "$UNINSTALL_VERSION" "$WIPE_CLEAN"

        # validate removal
        test_removal "$UNINSTALL_VERSION"

    fi

elif [ $DISPLAY_VERSION ]; then

    display_program_version

fi


# <-- end -->
exit 0

Back to buildpy


Back to buildpy Source Code