Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson committed Mar 8, 2021
1 parent 0539fcd commit 9fbc254
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
4 changes: 1 addition & 3 deletions generate_osi_list/generate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ spdx = JSON3.read(String(r.body))

@assert spdx.licenseListVersion == "3.10"

osi_licenses = sort!([lic.licenseId
for lic in spdx.licenses
if lic.isOsiApproved])
osi_licenses = sort!([lic.licenseId for lic in spdx.licenses if lic.isOsiApproved])

open(joinpath(@__DIR__, "..", "src", "OSI_LICENSES.jl"); write=true) do io
header = """
Expand Down
14 changes: 9 additions & 5 deletions src/LicenseCheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ using licensecheck_jll: licensecheck_jll

export licensecheck, is_osi_approved
export find_licenses, find_license
export find_licenses_by_bruteforce, find_licenses_by_list, find_licenses_by_list_intersection
export find_licenses_by_bruteforce, find_licenses_by_list,
find_licenses_by_list_intersection

include("OSI_LICENSES.jl")
include("find_licenses.jl")
Expand All @@ -30,9 +31,10 @@ julia> licensecheck(text)
```
"""
function licensecheck(text::String)
arr, dims, license_file_percent_covered = ccall((:License, licensecheck_jll.licensecheck),
Tuple{Ptr{Ptr{UInt8}},Cint,Float64}, (Cstring,),
text)
arr, dims, license_file_percent_covered = ccall((:License,
licensecheck_jll.licensecheck),
Tuple{Ptr{Ptr{UInt8}},Cint,Float64},
(Cstring,), text)
return (; licenses_found=unsafe_string.(unsafe_wrap(Array, arr, dims; own=true)),
license_file_percent_covered=license_file_percent_covered)
end
Expand Down Expand Up @@ -67,7 +69,9 @@ false
```
"""
is_osi_approved(spdx_identifier::String) = spdx_identifier OSI_LICENSES
is_osi_approved(nt::NamedTuple) = !isempty(nt.licenses_found) && all(is_osi_approved, nt.licenses_found)
function is_osi_approved(nt::NamedTuple)
return !isempty(nt.licenses_found) && all(is_osi_approved, nt.licenses_found)
end
is_osi_approved(::Nothing) = false # so that it can always be used with `find_license`

end # module
20 changes: 11 additions & 9 deletions src/find_licenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# is the `APL-1.0` license which is 45 KB.
# We take a factor of 10 larger, to allow for
# compound licenses.
const MAX_LICENSE_SIZE_IN_BYTES = 45958*10
const MAX_LICENSE_SIZE_IN_BYTES = 45958 * 10

# Based on
# https://github.com/ericphanson/LicenseCheck.jl/issues/2#issue-805995984
Expand All @@ -11,7 +11,8 @@ const LICENSE_NAMES = let
name_cases = (uppercase, uppercasefirst, lowercase)
ext_cases = (uppercase, lowercase)
extensions = (".md", ".txt", "", ".rst")
Set(string(case(name), extcase(ext)) for name in names, case in name_cases, ext in extensions, extcase in ext_cases)
Set(string(case(name), extcase(ext))
for name in names, case in name_cases, ext in extensions, extcase in ext_cases)
end

# only ~500 items this way
Expand All @@ -26,7 +27,7 @@ const LICENSE_TABLE_TYPE_STRING = "Vector{@NamedTuple{license_filename::String,
readfiles(dir) = filter!(f -> isfile(joinpath(dir, f)), readdir(dir))

# constructs a table of `licensecheck` results
function license_table(dir, names; validate_strings = true, validate_paths = true)
function license_table(dir, names; validate_strings=true, validate_paths=true)
table = LICENSE_TABLE_TYPE()
for lic in names
path = joinpath(dir, lic)
Expand All @@ -40,7 +41,7 @@ function license_table(dir, names; validate_strings = true, validate_paths = tru
push!(table, (; license_filename=lic, lc...))
end
end
sort!(table; by = x -> x.license_file_percent_covered, rev=true)
sort!(table; by=x -> x.license_file_percent_covered, rev=true)
return table
end

Expand All @@ -56,9 +57,9 @@ Operates by filtering the results of `readdir`, which should be efficient
for small and moderately sized directories. See [`find_licenses_by_list`](@ref)
for an alternate approach for very large directories.
"""
function find_licenses_by_list_intersection(dir; files = readfiles(dir))
function find_licenses_by_list_intersection(dir; files=readfiles(dir))
names = filter!(lic -> lowercase(lic) LOWERCASE_LICENSE_NAMES, files)
return license_table(dir, names; validate_paths = false)
return license_table(dir, names; validate_paths=false)
end

"""
Expand All @@ -84,9 +85,10 @@ find_licenses_by_list(dir) = license_table(dir, LICENSE_NAMES)
Calls [`licensecheck`](@ref) on every plaintext file in `dir` whose size is less than `max_bytes`,
returning the results as a table. The parameter `max_bytes` defaults to $(MAX_LICENSE_SIZE_IN_BYTES ÷ 1000) KiB.
"""
function find_licenses_by_bruteforce(dir; max_bytes = MAX_LICENSE_SIZE_IN_BYTES, files = readfiles(dir))
function find_licenses_by_bruteforce(dir; max_bytes=MAX_LICENSE_SIZE_IN_BYTES,
files=readfiles(dir))
names = filter!(file -> stat(joinpath(dir, file)).size < max_bytes, files)
return license_table(dir, names; validate_paths = false)
return license_table(dir, names; validate_paths=false)
end

const CUTOFF = 100
Expand All @@ -108,7 +110,7 @@ julia> find_licenses(".")
```
"""
function find_licenses(dir; allow_brute=true, max_bytes = MAX_LICENSE_SIZE_IN_BYTES)
function find_licenses(dir; allow_brute=true, max_bytes=MAX_LICENSE_SIZE_IN_BYTES)
files = readfiles(dir)
if allow_brute && (length(files) < CUTOFF)
return find_licenses_by_bruteforce(dir; files=files, max_bytes=max_bytes)
Expand Down
19 changes: 11 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ dorian_gray = """

