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 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, "")

View file

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

View file

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

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