Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated logic to ensure HTML files are output as requested (with --outfile) #881

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions scalene/scalene_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@

console = Console(style="white on blue")


# Assigning to `nada` disables any console.log commands.
def nada(*args: Any) -> None:
pass
Expand Down Expand Up @@ -857,7 +856,11 @@ def output_profile(program_args: Optional[List[str]] = None) -> bool:
return True
outfile = Scalene.__output.output_file
if Scalene.__args.outfile:
outfile = Scalene.__args.outfile
outfile = os.path.join(
os.path.dirname(Scalene.__args.outfile),
os.path.splitext(os.path.basename(Scalene.__args.outfile))[0] + ".json"
)
# outfile = Scalene.__args.outfile
# If there was no output file specified, print to the console.
if not outfile:
if sys.platform == "win32":
Expand Down Expand Up @@ -1826,6 +1829,7 @@ def profile_code(
"Scalene can only profile code that runs for at least one second or allocates at least 10MB.",
file=sys.stderr,
)

if not (
did_output
and Scalene.__args.web
Expand All @@ -1834,12 +1838,18 @@ def profile_code(
):
return exit_status

if Scalene.__args.web:
# Only generate HTML file if needed for display in the browser.
if Scalene.__args.web or Scalene.__args.html:
profile_filename = Scalene.__profile_filename
if Scalene.__args.outfile:
profile_filename = os.path.join(
os.path.dirname(Scalene.__args.outfile),
os.path.splitext(os.path.basename(Scalene.__args.outfile))[0] + ".json"
)
generate_html(
profile_fname=Scalene.__profile_filename,
profile_fname=profile_filename,
output_fname=(
Scalene.__profiler_html
Scalene.__profiler_html if not Scalene.__args.outfile
else Scalene.__args.outfile
),
)
if Scalene.in_jupyter():
Expand Down
11 changes: 8 additions & 3 deletions test/smoketest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env python3
import json
import pathlib
import tempfile
import subprocess
import sys
import json

def smoketest(fname, rest):
cmd = [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", "/dev/stderr", *rest, fname]
outfile = pathlib.Path(tempfile.mkdtemp(prefix="scalene") / pathlib.Path("smoketest.json"))
cmd = [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", str(outfile), *rest, fname]
print("COMMAND", ' '.join(cmd))
proc = subprocess.run(cmd ,capture_output=True)
stdout = proc.stdout.decode('utf-8')
Expand All @@ -19,7 +22,9 @@ def smoketest(fname, rest):
# print("STDOUT", stdout)
# print("\nSTDERR", stderr)
try:
scalene_json = json.loads(stderr)
with open(outfile, "r") as f:
outfile_contents = f.read()
scalene_json = json.loads(outfile_contents)
except json.JSONDecodeError:
print("Invalid JSON", stderr)
print("STDOUT", stdout)
Expand Down
11 changes: 8 additions & 3 deletions test/smoketest_profile_decorator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env python3
import json
import pathlib
import subprocess
import sys
import json
import tempfile

def smoketest(fname):
proc = subprocess.run( [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", "/dev/stderr", fname] ,capture_output=True)
outfile = pathlib.Path(tempfile.mkdtemp(prefix="scalene") / pathlib.Path("smoketest.json"))
proc = subprocess.run( [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", str(outfile), fname] ,capture_output=True)
if proc.returncode != 0:
print("Exited with a non-zero code:", proc.returncode)
print("Stdout:", proc.stdout.decode('utf-8'))
Expand All @@ -14,7 +17,9 @@ def smoketest(fname):

stderr = proc.stderr.decode('utf-8')
try:
scalene_json = json.loads(stderr)
with open(outfile, "r") as f:
outfile_contents = f.read()
scalene_json = json.loads(outfile_contents)
except json.JSONDecodeError:
print("Invalid JSON", stderr)
exit(1)
Expand Down
Loading