1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2009 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
3 // Copyright (C) 2011 - DIGITEO - Allan CORNET
5 // Copyright (C) 2012 - 2016 - Scilab Enterprises
7 // This file is hereby licensed under the terms of the GNU GPL v2.0,
8 // pursuant to article 5.3.4 of the CeCILL v.2.1.
9 // This file was originally licensed under the terms of the CeCILL v2.1,
10 // and continues to be available under such terms.
11 // For more information, see the COPYING file which you should have received
12 // along with this program.
16 function atomsDownload(url_in,file_out,md5sum)
18 // Operating system detection + Architecture detection
19 // =========================================================================
20 [OSNAME, ARCH, LINUX, MACOSX, SOLARIS,BSD] = atomsGetPlatform();
22 // Check input parameters number
23 // =========================================================================
26 if rhs < 2 | rhs > 3 then
27 error(msprintf(gettext("%s: Wrong number of input argument: %d to %d expected.\n"), "atomsDownload", 2, 3));
30 // Check input parameters type
31 // =========================================================================
32 if type(url_in) <> 10 then
33 error(msprintf(gettext("%s: Wrong type for input argument #%d: string expected.\n"), "atomsDownload", 1));
36 if type(file_out) <> 10 then
37 error(msprintf(gettext("%s: Wrong type for input argument #%d: string expected.\n"), "atomsDownload", 2));
40 if (rhs>2) & (type(md5sum) <> 10) then
41 error(msprintf(gettext("%s: Wrong type for input argument #%d: string expected.\n"), "atomsDownload", 3));
44 // Check input parameters size
45 // =========================================================================
47 if size(url_in, "*") <> 1 then
48 error(msprintf(gettext("%s: Wrong size for input argument #%d: string expected.\n"),"atomsDownload",1));
51 if size(file_out, "*") <> 1 then
52 error(msprintf(gettext("%s: Wrong size for input argument #%d: string expected.\n"),"atomsDownload",2));
55 if (rhs>2) & (size(md5sum,"*") <> 1) then
56 error(msprintf(gettext("%s: Wrong type for input argument #%d: string expected.\n"),"atomsDownload",3));
59 // Check input parameters value
60 // =========================================================================
62 if regexp(url_in,"/^(https?|ftp|file):\/\//","o") <> 1 then
63 error(msprintf(gettext("%s: Wrong value for input argument #%d: String that starts with ''http(s)?://'',''ftp://'' or ''file://'' expected.\n"),"atomsDownload",1));
66 if (rhs>2) & (length(md5sum)<>32) then
67 error(msprintf(gettext("%s: Wrong length for input argument #%d: String which has 32-characters length expected.\n"),"atomsDownload",3));
70 // curl, wget or httpdownload
71 // =========================================================================
77 // Maybe the detection has already been done
79 if atomsGetConfig("downloadTool") == "wget" then
81 elseif atomsGetConfig("downloadTool") == "curl" then
83 elseif atomsGetConfig("downloadTool") == "httpdownload" & getos() == "Windows" then
86 // Default values according to platform
87 if LINUX | SOLARIS | BSD then
89 // Need to detect under Linux platforms
90 [rep, stat, err] = unix_g("wget --version");
94 atomsSetConfig("downloadTool", "wget");
96 [rep, stat, err] = unix_g("curl --version");
99 atomsSetConfig("downloadTool", "curl");
101 error(msprintf(gettext("%s: Neither Wget or Curl found: Please install one of them\n"), "atomsDownload"));
104 elseif MACOSX | getos() == "Windows" then
106 atomsSetConfig("downloadTool", "curl");
111 // =========================================================================
114 if regexp(url_in, "/^(https?|ftp):\/\//", "o") == 1 then
119 // Timeout configuration
122 timeout_arg = " --connect-timeout ";
125 timeout_arg = " --timeout=";
128 timeout = strtod(atomsGetConfig("downloadTimeout"));
130 if ~isnan(timeout) then // Value found in config file
131 timeout_arg = timeout_arg + string(timeout);
132 else // Default timeout
133 timeout_arg = timeout_arg + "5";
136 timeout_arg = timeout_arg + " ";
138 // Proxy configuration
139 if (atomsGetConfig("useProxy") == "True") & (atomsGetConfig("proxyHost") <> "") then
141 proxy_host = atomsGetConfig("proxyHost");
144 if atomsGetConfig("proxyPort") <> "" then
145 proxy_host = proxy_host + ":" + atomsGetConfig("proxyPort");
148 // Host/port Argument
151 proxy_host_arg = " --proxy "+ proxy_host;
154 proxy_host_arg = " http_proxy="""+proxy_host+""" ";
158 if and([atomsGetConfig("proxyUser");atomsGetConfig("proxyPassword")]<> "") then
161 proxy_user_arg = " --proxy-user "+atomsGetConfig("proxyUser")+":"+atomsGetConfig("proxyPassword");
164 proxy_user_arg = " --proxy-user="""+atomsGetConfig("proxyUser")+""" --proxy-password="""+atomsGetConfig("proxyPassword")+"""";
170 if WGET & atomsGetConfig("useProxy") == "False" then
171 proxy_user_arg = " --no-proxy";
175 if getos() == "Windows" & CURL then
176 download_cmd = """" + pathconvert(SCI+"/tools/curl/curl.exe",%F) + """" + proxy_host_arg + proxy_user_arg + timeout_arg + " -s """ + url_in + """ -o """ + file_out + """";
179 download_cmd = "curl "+proxy_host_arg+proxy_user_arg+timeout_arg+" -s "+url_in + " -o " + file_out;
182 download_cmd = proxy_host_arg+"wget"+proxy_user_arg+timeout_arg+" "+url_in + " -O " + file_out;
185 winId = atomsOpenProgressBar(_("Download in progress... Please be patient."), %f);
188 [rep,stat,err] = unix_g(download_cmd);
190 // Second try with httpdownload
191 if ( HTTPDOWNLOAD | stat<>0 ) & (getos() == "Windows") then
192 imode = ilib_verbose();
194 id = link(SCI+"/bin/windows_tools.dll","httpdownload","c");
195 stat = call("httpdownload", url_in, 1, "c", file_out, 2, "c", "out", [1,1], 3, "d");
199 // Save the parameter to always download with httpdownload
201 atomsSetConfig("downloadTool","httpdownload");
204 case -1 then mprintf(gettext("%s: Error: the response status from the URL %s is invalid.\n"), "atomsDownload", url_in),
205 case -2 then mprintf(gettext("%s: Error while opening an Internet connection.\n"), "atomsDownload"),
206 case -3 then mprintf(gettext("%s: Error while opening the URL %s.\n"), "atomsDownload", url_in),
207 case -4 then mprintf(gettext("%s: Error while creating the file %s on disk.\n"), "atomsDownload", file_out),
208 case -5 then mprintf(gettext("%s: Error while retrieving the size of file at URL %s.\n"), "atomsDownload", url_in),
209 case -6 then mprintf(gettext("%s: Error while reading the file from the URL %s.\n"), "atomsDownload", url_in),
210 case -7 then mprintf(gettext("%s: Error while writing the file %s on disk.\n"), "atomsDownload", file_out),
211 case -8 then mprintf(gettext("%s: Error while downloading the file from the URL %s.\n"), "atomsDownload", url_in),
212 case -9 then mprintf(gettext("%s: Error: out of memory.\n"), "atomsDownload"),
218 mprintf(gettext("%s: The following file hasn''t been downloaded:\n"), "atomsDownload");
219 mprintf(gettext("\t - URL : ''%s''\n"), url_in);
220 mprintf(gettext("\t - Local location : ''%s''\n"), file_out);
222 atomsCloseProgressBar(winId);
223 error(strcat(err, ascii(10)));
227 elseif regexp(url_in,"/^file:\/\//","o") == 1 then
229 if getos() == "Windows" then
230 url_pattern = "file:///";
232 url_pattern = "file://";
235 file_in = pathconvert(part(url_in,length(url_pattern)+1:length(url_in)),%F);
237 if copyfile(file_in,file_out) <> 1 then
238 mprintf(gettext("%s: The following file has not been copied:\n"),"atomsDownload");
239 mprintf(gettext("\t - source : ''%s''\n"),file_in);
240 mprintf(gettext("\t - destination : ''%s''\n"),file_out);
241 atomsCloseProgressBar(winId);
242 error(strcat(lasterror(), ascii(10)));
247 // If md5sum is gived, check the md5sum of the downloaded file
248 // =========================================================================
252 filemd5 = getmd5(file_out);
254 if filemd5 <> md5sum then
255 mprintf(gettext("%s: The downloaded file does not match the MD5SUM:\n"),"atomsDownload");
256 mprintf(gettext("\t - file : ''%s''\n"),file_out);
257 mprintf(gettext("\t - MD5SUM expected : ''%s''\n"),md5sum);
258 mprintf(gettext("\t - MD5SUM watched : ''%s''\n"),filemd5);
259 atomsCloseProgressBar(winId);
265 // Close progress bar handle, if not closed yet
266 // =========================================================================
268 atomsCloseProgressBar(winId);