Skip to content

Commit 4dabb6c

Browse files
jet-logicjet-logic
authored andcommitted
winci67
1 parent 5ba0ec3 commit 4dabb6c

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

runce/cli.py

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from sys import stderr, stdout
44
from shutil import copyfileobj
55
from subprocess import Popen, PIPE, run
6+
import sys
67
from typing import Dict, Any
78
from .utils import check_pid, filesizepu, kill_pid, tail_bytes, tail_file
89
from .main import Main, flag, arg
@@ -131,6 +132,26 @@ def start(self) -> None:
131132
sp.drop(x)
132133

133134

135+
def _tail(n: float, u="", out="", tab=None):
136+
if u:
137+
stdout.buffer.write(tail_bytes(out, int(n)))
138+
elif n > 0:
139+
if sys.platform.startswith("win"):
140+
cmd = [
141+
"powershell",
142+
"-c",
143+
f"Get-Content -Tail {int(n)} '{out}'",
144+
]
145+
else:
146+
cmd = ["tail", "-n", str(int(n)), out]
147+
if tab:
148+
with Popen(cmd, stdout=PIPE).stdout as o:
149+
for line in o:
150+
stdout.buffer.write(b"\t" + line)
151+
else:
152+
run(cmd)
153+
154+
134155
class Tail(Main):
135156
"""Tail process output."""
136157

@@ -152,37 +173,18 @@ def start(self) -> None:
152173
hf = None
153174
else:
154175
hf = format_prep(self.format or r"{pid?}: {name}")
155-
lines = filesizepu(self.lines or "10")
176+
n, u = filesizepu(self.lines or "10")
156177
j = 0
157178
out = "err" if self.err else "out"
158179

159180
for x in Manager().find_names(self.ids, ambiguous, no_record):
160181
if self.existing and not check_pid(x["pid"]):
161182
continue
162183

163-
j > 1 and lines[0] > 0 and print()
184+
j > 1 and n > 0 and print()
164185
if hf:
165186
print(f"{self.p_open}{hf(x)}{self.p_close}", flush=True)
166-
167-
if lines[1]:
168-
stdout.buffer.write(tail_bytes(x[out], int(lines[0])))
169-
elif lines[0] > 0:
170-
# TODO: pythonify
171-
if sys.platform.startswith("win"):
172-
cmd = [
173-
"powershell",
174-
"-c",
175-
f"Get-Content -Tail {int(lines[0])} '{x[out]}'",
176-
]
177-
else:
178-
cmd = ["tail", "-n", str(int(lines[0])), x[out]]
179-
if self.tab:
180-
with Popen(cmd, stdout=PIPE).stdout as o:
181-
for line in o:
182-
stdout.buffer.write(b"\t" + line)
183-
else:
184-
run(cmd)
185-
stdout.flush()
187+
_tail(n, u, x[out], self.tab)
186188
j += 1
187189

188190

@@ -193,11 +195,11 @@ class Run(Main):
193195
tail: int = flag("t", "tail", "tail the output with n lines", default=0)
194196
run_id: str = flag("id", "Unique run identifier", default="")
195197
cwd: str = flag("Working directory for the command")
196-
tail: int = flag(
198+
tail: str = flag(
197199
"t",
198200
"tail",
199201
"Tail the output (n lines). Use `-t -1` to print the entire output",
200-
default=0,
202+
# default=0,
201203
)
202204
overwrite: bool = flag("overwrite", "Overwrite existing entry", default=False)
203205
cmd_after: str = flag("run-after", "Run command after", metavar="command")
@@ -228,13 +230,8 @@ def start(self) -> None:
228230

229231
# Handle tail output
230232
if self.tail:
231-
if self.tail < 0:
232-
with open(e["out"], "rb") as f:
233-
copyfileobj(f, stdout.buffer)
234-
elif self.tail > 0:
235-
for x in tail_file(e["out"], self.tail):
236-
stdout.buffer.write(x)
237-
stdout.buffer.write(b"\n")
233+
n, u = filesizepu(self.tail)
234+
_tail(n, u, e["out"])
238235

239236
# Run post-command if specified
240237
if self.cmd_after:

runce/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,17 @@ def tail_file(filename="", n=10):
256256
return f.read().splitlines()[-n:]
257257

258258

259-
def filesizepu(s: str) -> tuple[float, str]:
259+
def filesizepu(s: str) -> tuple[int, str]:
260260
u = ""
261261
if s[0].isnumeric():
262262
q = s.lower()
263263
if q.endswith("b"):
264264
q, u = q[0:-1], q[-1]
265265
for i, v in enumerate("kmgtpezy"):
266266
if q[-1].endswith(v):
267-
return float(q[0:-1]) * (2 ** (10 * (i + 1))), v
268-
return float(q), u
269-
return float(s), u
267+
return int(float(q[0:-1]) * (2 ** (10 * (i + 1)))), v
268+
return int(q), u
269+
return int(s), u
270270

271271

272272
def tail_bytes(filename: str, num_bytes: int):

tests/test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_cli(self):
109109
"run", "--id", "apple", "--", "python", "tests/echo.py", "apple"
110110
)
111111

112-
self.assertRegex(o, r"(?xim) \W+ started: \W+ .+ \W+ apple \W+")
112+
self.assertRegex(o, r"(?xim) (?:\W+|\A) started: \W+ .+ \W+ apple \W+")
113113

114114
o = self.run_runce(
115115
"run",
@@ -122,12 +122,12 @@ def test_cli(self):
122122
"banana",
123123
"err",
124124
)
125-
self.assertRegex(o, r"(?xim) \W+ started: \W+ .+ \W+ banana \W+")
125+
self.assertRegex(o, r"(?xim) (?:\W+|\A) started: \W+ .+ \W+ banana \W+")
126126

127127
o = self.run_runce(
128128
"run", "--id", "pineapple", "--", "python", "tests/echo.py", "pineapple"
129129
)
130-
self.assertRegex(o, r"(?xim) \W+ started: \W+ .+ \W+ pineapple \W+")
130+
self.assertRegex(o, r"(?xim) (?:\W+|\A) started: \W+ .+ \W+ pineapple \W+")
131131

132132
a = [x for x in self.run_runce("status").strip().splitlines()]
133133

tests/test_filesizepu.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def test_zettabytes(self):
4141

4242
def test_no_unit(self):
4343
self.assertEqual(filesizepu("1024"), (1024.0, ""))
44-
self.assertEqual(filesizepu("3.14"), (3.14, ""))
44+
self.assertEqual(
45+
filesizepu("1208925819614629174706176"), (1208925819614629174706176, "")
46+
)
4547

4648
def test_invalid_input(self):
4749
with self.assertRaises(ValueError):
@@ -54,6 +56,8 @@ def test_invalid_input(self):
5456
filesizepu("1BB")
5557
with self.assertRaises(ValueError):
5658
filesizepu("9xb")
59+
with self.assertRaises(ValueError):
60+
filesizepu("3.14")
5761

5862
def test_case_insensitive(self):
5963
# self.assertEqual(filesizepu("1K"), 1024)

0 commit comments

Comments
 (0)