/
botutils.jl
341 lines (307 loc) · 9.85 KB
/
botutils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
export timesum
################################################################
"""
timesum(snoop::Vector{Tuple{Float64, Core.MethodInstance}}, unit = :s)
Calculates the total time measured by a snoop macro. `unit` can be :s or :ms.
# Examples
```julia
using SnoopCompile
data = @snoopi begin
using MatLang
MatLang_rootpath = dirname(dirname(pathof("MatLang")))
include("\$MatLang_rootpath/test/runtests.jl")
end
println(timesum(data, :ms))
```
"""
function timesum(snoop::Vector{Tuple{Float64, Core.MethodInstance}}, unit::Symbol = :s)
if isempty(snoop)
t = 0.0
else
t = sum(first, snoop)
end
if unit == :s
t = round(t, digits = 6)
elseif unit == :ms
t = round(t * 1000, digits = 3)
else
throw(ArgumentError("unit should be `:s` or `:ms`"))
end
return t
end
################################################################
"""
os_string, os_func = detectOS()
Returns Operating System of a machine as a string and a function `os_func()` that will return
`true` on the current OS.
# Examples
```julia
julia> detectOS()
("windows", Base.Sys.iswindows)
```
"""
function detectOS()
allos_funs = [
Sys.iswindows,
Sys.isapple,
Sys.islinux,
Sys.isbsd]
if VERSION >= v"1.1"
allos_funs = [allos_funs...,
Sys.isdragonfly,
Sys.isfreebsd,
Sys.isnetbsd,
Sys.isopenbsd]
end
if VERSION >= v"1.2"
push!(allos_funs, Sys.isjsvm)
end
os = ""
osfun = allos_funs[1] # temp
for osfun in allos_funs
if osfun()
os = split(string(osfun), '.')[end][3:end]
return os, osfun
end
end
@error "os is not detected"
end
################################################################
"""
standardize_osname(input::String)
standardize_osname(inputs::Vector{String})
Standardize different names from Github actions, Travis, etc
https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources
# Examples
```jldoctest; setup=:(using SnoopCompile)
julia> SnoopCompile.standardize_osname("ubuntu-latest")
"linux"
julia> SnoopCompile.standardize_osname(["ubuntu-latest", "macos-latest"])
2-element Array{String,1}:
"linux"
"apple"
```
"""
function standardize_osname(input::String)
for (key, standard) in OS_MAP
if occursin(key, input)
input = standard
end
end
return input
end
standardize_osname(inputs::Vector{String}) = standardize_osname.(inputs)
standardize_osname(input::Nothing) = input
const OS_MAP = Dict(
"macos" => "apple",
"mac" => "apple",
"osx" => "apple",
"apple" => "apple",
"ubuntu" => "linux",
"linux" => "linux",
"win" => "windows",
"windows" => "windows",
)
################################################################
using FilePathsBase
export GoodPath
# to convert WindowsPath before they get inside the SnoopCompile code
# GoodPath(inp::String) = inp |> Path |> _GoodPath |> string
# _GoodPath(path::WindowsPath) = PosixPath((path.drive, path.segments...))
# _GoodPath(path) = path
GoodPath(x::String) = replace(x, "\\" => "/") # doesn't remove / from the end of the strings
goodjoinpath(args...) = GoodPath(joinpath(args...))
################################################################
"""
searchdir(rootpath::String, pattern::AbstractString)
a function to search a directory.
```julia
julia> searchdir("src", "/bot.jl")
1-element Array{String,1}:
"src/bot.jl"
```
"""
function searchdir(rootpath::String, pattern::AbstractString)
found_files = String[]
for file in walkpath(Path(rootpath))
if occursin(pattern, GoodPath(string(file)))
push!(found_files, GoodPath(string(file)))
end
end
return found_files
end
"""
Searches for the file rather a path.
"""
searchdirfile(rootpath::String, file::AbstractString) = searchdir(rootpath, string(Path(file).segments[end]))
"""
searchdirs(rootpaths::Array{String}, pattern::AbstractString)
a function to search multiple directories.
```julia
julia> searchdirs(["./src", "./test"], "/bot.jl")
2-element Array{String,1}:
"./src/bot.jl"
"./test/bot/bot.jl"
```
"""
function searchdirs(rootpaths::Array{String}, pattern::AbstractString)
unique(
reduce(vcat,
searchdir.(rootpaths, pattern)
)
)
end
"""
Searches for the file rather a path.
"""
searchdirsfile(rootpaths::Array{String}, file::AbstractString) = searchdirs(rootpaths, basename(file))
"""
search for the script! - needed because of confusing paths when referencing pattern_or_file in CI
"""
function searchdirsboth(rootpaths::Array{String}, pattern_or_file::AbstractString)
if isfile(pattern_or_file)
return pattern_or_file
elseif isfile("../../$pattern_or_file") # if in deps/SnoopCompile
return "../../$pattern_or_file"
else
found_files = searchdirs(rootpaths, pattern_or_file)
if length(found_files) === 0
found_files = searchdirsfile(rootpaths, pattern_or_file)
if length(found_files) === 0
@error "Couldn't find $(pattern_or_file)"
elseif length(found_files) > 1
@error "Multiple $(pattern_or_file) exists at the current directory."
else
pattern_or_file = found_files[1]
end
elseif length(found_files) > 1
@error "Multiple $(pattern_or_file) exists at the current directory."
else
pattern_or_file = found_files[1]
end
end
return pattern_or_file
end
################################################################
export pathof_noload
"""
Returns a package's path without loading the package in the main Julia process.
May launch a separate Julia process to find the package.
# Examples
```julia
pathof_noload("MatLang")
```
"""
function pathof_noload(package_name::String)
path = Base.find_package(package_name)
if isnothing(path)
cmd = "import $package_name; print(pathof($package_name))"
try
path = Base.read(`$(Base.julia_cmd()) -e $cmd`, String)
return GoodPath(path)
catch
try
path = Base.read(`$(Base.julia_cmd()) --project=$(Base.active_project()) -e $cmd`, String)
return GoodPath(path)
catch
@error "Couldn't find the path of $package_name"
end
end
else
return GoodPath(path)
end
end
################################################################
import Pkg
"""
Should be removed once Pkg allows adding test dependencies to the current environment
Used in Github Action workflow yaml file
"""
function addtestdep()
if isfile("test/Test.toml")
toml = Pkg.TOML.parsefile("test/Test.toml")
test_deps = get(toml, "deps", nothing)
elseif isfile("test/Project.toml")
toml = Pkg.TOML.parsefile("test/Project.toml")
test_deps = get(toml, "deps", nothing)
else
toml = Pkg.TOML.parsefile("Project.toml")
test_deps = get(toml, "extras", nothing)
end
if !isnothing(test_deps)
Pkg.add([Pkg.PackageSpec(name = name, uuid = uuid) for (name, uuid) in test_deps])
end
end
################################################################
"""
Get the float version from semver version
```
julia> VersionFloat(v"1.5.0")
"1.4"
```
"""
VersionFloat(v::VersionNumber) = join(split(string(v),'.')[1:2],'.')
# https://github.com/JuliaLang/julia/pull/36223:
"""
JuliaVersionNumber(v::String)
JuliaVersionNumber(v::VersionNumber)
returns the Julia version number by following the same specifications as VersionNumber.
`JuliaVersionNumber` will fetch the latest nightly version number if `"nightly"` or `"latest"` is given as the input.
# Examples
```julia
julia> SnoopCompile.JuliaVersionNumber("nightly")
v"1.6.0-DEV"
```
```jldoctest; setup=:(using SnoopCompile)
julia> SnoopCompile.JuliaVersionNumber("1.2.3")
v"1.2.3"
julia> SnoopCompile.JuliaVersionNumber(v"1.2.3")
v"1.2.3"
```
"""
JuliaVersionNumber(v::VersionNumber) = v
function JuliaVersionNumber(v::String)
if in(v, ["nightly", "latest"])
version_path = download(
"https://raw.githubusercontent.com/JuliaLang/julia/master/VERSION",
joinpath(tempdir(), "VERSION.txt"),
)
version_str = replace(Base.read(version_path, String), "\n" => "")
return VersionNumber(version_str)
else
return VersionNumber(v)
end
end
################################################################
# TODO we need to remove begin and end manually
# https://github.com/timholy/SnoopCompile.jl/pull/98#issuecomment-645969610
toplevel_string(expr::Expr) = replace(string(expr), r"^begin([\s\S]*)end$"=>s"\1")
################################################################
"""
Adds a package if it is not defined in the given module.
"""
function addpkg_ifnotfound(package_name::Symbol, env_modul::Module)
package_name_str = string(package_name)
if !isdefined(env_modul, package_name) && Core.eval(env_modul, :( Base.find_package($package_name_str) === nothing ))
@info "Adding $package_name_str to $env_modul"
Core.eval(env_modul, quote
using Pkg
Pkg.add($package_name_str)
end)
end
end
"""
Develop a package if it is not defined in the given module.
"""
function devpkg_ifnotfound(package_name::Symbol, package_path::String, env_modul::Module)
package_name_str = string(package_name)
if !isdefined(env_modul, package_name) && Core.eval(env_modul, :( Base.find_package($package_name_str) === nothing ))
if dirname(Base.current_project()) !== package_path
@info "Developing $package_name_str to $env_modul"
Core.eval(env_modul, quote
using Pkg
Pkg.develop(PackageSpec(path=$package_path))
end)
end
end
end