Skip to content

Commit

Permalink
turn mitmproxy_windows.executable_path into a method (#115)
Browse files Browse the repository at this point in the history
now that this is auto-imported to check for missing packages, we don't want dev compiles on every run
  • Loading branch information
mhils authored Sep 24, 2023
1 parent 8d6cf18 commit 2e6c2b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion mitmproxy-rs/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub fn start_os_proxy(
{
let executable_path: PathBuf = py
.import("mitmproxy_windows")?
.getattr("executable_path")?
.call_method0("executable_path")?
.extract()?;
if !executable_path.exists() {
return Err(anyhow::anyhow!("{} does not exist", executable_path.display()).into());
Expand Down
54 changes: 32 additions & 22 deletions mitmproxy-windows/mitmproxy_windows/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
from pathlib import Path

here = Path(__file__).parent.absolute()
executable_path = here / "windows-redirector.exe"

# Development path: This should never happen with precompiled wheels.
if not executable_path.exists() and (here / "editable.marker").exists():
import logging
import shutil
import subprocess

logger = logging.getLogger(__name__)
logger.warning("Development mode: Compiling windows-redirector.exe...")

# Build Redirector
subprocess.run(["cargo", "build"], cwd=here.parent / "redirector", check=True)
# Copy WinDivert to target/debug/
target_debug = here.parent.parent / "target/debug"
for f in ["WinDivert.dll", "WinDivert.lib", "WinDivert64.sys"]:
if not (target_debug / f).exists():
shutil.copy(here / f, target_debug / f)

logger.warning("Development mode: Using target/debug/windows-redirector.exe...")
executable_path = target_debug / "windows-redirector.exe"

def executable_path() -> Path:
"""
Return the Path for windows-redirector.exe.
For shipped wheels this is just the file in the package,
for development setups this may invoke cargo to build it.
"""
here = Path(__file__).parent.absolute()
exe = here / "windows-redirector.exe"

# Development path: This should never happen with precompiled wheels.
if not exe.exists() and (here / "editable.marker").exists():
import logging
import shutil
import subprocess

logger = logging.getLogger(__name__)
logger.warning("Development mode: Compiling windows-redirector.exe...")

# Build Redirector
subprocess.run(["cargo", "build"], cwd=here.parent / "redirector", check=True)
# Copy WinDivert to target/debug/
target_debug = here.parent.parent / "target/debug"
for f in ["WinDivert.dll", "WinDivert.lib", "WinDivert64.sys"]:
if not (target_debug / f).exists():
shutil.copy(here / f, target_debug / f)

logger.warning("Development mode: Using target/debug/windows-redirector.exe...")
exe = target_debug / "windows-redirector.exe"

return exe

0 comments on commit 2e6c2b0

Please sign in to comment.