mirror of
https://github.com/rwinkhart/sshyp.git
synced 2026-09-06 00:57:14 -04:00
Use tempfile.NamedTemporaryFile() for editing notes, removing the need for shm_gen()
Former-commit-id: 2d2e733945ba2748a24ed93f861145cf26ac1f13 Former-commit-id: e3b5add08675438e1a08607d081a9a44f426425e
This commit is contained in:
+2
-23
@@ -1,34 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
import re
|
||||
from sys import argv, exit as s_exit
|
||||
|
||||
# read arguments
|
||||
arguments, replacement = argv[1:], None
|
||||
|
||||
# define replacement text depending on arguments
|
||||
if len(arguments) > 0:
|
||||
if arguments[0] == 'TMP':
|
||||
replacement = 'symlink("/tmp", f"{home}/.config/sshyp/tmp")'
|
||||
elif arguments[0] == 'TERMUX':
|
||||
replacement = 'symlink("/data/data/com.termux/files/usr/tmp", f"{home}/.config/sshyp/tmp")\n'
|
||||
elif arguments[0] == 'LINUX':
|
||||
replacement = 'symlink("/dev/shm", f"{home}/.config/sshyp/tmp")'
|
||||
else:
|
||||
s_exit()
|
||||
|
||||
targets = (('UNAME-IMPORT-SSHYP', 'sshyp.py', ''), ('UNAME-IMPORT-STWEAK', 'stweak.py', ''),
|
||||
('UNAME-TMP', 'stweak.py', replacement))
|
||||
targets = (('UNAME-IMPORT-SSHYP', 'sshyp.py', ''), ('UNAME-IMPORT-STWEAK', 'stweak.py', ''))
|
||||
|
||||
for target in targets:
|
||||
# read text from target file
|
||||
text, special = open(f"working/{target[1]}", 'r').read(), ''
|
||||
# determine text to end regex with
|
||||
if target[2] == '':
|
||||
regend = '\n'
|
||||
else:
|
||||
regend = ''
|
||||
# compile regex and modify text
|
||||
regex = re.compile(f"# PORT START {target[0]}.*?# PORT END {target[0]}{regend}", re.DOTALL)
|
||||
regex = re.compile(f"# PORT START {target[0]}.*?# PORT END {target[0]}\n", re.DOTALL)
|
||||
new_text = re.sub(regex, target[2], text)
|
||||
# write updated text to target file
|
||||
open(f"working/{target[1]}", 'w').write(new_text)
|
||||
|
||||
Reference in New Issue
Block a user