diff options
-rw-r--r-- | hooks/bake_version.py | 26 | ||||
-rw-r--r-- | nsploit/__init__.py | 17 | ||||
-rw-r--r-- | nsploit/util/cmd.py | 12 | ||||
-rw-r--r-- | pyproject.toml | 11 |
4 files changed, 16 insertions, 50 deletions
diff --git a/hooks/bake_version.py b/hooks/bake_version.py deleted file mode 100644 index f4cd935..0000000 --- a/hooks/bake_version.py +++ /dev/null @@ -1,26 +0,0 @@ -from hatchling.builders.hooks.plugin.interface import BuildHookInterface - -import os -import re - -filename = os.path.normpath(os.path.join("nsploit","__init__.py")) - -#put the file back when the build ends -class RestoreVersionFile: - def __init__(self,contents): - self.contents = contents - def __del__(self): - with open(filename,"w") as f: - f.write(self.contents) - -class BakeVersionBuildHook(BuildHookInterface): - def initialize(self,version,build_data): - with open(filename,"r") as f: - self.restore = RestoreVersionFile(f.read()) - pattern = r'(?i)^__version__ *= *(?P<version>.+?)$' - match = re.search(pattern, self.restore.contents, flags=re.MULTILINE) - if not match: - raise ValueError("regex of version file failed") - span = match.span('version') - with open(filename,"w") as f: - f.write(f'{self.restore.contents[:span[0]]}"v{self.metadata.version}"{self.restore.contents[span[1]:]}') diff --git a/nsploit/__init__.py b/nsploit/__init__.py index e52883b..aef38c4 100644 --- a/nsploit/__init__.py +++ b/nsploit/__init__.py @@ -2,5 +2,18 @@ from nsploit.arch import * from nsploit.symtbl import * from nsploit.until import * -from nsploit.util import git_version as __git_version -__version__ = __git_version() +# Shout out: https://stackoverflow.com/questions/67085041 +def __extract_version(): + import importlib.metadata + from contextlib import suppress + from pathlib import Path + + with suppress(FileNotFoundError, StopIteration): + pyproject = Path(__file__).parent.parent / "pyproject.toml" + with open(pyproject, "r") as f: + option = [ line for line in f if line.startswith("version") ] + version = next(iter(option)).split("=")[1].strip("'\"\n ") + return f"{version}-uninstalled" + return importlib.metadata.version(__package__ or __name__) + +__version__ = f"v{__extract_version()}" diff --git a/nsploit/util/cmd.py b/nsploit/util/cmd.py index 3a2b842..5025a55 100644 --- a/nsploit/util/cmd.py +++ b/nsploit/util/cmd.py @@ -1,4 +1,3 @@ -from os import path from subprocess import run def run_cmd(cmd,cwd=None): @@ -13,14 +12,3 @@ def run_cmd_cached(cmd,cwd=None): 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" diff --git a/pyproject.toml b/pyproject.toml index b2f5a49..63c07ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ authors = [ {name="dusoleil",email="howcansocksbereal@gmail.com"}, {name="Malfurious",email="m@lfurio.us"}, ] -dynamic = ["version"] +version = "0.4.0" [project.urls] "Homepage" = "https://github.com/dusoleil/sploit" @@ -20,12 +20,3 @@ nsploit = "nsploit.__main__:main" [build-system] requires = ["hatchling"] build-backend = "hatchling.build" - -[tool.hatch.version] -source = "code" -path = "nsploit/__init__.py" -search-paths = ["."] -expression = "__version__" - -[tool.hatch.build.hooks.custom] -path = "hooks/bake_version.py" |