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
Show file tree
Hide file tree
Changes from 5 commits
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
36 changes: 32 additions & 4 deletions src/Xpress.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,40 @@ include("helper.jl")
include("api.jl")
include("license.jl")

function initialize()
"""
initialize(;
liccheck::Function = identity,
verbose::Bool = true,
xpauth_path::String = ""
)

Performs license checking with `liccheck` validation function on dir
`xpauth_path`.

This function must be called before any XPRS functions can be called.

By default, `__init__` calls this with no keyword arguments.

## Example

```julia
ENV["XPRESS_JL_NO_AUTO_INIT"] = true
using Xpress
liccheck(x::Vector{Cint}) = Cint[xor(x[1], 0x0123)]
Xpress.initialize(;
liccheck = liccheck,
verbose = false,
xpauth_path = "/tmp/xpauth.xpr,
)
# Now you can use Xpress
```
"""
function initialize(; kwargs...)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joaquimg any reason to call userlic instead of this initialize? We'll keep the old behavior, but this might be simpler. No need for dlopen and XPRSinit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have had to load some libraries explicitly in some cases. For instance, xprl.dll on windows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. We should be able to fix that. The CI binaries work find? I think it should probably also work without the explicit dlopen. ccall should do it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can simply start using this new reasonable function and report back

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I removed the dlopen and CI still passing. So let me know

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good to document how to swap versions with he jll.
It is very common that we need specific versions to reproduce and debug

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does it find the dll with no dlopen?

libxprs is the full path, and that should be able to open any dependencies.

it would be good to document how to swap versions with he jll.

This PR is unrelated to the jll?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is working on my naive tests. Will test in production during the week!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're going to need to do a bunch of testing before tagging the next release (which I'll make as breaking, just to be on the safe side), objections to merging this now so you can test on master or with the Xpress_jll patch?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wait Monday night. Today is Sunday in Brazil :)

Libdl.dlopen(libxprs)
odow marked this conversation as resolved.
Show resolved Hide resolved
userlic()
userlic(; kwargs...)
Lib.XPRSinit(C_NULL)
odow marked this conversation as resolved.
Show resolved Hide resolved
# Calling free is not necessary since destroyprob is called
# in the finalizer.
# Calling XPRSfree is not necessary since XPRSdestroyprob is called in the
# finalizer.
return
end

Expand Down
156 changes: 62 additions & 94 deletions src/license.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,114 +3,82 @@
# 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)
function _get_xpauthpath(xpauth_path = "", verbose::Bool = true)
# The directory of the xprs shared object. This is the root from which we
# search for licenses.
libdir = dirname(libxprs)
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))
# Search in absolute path given by the user (assuming it was a file), and as
# a directory by appending XPAUTH.
candidates = [xpauth_path, joinpath(xpauth_path, XPAUTH)]
# Search in the directories of the XPAUTH_PATH and XPRESSDIR environnment
# variables.
for key in ("XPAUTH_PATH", "XPRESSDIR")
if haskey(ENV, key)
push!(candidates, joinpath(replace(ENV[key], "\"" => ""), XPAUTH))

Check warning on line 18 in src/license.jl

View check run for this annotation

Codecov / codecov/patch

src/license.jl#L18

Added line #L18 was not covered by tests
end
end

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

for i in candidates
if isfile(i)
# Search in `xpress/lib/../bin/xpauth.xpr`. This is a common location on
# Windows.
push!(candidates, joinpath(dirname(libdir), "bin", XPAUTH))
for candidate in candidates
# We assume a relative root directory of the shared library. If
# `candidate` is an absolute path, thhen joinpath will ignore libdir and
# return candidate.
filename = joinpath(libdir, candidate)
# If the file exists, we assume it is a license. We don't attempt to
# validate the contents of the file.
if isfile(filename)
if verbose && !haskey(ENV, "XPRESS_JL_NO_INFO")
@info("Xpress: Found license file $i")
@info("Xpress: Found license file $filename")
end
return i
return filename
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`
"""

# Keep `userlic` for backwards compatibility. PSR have a customized setup for
# managing licenses.
#
# New users should use `Xpress.initialize`.
function userlic(;
liccheck::Function = identity,
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
Comment on lines -71 to -73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can try removing this. We´d need to test the PR before merging

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've done it correctly, but try testing it.


# 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
verbose &= !haskey(ENV, "XPRESS_JL_NO_INFO")
# Open and free xpauth.xpr (touches the file to release it). It's not
# obvious why we need to touch the license file, but the code has done this
# since https://github.com/jump-dev/Xpress.jl/pull/9.
path_lic = _get_xpauthpath(xpauth_path, verbose)
touch(path_lic)
# Pre-allocate storage for the license integer. For backward compatibility,
# we use `Vector{Cint}`, because some users may have `liccheck` functions
# which rely on this.
license = Cint[1]
odow marked this conversation as resolved.
Show resolved Hide resolved
# First, call XPRSlicense to populate `license` with an integer. We don't
# check the return code.
_ = 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.
err = Lib.XPRSlicense(license, path_lic)
if !(err == 16 || err == 0)
@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)))
buffer = Vector{Cchar}(undef, 1024 * 8)
p_buffer = pointer(buffer)
GC.@preserve buffer begin
Lib.XPRSgetlicerrmsg(p_buffer, 1024)
throw(XpressError(err, unsafe_string(p_buffer)))
end
elseif verbose
type = err == 16 ? "Development" : "User"
@info("Xpress: $type license detected.")
end

# go back to initial folder
return cd(initdir)
return
end
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
using Test
using Xpress

function test_licensing()
# Create a bogus license file
xpauth_path = mktempdir()
filename = joinpath(xpauth_path, "xpauth.xpr")
write(filename, "bogus_license")
# Test that passing `""` can find our current license. This should already
# be the case because we managed to install and start the tests...
@test isfile(Xpress._get_xpauthpath("", false))
# Test that using the test directory cannot find a license.
@test_throws ErrorException Xpress._get_xpauthpath(@__DIR__, false)
# Now're going to test checking for new licenses. To do so, we first need to
# free the current one:
Xpress.Lib.XPRSfree()
# Then, we can check that using the root fails to find a license
@test_throws Xpress.XpressError Xpress.initialize(; xpauth_path)
# Now we need to re-initialize the license so that we can run other tests.
Xpress.initialize()
return
end

@testset "test_licensing" begin
# It is important that we test this first, so that there no XpressProblem
# objects with finalizers that may get run during the function call.
test_licensing()
end

println(Xpress.get_banner())
println("Optimizer version: $(Xpress.get_version())")

Expand Down
Loading