abstract-utilities 0.2.2.387__py3-none-any.whl → 0.2.2.480__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of abstract-utilities might be problematic. Click here for more details.
- abstract_utilities/__init__.py +14 -43
- abstract_utilities/abstract_classes.py +49 -0
- abstract_utilities/class_utils.py +38 -3
- abstract_utilities/cmd_utils/imports/__init__.py +1 -0
- abstract_utilities/cmd_utils/imports/imports.py +10 -0
- abstract_utilities/cmd_utils/pexpect_utils.py +310 -0
- abstract_utilities/cmd_utils/user_utils.py +1 -1
- abstract_utilities/dynimport.py +7 -15
- abstract_utilities/env_utils/__init__.py +3 -0
- abstract_utilities/env_utils/abstractEnv.py +129 -0
- abstract_utilities/env_utils/envy_it.py +33 -0
- abstract_utilities/env_utils/imports/__init__.py +2 -0
- abstract_utilities/env_utils/imports/imports.py +8 -0
- abstract_utilities/env_utils/imports/utils.py +122 -0
- abstract_utilities/file_utils/__init__.py +3 -0
- abstract_utilities/file_utils/file_utils/__init__.py +8 -0
- abstract_utilities/file_utils/file_utils/file_filters.py +104 -0
- abstract_utilities/{robust_reader → file_utils/file_utils}/file_reader.py +5 -19
- abstract_utilities/{robust_readers/file_filters.py → file_utils/file_utils/file_utils.py} +5 -4
- abstract_utilities/{robust_readers → file_utils/file_utils}/filter_params.py +1 -38
- abstract_utilities/file_utils/file_utils/find_collect.py +154 -0
- abstract_utilities/file_utils/file_utils/imports/__init__.py +3 -0
- abstract_utilities/file_utils/file_utils/imports/constants.py +39 -0
- abstract_utilities/file_utils/file_utils/imports/file_functions.py +10 -0
- abstract_utilities/file_utils/file_utils/imports/imports.py +39 -0
- abstract_utilities/file_utils/file_utils/imports/module_imports.py +14 -0
- abstract_utilities/file_utils/file_utils/imports.py +10 -0
- abstract_utilities/file_utils/file_utils/map_utils.py +29 -0
- abstract_utilities/{robust_reader → file_utils/file_utils}/pdf_utils.py +1 -9
- abstract_utilities/file_utils/file_utils/type_checks.py +91 -0
- abstract_utilities/file_utils/imports/__init__.py +4 -0
- abstract_utilities/file_utils/imports/classes.py +381 -0
- abstract_utilities/file_utils/imports/clean_imps.py +158 -0
- abstract_utilities/file_utils/imports/constants.py +39 -0
- abstract_utilities/file_utils/imports/file_functions.py +10 -0
- abstract_utilities/file_utils/imports/imports.py +65 -0
- abstract_utilities/file_utils/imports/module_imports.py +13 -0
- abstract_utilities/file_utils/req.py +329 -0
- abstract_utilities/log_utils.py +1 -1
- abstract_utilities/path_utils.py +90 -6
- abstract_utilities/read_write_utils.py +250 -157
- abstract_utilities/robust_reader/__init__.py +1 -1
- abstract_utilities/robust_reader/imports/__init__.py +1 -0
- abstract_utilities/robust_reader/imports/imports.py +3 -0
- abstract_utilities/robust_readers/__init__.py +0 -1
- abstract_utilities/robust_readers/import_utils/__init__.py +1 -0
- abstract_utilities/robust_readers/import_utils/clean_imports.py +175 -0
- abstract_utilities/robust_readers/imports.py +8 -0
- abstract_utilities/robust_readers/initFuncGen.py +92 -76
- abstract_utilities/safe_utils.py +133 -0
- abstract_utilities/ssh_utils/__init__.py +3 -0
- abstract_utilities/ssh_utils/classes.py +127 -0
- abstract_utilities/ssh_utils/imports.py +10 -0
- abstract_utilities/ssh_utils/pexpect_utils.py +315 -0
- abstract_utilities/ssh_utils/utils.py +188 -0
- abstract_utilities/string_clean.py +40 -1
- abstract_utilities/string_utils.py +51 -0
- abstract_utilities/type_utils.py +25 -2
- {abstract_utilities-0.2.2.387.dist-info → abstract_utilities-0.2.2.480.dist-info}/METADATA +1 -1
- abstract_utilities-0.2.2.480.dist-info/RECORD +92 -0
- abstract_utilities-0.2.2.387.dist-info/RECORD +0 -52
- {abstract_utilities-0.2.2.387.dist-info → abstract_utilities-0.2.2.480.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.387.dist-info → abstract_utilities-0.2.2.480.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
from .classes import *
|
|
2
|
+
from .utils import *
|
|
3
|
+
from ..env_utils import *
|
|
4
|
+
# pexpect is optional; import lazily if you prefer
|
|
5
|
+
|
|
6
|
+
# keep your execute_cmd; add a thin wrapper that supports stdin text cleanly
|
|
7
|
+
def execute_cmd_input(
|
|
8
|
+
*args,
|
|
9
|
+
input_text: str | None = None,
|
|
10
|
+
outfile: str | None = None,
|
|
11
|
+
**kwargs
|
|
12
|
+
) -> str:
|
|
13
|
+
"""
|
|
14
|
+
Like execute_cmd, but lets you pass text to stdin (subprocess.run(input=...)).
|
|
15
|
+
"""
|
|
16
|
+
if input_text is not None:
|
|
17
|
+
kwargs["input"] = input_text
|
|
18
|
+
# ensure text mode so Python passes str not bytes
|
|
19
|
+
kwargs.setdefault("text", True)
|
|
20
|
+
return execute_cmd(*args, outfile=outfile, **kwargs)
|
|
21
|
+
|
|
22
|
+
# -------------------------
|
|
23
|
+
# Core: capture + printing
|
|
24
|
+
# -------------------------
|
|
25
|
+
def exec_sudo_capture(
|
|
26
|
+
cmd: str,
|
|
27
|
+
*,
|
|
28
|
+
password: str | None = None,
|
|
29
|
+
key: str | None = None,
|
|
30
|
+
user_at_host: str | None = None,
|
|
31
|
+
cwd: str | None = None,
|
|
32
|
+
print_output: bool = False,
|
|
33
|
+
) -> str:
|
|
34
|
+
"""
|
|
35
|
+
Run a sudo command and return its output (no temp file).
|
|
36
|
+
"""
|
|
37
|
+
if password is None:
|
|
38
|
+
password = get_env_value(key=key) if key else get_sudo_password()
|
|
39
|
+
password = f"{password}\n" if password else password
|
|
40
|
+
sudo_cmd = f"sudo -S -k {cmd}"
|
|
41
|
+
|
|
42
|
+
if user_at_host:
|
|
43
|
+
# build the remote command (bash -lc + optional cd)
|
|
44
|
+
remote = get_remote_cmd(cmd=sudo_cmd, user_at_host=user_at_host, cwd=cwd)
|
|
45
|
+
# feed password to remote's stdin (ssh forwards stdin)
|
|
46
|
+
out = execute_cmd_input(remote, input_text=password,
|
|
47
|
+
shell=True, text=True, capture_output=True)
|
|
48
|
+
else:
|
|
49
|
+
out = execute_cmd_input(sudo_cmd, input_text=password,
|
|
50
|
+
shell=True, text=True, capture_output=True, cwd=cwd)
|
|
51
|
+
|
|
52
|
+
if print_output:
|
|
53
|
+
print_cmd(cmd, out or "")
|
|
54
|
+
return out or ""
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# ---------------------------------------------------
|
|
59
|
+
# SUDO helpers (local + SSH) with env/password options
|
|
60
|
+
# ---------------------------------------------------
|
|
61
|
+
def exec_sudo(
|
|
62
|
+
cmd: str,
|
|
63
|
+
*,
|
|
64
|
+
password: Optional[str] = None,
|
|
65
|
+
key: Optional[str] = None,
|
|
66
|
+
user_at_host: Optional[str] = None,
|
|
67
|
+
cwd: Optional[str] = None,
|
|
68
|
+
outfile: Optional[str] = None,
|
|
69
|
+
print_output: bool = False,
|
|
70
|
+
) -> str:
|
|
71
|
+
"""
|
|
72
|
+
Execute `cmd` via sudo either locally or on remote.
|
|
73
|
+
Password order of precedence:
|
|
74
|
+
1) `password` arg
|
|
75
|
+
2) `key` -> get_env_value(key)
|
|
76
|
+
3) get_sudo_password()
|
|
77
|
+
|
|
78
|
+
Uses: sudo -S -k (-S read password from stdin, -k invalidate cached timestamp)
|
|
79
|
+
"""
|
|
80
|
+
if password is None:
|
|
81
|
+
if key:
|
|
82
|
+
password = get_env_value(key=key)
|
|
83
|
+
else:
|
|
84
|
+
password = get_sudo_password()
|
|
85
|
+
|
|
86
|
+
# Compose the sudo command that reads from stdin
|
|
87
|
+
sudo_cmd = f"sudo -S -k {cmd}"
|
|
88
|
+
|
|
89
|
+
if user_at_host:
|
|
90
|
+
# For remote: the password is piped to SSH stdin, which flows to remote sudo's stdin.
|
|
91
|
+
remote = get_remote_cmd(cmd=sudo_cmd, user_at_host=user_at_host, cwd=cwd)
|
|
92
|
+
full = f"printf %s {shlex.quote(password)} | {remote}"
|
|
93
|
+
out = execute_cmd(full, shell=True, text=True, capture_output=True, outfile=outfile)
|
|
94
|
+
else:
|
|
95
|
+
# Local
|
|
96
|
+
full = f"printf %s {shlex.quote(password)} | {sudo_cmd}"
|
|
97
|
+
out = execute_cmd(full, shell=True, text=True, capture_output=True, outfile=outfile)
|
|
98
|
+
|
|
99
|
+
if print_output:
|
|
100
|
+
print_cmd(cmd, out or "")
|
|
101
|
+
return out or ""
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
# -------------------------------------------------
|
|
105
|
+
# Fire-and-forget (file-backed) compatible runner
|
|
106
|
+
# -------------------------------------------------
|
|
107
|
+
def cmd_run(
|
|
108
|
+
cmd: str,
|
|
109
|
+
output_text: str | None = None,
|
|
110
|
+
print_output: bool = False,
|
|
111
|
+
*,
|
|
112
|
+
user_at_host: str | None = None,
|
|
113
|
+
cwd: str | None = None,
|
|
114
|
+
) -> str | None:
|
|
115
|
+
"""
|
|
116
|
+
If output_text is None → capture+return output (no file).
|
|
117
|
+
If output_text is provided → legacy file-backed behavior.
|
|
118
|
+
"""
|
|
119
|
+
if output_text is None:
|
|
120
|
+
# capture mode
|
|
121
|
+
if user_at_host:
|
|
122
|
+
remote = get_remote_cmd(cmd=cmd, user_at_host=user_at_host, cwd=cwd)
|
|
123
|
+
out = execute_cmd(remote, shell=True, text=True, capture_output=True)
|
|
124
|
+
else:
|
|
125
|
+
out = execute_cmd(cmd, shell=True, text=True, capture_output=True, cwd=cwd)
|
|
126
|
+
## if print_output:
|
|
127
|
+
## print_cmd(cmd, out or "")
|
|
128
|
+
return out or ""
|
|
129
|
+
|
|
130
|
+
# ---- legacy file-backed path (unchanged in spirit) ----
|
|
131
|
+
# Clear output file
|
|
132
|
+
with open(output_text, 'w'):
|
|
133
|
+
pass
|
|
134
|
+
|
|
135
|
+
# Append redirection + sentinel
|
|
136
|
+
full_cmd = f'{cmd} >> {output_text}; echo END_OF_CMD >> {output_text}'
|
|
137
|
+
|
|
138
|
+
# Execute local/remote
|
|
139
|
+
if user_at_host:
|
|
140
|
+
remote_line = get_remote_cmd(cmd=full_cmd, user_at_host=user_at_host, cwd=cwd)
|
|
141
|
+
subprocess.call(remote_line, shell=True)
|
|
142
|
+
else:
|
|
143
|
+
subprocess.call(full_cmd, shell=True, cwd=cwd)
|
|
144
|
+
|
|
145
|
+
# Wait for sentinel
|
|
146
|
+
while True:
|
|
147
|
+
get_sleep(sleep_timer=0.5)
|
|
148
|
+
with open(output_text, 'r') as f:
|
|
149
|
+
lines = f.readlines()
|
|
150
|
+
if lines and lines[-1].strip() == 'END_OF_CMD':
|
|
151
|
+
break
|
|
152
|
+
|
|
153
|
+
## if print_output:
|
|
154
|
+
## print(full_cmd)
|
|
155
|
+
## with open(output_text, 'r') as f:
|
|
156
|
+
## print_cmd(full_cmd, f.read().strip())
|
|
157
|
+
|
|
158
|
+
try:
|
|
159
|
+
os.remove(output_text)
|
|
160
|
+
except OSError:
|
|
161
|
+
pass
|
|
162
|
+
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
# ----------------------------------------------------
|
|
167
|
+
# pexpect wrappers (local + SSH) for interactive flows
|
|
168
|
+
# ----------------------------------------------------
|
|
169
|
+
def exec_expect(
|
|
170
|
+
command: str,
|
|
171
|
+
child_runs: List[Dict[str, Any]],
|
|
172
|
+
*,
|
|
173
|
+
user_at_host: Optional[str] = None,
|
|
174
|
+
cwd: Optional[str] = None,
|
|
175
|
+
print_output: bool = False,
|
|
176
|
+
) -> int:
|
|
177
|
+
"""
|
|
178
|
+
Run `command` and answer interactive prompts.
|
|
179
|
+
|
|
180
|
+
child_runs: list of dicts like:
|
|
181
|
+
{ "prompt": r"Password:", "pass": "xyz" }
|
|
182
|
+
{ "prompt": r"Enter passphrase:", "key": "MY_KEY", "env_path": "/path/for/.env" }
|
|
183
|
+
If "pass" is None, we resolve via get_env_value(key=..., start_path=env_path).
|
|
184
|
+
|
|
185
|
+
Returns exitstatus (0=success).
|
|
186
|
+
"""
|
|
187
|
+
if user_at_host:
|
|
188
|
+
# Wrap command for remote execution
|
|
189
|
+
remote_line = get_remote_cmd(cmd=command, user_at_host=user_at_host, cwd=cwd)
|
|
190
|
+
spawn_cmd = f"{remote_line}"
|
|
191
|
+
else:
|
|
192
|
+
spawn_cmd = f"bash -lc {shlex.quote((f'cd {shlex.quote(cwd)} && {command}') if cwd else command)}"
|
|
193
|
+
|
|
194
|
+
child = pexpect.spawn(spawn_cmd)
|
|
195
|
+
|
|
196
|
+
for each in child_runs:
|
|
197
|
+
child.expect(each["prompt"])
|
|
198
|
+
|
|
199
|
+
if each.get("pass") is not None:
|
|
200
|
+
pass_phrase = each["pass"]
|
|
201
|
+
else:
|
|
202
|
+
args = {}
|
|
203
|
+
if "key" in each and each["key"] is not None:
|
|
204
|
+
args["key"] = each["key"]
|
|
205
|
+
if "env_path" in each and each["env_path"] is not None:
|
|
206
|
+
args["start_path"] = each["env_path"]
|
|
207
|
+
pass_phrase = get_env_value(**args)
|
|
208
|
+
|
|
209
|
+
child.sendline(pass_phrase)
|
|
210
|
+
if print_output:
|
|
211
|
+
print("Answered prompt:", each["prompt"])
|
|
212
|
+
|
|
213
|
+
child.expect(pexpect.EOF)
|
|
214
|
+
out = child.before.decode("utf-8", errors="ignore")
|
|
215
|
+
## if print_output:
|
|
216
|
+
## print_cmd(command, out)
|
|
217
|
+
|
|
218
|
+
return child.exitstatus if child.exitstatus is not None else 0
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
# ---------------------------------------
|
|
222
|
+
# Convenience shims to mirror your names
|
|
223
|
+
# ---------------------------------------
|
|
224
|
+
def cmd_run_sudo(
|
|
225
|
+
cmd: str,
|
|
226
|
+
password: str | None = None,
|
|
227
|
+
key: str | None = None,
|
|
228
|
+
output_text: str | None = None,
|
|
229
|
+
*,
|
|
230
|
+
user_at_host: str | None = None,
|
|
231
|
+
cwd: str | None = None,
|
|
232
|
+
print_output: bool = False,
|
|
233
|
+
) -> str | None:
|
|
234
|
+
"""
|
|
235
|
+
If output_text is None → capture sudo output and return it.
|
|
236
|
+
If output_text is provided → legacy file-backed behavior feeding sudo via stdin.
|
|
237
|
+
"""
|
|
238
|
+
if output_text is None:
|
|
239
|
+
return exec_sudo_capture(
|
|
240
|
+
cmd,
|
|
241
|
+
password=password,
|
|
242
|
+
key=key,
|
|
243
|
+
user_at_host=user_at_host,
|
|
244
|
+
cwd=cwd,
|
|
245
|
+
print_output=print_output,
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
# ---- legacy file-backed path ----
|
|
249
|
+
# build the underlying sudo command
|
|
250
|
+
sudo_cmd = f"sudo -S -k {cmd}"
|
|
251
|
+
pw = password if password is not None else (get_env_value(key=key) if key else get_sudo_password())
|
|
252
|
+
|
|
253
|
+
# We need to feed password to stdin in the same shell that runs sudo.
|
|
254
|
+
# For file-backed mode we’ll inline a small shell that reads from a here-string.
|
|
255
|
+
# Local:
|
|
256
|
+
if not user_at_host:
|
|
257
|
+
full = f'bash -lc {shlex.quote((f"cd {shlex.quote(cwd)} && " if cwd else "") + f"printf %s {shlex.quote(pw)} | {sudo_cmd}")}'
|
|
258
|
+
return cmd_run(full, output_text=output_text, print_output=print_output)
|
|
259
|
+
# Remote:
|
|
260
|
+
# On remote, do the same in the remote bash -lc
|
|
261
|
+
remote_sudo_line = f'printf %s {shlex.quote(pw)} | {sudo_cmd}'
|
|
262
|
+
remote_full = get_remote_cmd(cmd=remote_sudo_line, user_at_host=user_at_host, cwd=cwd)
|
|
263
|
+
return cmd_run(remote_full, output_text=output_text, print_output=print_output)
|
|
264
|
+
|
|
265
|
+
def pexpect_cmd_with_args(
|
|
266
|
+
command: str,
|
|
267
|
+
child_runs: list,
|
|
268
|
+
output_text: str | None = None,
|
|
269
|
+
*,
|
|
270
|
+
user_at_host: str | None = None,
|
|
271
|
+
cwd: str | None = None,
|
|
272
|
+
print_output: bool = False
|
|
273
|
+
) -> int:
|
|
274
|
+
"""
|
|
275
|
+
If output_text is None → return output string via print_output, else write to file then remove (legacy).
|
|
276
|
+
"""
|
|
277
|
+
if user_at_host:
|
|
278
|
+
spawn_cmd = get_remote_cmd(cmd=command, user_at_host=user_at_host, cwd=cwd)
|
|
279
|
+
else:
|
|
280
|
+
spawn_cmd = f"bash -lc {shlex.quote((f'cd {shlex.quote(cwd)} && {command}') if cwd else command)}"
|
|
281
|
+
|
|
282
|
+
child = pexpect.spawn(spawn_cmd)
|
|
283
|
+
|
|
284
|
+
for each in child_runs:
|
|
285
|
+
child.expect(each["prompt"])
|
|
286
|
+
if each.get("pass") is not None:
|
|
287
|
+
pass_phrase = each["pass"]
|
|
288
|
+
else:
|
|
289
|
+
args = {}
|
|
290
|
+
if "key" in each and each["key"] is not None:
|
|
291
|
+
args["key"] = each["key"]
|
|
292
|
+
if "env_path" in each and each["env_path"] is not None:
|
|
293
|
+
args["start_path"] = each["env_path"]
|
|
294
|
+
pass_phrase = get_env_value(**args)
|
|
295
|
+
child.sendline(pass_phrase)
|
|
296
|
+
if print_output:
|
|
297
|
+
print("Answered prompt:", each["prompt"])
|
|
298
|
+
|
|
299
|
+
child.expect(pexpect.EOF)
|
|
300
|
+
out = child.before.decode("utf-8", errors="ignore")
|
|
301
|
+
|
|
302
|
+
if output_text:
|
|
303
|
+
with open(output_text, "w") as f:
|
|
304
|
+
f.write(out)
|
|
305
|
+
if print_output:
|
|
306
|
+
print_cmd(command, out)
|
|
307
|
+
# keep legacy? your old code removed the file; here we’ll keep it (safer).
|
|
308
|
+
# If you want the old behavior, uncomment:
|
|
309
|
+
# os.remove(output_text)
|
|
310
|
+
else:
|
|
311
|
+
if print_output:
|
|
312
|
+
print_cmd(command, out)
|
|
313
|
+
|
|
314
|
+
return child.exitstatus if child.exitstatus is not None else 0
|
|
315
|
+
cmd_input = execute_cmd_input
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def get_pass_from_key(key=None,env_path=None):
|
|
3
|
+
if key:
|
|
4
|
+
return get_env_value(key=key,path=env_path)
|
|
5
|
+
def get_password(password=None,key=None,env_path=None):
|
|
6
|
+
password = password or get_pass_from_key(key=key,env_path=env_path)
|
|
7
|
+
return password
|
|
8
|
+
|
|
9
|
+
def get_print_sudo_cmd(
|
|
10
|
+
cmd: str,
|
|
11
|
+
password=None,
|
|
12
|
+
key=None,
|
|
13
|
+
env_path=None
|
|
14
|
+
):
|
|
15
|
+
password = get_password(password=password,key=key,env_path=env_path)
|
|
16
|
+
if password != None:
|
|
17
|
+
|
|
18
|
+
cmd = get_password_cmd(password=password,cmd=cmd)
|
|
19
|
+
return cmd
|
|
20
|
+
def get_password_cmd(password:str,cmd:str):
|
|
21
|
+
sudo_cmd = get_sudo_cmd(cmd)
|
|
22
|
+
password_sudo_cmd = get_raw_password_sudo_cmd(password=password,sudo_cmd=sudo_cmd)
|
|
23
|
+
return password_sudo_cmd
|
|
24
|
+
def get_sudo_cmd(cmd: str):
|
|
25
|
+
return f"sudo -S -k {cmd}"
|
|
26
|
+
def get_raw_password_sudo_cmd(password:str,sudo_cmd:str):
|
|
27
|
+
return f"printf %s {shlex.quote(password)} | {sudo_cmd}"
|
|
28
|
+
def get_remote_bash(
|
|
29
|
+
cmd: str,
|
|
30
|
+
cwd: str | None = None
|
|
31
|
+
):
|
|
32
|
+
return f"bash -lc {shlex.quote((f'cd {shlex.quote(cwd)} && {cmd}') if cwd else cmd)}"
|
|
33
|
+
def get_remote_ssh(
|
|
34
|
+
user_at_host: str=None,
|
|
35
|
+
remote:str=None
|
|
36
|
+
):
|
|
37
|
+
return f"ssh {shlex.quote(user_at_host)} {shlex.quote(remote)}"
|
|
38
|
+
def get_remote_cmd(
|
|
39
|
+
cmd: str,
|
|
40
|
+
user_at_host: str,
|
|
41
|
+
cwd: str | None = None,
|
|
42
|
+
password=None,
|
|
43
|
+
key=None,
|
|
44
|
+
env_path=None
|
|
45
|
+
|
|
46
|
+
):
|
|
47
|
+
cmd = get_print_sudo_cmd(
|
|
48
|
+
cmd=cmd,
|
|
49
|
+
password=password,
|
|
50
|
+
key=key,
|
|
51
|
+
env_path=env_path
|
|
52
|
+
)
|
|
53
|
+
remote = get_remote_bash(
|
|
54
|
+
cmd=cmd,
|
|
55
|
+
cwd=cwd
|
|
56
|
+
)
|
|
57
|
+
full = get_remote_ssh(
|
|
58
|
+
user_at_host=user_at_host,
|
|
59
|
+
remote=remote
|
|
60
|
+
)
|
|
61
|
+
return full
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def execute_cmd(
|
|
65
|
+
*args,
|
|
66
|
+
outfile=None,
|
|
67
|
+
**kwargs
|
|
68
|
+
) -> str:
|
|
69
|
+
proc = subprocess.run(*args, **kwargs)
|
|
70
|
+
output = (proc.stdout or "") + (proc.stderr or "")
|
|
71
|
+
if outfile:
|
|
72
|
+
try:
|
|
73
|
+
with open(outfile, "w", encoding="utf-8", errors="ignore") as f:
|
|
74
|
+
f.write(output)
|
|
75
|
+
except Exception:
|
|
76
|
+
pass
|
|
77
|
+
return output
|
|
78
|
+
|
|
79
|
+
def run_local_cmd(
|
|
80
|
+
cmd: str,
|
|
81
|
+
cwd: str | None = None,
|
|
82
|
+
outfile: Optional[str] = None,
|
|
83
|
+
shell=True,
|
|
84
|
+
text=True,
|
|
85
|
+
capture_output=True,
|
|
86
|
+
user_at_host: str=None,
|
|
87
|
+
password=None,
|
|
88
|
+
key=None,
|
|
89
|
+
env_path=None
|
|
90
|
+
) -> str:
|
|
91
|
+
cmd = get_print_sudo_cmd(
|
|
92
|
+
cmd=cmd,
|
|
93
|
+
password=password,
|
|
94
|
+
key=key,
|
|
95
|
+
env_path=env_path
|
|
96
|
+
)
|
|
97
|
+
return execute_cmd(
|
|
98
|
+
cmd,
|
|
99
|
+
outfile=outfile,
|
|
100
|
+
shell=shell,
|
|
101
|
+
cwd=cwd,
|
|
102
|
+
text=text,
|
|
103
|
+
capture_output=capture_output
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def run_remote_cmd(
|
|
107
|
+
user_at_host: str,
|
|
108
|
+
cmd: str,
|
|
109
|
+
cwd: str | None = None,
|
|
110
|
+
outfile: Optional[str] = None,
|
|
111
|
+
shell=True,
|
|
112
|
+
text=True,
|
|
113
|
+
capture_output=True,
|
|
114
|
+
password=None,
|
|
115
|
+
key=None,
|
|
116
|
+
env_path=None
|
|
117
|
+
) -> str:
|
|
118
|
+
"""
|
|
119
|
+
Run on remote via SSH; capture stdout+stderr locally; write to local outfile.
|
|
120
|
+
NOTE: we do *not* try to write the file on the remote to avoid later scp.
|
|
121
|
+
"""
|
|
122
|
+
cmd = get_print_sudo_cmd(
|
|
123
|
+
cmd=cmd,
|
|
124
|
+
password=password,
|
|
125
|
+
key=key,
|
|
126
|
+
env_path=env_path
|
|
127
|
+
)
|
|
128
|
+
# wrap in bash -lc for PATH/profile + allow 'cd && ...'
|
|
129
|
+
cmd = get_remote_cmd(
|
|
130
|
+
cmd=cmd,
|
|
131
|
+
user_at_host=user_at_host,
|
|
132
|
+
cwd=cwd
|
|
133
|
+
)
|
|
134
|
+
return execute_cmd(
|
|
135
|
+
cmd,
|
|
136
|
+
outfile=outfile,
|
|
137
|
+
shell=shell,
|
|
138
|
+
text=text,
|
|
139
|
+
capture_output=capture_output
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
def run_cmd(
|
|
143
|
+
cmd: str=None,
|
|
144
|
+
cwd: str | None = None,
|
|
145
|
+
outfile: Optional[str] = None,
|
|
146
|
+
shell=True,
|
|
147
|
+
text=True,
|
|
148
|
+
capture_output=True,
|
|
149
|
+
user_at_host: str=None,
|
|
150
|
+
password=None,
|
|
151
|
+
key=None,
|
|
152
|
+
env_path=None
|
|
153
|
+
) -> str:
|
|
154
|
+
|
|
155
|
+
if user_at_host:
|
|
156
|
+
return run_ssh_cmd(
|
|
157
|
+
user_at_host=user_at_host,
|
|
158
|
+
cmd=cmd,
|
|
159
|
+
cwd=cwd,
|
|
160
|
+
outfile=outfile,
|
|
161
|
+
shell=shell,
|
|
162
|
+
text=text,
|
|
163
|
+
capture_output=capture_output,
|
|
164
|
+
password=password,
|
|
165
|
+
key=key,
|
|
166
|
+
env_path=env_path
|
|
167
|
+
)
|
|
168
|
+
return run_local_cmd(
|
|
169
|
+
cmd=cmd,
|
|
170
|
+
cwd=cwd,
|
|
171
|
+
outfile=outfile,
|
|
172
|
+
shell=shell,
|
|
173
|
+
text=text,
|
|
174
|
+
capture_output=capture_output,
|
|
175
|
+
password=password,
|
|
176
|
+
key=key,
|
|
177
|
+
env_path=env_path
|
|
178
|
+
)
|
|
179
|
+
run_ssh_cmd = run_remote_cmd
|
|
180
|
+
remote_cmd = run_remote_cmd
|
|
181
|
+
ssh_cmd = run_remote_cmd
|
|
182
|
+
|
|
183
|
+
local_cmd = run_local_cmd
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
run_any_cmd = run_cmd
|
|
187
|
+
any_cmd = run_cmd
|
|
188
|
+
cmd_run = run_cmd
|
|
@@ -22,6 +22,8 @@ Date: 05/31/2023
|
|
|
22
22
|
Version: 0.1.2
|
|
23
23
|
"""
|
|
24
24
|
import os
|
|
25
|
+
from .list_utils import make_list
|
|
26
|
+
from .type_utils import get_alpha_ints
|
|
25
27
|
def quoteIt(st: str, ls: list) -> str:
|
|
26
28
|
"""
|
|
27
29
|
Quotes specific elements in a string.
|
|
@@ -110,6 +112,42 @@ def eatAll(string: str, list_objects:(str or list)) -> any:
|
|
|
110
112
|
if string and list_objects:
|
|
111
113
|
string = eatOuter(string, list_objects)
|
|
112
114
|
return string
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def eatElse(
|
|
119
|
+
stringObj,
|
|
120
|
+
chars=None,
|
|
121
|
+
ints=True,
|
|
122
|
+
alpha=True,
|
|
123
|
+
lower=True,
|
|
124
|
+
capitalize=True,
|
|
125
|
+
string=True,
|
|
126
|
+
listObj=True
|
|
127
|
+
):
|
|
128
|
+
alpha_ints = get_alpha_ints(
|
|
129
|
+
ints=True,
|
|
130
|
+
alpha=True,
|
|
131
|
+
lower=True,
|
|
132
|
+
capitalize=True,
|
|
133
|
+
string=True,
|
|
134
|
+
listObj=True
|
|
135
|
+
)
|
|
136
|
+
chars = make_list(chars or [])+alpha_ints
|
|
137
|
+
|
|
138
|
+
while True:
|
|
139
|
+
if stringObj:
|
|
140
|
+
str_0 = stringObj[0] not in chars
|
|
141
|
+
str_1 = stringObj[-1] not in chars
|
|
142
|
+
str_eat = str_0 or str_1
|
|
143
|
+
if not str_eat:
|
|
144
|
+
return stringObj
|
|
145
|
+
if stringObj and str_0:
|
|
146
|
+
stringObj = stringObj[1:] if len(stringObj) !=1 else ""
|
|
147
|
+
if stringObj and str_1:
|
|
148
|
+
stringObj = stringObj[:-1] if len(stringObj) !=1 else ""
|
|
149
|
+
else:
|
|
150
|
+
return stringObj
|
|
113
151
|
def safe_split(obj, ls):
|
|
114
152
|
"""
|
|
115
153
|
Safely splits a string using multiple delimiters.
|
|
@@ -185,6 +223,7 @@ def url_join(*paths):
|
|
|
185
223
|
final_path = f"{final_path}/{path}"
|
|
186
224
|
return final_path
|
|
187
225
|
|
|
188
|
-
|
|
226
|
+
def clean_line(line):
|
|
227
|
+
return eatAll(line,[' ','','\t','\n'])
|
|
189
228
|
def capitalize(string):
|
|
190
229
|
return string[:1].upper() + string[1:].lower() if string else string
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from .list_utils import make_list
|
|
2
|
+
def get_from_kwargs(*args,**kwargs):
|
|
3
|
+
del_kwargs = kwargs.get('del_kwargs',False)
|
|
4
|
+
values = {}
|
|
5
|
+
for key in args:
|
|
6
|
+
if key:
|
|
7
|
+
key = str(key)
|
|
8
|
+
if key in kwargs:
|
|
9
|
+
values[key] = kwargs.get(key)
|
|
10
|
+
if del_kwarg:
|
|
11
|
+
del kwargs[key]
|
|
12
|
+
return values,kwargs
|
|
13
|
+
|
|
14
|
+
def replace_it(string,item,rep):
|
|
15
|
+
if item in string:
|
|
16
|
+
string = string.replace(item,rep)
|
|
17
|
+
return string
|
|
18
|
+
def while_replace(string,item,rep):
|
|
19
|
+
while True:
|
|
20
|
+
string = replace_it(string,item,rep)
|
|
21
|
+
if item not in string or item in rep:
|
|
22
|
+
return string
|
|
23
|
+
def for_replace(string,item,replace):
|
|
24
|
+
replace = make_list(replace)
|
|
25
|
+
for rep in replace:
|
|
26
|
+
string = while_replace(string,item,rep)
|
|
27
|
+
return string
|
|
28
|
+
def replace_all(string,*args,**kwargs):
|
|
29
|
+
for items in args:
|
|
30
|
+
if items and isinstance(items,list):
|
|
31
|
+
item = items[0]
|
|
32
|
+
replace = items[1:] if len(items)>1 else items[-1]
|
|
33
|
+
string = for_replace(string,item,replace)
|
|
34
|
+
values,kwargs = get_from_kwargs('item','replace',**kwargs)
|
|
35
|
+
if values:
|
|
36
|
+
string = for_replace(string,**values)
|
|
37
|
+
for item,replace in kwargs.items():
|
|
38
|
+
string = for_replace(string,item,rep)
|
|
39
|
+
return string
|
|
40
|
+
def get_lines(string,strip=True):
|
|
41
|
+
lines = string.split('\n')
|
|
42
|
+
if strip:
|
|
43
|
+
lines = [line for line in lines if line]
|
|
44
|
+
return lines
|
|
45
|
+
def get_alpha():
|
|
46
|
+
return 'abcdefghijklmnopqrstuvwxyz'
|
|
47
|
+
def is_alpha(char,case_sensative=False):
|
|
48
|
+
alphas = get_alpha()
|
|
49
|
+
if not case_sensative:
|
|
50
|
+
alphas+=alphas.upper()
|
|
51
|
+
return char in alphas
|
abstract_utilities/type_utils.py
CHANGED
|
@@ -59,8 +59,8 @@ Version: 0.1.2
|
|
|
59
59
|
import os
|
|
60
60
|
from pathlib import Path
|
|
61
61
|
from typing import Union
|
|
62
|
-
from .path_utils import get_all_item_paths,get_files
|
|
63
62
|
from .list_utils import make_list
|
|
63
|
+
|
|
64
64
|
# A big, but by no means exhaustive, map of extensions to mime‐types by category:
|
|
65
65
|
MIME_TYPES = {
|
|
66
66
|
'image': {
|
|
@@ -937,7 +937,30 @@ def is_any_instance(value):
|
|
|
937
937
|
for each in [dict, list, int, float]:
|
|
938
938
|
if is_instance(value, each):
|
|
939
939
|
return True
|
|
940
|
-
|
|
940
|
+
def getAlphas(lower=True,capitalize=False,listObj=False):
|
|
941
|
+
obj = ''
|
|
942
|
+
alphas = 'abcdefghijklmoprstuvwxyz'
|
|
943
|
+
if lower:
|
|
944
|
+
obj+=alphas
|
|
945
|
+
if capitalize:
|
|
946
|
+
obj+=alphas.upper()
|
|
947
|
+
if listObj:
|
|
948
|
+
obj = list(obj)
|
|
949
|
+
return obj
|
|
950
|
+
def getInts(string=False,listObj=False):
|
|
951
|
+
obj=12345678909
|
|
952
|
+
if string:
|
|
953
|
+
obj = str(obj)
|
|
954
|
+
if listObj:
|
|
955
|
+
obj = list(obj)
|
|
956
|
+
return obj
|
|
957
|
+
def get_alpha_ints(ints=True,alpha=True,lower=True,capitalize=True,string=True,listObj=True):
|
|
958
|
+
objs = [] if listObj else ""
|
|
959
|
+
if ints:
|
|
960
|
+
objs+=getInts(string=string,listObj=listObj)
|
|
961
|
+
if alpha:
|
|
962
|
+
objs+=getAlphas(lower=lower,capitalize=capitalize,listObj=listObj)
|
|
963
|
+
return objs
|
|
941
964
|
# Function: is_number
|
|
942
965
|
# Function: is_str
|
|
943
966
|
# Function: is_int
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: abstract_utilities
|
|
3
|
-
Version: 0.2.2.
|
|
3
|
+
Version: 0.2.2.480
|
|
4
4
|
Summary: abstract_utilities is a collection of utility modules providing a variety of functions to aid in tasks such as data comparison, list manipulation, JSON handling, string manipulation, mathematical computations, and time operations.
|
|
5
5
|
Home-page: https://github.com/AbstractEndeavors/abstract_utilities
|
|
6
6
|
Author: putkoff
|