From ead4ec1340555569e00919891383e05dca839b01 Mon Sep 17 00:00:00 2001 From: Malfurious Date: Fri, 12 Jan 2024 14:28:26 -0500 Subject: util: Promote from module to package We would like to move additional modules under the namespace of "util" to clean up the top-level "sploit" package. To start, the functions from the previous util module are moved. Given the package is named "util" the module is renamed to "cmd" to somewhat match the theme of the contained functions. Per the previous commits, these functions are now exposed via the util package as well. Signed-off-by: Malfurious --- sploit/util/__init__.py | 1 + sploit/util/cmd.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 sploit/util/__init__.py create mode 100644 sploit/util/cmd.py (limited to 'sploit/util') diff --git a/sploit/util/__init__.py b/sploit/util/__init__.py new file mode 100644 index 0000000..9103509 --- /dev/null +++ b/sploit/util/__init__.py @@ -0,0 +1 @@ +from .cmd import * diff --git a/sploit/util/cmd.py b/sploit/util/cmd.py new file mode 100644 index 0000000..3a2b842 --- /dev/null +++ b/sploit/util/cmd.py @@ -0,0 +1,26 @@ +from os import path +from subprocess import run + +def run_cmd(cmd,cwd=None): + return run(cmd,cwd=cwd,capture_output=True,text=True,check=True).stdout.split('\n')[:-1] + +__RUN_CACHE__ = {} +def run_cmd_cached(cmd,cwd=None): + key = ''.join(cmd) + if key in __RUN_CACHE__: + return __RUN_CACHE__[key] + else: + result = run_cmd(cmd,cwd) + __RUN_CACHE__[key] = result + return result + +#try to get the version through git +def git_version(): + try: + cwd = path.dirname(path.realpath(__file__)) + version = run_cmd(["git","describe","--always","--first-parent","--dirty"],cwd=cwd)[0] + #PEP440 compliance + version = version.replace('-','+',1).replace('-','.') + return version + except: + return "0+unknown.version" -- cgit v1.2.3 From 074a15310b8bbeeeeb00bf5ab5877c12f1ca1861 Mon Sep 17 00:00:00 2001 From: Malfurious Date: Fri, 12 Jan 2024 16:26:03 -0500 Subject: log: Move to sploit.util package Signed-off-by: Malfurious --- sploit/util/__init__.py | 1 + sploit/util/log.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 sploit/util/log.py (limited to 'sploit/util') diff --git a/sploit/util/__init__.py b/sploit/util/__init__.py index 9103509..32a079b 100644 --- a/sploit/util/__init__.py +++ b/sploit/util/__init__.py @@ -1 +1,2 @@ from .cmd import * +from .log import * diff --git a/sploit/util/log.py b/sploit/util/log.py new file mode 100644 index 0000000..823b252 --- /dev/null +++ b/sploit/util/log.py @@ -0,0 +1,32 @@ +import codecs +import sys + +# https://docs.python.org/3/library/codecs.html#standard-encodings +ENCODING = None + +ERROR = 31 +WARNING = 33 +STATUS = 32 +NORMAL = 0 +ALT = 90 + +def enc_value(value, enc): + if type(value) is bytes: + if enc is not None: + value = codecs.encode(value, enc) + elif ENCODING is not None: + value = codecs.encode(value, ENCODING) + value = str(value)[2:-1] # strip b'' + return str(value) + +def generic_log(*values, sep, end, file, flush, enc, color): + string = sep.join([ enc_value(x, enc) for x in values ]) + print(f'\033[{color}m{string}\033[0m', end=end, file=file, flush=flush) + +# For library internal use +def ilog(*values, sep=' ', end='\n', file=sys.stderr, flush=True, enc=None, color=STATUS): + generic_log(*values, sep=sep, end=end, file=file, flush=flush, enc=enc, color=color) + +# For external use in user script (via print = elog) +def elog(*values, sep=' ', end='\n', file=sys.stdout, flush=True, enc=None, color=ALT): + generic_log(*values, sep=sep, end=end, file=file, flush=flush, enc=enc, color=color) -- cgit v1.2.3