Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor src/license.jl #251

Merged
merged 7 commits into from
Mar 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 41 additions & 78 deletions src/license.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,114 +3,77 @@
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

# lic checking file
# -----------------

# license check empty function
# ----------------------------
function emptyliccheck(lic::Vector{Cint})
return lic
end

function touchlic(path)
f = open(path)
return close(f)
end

function get_xpauthpath(xpauth_path = "", verbose::Bool = true)
odow marked this conversation as resolved.
Show resolved Hide resolved
XPAUTH = "xpauth.xpr"

candidates = []

# user sent the complete path
push!(candidates, xpauth_path)

# user sent directory
push!(candidates, joinpath(xpauth_path, XPAUTH))

# default env (not metioned in manual)
if haskey(ENV, "XPAUTH_PATH")
xpauth_path = replace(ENV["XPAUTH_PATH"], "\"" => "")
push!(candidates, joinpath(xpauth_path, XPAUTH))
end

# default lib dir
if haskey(ENV, "XPRESSDIR")
xpressdir = replace(ENV["XPRESSDIR"], "\"" => "")
push!(candidates, joinpath(xpressdir, "bin", XPAUTH))
candidates = [xpauth_path, joinpath(xpauth_path, XPAUTH)]
for key in ("XPAUTH_PATH", "XPRESSDIR")
if haskey(ENV, key)
push!(candidates, joinpath(replace(ENV[key], "\"" => ""), XPAUTH))
end
end

# user´s lib dir
push!(candidates, joinpath(dirname(dirname(libxprs)), "bin", XPAUTH))

for i in candidates
if isfile(i)
for candidate in candidates
if isfile(candidate)
if verbose && !haskey(ENV, "XPRESS_JL_NO_INFO")
@info("Xpress: Found license file $i")
@info("Xpress: Found license file $candidate")
end
return i
return candidate
end
end

return error(
"Could not find xpauth.xpr license file. Check XPRESSDIR or XPAUTH_PATH environment variables.",
"Could not find xpauth.xpr license file. Set the `XPRESSDIR` or " *
"`XPAUTH_PATH` environment variables.",
)
end

"""
userlic(; liccheck::Function = emptyliccheck, xpauth_path::String = "" )
Performs license chhecking with `liccheck` validation function on dir `xpauth_path`
userlic(;
liccheck::Function = identity,
verbose::Bool = true,
xpauth_path::String = ""
)

Performs license chhecking with `liccheck` validation function on dir
`xpauth_path`
"""
function userlic(;
liccheck::Function = identity,
verbose::Bool = true,
liccheck::Function = emptyliccheck,
xpauth_path::String = "",
)

verbose &= !haskey(ENV, "XPRESS_JL_NO_INFO")
# change directory to reach all libs
initdir = pwd()
if isdir(dirname(libxprs))
cd(dirname(libxprs))
end
odow marked this conversation as resolved.
Show resolved Hide resolved

# open and free xpauth.xpr (touches the file to release it)
path_lic = get_xpauthpath(xpauth_path, verbose)
touchlic(path_lic)

touch(path_lic)
# pre allocate vars
lic = Cint[1]
slicmsg = path_lic #xpauth_path == "dh" ? Array{Cchar}(undef, 1024*8) :

# FIRST call do xprslicense to get BASE LIC
Lib.XPRSlicense(lic, slicmsg)

# convert BASE LIC to GIVEN LIC
lic = liccheck(lic)

license = Cint[1]
odow marked this conversation as resolved.
Show resolved Hide resolved
# First, call XPRSlicense to populate `license` with an integer.
Lib.XPRSlicense(license, path_lic)
# Then, for some licenses, we need to modify the license integer by a
# secret password.
license = liccheck(license)
# Now, we need to send the password back to Xpress
# Send GIVEN LIC to XPRESS lib
buffer = Array{Cchar}(undef, 1024 * 8)
buffer_p = pointer(buffer)
ierr = GC.@preserve buffer begin
Lib.XPRSlicense(lic, Cstring(buffer_p))
end

# check LIC TYPE
if ierr == 16
# DEVELOPER
if verbose && !haskey(ENV, "XPRESS_JL_NO_INFO")
err = Lib.XPRSlicense(license, path_lic)
if err == 16 # DEVELOPER
if verbose
@info("Xpress: Development license detected.")
end
elseif ierr != 0
# FAIL
@info("Xpress: Failed to find working license.")
error(getlicerrmsg())
else
# USER
if verbose && !haskey(ENV, "XPRESS_JL_NO_INFO")
elseif err == 0 # USER
if verbose

Check warning on line 68 in src/license.jl

View check run for this annotation

Codecov / codecov/patch

src/license.jl#L67-L68

Added lines #L67 - L68 were not covered by tests
@info("Xpress: User license detected.")
@info(unsafe_string(pointer(slicmsg)))
@info(path_lic)

Check warning on line 70 in src/license.jl

View check run for this annotation

Codecov / codecov/patch

src/license.jl#L70

Added line #L70 was not covered by tests
end
else
@info("Xpress: Failed to find working license.")
error(getlicerrmsg())

Check warning on line 74 in src/license.jl

View check run for this annotation

Codecov / codecov/patch

src/license.jl#L73-L74

Added lines #L73 - L74 were not covered by tests
end

# go back to initial folder
return cd(initdir)
cd(initdir)
return
end
Loading