From aea160a791bd66a5c3b97b5f401200f06c88d4ce Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 19 Mar 2024 16:00:29 +1300 Subject: [PATCH] Refactor and tidy src/license.jl --- src/Xpress.jl | 53 ++++++++++++++++++++-- src/license.jl | 116 ------------------------------------------------- 2 files changed, 49 insertions(+), 120 deletions(-) delete mode 100644 src/license.jl diff --git a/src/Xpress.jl b/src/Xpress.jl index bcd1ce42..7d5693ef 100644 --- a/src/Xpress.jl +++ b/src/Xpress.jl @@ -29,14 +29,59 @@ include("Lib/Lib.jl") include("helper.jl") include("api.jl") include("xprs_callbacks.jl") -include("license.jl") + +function _get_xpauthpath(; verbose::Bool) + candidates = String[pwd(), dirname(libxprs)] + for key in ("XPAUTH_PATH", "XPRESS_DIR") + if haskey(ENV, key) + push!(candidates, replace(ENV[key], "\"" => "")) + end + end + push!(candidates, joinpath(dirname(dirname(libxprs)), "bin")) + for candidate in candidates + for filename in (candidate, joinpath(candidate, "xpauth.xpr")) + if isfile(filename) + if verbose + @info("Xpress: Found license file $filename") + end + return filename + end + end + end + return error( + "Could not find xpauth.xpr license file. Set the `XPRESSDIR` or " * + "`XPAUTH_PATH` environment variables.", + ) +end function initialize() Libdl.dlopen(libxprs) - userlic() + verbose = !haskey(ENV, "XPRESS_JL_NO_INFO") + path_lic = _get_xpauthpath(; verbose) + touch(path_lic) + lic = Ref{Cint}(1) + # TODO(odow): why do we call XPRSlicense twice? + Lib.XPRSlicense(lic, path_lic) + buffer = Vector{Cchar}(undef, 1024 * 8) + ierr = GC.@preserve buffer begin + Lib.XPRSlicense(lic, Cstring(pointer(buffer))) + end + if ierr == 16 + if verbose + @info("Xpress: Development license detected.") + end + elseif ierr == 0 + if verbose + @info("Xpress: User license detected.") + end + else + @info("Xpress: Failed to find working license.") + GC.@preserve buffer begin + Lib.XPRSgetlicerrmsg(pointer(buffer), 1024) + error(unsafe_string(pointer(buffer))) + end + end Lib.XPRSinit(C_NULL) - # Calling free is not necessary since destroyprob is called - # in the finalizer. return end diff --git a/src/license.jl b/src/license.jl deleted file mode 100644 index 665830df..00000000 --- a/src/license.jl +++ /dev/null @@ -1,116 +0,0 @@ -# Copyright (c) 2016: Joaquim Garcia, and contributors -# -# 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) - 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)) - end - - # userĀ“s lib dir - push!(candidates, joinpath(dirname(dirname(libxprs)), "bin", XPAUTH)) - - for i in candidates - if isfile(i) - if verbose && !haskey(ENV, "XPRESS_JL_NO_INFO") - @info("Xpress: Found license file $i") - end - return i - end - end - - return error( - "Could not find xpauth.xpr license file. Check 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` -""" -function userlic(; - verbose::Bool = true, - liccheck::Function = emptyliccheck, - xpauth_path::String = "", -) - - # change directory to reach all libs - initdir = pwd() - if isdir(dirname(libxprs)) - cd(dirname(libxprs)) - end - - # open and free xpauth.xpr (touches the file to release it) - path_lic = get_xpauthpath(xpauth_path, verbose) - touchlic(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) - - # 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") - @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") - @info("Xpress: User license detected.") - @info(unsafe_string(pointer(slicmsg))) - end - end - - # go back to initial folder - return cd(initdir) -end