-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,114 +3,78 @@ | |
# 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)) | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
path_lic = _get_xpauthpath(xpauth_path, verbose) | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thisinitialize
? We'll keep the old behavior, but this might be simpler. No need fordlopen
andXPRSinit
.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 knowThere was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libxprs
is the full path, and that should be able to open any dependencies.This PR is unrelated to the jll?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 theXpress_jll
patch?There was a problem hiding this comment.
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 :)