Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
F S UID PID PPID C PRI NI ADDR SZ RSS WCHAN TTY TIME CMD
240001 A 203 14549132 8192226 1 60 20 96aaf7590 2620 2552 pts/0 0:00 -bash
200001 A 203 6384118 14549132 3 61 20 8aaeeb590 1440 1468 pts/0 0:00 ps wwl
""", # noqa
"""\
F S UID PID PPID C PRI NI ADDR SZ RSS WCHAN TTY TIME CMD
240000 A 105 932687 932686 0 20 20 0 7516 0 pts/0 0:00 -bash
200000 A 105 932807 932687 0 20 20 0 16504 0 pts/0 0:00 python3
200000 A 105 932808 932807 0 20 20 0 6792 0 pts/0 0:00 ps wwl
""", # noqa
]
PS_EXPECTED = [
{
'90585': Process(pid='90585', ppid='90584', args=('-bash',)),
'96095': Process(pid='96095', ppid='90585', args=('pbcopy',)),
'82490': Process(pid='82490', ppid='82489', args=('-bash',)),
'82557': Process(pid='82557', ppid='82556', args=('-bash',)),
},
{
'14549132': Process(pid='14549132', ppid='8192226', args=('-bash',)),
'6384118': Process(pid='6384118', ppid='14549132', args=('ps', 'wwl')),
},
{
'932687': Process(pid='932687', ppid='932686', args=('-bash',)),
'932807': Process(pid='932807', ppid='932687', args=('python3')),
'932808': Process(pid='932808', ppid='932807', args=('ps', 'wwl')),
},
]
@pytest.mark.parametrize(
def test_parse_ps_header():
header = ' UID PID PPID CPU PRI NI VSZ TIME COMMAND'
parsed = dict(_parse_ps_header(header))
assert parsed == {
'UID': slice(0, 6),
'PID': slice(6, 12),
'PPID': slice(12, 18),
'CPU': slice(18, 22),
'PRI': slice(22, 26),
'NI': slice(26, 29),
'VSZ': slice(29, 38),
'TIME': slice(38, 46),
'COMMAND': slice(46, None),
}
def test_parse_ps_output(output, expected):
parsed = dict(_parse_ps_output(output))
assert parsed == expected
def _detect_shell():
shell = os.environ.get('SHELL', None)
if not shell:
if 'CMDER_ROOT' in os.environ:
shell = 'Cmder'
elif windows:
try:
_, shell = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
shell = os.environ.get('COMSPEC', 'cmd.exe')
else:
shell = 'sh'
return shell
def _shell_info(self) -> Tuple[str, Path]:
# detect by shellingham
try:
name, path = detect_shell()
except (ShellDetectionFailure, RuntimeError):
pass
else:
return name, Path(path)
# detect by env
for env in ('SHELL', 'COMSPEC'):
path = os.environ.get(env)
if path:
path = Path(path).resolve()
return path.stem, path
# try to find any known shell
for name in sorted(self.shells):
path = shutil.which(name)
if path is not None:
def get_auto_shell():
"""Returns the current shell"""
return shellingham.detect_shell()[0]
def get(cls): # type: () -> Shell
"""
Retrieve the current shell.
"""
if cls._shell is not None:
return cls._shell
try:
name, path = detect_shell(os.getpid())
except (RuntimeError, ShellDetectionFailure):
raise RuntimeError("Unable to detect the current shell.")
cls._shell = cls(name, path)
return cls._shell
COLORS = __all__[:-2]
is_ipython = "get_ipython" in dir()
if (
os.environ.get("CMDER_ROOT")
or os.environ.get("VSCODE_PID")
or os.environ.get("TERM_PROGRAM") == "Hyper"
):
is_native_powershell = False
else:
is_native_powershell = True
try:
is_powershell = "powershell" in shellingham.detect_shell()[0]
except shellingham.ShellDetectionFailure:
is_powershell = False
if is_ipython or (is_powershell and is_native_powershell):
"""when ipython is fired lot of variables like _oh, etc are used.
There are so many ways to find current python interpreter is ipython.
get_ipython is easiest is most appealing for readers to understand.
"""
DISABLE_COLOR = True
else:
DISABLE_COLOR = False
class ColoredString(object):
"""Enhanced string for __len__ operations on Colored output."""
def __init__(self, color, s, always_color=False, bold=False):
def get(cls): # type: () -> Shell
"""
Retrieve the current shell.
"""
if cls._shell is not None:
return cls._shell
try:
name, path = detect_shell(os.getpid())
except (RuntimeError, ShellDetectionFailure):
raise RuntimeError("Unable to detect the current shell.")
cls._shell = cls(name, path)
return cls._shell
def _detect_shell():
shell = os.environ.get('SHELL', None)
if not shell:
if 'CMDER_ROOT' in os.environ:
shell = 'Cmder'
elif windows:
try:
_, shell = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
shell = os.environ.get('COMSPEC', 'cmd.exe')
else:
shell = 'sh'
return shell