Skip to content

Commit f8fc9b1

Browse files
Merge pull request #954 from linsword13/profile
Tweaks on profile
2 parents 0021425 + 7f287f6 commit f8fc9b1

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

lib/ramble/ramble/main.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,19 @@ def make_argument_parser(**kwargs):
502502
action="store",
503503
help="lines of profile output or 'all' (default: 20)",
504504
)
505+
parser.add_argument(
506+
"--profile-restrictions",
507+
default="",
508+
action="store",
509+
help=(
510+
"Comma-separated restrictions applied to the cProfiler. "
511+
"When specified, it takes precedence over `--lines`. "
512+
"Example: `--profile-restrictions 'logger,5'` limits the list to functions with "
513+
"'logger' in the path, and then shows the first 5 of them. "
514+
"See https://docs.python.org/3/library/profile.html#pstats.Stats.print_stats for more "
515+
"details."
516+
),
517+
)
505518
parser.add_argument(
506519
"-v", "--verbose", action="store_true", help="print additional output during builds"
507520
)
@@ -714,12 +727,13 @@ def _log_command_output(self, out):
714727
def _profile_wrapper(command, parser, args, unknown_args):
715728
import cProfile
716729

717-
try:
718-
nlines = int(args.lines)
719-
except ValueError:
720-
if args.lines != "all":
721-
logger.die(f"Invalid number for --lines: {args.lines}")
722-
nlines = -1
730+
from ramble.util import conversions
731+
732+
if args.profile_restrictions:
733+
restrictions_raw = args.profile_restrictions.split(",")
734+
else:
735+
restrictions_raw = [args.lines if args.lines != "all" else -1]
736+
restrictions = [conversions.convert_to_number(v) for v in restrictions_raw]
723737

724738
# allow comma-separated list of fields
725739
sortby = ["time"]
@@ -739,9 +753,9 @@ def _profile_wrapper(command, parser, args, unknown_args):
739753
pr.disable()
740754

741755
# print out profile stats.
742-
stats = pstats.Stats(pr)
756+
stats = pstats.Stats(pr, stream=sys.stderr)
743757
stats.sort_stats(*sortby)
744-
stats.print_stats(nlines)
758+
stats.print_stats(*restrictions)
745759

746760

747761
def print_setup_info(*info):

lib/ramble/ramble/test/util/conversions.py

+16
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,19 @@ def test_list_str_to_list(input, expect):
3434
)
3535
def test_canonical_none(input, expect):
3636
assert conversions.canonical_none(input) == expect
37+
38+
39+
@pytest.mark.parametrize(
40+
"input,expect",
41+
[
42+
(None, None),
43+
("foo", "foo"),
44+
(1, 1),
45+
(-1.2, -1.2),
46+
("-1.2", -1.2),
47+
("-10", -10),
48+
("1.0", 1),
49+
],
50+
)
51+
def test_convert_to_number(input, expect):
52+
assert conversions.convert_to_number(input) == expect

lib/ramble/ramble/util/conversions.py

+13
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,16 @@ def canonical_none(maybe_none):
3737
if isinstance(maybe_none, str) and maybe_none.lower() == "none":
3838
return None
3939
return maybe_none
40+
41+
42+
def convert_to_number(val):
43+
"Convert (in order of preference) to int, or float, or simply return itself"
44+
if not isinstance(val, str):
45+
return val
46+
try:
47+
f_val = float(val)
48+
if f_val.is_integer():
49+
return int(f_val)
50+
return f_val
51+
except ValueError:
52+
return val

share/ramble/ramble-completion.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ complete -o bashdefault -o default -F _bash_completion_ramble ramble
264264
_ramble() {
265265
if $list_options
266266
then
267-
RAMBLE_COMPREPLY="-h --help -H --all-help --color -c --config -C --config-scope -d --debug --disable-passthrough -N --disable-logger -P --disable-progress-bar --timestamp --pdb -w --workspace -D --workspace-dir -W --no-workspace --use-workspace-repo -k --insecure -l --enable-locks -L --disable-locks -m --mock --mock-applications --mock-modifiers --mock-package-managers --mock-workflow-managers --mock-base-applications --mock-base-modifiers --mock-base-package-managers --mock-base-workflow-managers -p --profile --sorted-profile --lines -v --verbose --stacktrace -V --version --print-shell-vars"
267+
RAMBLE_COMPREPLY="-h --help -H --all-help --color -c --config -C --config-scope -d --debug --disable-passthrough -N --disable-logger -P --disable-progress-bar --timestamp --pdb -w --workspace -D --workspace-dir -W --no-workspace --use-workspace-repo -k --insecure -l --enable-locks -L --disable-locks -m --mock --mock-applications --mock-modifiers --mock-package-managers --mock-workflow-managers --mock-base-applications --mock-base-modifiers --mock-base-package-managers --mock-base-workflow-managers -p --profile --sorted-profile --lines --profile-restrictions -v --verbose --stacktrace -V --version --print-shell-vars"
268268
else
269269
RAMBLE_COMPREPLY="attributes clean commands config debug deployment docs edit help info license list mirror on python repo results software-definitions style unit-test workspace"
270270
fi

0 commit comments

Comments
 (0)