binaryninja-finder 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,59 @@
1
+ Metadata-Version: 2.4
2
+ Name: binaryninja-finder
3
+ Version: 0.3.0
4
+ Summary: Find and install Binary Ninja's Python API
5
+ License-Expression: MIT
6
+ License-File: LICENSE
7
+ Requires-Python: >=3.9
8
+ Project-URL: Issues, https://github.com/trailofbits/binaryninja-finder/issues
9
+ Project-URL: Source, https://github.com/trailofbits/binaryninja-finder
10
+ Description-Content-Type: text/markdown
11
+
12
+ # binaryninja-finder
13
+
14
+ Finds a local Binary Ninja installation so you can `import binaryninja` in Python. Running outside the GUI requires a [license that supports headless use](https://docs.binary.ninja/dev/batch.html).
15
+
16
+ ## Usage
17
+
18
+ Add it to a project.
19
+
20
+ ```console
21
+ uv add binaryninja-finder
22
+ ```
23
+
24
+ Try it without adding a project dependency.
25
+
26
+ ```console
27
+ uv run --with binaryninja-finder python -c 'import binaryninja; print(binaryninja.core_version())'
28
+ ```
29
+
30
+ ## Standalone script
31
+
32
+ Save this as `example.py`.
33
+
34
+ ```python
35
+ #!/usr/bin/env -S uv run --script
36
+ # /// script
37
+ # requires-python = ">=3.10"
38
+ # dependencies = ["binaryninja-finder"]
39
+ # ///
40
+
41
+ import binaryninja
42
+
43
+ print(binaryninja.core_version())
44
+ ```
45
+
46
+ Run the script with `uv`.
47
+
48
+ ```console
49
+ uv run --script example.py
50
+ ```
51
+
52
+ On systems that support shebangs, you can also run `chmod +x example.py` and then `./example.py`.
53
+
54
+ ## Configuration
55
+
56
+ - Set `BN_INSTALL_DIR` to your Binary Ninja installation path if automatic discovery misses it.
57
+ - Set `BN_USER_DIRECTORY` if you use a custom Binary Ninja settings directory.
58
+
59
+ [MIT License](LICENSE)
@@ -0,0 +1,48 @@
1
+ # binaryninja-finder
2
+
3
+ Finds a local Binary Ninja installation so you can `import binaryninja` in Python. Running outside the GUI requires a [license that supports headless use](https://docs.binary.ninja/dev/batch.html).
4
+
5
+ ## Usage
6
+
7
+ Add it to a project.
8
+
9
+ ```console
10
+ uv add binaryninja-finder
11
+ ```
12
+
13
+ Try it without adding a project dependency.
14
+
15
+ ```console
16
+ uv run --with binaryninja-finder python -c 'import binaryninja; print(binaryninja.core_version())'
17
+ ```
18
+
19
+ ## Standalone script
20
+
21
+ Save this as `example.py`.
22
+
23
+ ```python
24
+ #!/usr/bin/env -S uv run --script
25
+ # /// script
26
+ # requires-python = ">=3.10"
27
+ # dependencies = ["binaryninja-finder"]
28
+ # ///
29
+
30
+ import binaryninja
31
+
32
+ print(binaryninja.core_version())
33
+ ```
34
+
35
+ Run the script with `uv`.
36
+
37
+ ```console
38
+ uv run --script example.py
39
+ ```
40
+
41
+ On systems that support shebangs, you can also run `chmod +x example.py` and then `./example.py`.
42
+
43
+ ## Configuration
44
+
45
+ - Set `BN_INSTALL_DIR` to your Binary Ninja installation path if automatic discovery misses it.
46
+ - Set `BN_USER_DIRECTORY` if you use a custom Binary Ninja settings directory.
47
+
48
+ [MIT License](LICENSE)
@@ -1,3 +1,4 @@
1
+ # Fallback for Python versions before 3.15. Keep the basename paired with bn_finder.start.
1
2
  # Keep this filename alphabetically after binaryninja_finder.pth. Editable
2
3
  # installs need that file to add the module to sys.path before this hook runs.
3
4
  import binaryninja_finder; binaryninja_finder._setup()
@@ -0,0 +1 @@
1
+ binaryninja_finder:_setup
@@ -0,0 +1,19 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.13.12,<0.14"]
3
+ build-backend = "uv_build"
4
+
5
+ [project]
6
+ name = "binaryninja-finder"
7
+ version = "0.3.0"
8
+ description = "Find and install Binary Ninja's Python API"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ license-files = ["LICENSE"]
12
+ requires-python = ">=3.9"
13
+
14
+ [project.urls]
15
+ Issues = "https://github.com/trailofbits/binaryninja-finder/issues"
16
+ Source = "https://github.com/trailofbits/binaryninja-finder"
17
+
18
+ [tool.uv.build-backend.data]
19
+ purelib = "_pth"
@@ -1,10 +1,10 @@
1
1
  [build-system]
2
- requires = ["uv_build>=0.11.28,<0.12.0"]
2
+ requires = ["uv_build>=0.13.12,<0.14"]
3
3
  build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "binaryninja-finder"
7
- version = "0.2.0"
7
+ version = "0.3.0"
8
8
  description = "Find and install Binary Ninja's Python API"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -27,35 +27,40 @@ def _common_paths() -> list[Path]:
27
27
  if sys.platform == "darwin":
28
28
  return [Path("/Applications/Binary Ninja.app/Contents/Resources/python")]
29
29
  if sys.platform == "win32":
30
- return [Path("C:/Program Files/Vector35/BinaryNinja/python")]
30
+ paths = [Path("C:/Program Files/Vector35/BinaryNinja/python")]
31
+ local_appdata = os.environ.get("LOCALAPPDATA")
32
+ if local_appdata:
33
+ paths.append(Path(local_appdata) / "Vector35" / "BinaryNinja" / "python")
34
+ return paths
31
35
  return []
32
36
 
33
37
 
34
38
  def _python_dir(install_dir: Path) -> Path:
35
39
  if sys.platform == "darwin":
40
+ # Binary Ninja records the executable directory in lastrun.
41
+ if install_dir.parts[-2:] == ("Contents", "MacOS"):
42
+ install_dir = install_dir.parent.parent
36
43
  return install_dir / "Contents" / "Resources" / "python"
37
44
  return install_dir / "python"
38
45
 
39
46
 
40
47
  def _find_binary_ninja(*, validate: bool = True) -> Path | None:
41
48
  """Find the Binary Ninja Python API directory."""
42
- candidates: list[Path] = []
43
-
44
49
  env_install = os.environ.get("BN_INSTALL_DIR")
45
50
  if env_install:
46
- p = Path(env_install)
47
- candidates.append(_python_dir(p))
51
+ path = _python_dir(Path(env_install))
52
+ if not validate or (path / "binaryninja" / "__init__.py").is_file():
53
+ return path
48
54
 
49
55
  try:
50
- install_dir = Path((_user_dir() / "lastrun").read_text().strip())
51
- candidates.append(_python_dir(install_dir))
52
- except OSError:
56
+ last_run = (_user_dir() / "lastrun").read_text().strip()
57
+ if last_run and "\0" not in last_run:
58
+ path = _python_dir(Path(last_run))
59
+ if not validate or (path / "binaryninja" / "__init__.py").is_file():
60
+ return path
61
+ except (OSError, UnicodeDecodeError):
53
62
  pass
54
63
 
55
- for path in candidates:
56
- if not validate or (path / "binaryninja" / "__init__.py").is_file():
57
- return path
58
-
59
64
  for path in _common_paths():
60
65
  if (path / "binaryninja" / "__init__.py").is_file():
61
66
  return path
@@ -64,7 +69,9 @@ def _find_binary_ninja(*, validate: bool = True) -> Path | None:
64
69
 
65
70
 
66
71
  def _setup() -> None:
67
- if importlib.util.find_spec("binaryninja") is not None:
72
+ spec = importlib.util.find_spec("binaryninja")
73
+ # An unimported namespace package has no loader and can yield to the SDK.
74
+ if spec is not None and spec.loader is not None:
68
75
  return
69
76
  if (path := _find_binary_ninja()) is not None:
70
- sys.path.append(str(path))
77
+ sys.path.append(str(path.absolute()))
@@ -1,49 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: binaryninja-finder
3
- Version: 0.2.0
4
- Summary: Find and install Binary Ninja's Python API
5
- License-Expression: MIT
6
- License-File: LICENSE
7
- Requires-Python: >=3.9
8
- Project-URL: Issues, https://github.com/trailofbits/binaryninja-finder/issues
9
- Project-URL: Source, https://github.com/trailofbits/binaryninja-finder
10
- Description-Content-Type: text/markdown
11
-
12
- # binaryninja-finder
13
-
14
- Automatically finds and adds Binary Ninja's Python API to `sys.path` at interpreter startup.
15
-
16
- ## Installation
17
-
18
- ```
19
- pip install git+https://github.com/trailofbits/binaryninja-finder
20
- ```
21
-
22
- Or with [uv](https://docs.astral.sh/uv/):
23
-
24
- ```
25
- uv add git+https://github.com/trailofbits/binaryninja-finder
26
- ```
27
-
28
- Once installed, `import binaryninja` will work in any script or REPL that uses the same Python environment.
29
-
30
- ## How it works
31
-
32
- The package installs a `.pth` file that runs automatically when Python starts. It checks whether `binaryninja` is already importable and, if not, searches for a local Binary Ninja installation and adds its `python/` directory to `sys.path`.
33
-
34
- The search order is:
35
-
36
- 1. **`BN_INSTALL_DIR`** environment variable — points to the BN installation root
37
- 2. **`lastrun` file** — in the BN user config directory (written by Binary Ninja on launch)
38
- 3. **Common platform paths** — e.g. `/Applications/Binary Ninja.app` on macOS, `/opt/binaryninja` on Linux, `C:\Program Files\Vector35\BinaryNinja` on Windows
39
-
40
- ## Configuration
41
-
42
- | Environment variable | Description |
43
- |---|---|
44
- | `BN_INSTALL_DIR` | Override the Binary Ninja installation directory |
45
- | `BN_USER_DIRECTORY` | Override the Binary Ninja user config directory (where `lastrun` is stored) |
46
-
47
- ## License
48
-
49
- See [LICENSE](LICENSE) for details.
@@ -1,38 +0,0 @@
1
- # binaryninja-finder
2
-
3
- Automatically finds and adds Binary Ninja's Python API to `sys.path` at interpreter startup.
4
-
5
- ## Installation
6
-
7
- ```
8
- pip install git+https://github.com/trailofbits/binaryninja-finder
9
- ```
10
-
11
- Or with [uv](https://docs.astral.sh/uv/):
12
-
13
- ```
14
- uv add git+https://github.com/trailofbits/binaryninja-finder
15
- ```
16
-
17
- Once installed, `import binaryninja` will work in any script or REPL that uses the same Python environment.
18
-
19
- ## How it works
20
-
21
- The package installs a `.pth` file that runs automatically when Python starts. It checks whether `binaryninja` is already importable and, if not, searches for a local Binary Ninja installation and adds its `python/` directory to `sys.path`.
22
-
23
- The search order is:
24
-
25
- 1. **`BN_INSTALL_DIR`** environment variable — points to the BN installation root
26
- 2. **`lastrun` file** — in the BN user config directory (written by Binary Ninja on launch)
27
- 3. **Common platform paths** — e.g. `/Applications/Binary Ninja.app` on macOS, `/opt/binaryninja` on Linux, `C:\Program Files\Vector35\BinaryNinja` on Windows
28
-
29
- ## Configuration
30
-
31
- | Environment variable | Description |
32
- |---|---|
33
- | `BN_INSTALL_DIR` | Override the Binary Ninja installation directory |
34
- | `BN_USER_DIRECTORY` | Override the Binary Ninja user config directory (where `lastrun` is stored) |
35
-
36
- ## License
37
-
38
- See [LICENSE](LICENSE) for details.