<commit_before>module SnoopCompile

export
    snoop_on,
    snoop_off,
    @snoop,
    @snoop1

function snoop_path()
    snoopdir = Base.find_in_path("SnoopCompile.jl")
    isempty(snoopdir) && error("SnoopCompile is not your path, please fix (e.g., push!(LOAD_PATH, \"/path/to/SnoopCompile\"))")
    splitdir(snoopdir)[1]
end

"""
`snoop_on()` modifies the julia compiler to turn on logging. To
actually use logging, you'll need to start a fresh julia session and
then use `@snoop1`. When finished, restore the compiler to its
original state with `snoop_off()`.
"""
function snoop_on()
    cd(snoop_path()) do
        run(`sh snoop.sh ""`)
    end
end

"""
`snoop_off()` restores the julia compiler to its original
state. Alternatively, you can just use `git checkout src/codegen.cpp
&& make` from the julia source folder.
"""
function snoop_off()
    cd(snoop_path()) do
        run(`sh snoop.sh "--reverse"`)
    end
end

"""
```
@snoop "compiledata.csv" begin
    # Commands to execute
end
```
causes the julia compiler to log all functions compiled in the course
of executing the commands to the file "compiledata.csv".  This file
can be used for the input to `SnoopCompile.read`.

After recompiling `codegen.cpp`, this launches a new julia process in
which to run the commands. Once finished, it closes the process and
restores `codegen.cpp` to its original state.  If you're recompiling
manually with `snoop_on()` and `snoop_off()`, use `@snoop1` in a fresh
julia session instead.
"""
macro snoop(filename, commands)
    println("Turning on compiler logging")
    snoop_on()
    try
        print("Launching new julia process to run commands...")
        # addprocs will run the unmodified version of julia, so we
        # launch it as a command.
        code_object = """
            while !eof(STDIN)
                eval(Main, deserialize(STDIN))
            end
            """
        io, pobj = open(`$(Base.julia_cmd()) --eval $code_object`, "w", STDOUT)
        serialize(io, quote
            using SnoopCompile
        end)
        # Now that the new process knows about SnoopCompile, it can
        # deserialize this next expression
        serialize(io, quote<commit_msg>For those that don't have JULIAHOME defined, autogenerate it!<commit_after>module SnoopCompile

export
    snoop_on,
    snoop_off,
    @snoop,
    @snoop1


# If we don't have a JULIAHOME environment variable, set it here.
if !haskey(ENV, "JULIAHOME")
    ENV["JULIAHOME"] = dirname(dirname(JULIA_HOME))
end

function snoop_path()
    snoopdir = Base.find_in_path("SnoopCompile.jl")
    isempty(snoopdir) && error("SnoopCompile is not your path, please fix (e.g., push!(LOAD_PATH, \"/path/to/SnoopCompile\"))")
    splitdir(snoopdir)[1]
end

"""
`snoop_on()` modifies the julia compiler to turn on logging. To
actually use logging, you'll need to start a fresh julia session and
then use `@snoop1`. When finished, restore the compiler to its
original state with `snoop_off()`.
"""
function snoop_on()
    cd(snoop_path()) do
        run(`sh snoop.sh ""`)
    end
end

"""
`snoop_off()` restores the julia compiler to its original
state. Alternatively, you can just use `git checkout src/codegen.cpp
&& make` from the julia source folder.
"""
function snoop_off()
    cd(snoop_path()) do
        run(`sh snoop.sh "--reverse"`)
    end
end

"""
```
@snoop "compiledata.csv" begin
    # Commands to execute
end
```
causes the julia compiler to log all functions compiled in the course
of executing the commands to the file "compiledata.csv".  This file
can be used for the input to `SnoopCompile.read`.

After recompiling `codegen.cpp`, this launches a new julia process in
which to run the commands. Once finished, it closes the process and
restores `codegen.cpp` to its original state.  If you're recompiling
manually with `snoop_on()` and `snoop_off()`, use `@snoop1` in a fresh
julia session instead.
"""
macro snoop(filename, commands)
    println("Turning on compiler logging")
    snoop_on()
    try
        println("Launching new julia process to run commands...")
        # addprocs will run the unmodified version of julia, so we
        # launch it as a command.
        code_object = """
            while !eof(STDIN)
                eval(Main, deserialize(STDIN))
            end
            """
        io, pobj = open(`$(Base.julia_cmd()) --eval $code_object`, "w", STDOUT)
        serialize(io, quote
            using SnoopCompile
        end)
        # Now that the new process knows about SnoopCompile, it can
        # deserialize this next expression
        serialize(io, quote
{'fraction_non_alphanumeric': 0.10368111522544109, 'fraction_numerical': 0.0017425397516880855, 'mean_word_length': 3.1859617137648133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2715281', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1405, 'n_words': 516}