-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathmalcolm_common.py
814 lines (665 loc) · 29 KB
/
malcolm_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2023 Battelle Energy Alliance, LLC. All rights reserved.
import contextlib
import getpass
import importlib
import json
import os
import platform
import re
import string
import sys
import time
from enum import IntFlag, auto
try:
from pwd import getpwuid
except ImportError:
getpwuid = None
from subprocess import PIPE, STDOUT, Popen, CalledProcessError
from collections import defaultdict, namedtuple
try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable
try:
from dialog import Dialog
MainDialog = Dialog(dialog='dialog', autowidgetsize=True)
except ImportError:
Dialog = None
MainDialog = None
###################################################################################################
ScriptPath = os.path.dirname(os.path.realpath(__file__))
MalcolmPath = os.path.abspath(os.path.join(ScriptPath, os.pardir))
MalcolmTmpPath = os.path.join(MalcolmPath, '.tmp')
###################################################################################################
PLATFORM_WINDOWS = "Windows"
PLATFORM_MAC = "Darwin"
PLATFORM_LINUX = "Linux"
PLATFORM_LINUX_CENTOS = 'centos'
PLATFORM_LINUX_DEBIAN = 'debian'
PLATFORM_LINUX_FEDORA = 'fedora'
PLATFORM_LINUX_UBUNTU = 'ubuntu'
class UserInputDefaultsBehavior(IntFlag):
DefaultsPrompt = auto()
DefaultsAccept = auto()
DefaultsNonInteractive = auto()
class UserInterfaceMode(IntFlag):
InteractionDialog = auto()
InteractionInput = auto()
BoundPath = namedtuple(
"BoundPath",
["service", "container_dir", "files", "relative_dirs", "clean_empty_dirs"],
rename=False,
)
# URLS for figuring things out if something goes wrong
DOCKER_INSTALL_URLS = defaultdict(lambda: 'https://docs.docker.com/install/')
DOCKER_INSTALL_URLS[PLATFORM_WINDOWS] = [
'https://stefanscherer.github.io/how-to-install-docker-the-chocolatey-way/',
'https://docs.docker.com/docker-for-windows/install/',
]
DOCKER_INSTALL_URLS[PLATFORM_LINUX_UBUNTU] = 'https://docs.docker.com/install/linux/docker-ce/ubuntu/'
DOCKER_INSTALL_URLS[PLATFORM_LINUX_DEBIAN] = 'https://docs.docker.com/install/linux/docker-ce/debian/'
DOCKER_INSTALL_URLS[PLATFORM_LINUX_CENTOS] = 'https://docs.docker.com/install/linux/docker-ce/centos/'
DOCKER_INSTALL_URLS[PLATFORM_LINUX_FEDORA] = 'https://docs.docker.com/install/linux/docker-ce/fedora/'
DOCKER_INSTALL_URLS[PLATFORM_MAC] = [
'https://www.code2bits.com/how-to-install-docker-on-macos-using-homebrew/',
'https://docs.docker.com/docker-for-mac/install/',
]
DOCKER_COMPOSE_INSTALL_URLS = defaultdict(lambda: 'https://docs.docker.com/compose/install/')
HOMEBREW_INSTALL_URLS = defaultdict(lambda: 'https://brew.sh/')
###################################################################################################
# chdir to directory as context manager, returning automatically
@contextlib.contextmanager
def pushd(directory):
prevDir = os.getcwd()
os.chdir(directory)
try:
yield
finally:
os.chdir(prevDir)
###################################################################################################
# print to stderr
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
###################################################################################################
def EscapeAnsi(line):
ansiEscape = re.compile(r'(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]')
return ansiEscape.sub('', line)
###################################################################################################
def EscapeForCurl(s):
return s.translate(
str.maketrans(
{
'"': r'\"',
"\\": r"\\",
"\t": r"\t",
"\n": r"\n",
"\r": r"\r",
"\v": r"\v",
}
)
)
###################################################################################################
def custom_make_translation(text, translation):
regex = re.compile('|'.join(map(re.escape, translation)))
return regex.sub(lambda match: translation[match.group(0)], text)
##################################################################################################
def UnescapeForCurl(s):
return custom_make_translation(
s,
{
r'\"': '"',
r"\t": "\t",
r"\n": "\n",
r"\r": "\r",
r"\v": "\v",
r"\\": "\\",
},
)
###################################################################################################
# if the object is an iterable, return it, otherwise return a tuple with it as a single element.
# useful if you want to user either a scalar or an array in a loop, etc.
def GetIterable(x):
if isinstance(x, Iterable) and not isinstance(x, str):
return x
else:
return (x,)
##################################################################################################
def ReplaceBindMountLocation(line, location, linePrefix):
if os.path.isdir(location):
volumeParts = line.strip().lstrip('-').lstrip().split(':')
volumeParts[0] = location
return "{}- {}".format(linePrefix, ':'.join(volumeParts))
else:
return line
##################################################################################################
def LocalPathForContainerBindMount(service, dockerComposeContents, containerPath, localBasePath=None):
localPath = None
if service and dockerComposeContents and containerPath:
vols = DeepGet(dockerComposeContents, ['services', service, 'volumes'])
if (vols is not None) and (len(vols) > 0):
for vol in vols:
volSplit = vol.split(':')
if (len(volSplit) >= 2) and (volSplit[1] == containerPath):
if localBasePath and not os.path.isabs(volSplit[0]):
localPath = os.path.realpath(os.path.join(localBasePath, volSplit[0]))
else:
localPath = volSplit[0]
break
return localPath
###################################################################################################
def same_file_or_dir(path1, path2):
try:
return os.path.samefile(path1, path2)
except Exception:
return False
###################################################################################################
# parse a curl-formatted config file, with special handling for user:password and URL
# see https://everything.curl.dev/cmdline/configfile
# e.g.:
#
# given .opensearch.primary.curlrc containing:
# -
# user: "sikari:changethis"
# insecure
# -
#
# ParseCurlFile('.opensearch.primary.curlrc') returns:
# {
# 'user': 'sikari',
# 'password': 'changethis',
# 'insecure': ''
# }
def ParseCurlFile(curlCfgFileName):
result = defaultdict(lambda: None)
if os.path.isfile(curlCfgFileName):
itemRegEx = re.compile(r'^([^\s:=]+)((\s*[:=]?\s*)(.*))?$')
with open(curlCfgFileName, 'r') as f:
allLines = [x.strip().lstrip('-') for x in f.readlines() if not x.startswith('#')]
for line in allLines:
found = itemRegEx.match(line)
if found is not None:
key = found.group(1)
value = UnescapeForCurl(found.group(4).lstrip('"').rstrip('"'))
if (key == 'user') and (':' in value):
splitVal = value.split(':', 1)
result[key] = splitVal[0]
if len(splitVal) > 1:
result['password'] = splitVal[1]
else:
result[key] = value
return result
###################################################################################################
def contains_whitespace(s):
return True in [c in s for c in string.whitespace]
###################################################################################################
# attempt to clear the screen
def ClearScreen():
try:
os.system("clear" if platform.system() != PLATFORM_WINDOWS else "cls")
except Exception:
pass
###################################################################################################
# get interactive user response to Y/N question
def YesOrNo(
question,
default=None,
defaultBehavior=UserInputDefaultsBehavior.DefaultsPrompt,
uiMode=UserInterfaceMode.InteractionDialog | UserInterfaceMode.InteractionInput,
clearScreen=False,
):
if (default is not None) and (
(defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept)
and (defaultBehavior & UserInputDefaultsBehavior.DefaultsNonInteractive)
):
reply = ""
elif (uiMode & UserInterfaceMode.InteractionDialog) and (MainDialog is not None):
defaultYes = (default is not None) and str2bool(default)
reply = MainDialog.yesno(
question, yes_label='Yes' if defaultYes else 'No', no_label='no' if defaultYes else 'yes'
)
if defaultYes:
reply = 'y' if (reply == Dialog.OK) else 'n'
else:
reply = 'n' if (reply == Dialog.OK) else 'y'
elif uiMode & UserInterfaceMode.InteractionInput:
if (default is not None) and defaultBehavior & UserInputDefaultsBehavior.DefaultsPrompt:
if str2bool(default):
questionStr = f"\n{question} (Y/n): "
else:
questionStr = f"\n{question} (y/N): "
else:
questionStr = f"\n{question}: "
while True:
reply = str(input(questionStr)).lower().strip()
if len(reply) > 0:
try:
str2bool(reply)
break
except ValueError:
pass
elif (defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept) and (default is not None):
break
else:
raise RuntimeError("No user interfaces available")
if (len(reply) == 0) and (defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept):
reply = "y" if (default is not None) and str2bool(default) else "n"
if clearScreen is True:
ClearScreen()
try:
return str2bool(reply)
except ValueError:
return YesOrNo(
question,
default=default,
uiMode=uiMode,
defaultBehavior=defaultBehavior - UserInputDefaultsBehavior.DefaultsAccept,
clearScreen=clearScreen,
)
###################################################################################################
# get interactive user response
def AskForString(
question,
default=None,
defaultBehavior=UserInputDefaultsBehavior.DefaultsPrompt,
uiMode=UserInterfaceMode.InteractionDialog | UserInterfaceMode.InteractionInput,
clearScreen=False,
):
if (default is not None) and (
(defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept)
and (defaultBehavior & UserInputDefaultsBehavior.DefaultsNonInteractive)
):
reply = default
elif (uiMode & UserInterfaceMode.InteractionDialog) and (MainDialog is not None):
code, reply = MainDialog.inputbox(
question,
init=default
if (default is not None) and (defaultBehavior & UserInputDefaultsBehavior.DefaultsPrompt)
else "",
)
if (code == Dialog.CANCEL) or (code == Dialog.ESC):
raise RuntimeError("Operation cancelled")
else:
reply = reply.strip()
elif uiMode & UserInterfaceMode.InteractionInput:
reply = str(
input(
f"\n{question}{f' ({default})' if (default is not None) and (defaultBehavior & UserInputDefaultsBehavior.DefaultsPrompt) else ''}: "
)
).strip()
if (len(reply) == 0) and (default is not None) and (defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept):
reply = default
else:
raise RuntimeError("No user interfaces available")
if clearScreen is True:
ClearScreen()
return reply
###################################################################################################
# get interactive password (without echoing)
def AskForPassword(
prompt,
uiMode=UserInterfaceMode.InteractionDialog | UserInterfaceMode.InteractionInput,
clearScreen=False,
):
if (uiMode & UserInterfaceMode.InteractionDialog) and (MainDialog is not None):
code, reply = MainDialog.passwordbox(prompt, insecure=True)
if (code == Dialog.CANCEL) or (code == Dialog.ESC):
raise RuntimeError("Operation cancelled")
elif uiMode & UserInterfaceMode.InteractionInput:
reply = getpass.getpass(prompt=f"{prompt}: ")
else:
raise RuntimeError("No user interfaces available")
if clearScreen is True:
ClearScreen()
return reply
###################################################################################################
# Choose one of many.
# choices - an iterable of (tag, item, status) tuples where status specifies the initial
# selected/unselected state of each entry; can be True or False, 1 or 0, "on" or "off"
# (True, 1 and "on" meaning selected), or any case variation of these two strings.
# No more than one entry should be set to True.
def ChooseOne(
prompt,
choices=[],
defaultBehavior=UserInputDefaultsBehavior.DefaultsPrompt,
uiMode=UserInterfaceMode.InteractionDialog | UserInterfaceMode.InteractionInput,
clearScreen=False,
):
validChoices = [x for x in choices if len(x) == 3 and isinstance(x[0], str) and isinstance(x[2], bool)]
defaulted = next(iter([x for x in validChoices if x[2] is True]), None)
if (defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept) and (
defaultBehavior & UserInputDefaultsBehavior.DefaultsNonInteractive
):
reply = defaulted[0] if defaulted is not None else ""
elif (uiMode & UserInterfaceMode.InteractionDialog) and (MainDialog is not None):
code, reply = MainDialog.radiolist(
prompt,
choices=validChoices,
)
if code == Dialog.CANCEL or code == Dialog.ESC:
raise RuntimeError("Operation cancelled")
elif uiMode & UserInterfaceMode.InteractionInput:
index = 0
for choice in validChoices:
index = index + 1
print(
f"{index}: {choice[0]}{f' - {choice[1]}' if isinstance(choice[1], str) and len(choice[1]) > 0 else ''}"
)
while True:
inputRaw = input(
f"{prompt}{f' ({defaulted[0]})' if (defaulted is not None) and (defaultBehavior & UserInputDefaultsBehavior.DefaultsPrompt) else ''}: "
).strip()
if (
(len(inputRaw) == 0)
and (defaulted is not None)
and (defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept)
):
reply = defaulted[0]
break
elif (len(inputRaw) > 0) and inputRaw.isnumeric():
inputIndex = int(inputRaw) - 1
if inputIndex > -1 and inputIndex < len(validChoices):
reply = validChoices[inputIndex][0]
break
else:
raise RuntimeError("No user interfaces available")
if clearScreen is True:
ClearScreen()
return reply
###################################################################################################
# Choose multiple of many
# choices - an iterable of (tag, item, status) tuples where status specifies the initial
# selected/unselected state of each entry; can be True or False, 1 or 0, "on" or "off"
# (True, 1 and "on" meaning selected), or any case variation of these two strings.
def ChooseMultiple(
prompt,
choices=[],
defaultBehavior=UserInputDefaultsBehavior.DefaultsPrompt,
uiMode=UserInterfaceMode.InteractionDialog | UserInterfaceMode.InteractionInput,
clearScreen=False,
):
validChoices = [x for x in choices if len(x) == 3 and isinstance(x[0], str) and isinstance(x[2], bool)]
defaulted = [x[0] for x in validChoices if x[2] is True]
if (defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept) and (
defaultBehavior & UserInputDefaultsBehavior.DefaultsNonInteractive
):
reply = defaulted
elif (uiMode & UserInterfaceMode.InteractionDialog) and (MainDialog is not None):
code, reply = MainDialog.checklist(
prompt,
choices=validChoices,
)
if code == Dialog.CANCEL or code == Dialog.ESC:
raise RuntimeError("Operation cancelled")
elif uiMode & UserInterfaceMode.InteractionInput:
allowedChars = set(string.digits + ',' + ' ')
defaultValListStr = ",".join(defaulted)
print("0: NONE")
index = 0
for choice in validChoices:
index = index + 1
print(
f"{index}: {choice[0]}{f' - {choice[1]}' if isinstance(choice[1], str) and len(choice[1]) > 0 else ''}"
)
while True:
inputRaw = input(
f"{prompt}{f' ({defaultValListStr})' if (len(defaultValListStr) > 0) and (defaultBehavior & UserInputDefaultsBehavior.DefaultsPrompt) else ''}: "
).strip()
if (
(len(inputRaw) == 0)
and (len(defaulted) > 0)
and (defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept)
):
reply = defaulted
break
elif inputRaw == '0':
reply = []
break
elif (len(inputRaw) > 0) and (set(inputRaw) <= allowedChars):
reply = []
selectedIndexes = list(set([int(x.strip()) - 1 for x in inputRaw.split(',') if (len(x.strip())) > 0]))
for idx in selectedIndexes:
if idx > -1 and idx < len(validChoices):
reply.append(validChoices[idx][0])
if len(reply) > 0:
break
else:
raise RuntimeError("No user interfaces available")
if clearScreen is True:
ClearScreen()
return reply
###################################################################################################
# display a message to the user without feedback
def DisplayMessage(
message,
defaultBehavior=UserInputDefaultsBehavior.DefaultsPrompt,
uiMode=UserInterfaceMode.InteractionDialog | UserInterfaceMode.InteractionInput,
clearScreen=False,
):
reply = False
if (defaultBehavior & UserInputDefaultsBehavior.DefaultsAccept) and (
defaultBehavior & UserInputDefaultsBehavior.DefaultsNonInteractive
):
reply = True
elif (uiMode & UserInterfaceMode.InteractionDialog) and (MainDialog is not None):
code = MainDialog.msgbox(
message,
)
if (code == Dialog.CANCEL) or (code == Dialog.ESC):
raise RuntimeError("Operation cancelled")
else:
reply = True
else:
print(f"\n{message}")
reply = True
if clearScreen is True:
ClearScreen()
return reply
###################################################################################################
# convenient boolean argument parsing
def str2bool(v):
if isinstance(v, bool):
return v
elif isinstance(v, str):
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise ValueError("Boolean value expected")
else:
raise ValueError("Boolean value expected")
###################################################################################################
# determine if a program/script exists and is executable in the system path
def Which(cmd, debug=False):
result = any(os.access(os.path.join(path, cmd), os.X_OK) for path in os.environ["PATH"].split(os.pathsep))
if debug:
eprint(f"Which {cmd} returned {result}")
return result
###################################################################################################
# nice human-readable file sizes
def SizeHumanFormat(num, suffix='B'):
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}{'Yi'}{suffix}"
###################################################################################################
# is this string valid json? if so, load and return it
def LoadStrIfJson(jsonStr):
try:
return json.loads(jsonStr)
except ValueError:
return None
###################################################################################################
# safe deep get for a dictionary
#
# Example:
# d = {'meta': {'status': 'OK', 'status_code': 200}}
# DeepGet(d, ['meta', 'status_code']) # => 200
# DeepGet(d, ['garbage', 'status_code']) # => None
# DeepGet(d, ['meta', 'garbage'], default='-') # => '-'
def DeepGet(d, keys, default=None):
assert type(keys) is list
if d is None:
return default
if not keys:
return d
return DeepGet(d.get(keys[0]), keys[1:], default)
###################################################################################################
# run command with arguments and return its exit code, stdout, and stderr
def check_output_input(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden')
if 'stderr' in kwargs:
raise ValueError('stderr argument not allowed, it will be overridden')
if 'input' in kwargs and kwargs['input']:
if 'stdin' in kwargs:
raise ValueError('stdin and input arguments may not both be used')
inputdata = kwargs['input']
kwargs['stdin'] = PIPE
else:
inputdata = None
kwargs.pop('input', None)
process = Popen(*popenargs, stdout=PIPE, stderr=PIPE, **kwargs)
try:
output, errput = process.communicate(inputdata)
except Exception:
process.kill()
process.wait()
raise
retcode = process.poll()
return retcode, output, errput
###################################################################################################
# run command with arguments and return its exit code, stdout, and stderr
def run_process(
command, stdout=True, stderr=True, stdin=None, retry=0, retrySleepSec=5, cwd=None, env=None, debug=False
):
retcode = -1
output = []
try:
# run the command
retcode, cmdout, cmderr = check_output_input(
command, input=stdin.encode() if stdin else stdin, cwd=cwd, env=env
)
# split the output on newlines to return a list
if stderr and (len(cmderr) > 0):
output.extend(cmderr.decode(sys.getdefaultencoding()).split('\n'))
if stdout and (len(cmdout) > 0):
output.extend(cmdout.decode(sys.getdefaultencoding()).split('\n'))
except (FileNotFoundError, OSError, IOError):
if stderr:
output.append(f"Command {command} not found or unable to execute")
if debug:
eprint(f"{command}({stdin[:80] + bool(stdin[80:]) * '...' if stdin else ''}) returned {retcode}: {output}")
if (retcode != 0) and retry and (retry > 0):
# sleep then retry
time.sleep(retrySleepSec)
return run_process(command, stdout, stderr, stdin, retry - 1, retrySleepSec, cwd, env, debug)
else:
return retcode, output
###################################################################################################
# attempt dynamic imports, prompting for install via pip if possible
DynImports = defaultdict(lambda: None)
def DoDynamicImport(importName, pipPkgName, interactive=False, debug=False):
global DynImports
# see if we've already imported it
if not DynImports[importName]:
# if not, attempt the import
try:
tmpImport = importlib.import_module(importName)
if tmpImport:
DynImports[importName] = tmpImport
return DynImports[importName]
except ImportError:
pass
# see if we can help out by installing the module
pyPlatform = platform.system()
pyExec = sys.executable
pipCmd = "pip3"
if not Which(pipCmd, debug=debug):
pipCmd = "pip"
eprint(f"The {pipPkgName} module is required under Python {platform.python_version()} ({pyExec})")
if interactive and Which(pipCmd, debug=debug):
if YesOrNo(f"Importing the {pipPkgName} module failed. Attempt to install via {pipCmd}?"):
installCmd = None
if (pyPlatform == PLATFORM_LINUX) or (pyPlatform == PLATFORM_MAC):
# for linux/mac, we're going to try to figure out if this python is owned by root or the script user
if getpass.getuser() == getpwuid(os.stat(pyExec).st_uid).pw_name:
# we're running a user-owned python, regular pip should work
installCmd = [pipCmd, "install", pipPkgName]
else:
# python is owned by system, so make sure to pass the --user flag
installCmd = [pipCmd, "install", "--user", pipPkgName]
else:
# on windows (or whatever other platform this is) I don't know any other way other than pip
installCmd = [pipCmd, "install", pipPkgName]
err, out = run_process(installCmd, debug=debug)
if err == 0:
eprint(f"Installation of {pipPkgName} module apparently succeeded")
try:
tmpImport = importlib.import_module(importName)
if tmpImport:
DynImports[importName] = tmpImport
except ImportError as e:
eprint(f"Importing the {importName} module still failed: {e}")
else:
eprint(f"Installation of {importName} module failed: {out}")
if not DynImports[importName]:
eprint(
"System-wide installation varies by platform and Python configuration. Please consult platform-specific documentation for installing Python modules."
)
return DynImports[importName]
def RequestsDynamic(debug=False, forceInteraction=False):
return DoDynamicImport("requests", "requests", interactive=forceInteraction, debug=debug)
def YAMLDynamic(debug=False, forceInteraction=False):
return DoDynamicImport("yaml", "pyyaml", interactive=forceInteraction, debug=debug)
###################################################################################################
# do the required auth files for Malcolm exist?
def MalcolmAuthFilesExist():
return (
os.path.isfile(os.path.join(MalcolmPath, os.path.join('nginx', 'htpasswd')))
and os.path.isfile(os.path.join(MalcolmPath, os.path.join('nginx', 'nginx_ldap.conf')))
and os.path.isfile(os.path.join(MalcolmPath, os.path.join('nginx', os.path.join('certs', 'cert.pem'))))
and os.path.isfile(os.path.join(MalcolmPath, os.path.join('nginx', os.path.join('certs', 'key.pem'))))
and os.path.isfile(os.path.join(MalcolmPath, os.path.join('htadmin', 'config.ini')))
and os.path.isfile(os.path.join(MalcolmPath, os.path.join('netbox', os.path.join('env', 'netbox.env'))))
and os.path.isfile(os.path.join(MalcolmPath, os.path.join('netbox', os.path.join('env', 'postgres.env'))))
and os.path.isfile(os.path.join(MalcolmPath, os.path.join('netbox', os.path.join('env', 'redis-cache.env'))))
and os.path.isfile(os.path.join(MalcolmPath, os.path.join('netbox', os.path.join('env', 'redis.env'))))
and os.path.isfile(os.path.join(MalcolmPath, 'auth.env'))
and os.path.isfile(os.path.join(MalcolmPath, '.opensearch.primary.curlrc'))
)
###################################################################################################
# download to file
def DownloadToFile(url, local_filename, debug=False):
r = RequestsDynamic().get(url, stream=True, allow_redirects=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
fExists = os.path.isfile(local_filename)
fSize = os.path.getsize(local_filename)
if debug:
eprint(
f"Download of {url} to {local_filename} {'succeeded' if fExists else 'failed'} ({SizeHumanFormat(fSize)})"
)
return fExists and (fSize > 0)
###################################################################################################
# recursively remove empty subfolders
def RemoveEmptyFolders(path, removeRoot=True):
if not os.path.isdir(path):
return
files = os.listdir(path)
if len(files):
for f in files:
fullpath = os.path.join(path, f)
if os.path.isdir(fullpath):
RemoveEmptyFolders(fullpath)
files = os.listdir(path)
if len(files) == 0 and removeRoot:
try:
os.rmdir(path)
except Exception:
pass