-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnavi.py
66 lines (50 loc) · 1.92 KB
/
navi.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
import platform
import re
import subprocess # nosec
def get_command_path(command: str) -> bool | str:
windows_commands = get_windows_builtin_commands()
if windows_commands and is_windows_command(command, windows_commands):
return True
if platform.python_version() >= "3.12":
from shutil import which
return which(command)
else:
from distutils.spawn import find_executable
return find_executable(command)
def is_windows_command(command: str, windows_commands: list[str]) -> bool:
return command.upper() in (cmd.upper() for cmd in windows_commands)
def get_windows_builtin_commands() -> list[str]:
if platform.system() != "Windows":
return []
result = subprocess.run(["help"], capture_output=True, text=True) # nosec
if result.stdout:
help_text = result.stdout
# Extract commands from help text
commands = []
lines = help_text.splitlines()
for line in lines:
# Command names are typically in uppercase and are single words
match = re.match(r'^[A-Z]+\b', line.strip())
if match:
commands.append(match.group(0))
return commands
return []
def get_ip_address(input_str: str) -> str | None:
if re.match(r'(\d{1,3}\.){3}\d{1,3}', input_str):
return input_str
else:
return None
def get_hostname(input_str: str) -> str | None:
if re.match(r'[a-zA-Z0-9\-]+\.[a-zA-Z]{2,3}', input_str):
return input_str
else:
return None
def get_parameters(input_str: str) -> list[str]:
pattern = re.compile(r'''
"([^"]*)"| # Capture text within quotes
(\S+) # Capture other non-whitespace sequences
''', re.VERBOSE)
matches = pattern.findall(input_str)
# Flatten the list and filter out empty strings
parameters = [item for sublist in matches for item in sublist if item]
return parameters