Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Fix sources #96

Merged
merged 4 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 22 additions & 15 deletions deps/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import ZipFile
const Maybe{T} = Union{T, Nothing}

"""
Verify a list of `sources` located in `source_dir`. If AGF files are missing or invalid, try to download them using the
Verify a list of `sources` located in `sourcedir`. If AGF files are missing or invalid, try to download them using the
information provided in `sources`.

Each `source ∈ sources` is a collection of strings in the format `name, sha256sum, url [, POST_data]`, where the last
Expand All @@ -36,18 +36,18 @@ sources (e.g. Sumita).

Modifies `sources` in-place such that only verified sources remain.
"""
function verify_sources!(sources::AbstractVector{<:AbstractVector{<:AbstractString}}, source_dir::AbstractString)
function verify_sources!(sources::AbstractVector{<:AbstractVector{<:AbstractString}}, sourcedir::AbstractString)
# track missing sources as we go and delete them afterwards to avoid modifying our iterator
missing_sources = []

for (i, source) in enumerate(sources)
name, sha256sum = source[1:2]
source_file = joinpath(source_dir, "$(name).agf")
verified = verify_source(source_file, sha256sum)
sourcefile = joinpath(sourcedir, "$(name).agf")
verified = verify_source(sourcefile, sha256sum)
if !verified && length(source) >= 3
# try downloading and re-verifying the source if download information is provided (sources[3:end])
download_source(source_file, source[3:end]...)
verified = verify_source(source_file, sha256sum)
download_source(sourcefile, source[3:end]...)
verified = verify_source(sourcefile, sha256sum)
end
if !verified
push!(missing_sources, i)
Expand All @@ -60,15 +60,15 @@ end
"""
Verify a source file using SHA256, returning true if successful. Otherwise, remove the file and return false.
"""
function verify_source(source_file::AbstractString, sha256sum::AbstractString)
if !isfile(source_file)
@info "[-] Missing file at $source_file"
elseif sha256sum == SHA.bytes2hex(SHA.sha256(read(source_file)))
@info "[✓] Verified file at $source_file"
function verify_source(sourcefile::AbstractString, sha256sum::AbstractString)
if !isfile(sourcefile)
@info "[-] Missing file at $sourcefile"
elseif sha256sum == SHA.bytes2hex(SHA.sha256(read(sourcefile)))
@info "[✓] Verified file at $sourcefile"
return true
else
@info "[x] Removing unverified file at $source_file"
rm(source_file)
@info "[x] Removing unverified file at $sourcefile"
rm(sourcefile)
end
return false
end
Expand All @@ -81,8 +81,15 @@ function download_source(sourcefile::AbstractString, url::AbstractString, POST_d
try
headers = ["Content-Type" => "application/x-www-form-urlencoded"]
resp = isnothing(POST_data) ? HTTP.get(url) : HTTP.post(url, headers, POST_data)
reader = ZipFile.Reader(IOBuffer(resp.body))
write(sourcefile, read(reader.files[end])) # todo detect .agf file(s)

# save agf file, unzipping if necessary
if endswith(url, ".agf")
agfdata = resp.body
else
reader = ZipFile.Reader(IOBuffer(resp.body))
agfdata = read(reader.files[findfirst(f -> endswith(lowercase(f.name), ".agf"), reader.files)])
end
write(sourcefile, agfdata)
catch e
@error e
end
Expand Down
6 changes: 3 additions & 3 deletions deps/sources.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NIKON a49714470fa875ad4fd8d11cbc0edbf1adfe877f42926d7612c1649dd9441e75 https://www.nikon.com/products/optical-glass/assets/pdf/nikon_zemax_data.zip
NIKON a49714470fa875ad4fd8d11cbc0edbf1adfe877f42926d7612c1649dd9441e75 https://www.nikon.com/products/components/assets/pdf/nikon_zemax_data.zip
OHARA 0c9021bf11b8d4e660012191818685ad3110d4f9490699cabdc89aae1fd26d2e https://www.oharacorp.com/xls/OHARA_201130_CATALOG.zip
HOYA 9ca6b2bda157ba18333607f07a2af2458d7b097b86651ebb0743644b0ee96823 https://hoyaoptics.com/wp-content/uploads/2019/10/HOYA20170401.zip
HOYA b02c203e5a5b7a8918cc786badf0a9db1fe2572372c1c163dc306b0a8a908854 http://www.hoya-opticalworld.com/common/agf/HOYA20210105.agf
SCHOTT e9aabbb8ebff116ba0c106a71afd86e72f2a397ac9bc447469129e325e795f4e https://www.schott.com/d/advanced_optics/6959f9a4-0e4f-4ef2-a302-2347468a82f5/1.31/schott-optical-glass-overview-zemax-format.zip
Sumita 6cbc566d451877e36aecd9e8be516962d8d120e70fba770c566811337aa4414d https://www.sumita-opt.co.jp/en/wp/wp-content/themes/sumita-opt-en/download-files.php files%5B%5D=zemax.agf&category=data
Sumita c1093e42a1d08acbe30698aba730161e3b43f8f0d50533f65de8b6b11100fdc8 https://www.sumita-opt.co.jp/en/wp/wp-content/themes/sumita-opt-en/download-files.php files%5B%5D=new_sumita.agf&category=data
NHG b3db4c677b1ac67cd9cce5be2abff88d13bcf24e4349dc789b2da12fdfe1a280