@@ -502,6 +502,19 @@ def make_argument_parser(**kwargs):
502
502
action = "store" ,
503
503
help = "lines of profile output or 'all' (default: 20)" ,
504
504
)
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
+ )
505
518
parser .add_argument (
506
519
"-v" , "--verbose" , action = "store_true" , help = "print additional output during builds"
507
520
)
@@ -714,12 +727,13 @@ def _log_command_output(self, out):
714
727
def _profile_wrapper (command , parser , args , unknown_args ):
715
728
import cProfile
716
729
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 ]
723
737
724
738
# allow comma-separated list of fields
725
739
sortby = ["time" ]
@@ -739,9 +753,9 @@ def _profile_wrapper(command, parser, args, unknown_args):
739
753
pr .disable ()
740
754
741
755
# print out profile stats.
742
- stats = pstats .Stats (pr )
756
+ stats = pstats .Stats (pr , stream = sys . stderr )
743
757
stats .sort_stats (* sortby )
744
- stats .print_stats (nlines )
758
+ stats .print_stats (* restrictions )
745
759
746
760
747
761
def print_setup_info (* info ):
0 commit comments