Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #35 from JuliaPackaging/sf/product_rework
Browse files Browse the repository at this point in the history
Product rework
  • Loading branch information
staticfloat authored Feb 19, 2018
2 parents 127331e + 41c611e commit 8755d9c
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 189 deletions.
15 changes: 6 additions & 9 deletions src/LoggingUtils.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
print_cache = Dict()

"""
@info_onchange(msg, key)
info_onchange(msg, key, location)
This macro is used to gate verbose messages within a function; within functions
such as `verify()`, we want to print out that we successfully verified a file
Expand All @@ -15,13 +15,10 @@ execution path is branch A, A, B, A, B, B, B, A then what will be printed is
message A, B, A, B, A. In essence, this method calls `info()` only when the
message generated by this particular method changes.
"""
macro info_onchange(msg, key)
return quote
local location = string(@__FILE__, @__LINE__)
local cache_val = get(print_cache, $(esc(key)), nothing)
if cache_val != location
info($(esc(msg)))
print_cache[$(esc(key))] = location
end
function info_onchange(msg, key, location)
local cache_val = get(print_cache, key, nothing)
if cache_val != location
info(msg)
print_cache[key] = location
end
end
12 changes: 10 additions & 2 deletions src/PlatformEngines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,11 @@ function download_verify(url::AbstractString, hash::AbstractString,
if isfile(dest)
file_existed = true
if verbose
info("Destination file $(dest) already exists, verifying...")
info_onchange(
"Destination file $(dest) already exists, verifying...",
"download_verify_$(dest)",
@__LINE__,
)
end

# verify download, if it passes, return happy. If it fails, (and
Expand All @@ -478,7 +482,11 @@ function download_verify(url::AbstractString, hash::AbstractString,
rethrow()
end
if verbose
info("Verification failed, re-downloading...")
info_onchange(
"Verification failed, re-downloading...",
"download_verify_$(dest)",
@__LINE__,
)
end
end
end
Expand Down
60 changes: 51 additions & 9 deletions src/Prefix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ function list_tarball_files(path::AbstractString; verbose::Bool = false)
end

"""
verify(path::String, hash::String; verbose::Bool)
verify(path::AbstractString, hash::AbstractString;
verbose::Bool = false, report_cache_status::Bool = false)
Given a file `path` and a `hash`, calculate the SHA256 of the file and compare
it to `hash`. If an error occurs, `verify()` will throw an error. This method
Expand All @@ -434,8 +435,14 @@ with the calculated hash being stored within the `".sha256"` file.. If a
contained within matches the given `hash` parameter, and its modification time
shows that the file located at `path` has not been modified since the last
verification.
If `report_cache_status` is set to `true`, then the return value will be a
`Symbol` giving a granular status report on the state of the hash cache, in
addition to the `true`/`false` signifying whether verification completed
successfully.
"""
function verify(path::AbstractString, hash::AbstractString; verbose::Bool = false)
function verify(path::AbstractString, hash::AbstractString; verbose::Bool = false,
report_cache_status::Bool = false)
if length(hash) != 64
msg = "Hash must be 256 bits (64 characters) long, "
msg *= "given hash is $(length(hash)) characters long"
Expand All @@ -444,6 +451,7 @@ function verify(path::AbstractString, hash::AbstractString; verbose::Bool = fals

# Fist, check to see if the hash cache is consistent
hash_path = "$(path).sha256"
status = :hash_consistent

# First, it must exist
if isfile(hash_path)
Expand All @@ -453,29 +461,59 @@ function verify(path::AbstractString, hash::AbstractString; verbose::Bool = fals
if stat(hash_path).mtime >= stat(path).mtime
# If all of that is true, then we're good!
if verbose
@info_onchange("Hash cache is consistent, returning true", "verify_$(hash_path)")
info_onchange(
"Hash cache is consistent, returning true",
"verify_$(hash_path)",
@__LINE__,
)
end
status = :hash_cache_consistent

# If we're reporting our status, then report it!
if report_cache_status
return true, status
else
return true
end
return true
else
if verbose
@info_onchange("File has been modified, hash cache invalidated", "verify_$(hash_path)")
info_onchange(
"File has been modified, hash cache invalidated",
"verify_$(hash_path)",
@__LINE__,
)
end
status = :file_modified
end
else
if verbose
@info_onchange("Verification hash mismatch, hash cache invalidated", "verify_$(hash_path)")
info_onchange(
"Verification hash mismatch, hash cache invalidated",
"verify_$(hash_path)",
@__LINE__,
)
end
status = :hash_cache_mismatch
end
else
if verbose
@info_onchange("No hash cache found", "verify_$(hash_path)")
info_onchange(
"No hash cache found",
"verify_$(hash_path)",
@__LINE__,
)
end
status = :hash_cache_missing
end

open(path) do file
calc_hash = bytes2hex(sha256(file))
if verbose
info("Calculated hash $calc_hash for file $path")
info_onchange(
"Calculated hash $calc_hash for file $path",
"hash_$(hash_path)",
@__LINE__,
)
end

if calc_hash != hash
Expand All @@ -491,7 +529,11 @@ function verify(path::AbstractString, hash::AbstractString; verbose::Bool = fals
write(file, hash)
end

return true
if report_cache_status
return true, status
else
return true
end
end

"""
Expand Down
Loading

0 comments on commit 8755d9c

Please sign in to comment.