Minor refactors

This commit is contained in:
oitsjustjose 2025-09-09 12:47:52 -05:00
commit bf62abff05
10 changed files with 118 additions and 84 deletions

View file

@ -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!")

View file

@ -1,39 +1,7 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from os import unlink
from platform import release, system
from random import randint from random import randint
from subprocess import check_call
from tempfile import mktemp
from uuid import uuid4 from uuid import uuid4
from utils import copy_to_clipboard
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
def generate_uuid(length: int, current="") -> str: def generate_uuid(length: int, current="") -> str:
if len(current) >= length: if len(current) >= length:
@ -69,7 +37,7 @@ def main(args) -> None:
print(accumulator) print(accumulator)
else: else:
exclusions = list(map(lambda x: str(x).strip(), args.exclude_chars.split(","))) exclusions = list(map(lambda x: str(x).strip(), args.exclude_chars.split(",")))
char_choices = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmonpqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}\|;:,<.>/?`~" char_choices = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmonpqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}\\|;:,<.>/?`~"
for exclusion in exclusions: for exclusion in exclusions:
char_choices = char_choices.replace(exclusion, "") char_choices = char_choices.replace(exclusion, "")

View file

@ -1,2 +1,19 @@
import uuid from argparse import ArgumentParser
print(uuid.uuid4()) 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)

View file

@ -1,10 +1,9 @@
import argparse
import os import os
import re import re
import sys from argparse import ArgumentParser
import zipfile
from pathlib import Path from pathlib import Path
from sys import exit
from zipfile import ZipFile
def scan_jar(jar_path: Path, args) -> None: def scan_jar(jar_path: Path, args) -> None:
@ -32,14 +31,12 @@ def scan_jar(jar_path: Path, args) -> None:
try: try:
return re.compile(args.query).search(scan_target) is not None return re.compile(args.query).search(scan_target) is not None
except re.error as err: except re.error as err:
print( print(f"Regex Error! Invalid regular expression passed:\nFull Trace:\n{err}")
f"Regex Error! Invalid regular expression passed:\nFull Trace:\n{err}" exit(1)
)
sys.exit(1)
else: else:
return args.query in scan_target 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(): for file_name in jar.namelist():
if args.deep: # Perform a deep scan of the jar itself if args.deep: # Perform a deep scan of the jar itself
with jar.open(file_name) as file: with jar.open(file_name) as file:
@ -62,12 +59,8 @@ def main(args) -> None:
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser( parser = ArgumentParser(prog="Jar Scanner", description="Scans jars for a given file")
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(
"query", help="The term (or regex) to search for within the filename"
)
parser.add_argument( parser.add_argument(
"-d", "-d",
"--dir", "--dir",

54
linux/python/utils.py Normal file
View file

@ -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!")

View file

@ -1,10 +1,11 @@
import argparse from argparse import ArgumentParser
import dep_mgr
from utils import install_package
try: try:
import qrcode import qrcode
except ImportError: except ImportError:
dep_mgr.install_package("qrcode[pil]") install_package("qrcode[pil]")
finally: finally:
import qrcode import qrcode
@ -15,7 +16,7 @@ def main(args):
if __name__ == "__main__": 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("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("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") parser.add_argument("-o", "--output", required=True, help="Where to save the resulting image to")

View file

@ -17,6 +17,7 @@ fi
mkdir -p /opt/scripts mkdir -p /opt/scripts
cp "../python/$script_fn" "/opt/scripts/$script_fn" 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 # Create executable as a script, install to local/bin
echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name

View file

@ -17,6 +17,7 @@ fi
mkdir -p /opt/scripts mkdir -p /opt/scripts
cp "../python/$script_fn" "/opt/scripts/$script_fn" 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 # Create executable as a script, install to local/bin
echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name

View file

@ -17,6 +17,7 @@ fi
mkdir -p /opt/scripts mkdir -p /opt/scripts
cp "../python/$script_fn" "/opt/scripts/$script_fn" 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 # Create executable as a script, install to local/bin
echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name echo "/usr/bin/python3 /opt/scripts/$script_fn \"\$@\"" > $cmd_name

27
linux/setup/setup_wifiqr.sh Executable file
View file

@ -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!"