Skip to content

Commit e845d53

Browse files
Append stats to the file
1 parent 87f6c52 commit e845d53

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/python/pants/goal/stats_aggregator.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
from __future__ import annotations
55

66
import base64
7+
import datetime
78
import logging
89
from collections import Counter
910
from dataclasses import dataclass
11+
from pathlib import Path
1012
from typing import Optional
1113

1214
from pants.engine.internals.scheduler import Workunit
@@ -69,7 +71,7 @@ def _log_or_write_to_file(output_file: Optional[str], lines: list[str]) -> None:
6971
if lines:
7072
text = "\n".join(lines)
7173
if output_file:
72-
with safe_open(output_file, "w") as fh:
74+
with safe_open(output_file, "a") as fh:
7375
fh.write(text)
7476
logger.info(f"Wrote Pants stats to {output_file}")
7577
else:
@@ -103,6 +105,13 @@ def __call__(
103105
return
104106

105107
output_lines = []
108+
if self.output_file:
109+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
110+
# have an empty line between stats of different Pants invocations
111+
space = "\n\n" if Path(self.output_file).exists() else ""
112+
output_lines.append(
113+
f"{space}{timestamp} Executing goals: {','.join(context._run_tracker.goals)}"
114+
)
106115

107116
if self.log:
108117
# Capture global counters.

src/python/pants/goal/stats_aggregator_integration_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ def test_writing_to_output_file() -> None:
5959
"roots",
6060
]
6161
run_pants(argv).assert_success()
62+
argv = [
63+
"--backend-packages=['pants.backend.python']",
64+
"--stats-log",
65+
"--stats-memory-summary",
66+
"--stats-output-file=stats.txt",
67+
"list",
68+
"::",
69+
]
70+
run_pants(argv).assert_success()
6271
output_file_contents = Path("stats.txt").read_text()
6372
for item in ("Counters:", "Memory summary"):
73+
assert output_file_contents.count(item) == 2
74+
75+
for item in ("roots", "list"):
6476
assert item in output_file_contents

0 commit comments

Comments
 (0)