Fix bundled subbrute import fallback
This commit is contained in:
parent
729d649ec5
commit
d3b040c75a
21
sublist3r.py
21
sublist3r.py
|
|
@ -18,10 +18,29 @@ import json
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
|
|
||||||
# external modules
|
# external modules
|
||||||
from subbrute import subbrute
|
|
||||||
import dns.resolver
|
import dns.resolver
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
def load_subbrute_module():
|
||||||
|
"""Load the bundled subbrute module even if another package shadows it."""
|
||||||
|
try:
|
||||||
|
from subbrute import subbrute
|
||||||
|
return subbrute
|
||||||
|
except ImportError:
|
||||||
|
module_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'subbrute', 'subbrute.py')
|
||||||
|
if sys.version_info[0] >= 3:
|
||||||
|
import importlib.util
|
||||||
|
spec = importlib.util.spec_from_file_location('sublist3r_bundled_subbrute', module_path)
|
||||||
|
module = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(module)
|
||||||
|
return module
|
||||||
|
import imp
|
||||||
|
return imp.load_source('sublist3r_bundled_subbrute', module_path)
|
||||||
|
|
||||||
|
|
||||||
|
subbrute = load_subbrute_module()
|
||||||
|
|
||||||
# Python 2.x and 3.x compatiablity
|
# Python 2.x and 3.x compatiablity
|
||||||
if sys.version > '3':
|
if sys.version > '3':
|
||||||
import urllib.parse as urlparse
|
import urllib.parse as urlparse
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import textwrap
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class SubbruteImportTest(unittest.TestCase):
|
||||||
|
def test_import_uses_bundled_subbrute_when_external_package_shadows_it(self):
|
||||||
|
repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
|
shadow_package = os.path.join(temp_dir, "subbrute")
|
||||||
|
os.mkdir(shadow_package)
|
||||||
|
with open(os.path.join(shadow_package, "__init__.py"), "w", encoding="utf-8") as handle:
|
||||||
|
handle.write("# external package without a subbrute module\n")
|
||||||
|
|
||||||
|
command = textwrap.dedent(
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import sublist3r
|
||||||
|
|
||||||
|
bundled = os.path.join(os.path.dirname(sublist3r.__file__), "subbrute", "subbrute.py")
|
||||||
|
assert os.path.abspath(sublist3r.subbrute.__file__) == os.path.abspath(bundled)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
env = os.environ.copy()
|
||||||
|
env["PYTHONPATH"] = repo_root
|
||||||
|
result = subprocess.run(
|
||||||
|
[sys.executable, "-c", command],
|
||||||
|
cwd=temp_dir,
|
||||||
|
env=env,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(result.returncode, 0, result.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Loading…
Reference in New Issue