Skip to content

Commit

Permalink
Fix remote stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed Jan 30, 2024
1 parent 7dd3939 commit 747ebad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
33 changes: 25 additions & 8 deletions src/viztracer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import multiprocessing.util # type: ignore
import os
import platform
import shutil
import signal
import sys
Expand Down Expand Up @@ -479,15 +480,26 @@ def show_version(self) -> VizProcedureResult:
print(__version__)
return True, None

def _check_attach_availability(self) -> Tuple[bool, Optional[str]]:
if sys.platform == "win32":
return False, "VizTracer does not support this feature on Windows"

if sys.platform == "darwin" and platform.processor() == "arm":
return False, "VizTracer does not support this feature on Apple Silicon"

if sys.platform == "darwin" and sys.version_info >= (3, 11):
color_print("WARNING", "Warning: attach may not work on 3.11+ on Mac due to hardened runtime")

return True, None

def attach(self) -> VizProcedureResult:
pid = self.options.attach
interval = self.options.t

if sys.platform == "win32":
return False, "VizTracer does not support this feature on Windows"
success, err_msg = self._check_attach_availability()

if sys.platform == "darwin" and sys.version_info >= (3, 11):
print("Warning: attach may not work on 3.11+ on Mac due to hardened runtime")
if not success:
return False, err_msg

if not pid_exists(pid):
return False, f"pid {pid} does not exist!"
Expand Down Expand Up @@ -522,8 +534,10 @@ def attach(self) -> VizProcedureResult:
def uninstall(self) -> VizProcedureResult:
pid = self.options.uninstall

if sys.platform == "win32":
return False, "VizTracer does not support this feature on Windows"
success, err_msg = self._check_attach_availability()

if not success:
return False, err_msg

if not pid_exists(pid):
return False, f"pid {pid} does not exist!"
Expand All @@ -537,8 +551,11 @@ def uninstall(self) -> VizProcedureResult:
return True, None

def attach_installed(self) -> VizProcedureResult:
if sys.platform == "win32":
return False, "VizTracer does not support this feature on Windows"
success, err_msg = self._check_attach_availability()

if not success:
return False, err_msg

pid = self.options.attach_installed
interval = self.options.t
try:
Expand Down
24 changes: 15 additions & 9 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import base64
import json
import os
import platform
import re
import signal
import subprocess
Expand All @@ -21,9 +22,14 @@
from .util import cmd_with_coverage


@unittest.skipIf(sys.platform == "darwin" and sys.version_info >= (3, 11), "Does not support 3.11+ on Mac")
attach_unavailable = (sys.platform == "win32"
or (sys.platform == "darwin"
and (sys.version_info > (3, 11)
or "arm" in platform.processor())))


@unittest.skipIf(attach_unavailable, "Does not support attach on this platform")
class TestRemote(CmdlineTmpl):
@unittest.skipIf(sys.platform == "win32", "Does not support on Windows")
def test_install(self):
tracer = VizTracer(output_file="remote.json", verbose=0)
tracer.install()
Expand All @@ -33,7 +39,6 @@ def test_install(self):
self.assertFileExists("remote.json")
os.remove("remote.json")

@unittest.skipIf(sys.platform == "win32", "Does not support on Windows")
def test_attach_installed(self):
file_to_attach = textwrap.dedent("""
from viztracer import VizTracer
Expand All @@ -52,7 +57,6 @@ def test_attach_installed(self):
self.attach_check(file_to_attach, attach_cmd, output_file)
self.attach_check(file_to_attach, attach_installed_cmd, output_file, use_installed=True)

@unittest.skipIf(sys.platform == "win32", "Does not support on Windows")
def test_attach(self):
file_to_attach = textwrap.dedent("""
import time
Expand Down Expand Up @@ -138,7 +142,6 @@ def attach_check(self, file_to_attach, attach_cmd, output_file, file_should_exis
p_attach_invalid.wait()
self.assertTrue(p_attach_invalid.returncode != 0)

@unittest.skipIf(sys.platform == "win32", "Does not support on Windows")
def test_uninstall(self):
file_to_attach = textwrap.dedent("""
import time
Expand Down Expand Up @@ -208,7 +211,11 @@ def test_uninstall(self):
p_attach_uninstall.wait()
self.assertTrue(p_attach_uninstall.returncode != 0)

@unittest.skipIf(sys.platform != "win32", "Only test Windows")

class TestRemoteFail(CmdlineTmpl):
@unittest.skipUnless(sys.platform == "win32"
or (sys.platform == "darwin" and "arm" in platform.processor()),
"Only test unavailable platform")
def test_windows(self):
tracer = VizTracer(output_file="remote.json")
with self.assertRaises(SystemExit):
Expand All @@ -219,9 +226,8 @@ def test_windows(self):
self.template(["viztracer", "--uninstall", "1234"], success=False)


@unittest.skipIf(sys.platform == "darwin" and sys.version_info >= (3, 11), "Does not support 3.11+ on Mac")
@unittest.skipIf(attach_unavailable, "Does not support this platform")
class TestAttachSanity(CmdlineTmpl):
@unittest.skipIf(sys.platform == "win32", "Can't run attach on Windows")
def test_basic(self):
file_to_attach = textwrap.dedent("""
import time
Expand Down Expand Up @@ -249,7 +255,7 @@ def test_basic(self):
os.remove("attached_script.py")


@unittest.skipIf(sys.platform == "darwin" and sys.version_info >= (3, 11), "Does not support 3.11+ on Mac")
@unittest.skipIf(attach_unavailable, "Does not support this platform")
class TestAttachScript(CmdlineTmpl):
def test_attach_script(self):
# Isolate the attach stuff in a separate process
Expand Down

0 comments on commit 747ebad

Please sign in to comment.