Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions XSConsoleUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,36 @@ def __init__(self, *inParams):
self.stdout = []
self.stderr = []
self.called = False

def _NewPipe(self, *inParams):
if len(inParams) == 1 and isinstance(inParams, (list, tuple)):
params = inParams[0]
else:
params = inParams

self.pipe = subprocess.Popen(params,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
close_fds=True)
self.called = False
Copy link
Collaborator

@bernhardkaindl bernhardkaindl Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review any commits to not intermix removing trailing whitespace with your code changes. While it's possible to ignore such simple changes with tools, everyone reviewing the changes still have to know how to do ignore them in order to spot your change in the middle of all that whitespace.

On GitHub, you have to know that you can find the feature to ignore whitespace in the settings gear menu and for git log -p you have to know that git log -pw ignores whitespace.

Therefore, if you need to change whitespace, then please in a separate PR (or at least a separate commit).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review any commits to not intermix removing trailing whitespace with your code changes. While it's possible to ignore such simple changes with tools, everyone reviewing the changes still have to know how to do ignore them in order to spot your change in the middle of all that whitespace.

On GitHub, you have to know that you can find the feature to ignore whitespace in the settings gear menu and for git log -p you have to know that git log -pw ignores whitespace.

Therefore, if you need to change whitespace, then please in a separate PR (or at least a separate commit).

Thank for your suggestion and will keep it in mind.


def Stdout(self):
if not self.called:
self.Call()
return self.stdout

def Stderr(self):
if not self.called:
self.Call()
return self.stderr

def AllOutput(self):
if not self.called:
self.Call()
return self.stdout + self.stderr

def Communicate(self, inInput = None):
if self.called:
raise Exception("ShellPipe called more than once")
Expand All @@ -88,19 +89,19 @@ def Communicate(self, inInput = None):
stdout, stderr = self.pipe.communicate("\n".join(inInput))
else:
stdout, stderr = self.pipe.communicate(inInput)

self.stdout += stdout.splitlines()
self.stderr += stderr.splitlines()
break
except IOError as e:
if e.errno != errno.EINTR: # Loop if EINTR
raise
# Other exceptions propagate to the caller

def CallRC(self, inInput = None): # Raise exception or return the return code
self.Communicate(inInput)
return self.pipe.returncode

def Call(self, inInput = None): # Raise exception on failure
self.Communicate(inInput)
if self.pipe.returncode != 0:
Expand All @@ -111,14 +112,14 @@ def Call(self, inInput = None): # Raise exception on failure
else:
raise Exception("Unknown failure")
return self

def Chain(self, *inParams):
if not self.called:
self.Call()
self._NewPipe(*inParams)
self.Call()
return self

def Pipe(self, *inParams):
if not self.called:
self.Call()
Expand All @@ -135,7 +136,7 @@ def MakeSafeParam(cls, inParam):
if not re.match(r'[-A-Za-z0-9/._~:@]*$', inParam):
raise Exception("Invalid characters in parameter '"+inParam+"'")
return inParam

@classmethod
def WaitOnPipe(cls, inPipe):
# Wait on a popen2 pipe, handling Interrupted System Call exceptions
Expand All @@ -154,7 +155,7 @@ class TimeUtils:
@staticmethod
def AlarmHandler(inSigNum, inStackFrame):
raise TimeException("Operation timed out")

@classmethod
def TimeoutWrapper(cls, inCallable, inTimeout):
oldHandler = signal.signal(signal.SIGALRM, TimeUtils.AlarmHandler)
Expand All @@ -164,11 +165,11 @@ def TimeoutWrapper(cls, inCallable, inTimeout):
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, oldHandler)

@classmethod
def DurationString(cls, inSecs):
secs = max(0, int(inSecs))

hours = int(secs / 3600)
secs -= hours * 3600
mins = int(secs / 60)
Expand All @@ -178,15 +179,15 @@ def DurationString(cls, inSecs):
else:
retVal = "%d:%2.2d" % (mins, secs)
return retVal

@classmethod
def DateTimeToSecs(cls, inDateTime):
structTime = time.strptime(inDateTime.value, '%Y%m%dT%H:%M:%SZ')
retVal = time.mktime(structTime)
if retVal <= 3601.0: # Handle the effect of daylight savings on start of epoch
retVal = 0.0
return retVal

class IPUtils:
@classmethod
def ValidateIP(cls, text):
Expand All @@ -199,7 +200,7 @@ def ValidateIP(cls, text):
largest = max(largest, i)
if largest == 0: return False
return True

@classmethod
def ValidateNetmask(cls, text):
rc = re.match("^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$", text)
Expand Down Expand Up @@ -229,7 +230,7 @@ def AssertValidHostname(cls, inName):
if not re.match(r'[0-9A-Za-z]([-0-9A-Za-z]{0,61}[0-9A-Za-z]|)$', inName):
raise Exception(Lang('Invalid hostname'))
return inName

@classmethod
def AssertValidNetworkName(cls, inName):
# Also allow FQDN-style names
Expand Down Expand Up @@ -261,7 +262,7 @@ def AssertValidNFSPathName(cls, inName):
for subName in splitNames[1:]:
cls.AssertValidNFSDirectoryName(subName)
return inName

@classmethod
def AssertValidCIFSPathName(cls, inName):
splitNames = inName.split('\\')
Expand All @@ -277,7 +278,7 @@ def BinarySizeString(cls, inBytes, inInFix = None):
else:
inFix = FirstValue(inInFix, '')
bytes = int(inBytes)

if bytes is None or bytes < 0:
retVal = Lang('<Unknown>')
elif bytes >= 1073741824: # 1GiB
Expand Down Expand Up @@ -320,12 +321,12 @@ def DecimalSizeString(cls, inBytes, inPrefix = None):
@classmethod
def MemorySizeString(cls, inBytes):
return cls.BinarySizeString(inBytes)

@classmethod
def SRSizeString(cls, inBytes):
return cls.BinarySizeString(inBytes)

@classmethod
def DiskSizeString(cls, inBytes):
return cls.BinarySizeString(inBytes)+' ('+cls.DecimalSizeString(inBytes)+')'