Skip to content

Commit

Permalink
better handling of eq_zip download/extract. Allow .zip, .tar, .zstd
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Mar 17, 2021
1 parent 280c90f commit 5025a02
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
32 changes: 32 additions & 0 deletions +gemini3d/+fileio/extract_zstd.m
@@ -0,0 +1,32 @@
function extract_zstd(archive, out_dir)

if ~isfile(archive)
error("%s is not a file", archive)
end

[ret, ~] = system("zstd -h");
if ret ~= 0
if ismac
msg = "brew install zstd";
elseif isunix
msg = "apt install zstd";
else
msg = "https://github.com/facebook/zstd/releases extract and put that directory on PATH";
end
error("need to have Zstd installed: \n install zstd by: \n %s", msg)
end

[p, n] = fileparts(archive);
tar_arc = fullfile(p, n + ".tar");
if isfile(tar_arc)
delete(tar_arc)
end

ret = system("zstd -d " + archive + " -o " + tar_arc);
if ret ~= 0
error("problem extracting %s", archive)
end

untar(tar_arc, out_dir)

end
1 change: 0 additions & 1 deletion +gemini3d/+fileio/make_valid_paths.m
Expand Up @@ -38,7 +38,6 @@
end
end


end % function


Expand Down
46 changes: 29 additions & 17 deletions +gemini3d/+model/eq2dist.m
Expand Up @@ -46,25 +46,37 @@

function peq = read_equilibrium(p)

if ~isfolder(p.eq_dir)
if isfield(p, 'eq_url')
if ~isfile(p.eq_zip)
if isfield(p, 'ssl_verify') && ~p.ssl_verify
% disable SSL, better to fix your SSL certificates as arbitrary
% code can be downloaded
web_opt = weboptions('CertificateFilename', '');
elseif isfile(getenv("SSL_CERT_FILE"))
web_opt = weboptions('CertificateFilename', getenv("SSL_CERT_FILE"));
else
web_opt = weboptions('CertificateFilename', 'default');
end
gemini3d.fileio.makedir(p.eq_dir)
websave(p.eq_zip, p.eq_url, web_opt);
end
unzip(p.eq_zip, fullfile(p.eq_dir, '..'))
if isfolder(p.eq_dir)
peq = gemini3d.read.config(p.eq_dir);
return
end

if ~all(isfield(p, ["eq_url", "eq_zip"]))
error("gemini3d:model:eq2dist", "run equilibrium simulation in %s \n OR specify eq_url and eq_zip in %s", p.eq_dir, p.nml)
end

if ~isfile(p.eq_zip)
if isfield(p, 'ssl_verify') && ~p.ssl_verify
% disable SSL, better to fix your SSL certificates as arbitrary
% code can be downloaded
web_opt = weboptions('CertificateFilename', '');
elseif isfile(getenv("SSL_CERT_FILE"))
web_opt = weboptions('CertificateFilename', getenv("SSL_CERT_FILE"));
else
error('eq2dist:file_not_found', '%s not found--was the equilibrium simulation run first? Or specify eq_url and eq_zip to download.', p.eq_dir)
web_opt = weboptions('CertificateFilename', 'default');
end
gemini3d.fileio.makedir(p.eq_dir)
websave(p.eq_zip, p.eq_url, web_opt);
end

[~,~,arc_type] = fileparts(p.eq_zip);

switch arc_type
case ".zip", unzip(p.eq_zip, fullfile(p.eq_dir, '..'))
% old zip files had vestigial folder of same name instead of just files
case ".tar", untar(p.eq_zip, p.eq_dir)
case {".zst", ".zstd"}, gemini3d.fileio.extract_zstd(p.eq_zip, p.eq_dir)
otherwise, error("gemini3d:model:eq2dist", "unknown equilibrium archive type: " + arc_type)
end

peq = gemini3d.read.config(p.eq_dir);
Expand Down

0 comments on commit 5025a02

Please sign in to comment.