-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap.py
executable file
·157 lines (123 loc) · 5.1 KB
/
bootstrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/local/bin/python3
# ===================================== #
# NAME : bootstrap.py
# LANGUAGE : Python
# VERSION : 3
# AUTHOR : Bryan Dady
# UPDATED : 11/4/2019 - Enhance IsVerbose behavior with print_var function
# INTRO : To be loaded / dot-sourced from a python profile script, to establish (bootstrap) baseline consistent environment variables,
# regardless of version, or operating system
# ===================================== #
import argparse
import os
import platform
import sys
import sysconfig
import time
from pathlib import Path
# setup global variables to export
global HOME
global COMPUTERNAME
global hostOS
global hostOSCaption
global IsWindows
global IsLinux
global IsMacOS
IsVerbose = False # True
SleepTime = 3
# MyScriptInfo
MyCommandPath = sys.argv[0]
MyCommandName = Path(MyCommandPath).name # requires 'from pathlib import Path'
# Only print Start and Stop Header/Footer when called directly, so as to not confuse use of argv[0]
if MyCommandName == 'bootstrap.py':
print('\n Start {}: {}'.format(MyCommandName, time.strftime('%Y %m %d %H:%M:%S %Z', time.localtime())))
# http://www.effbot.org/librarybook/os.htm : where are we?
# pwd = os.getcwd()
#print('')
#print('PWD is: ', pwd)
# Region HostOS
# Setup common variables for the shell/host environment
IsWindows = False
IsLinux = False
IsMacOS = False
#IsAdmin = False
#IsServer = False
# -- print_var prints a label and a variable's value, if IsVerbose
def print_var(label, varname):
if IsVerbose:
print('< {} = \'{}\' >'.format(label, varname))
# -- RFE!: create a similar function for dev/test, which takes 1 arg of a dictionary, and prints the keys and values
# add blank line, only when IsVerbose
if IsVerbose: print('')
# Setup OS and version variables
COMPUTERNAME=platform.node()
hostOS = platform.system()
print_var('Platform: hostOS', hostOS)
hostOSCaption = platform.platform(aliased=1, terse=1)
print_var('Platform: hostOSCaption', hostOSCaption)
if sys.platform == "win32":
# hostOS = 'Windows'
IsWindows = True
#if hostOSCaption -like '*Windows Server*':
# IsServer = True
#if 'HOME' in os.environ:
HOME = os.environ['USERPROFILE']
# else:
# print('HOME does not exist')
# # derive it from sysconfig 'userbase' with help from os path dirname
# HOME = os.path.abspath(os.path.dirname(sysconfig.get_config_var('userbase')))
# Check admin rights / role; same approach as Test-LocalAdmin function in Sperry module
#IsAdmin = (([security.principal.windowsprincipal] [security.principal.windowsidentity]::GetCurrent()).isinrole([Security.Principal.WindowsBuiltInRole] 'Administrator'))
elif sys.platform == "mac" or sys.platform == "macos" or sys.platform == "darwin":
IsMacOS = True
hostOS = 'macOS'
#if platform.mac_ver()[0].__len__():
# Get the macOS major and minor version numbers (first 5 characters of first item in mac_ver dictionary)
macOS_ver = platform.mac_ver()[0][0:5]
# https://en.m.wikipedia.org/wiki/List_of_Apple_operating_systems#macOS
macOS_names = dict({'10.15': 'Catalina', '10.14': 'Mojave', '10.13': "High Sierra", '10.12': 'Sierra', '10.11': 'El Capitan', '10.10': 'Yosemite'})
hostOSCaption = 'Mac OS X {} {}'.format(macOS_ver, macOS_names[macOS_ver])
HOME = os.environ['HOME']
# Check root or sudo
#IsAdmin =~ ?
else:
IsLinux = True
#hostOS = 'Linux'
hostOSCaption = '{} {}'.format(platform.linux_distribution()[0], platform.linux_distribution()[1])
HOME = os.environ['HOME']
# Check root or sudo
#IsAdmin =~ ?
print_var('HOME', HOME)
# if we ever need to confirm that the path is available on the filesystem, use: path.exists(HOME)
py_version =sysconfig.get_config_var('py_version')
print(' # Python {} on {} - {} #'.format(py_version[0:3], hostOSCaption, COMPUTERNAME))
# Save what we've determined here in shell/system environment variables, so they can be easily referenced from other py scripts/functions
# #
# Verbose:
# print('\n Here are the persistent variables to import into the next script: ... ')
# print('from bootstrap import HOME')
# print('from bootstrap import COMPUTERNAME')
# print('from bootstrap import hostOS')
# print('from bootstrap import hostOSCaption')
# print('from bootstrap import IsWindows')
# print('from bootstrap import IsLinux')
# print('from bootstrap import IsMacOS')
#print('\n # # Python Environment Bootstrap Complete #\n')
# #
print_var('global variables','HOME, COMPUTERNAME, hostOS, hostOSCaption, IsWindows, IsLinux, IsMacOS')
"""
# Get the current locals().items() into a variable, as otherwise it changes during the subsequent for loop
varList = dict(locals())
# but remove varList as a key from itself
#del varList['varList']
print('List of final variables (locals()):')
for k, v in varList.items():
print(f'{k}={v}')
"""
if MyCommandName == 'bootstrap.py':
print(' End: {}\n'.format(time.strftime('%Y %m %d %H:%M:%S %Z', time.localtime())))
# When IsVerbose, pausing between profile/bootstrap scripts to aid in visual testing
if IsVerbose:
print('(pause ...)')
time.sleep( SleepTime )
#sys.exit(0)