Skip to content

Commit 31a8c63

Browse files
authored
Merge pull request #1514 from maurosoria/fix/windows-session-path
Fix PyInstaller sessions path on Windows
2 parents 6e602d0 + 519de85 commit 31a8c63

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ Options:
180180
Number of threads
181181
--list-sessions List resumable sessions and exit
182182
--sessions-dir=PATH Directory to search for resumable sessions (default:
183-
current directory)
183+
dirsearch path /sessions, or $HOME/.dirsearch/sessions
184+
when bundled)
184185
--async Enable asynchronous mode
185186
-r, --recursive Brute-force recursively
186187
--deep-recursive Perform recursive scan on every directory depth (e.g.

lib/core/options.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
from __future__ import annotations
2020

21+
import os
2122
import sys
2223
import time
2324
from optparse import Values
2425
from typing import Any
2526
from lib.core.settings import (
2627
AUTHENTICATION_TYPES,
2728
COMMON_EXTENSIONS,
29+
DEFAULT_SESSION_DIR,
2830
DEFAULT_TOR_PROXIES,
2931
FILE_BASED_OUTPUT_FORMATS,
3032
SCRIPT_PATH,
@@ -40,12 +42,23 @@
4042
def parse_options() -> dict[str, Any]:
4143
opt = merge_config(parse_arguments())
4244

45+
def _session_debug(message: str) -> None:
46+
if not os.environ.get("DIRSEARCH_SESSIONS_DEBUG"):
47+
return
48+
try:
49+
sys.stderr.write(f"[sessions] {message}\n")
50+
sys.stderr.flush()
51+
except Exception:
52+
return
53+
4354
if opt.list_sessions:
4455
from lib.controller.session import SessionStore
4556

46-
base_dir = opt.sessions_dir or "."
57+
base_dir = opt.sessions_dir or DEFAULT_SESSION_DIR
58+
_session_debug(f"--list-sessions enabled base_dir={base_dir!r}")
4759
session_store = SessionStore({})
4860
sessions = session_store.list_sessions(base_dir)
61+
_session_debug(f"--list-sessions completed total={len(sessions)}")
4962

5063
if not sessions:
5164
print(f"No resumable sessions found in {base_dir}")
@@ -74,9 +87,11 @@ def parse_options() -> dict[str, Any]:
7487
if opt.session_id:
7588
from lib.controller.session import SessionStore
7689

77-
base_dir = opt.sessions_dir or "."
90+
base_dir = opt.sessions_dir or DEFAULT_SESSION_DIR
91+
_session_debug(f"--session-id enabled base_dir={base_dir!r}")
7892
session_store = SessionStore({})
7993
sessions = session_store.list_sessions(base_dir)
94+
_session_debug(f"--session-id sessions found total={len(sessions)}")
8095
if not sessions:
8196
print(f"No resumable sessions found in {base_dir}")
8297
sys.exit(1)
@@ -85,12 +100,14 @@ def parse_options() -> dict[str, Any]:
85100
except ValueError:
86101
print(f"Invalid session id: {opt.session_id}")
87102
sys.exit(1)
103+
_session_debug(f"--session-id parsed index={session_index}")
88104
if session_index < 1 or session_index > len(sessions):
89105
print(
90106
f"Session id out of range: {session_index} (1-{len(sessions)})"
91107
)
92108
sys.exit(1)
93109
opt.session_file = sessions[session_index - 1]["path"]
110+
_session_debug(f"--session-id resolved path={opt.session_file!r}")
94111

95112
if opt.session_file:
96113
return vars(opt)

lib/core/settings.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,19 @@
7979
"cache-control": "max-age=0",
8080
}
8181

82-
DEFAULT_SESSION_FILE = "sessions/{date}/session_{datetime}"
82+
def _get_default_session_dir() -> str:
83+
if getattr(sys, "frozen", False) or hasattr(sys, "_MEIPASS"):
84+
home_dir = os.path.expanduser("~")
85+
return FileUtils.build_path(home_dir, ".dirsearch", "sessions")
86+
return FileUtils.build_path(SCRIPT_PATH, "sessions")
87+
88+
89+
DEFAULT_SESSION_DIR = _get_default_session_dir()
90+
DEFAULT_SESSION_FILE = FileUtils.build_path(
91+
DEFAULT_SESSION_DIR,
92+
"{date}",
93+
"session_{datetime}",
94+
)
8395

8496
REFLECTED_PATH_MARKER = "__REFLECTED_PATH__"
8597

lib/parse/cmdline.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ def parse_arguments() -> Values:
179179
action="store",
180180
dest="sessions_dir",
181181
metavar="PATH",
182-
help="Directory to search for resumable sessions (default: current directory)",
182+
help=(
183+
"Directory to search for resumable sessions (default: dirsearch path "
184+
"/sessions, or $HOME/.dirsearch/sessions when bundled)"
185+
),
183186
)
184187
general.add_option(
185188
"-a",

0 commit comments

Comments
 (0)