summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMalfurious <m@lfurio.us>2025-01-03 09:59:53 -0500
committerMalfurious <m@lfurio.us>2025-01-04 23:54:51 -0500
commitd4aacb45a070f05eddb5fbfec191b440e381e268 (patch)
tree3b06b2f7e8c1efbace542beb4f8665dd196cc292
parent6d1418c28161c51c6a0649207c51e9b03c7d197a (diff)
downloadnsploit-d4aacb45a070f05eddb5fbfec191b440e381e268.tar.gz
nsploit-d4aacb45a070f05eddb5fbfec191b440e381e268.zip
Don't rely on git for version information
The version information for the nsploit distribution is now defined in pyproject.toml, and we no longer attempt to obtain details via git-describe. As previously, the library version is made available via `nsploit.__version__`. The main benefit of working this way is to allow a proper install of nsploit from a simple tarball of the source code, or even a shallow git clone. In each of these cases, tag information will not usually be present. As an added feature over the previous system, nsploit will now report in its version string if the running version is from a source tree, rather than an installed copy. Signed-off-by: Malfurious <m@lfurio.us>
-rw-r--r--hooks/bake_version.py26
-rw-r--r--nsploit/__init__.py17
-rw-r--r--nsploit/util/cmd.py12
-rw-r--r--pyproject.toml11
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"