From bf62abff052e242f2d40033da4f2875d3cfa90c5 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Tue, 9 Sep 2025 12:47:52 -0500 Subject: [PATCH] Minor refactors --- linux/python/dep_mgr.py | 29 ------------------- linux/python/make_random.py | 36 ++--------------------- linux/python/new-uuid.py | 21 ++++++++++++-- linux/python/scan_jars.py | 23 ++++++--------- linux/python/utils.py | 54 +++++++++++++++++++++++++++++++++++ linux/python/wifi_qr.py | 9 +++--- linux/setup/setup_jarscan.sh | 1 + linux/setup/setup_makerand.sh | 1 + linux/setup/setup_uuid.sh | 1 + linux/setup/setup_wifiqr.sh | 27 ++++++++++++++++++ 10 files changed, 118 insertions(+), 84 deletions(-) delete mode 100644 linux/python/dep_mgr.py create mode 100644 linux/python/utils.py create mode 100755 linux/setup/setup_wifiqr.sh diff --git a/linux/python/dep_mgr.py b/linux/python/dep_mgr.py deleted file mode 100644 index 64b1458..0000000 --- a/linux/python/dep_mgr.py +++ /dev/null @@ -1,29 +0,0 @@ -# https://bootstrap.pypa.io/get-pip.py - -import os -import sys -import tempfile - -import requests - -PYTHON = sys.executable - - -def _prereq() -> None: - res = os.system(f"{PYTHON} -m pip") - if res == 0: - return - - data = requests.get("https://bootstrap.pypa.io/get-pip.py") - filename = tempfile.mktemp() - with open(filename, "w", encoding="utf-8") as fh: - fh.write(data.text) - - os.system(f"{PYTHON} {filename}") - os.unlink(filename) - - -def install_package(package_name: str) -> None: - _prereq() - os.system(f"{PYTHON} -m pip install {package_name}") - print(f"Package {package_name} has been installed - please rerun this command!") diff --git a/linux/python/make_random.py b/linux/python/make_random.py index 2019c66..0e575cf 100644 --- a/linux/python/make_random.py +++ b/linux/python/make_random.py @@ -1,39 +1,7 @@ from argparse import ArgumentParser -from os import unlink -from platform import release, system from random import randint -from subprocess import check_call -from tempfile import mktemp from uuid import uuid4 - - -def copy_to_clipboard(val: str) -> bool: - sys = system() - copy_cmd = "" - if sys == "Windows": - copy_cmd = "clip.exe" - elif sys == "macOS": - copy_cmd = "pbcopy" - elif sys == "Linux": - if "WSL" in release(): - copy_cmd = "clip.exe" # Windows native clip.exe still works in WSL - else: - copy_cmd = "xclip -selection clipboard" - else: - print(f"System {sys} is not supported - cannot copy to clipboard") - return False - - tmp_path = mktemp() - with open(tmp_path, "w", encoding="utf-8") as fh: - fh.write(val.strip()) - - try: - ret_val = check_call(f"cat {str(tmp_path)} | {copy_cmd}", shell=True) - finally: - unlink(tmp_path) - - return ret_val - +from utils import copy_to_clipboard def generate_uuid(length: int, current="") -> str: if len(current) >= length: @@ -69,7 +37,7 @@ def main(args) -> None: print(accumulator) else: exclusions = list(map(lambda x: str(x).strip(), args.exclude_chars.split(","))) - char_choices = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmonpqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}\|;:,<.>/?`~" + char_choices = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmonpqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}\\|;:,<.>/?`~" for exclusion in exclusions: char_choices = char_choices.replace(exclusion, "") diff --git a/linux/python/new-uuid.py b/linux/python/new-uuid.py index 8f7c232..aa6e974 100644 --- a/linux/python/new-uuid.py +++ b/linux/python/new-uuid.py @@ -1,2 +1,19 @@ -import uuid -print(uuid.uuid4()) +from argparse import ArgumentParser +from uuid import uuid4 + +from utils import copy_to_clipboard + + +def main(args): + if args.copy_to_clipboard: + copy_to_clipboard(str(uuid4())) + print("Copied to clipboard!") + else: + print(str(uuid4())) + + +if __name__ == "__main__": + parser = ArgumentParser(prog="new-uuid") + parser.add_argument("-c", "--copy-to-clipboard", action="store_true") + args = parser.parse_args() + main(args) diff --git a/linux/python/scan_jars.py b/linux/python/scan_jars.py index b82956a..4e97614 100644 --- a/linux/python/scan_jars.py +++ b/linux/python/scan_jars.py @@ -1,10 +1,9 @@ -import argparse import os import re -import sys -import zipfile - +from argparse import ArgumentParser from pathlib import Path +from sys import exit +from zipfile import ZipFile def scan_jar(jar_path: Path, args) -> None: @@ -32,14 +31,12 @@ def scan_jar(jar_path: Path, args) -> None: try: return re.compile(args.query).search(scan_target) is not None except re.error as err: - print( - f"Regex Error! Invalid regular expression passed:\nFull Trace:\n{err}" - ) - sys.exit(1) + print(f"Regex Error! Invalid regular expression passed:\nFull Trace:\n{err}") + exit(1) else: return args.query in scan_target - with zipfile.ZipFile(jar_path, "r") as jar: + with ZipFile(jar_path, "r") as jar: for file_name in jar.namelist(): if args.deep: # Perform a deep scan of the jar itself with jar.open(file_name) as file: @@ -62,12 +59,8 @@ def main(args) -> None: if __name__ == "__main__": - parser = argparse.ArgumentParser( - prog="Jar Scanner", description="Scans jars for a given file" - ) - parser.add_argument( - "query", help="The term (or regex) to search for within the filename" - ) + parser = ArgumentParser(prog="Jar Scanner", description="Scans jars for a given file") + parser.add_argument("query", help="The term (or regex) to search for within the filename") parser.add_argument( "-d", "--dir", diff --git a/linux/python/utils.py b/linux/python/utils.py new file mode 100644 index 0000000..8426f3d --- /dev/null +++ b/linux/python/utils.py @@ -0,0 +1,54 @@ +import os +from platform import release, system +from subprocess import check_call +from sys import executable as python +from tempfile import mktemp + +from requests import get as fetch + + +def copy_to_clipboard(val: str) -> bool: + sys = system() + copy_cmd = "" + if sys == "Windows": + copy_cmd = "clip.exe" + elif sys == "macOS": + copy_cmd = "pbcopy" + elif sys == "Linux": + if "WSL" in release(): + copy_cmd = "clip.exe" # Windows native clip.exe still works in WSL + else: + copy_cmd = "xclip -selection clipboard" + else: + print(f"System {sys} is not supported - cannot copy to clipboard") + return False + + tmp_path = mktemp() + with open(tmp_path, "w", encoding="utf-8") as fh: + fh.write(val.strip()) + + try: + ret_val = check_call(f"cat {str(tmp_path)} | {copy_cmd}", shell=True) + finally: + os.unlink(tmp_path) + + return ret_val == 0 + + +def install_package(package_name: str) -> None: + def _prereq() -> None: + res = os.system(f"{python} -m pip") + if res == 0: + return + + data = fetch("https://bootstrap.pypa.io/get-pip.py") + filename = mktemp() + with open(filename, "w", encoding="utf-8") as fh: + fh.write(data.text) + + os.system(f"{python} {filename}") + os.unlink(filename) + + _prereq() + os.system(f"{python} -m pip install {package_name}") + print(f"Package {package_name} has been installed - please rerun this command!") diff --git a/linux/python/wifi_qr.py b/linux/python/wifi_qr.py index b8cbe34..5255a3e 100644 --- a/linux/python/wifi_qr.py +++ b/linux/python/wifi_qr.py @@ -1,10 +1,11 @@ -import argparse -import dep_mgr +from argparse import ArgumentParser + +from utils import install_package try: import qrcode except ImportError: - dep_mgr.install_package("qrcode[pil]") + install_package("qrcode[pil]") finally: import qrcode @@ -15,7 +16,7 @@ def main(args): if __name__ == "__main__": - parser = argparse.ArgumentParser(prog="WiFi QR Generator", description="Creates QR Codes to join a Password Protected WiFi Network") + parser = ArgumentParser(prog="WiFi QR Generator", description="Creates QR Codes to join a Password Protected WiFi Network") parser.add_argument("ssid", help="The SSID of the WiFi Network to connect to") parser.add_argument("passwd", help="The passphrase of the WiFi Network to connect to") parser.add_argument("-o", "--output", required=True, help="Where to save the resulting image to") diff --git a/linux/setup/setup_jarscan.sh b/linux/setup/setup_jarscan.sh index ffe005c..1e82245 100755 --- a/linux/setup/setup_jarscan.sh +++ b/linux/setup/setup_jarscan.sh @@ -17,6 +17,7 @@ fi mkdir -p /opt/scripts cp "../python/$script_fn" "/opt/scripts/$script_fn" +cp "../python/utils.py" "/opt/scripts/utils.py" # Create executable as a script, install to local/bin echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name diff --git a/linux/setup/setup_makerand.sh b/linux/setup/setup_makerand.sh index 174ffd4..f3ab741 100755 --- a/linux/setup/setup_makerand.sh +++ b/linux/setup/setup_makerand.sh @@ -17,6 +17,7 @@ fi mkdir -p /opt/scripts cp "../python/$script_fn" "/opt/scripts/$script_fn" +cp "../python/utils.py" "/opt/scripts/utils.py" # Create executable as a script, install to local/bin echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name diff --git a/linux/setup/setup_uuid.sh b/linux/setup/setup_uuid.sh index 9226f07..c643d57 100755 --- a/linux/setup/setup_uuid.sh +++ b/linux/setup/setup_uuid.sh @@ -17,6 +17,7 @@ fi mkdir -p /opt/scripts cp "../python/$script_fn" "/opt/scripts/$script_fn" +cp "../python/utils.py" "/opt/scripts/utils.py" # Create executable as a script, install to local/bin echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name diff --git a/linux/setup/setup_wifiqr.sh b/linux/setup/setup_wifiqr.sh new file mode 100755 index 0000000..38c4ac7 --- /dev/null +++ b/linux/setup/setup_wifiqr.sh @@ -0,0 +1,27 @@ +#!/bin/bash +cmd_name="wifi-qr" +script_fn="wifi_qr.py" + +# Re-run the script as root if we're not +if [ "$EUID" != 0 ]; then + sudo "$0" "$@" + exit $? +fi + +echo "Setting up $cmd_name" +if ! command -v /usr/bin/python3 2>&1 >/dev/null +then + echo "/usr/bin/python3 does not exist! Please install it before continuing..." + exit 1 +fi + +mkdir -p /opt/scripts +cp "../python/$script_fn" "/opt/scripts/$script_fn" +cp "../python/utils.py" "/opt/scripts/utils.py" + +# Create executable as a script, install to local/bin +echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name +chmod +x $cmd_name +mv $cmd_name /usr/local/bin + +echo "Done! Try the command '$cmd_name' to see the effects!"