result = licensecheck(MIT * "\n" * dorian_gray)
@test result.licenses_found == ["MIT"]
@test result.license_file_percent_covered 100 * length(MIT) / (length(dorian_gray) + length(MIT)) atol = 5
@test result.license_file_percent_covered
100 * length(MIT) / (length(dorian_gray) + length(MIT)) atol = 5

result = licensecheck(MIT * "\n" * dorian_gray * "\n" * Latex2e)
@test result.licenses_found == ["MIT", "Latex2e"]
@test result.license_file_percent_covered
100 * (length(MIT) + length(Latex2e)) /
(length(dorian_gray) + length(MIT) + length(Latex2e)) atol = 5
100 * (length(MIT) + length(Latex2e)) /
(length(dorian_gray) + length(MIT) + length(Latex2e)) atol = 5
end

@testset "`is_osi_approved`" begin
Expand All @@ -91,19 +92,19 @@ dorian_gray = """
VERSION >= v"1.5" &&
@test is_osi_approved(find_license(pkgdir(LicenseCheck))) == true
@test !is_osi_approved(nothing) # for if `find_license` returns `nothing`
@test is_osi_approved((; licenses_found = ["MIT", "MIT"]))
@test !is_osi_approved((; licenses_found = String[]))
@test is_osi_approved((; licenses_found=["MIT", "MIT"]))
@test !is_osi_approved((; licenses_found=String[]))
end


@testset "`find_licenses_*`" begin
fl = find_license(joinpath(@__DIR__, ".."))
# check it found the right one
@test fl.license_filename == "LICENSE"
@test fl.licenses_found == ["MIT"]
@test fl.license_file_percent_covered > 90

for method in (find_licenses, dir -> find_licenses(dir; allow_brute=false), find_licenses_by_bruteforce, find_licenses_by_list_intersection)
for method in (find_licenses, dir -> find_licenses(dir; allow_brute=false),
find_licenses_by_bruteforce, find_licenses_by_list_intersection)
results = method(joinpath(@__DIR__, ".."))
@test only(results) == fl
end
Expand All @@ -118,6 +119,8 @@ dorian_gray = """
@test fl.license_filename == "LICENSE"
@test fl.licenses_found == ["MIT"]
@test fl.license_file_percent_covered > 90
@test_throws ArgumentError LicenseCheck.license_table("nul_string_dir", ["file_with_nul_in_the_middle.txt"]; validate_strings = false)
@test_throws ArgumentError LicenseCheck.license_table("nul_string_dir",
["file_with_nul_in_the_middle.txt"];
validate_strings=false)
end
end

0 comments on commit 9fbc254

Please sign in to comment.