Sublime Forum

Profile python using the exec command

#1

Hi, i’m making a plugin to profile python using cprofile and kcachegrind, i want to execute the first command:

python cProfile -o <output_file> <file>

if the command was successful then:

pyprof2calltree -k -i <output_file>

if it produced an error then:
delete the output_file

output_file = os.path.join(sublime.packages_path()[:-8], "Prof_logs", file_name[:-3] + ".cprof")

I want to use the ST builtin exec command if possible.
Thanks in advance :slight_smile:

0 Likes

#2

well that’s easy enough, just pass the shell_cmd to exec the same way you’d run it through the shell:

shell_cmd = 'python cProfile -o ' + quote(output_file) + ' ' + quote(file_name) + ' && pyprof2calltree -k -i ' + quote(output_file)
window.run_command('exec', { 'shell_cmd': shell_cmd })

if what produced an error? either of those 2 commands?
I’m guessing this is so that you don’t have an output file left from a previously successful run which could cause confusion? probably it would be easier to just delete the output file before calling exec.

import os
from shlex import quote

output_file = os.path.join(sublime.packages_path()[:-8], "Prof_logs", file_name[:-3] + ".cprof")
try:
    os.remove(output_file)
except OSError:
    pass
0 Likes

#3

I ve already done that:

what i want is to:

create a directory that contains all the output files of the files for wich python cProfile -o <output_file> <file> ran succesfully (return code 0)

so if this command ran successfully run pyprof2calltree -k -i <output_file>
if it isn’t delete the output file,
and in both cases i want output/errors to be logged to the output.exec panel.

strangely kcachegrind is opened even if python -m cProfile -o <output_file> <file> returned an error with profiling results not those of the file i want :confused:

hope you get what i mean.

0 Likes

#4

The easiest way to do complicated things with a build (where complicated is something other than just running a single command or chain of commands in sequence) is to create an external shell script/batch file that contains the logic that you need and then execute that.

To do something like this purely in Sublime you would need a custom build system target command that is capable of waiting for the first command to finish, checking the result, then doing other things. While doable, the script version is easier and arguably more useful because you can easily use it outside of Sublime as well.

0 Likes

#5

I would go for the shell/batch way but my knowledge of them is really poor, can you provide an example of a shell/batch script that does the job independetly of the platform or multiple scripts for multiple platforms. Thanks in advance :slight_smile:

0 Likes

#6

thanks @OdatNurd and @kingkeith , i figured it out and made a plugin for that Code Profiler for now it only works for python but i’m planning to include other languages in the future such as java and c++.

1 Like

#7

Good to hear! I was going to try and work up an example for you, but I had a fairly busy weekend. :slight_smile:

0 Likes