CMake/Source/CTest/cmCTestSubmit.cxx

397 lines
10 KiB
C++
Raw Normal View History

2003-01-07 07:07:24 +03:00
/*=========================================================================
2003-08-11 20:19:46 +04:00
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
2003-01-07 07:07:24 +03:00
2003-08-11 20:19:46 +04:00
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
2003-01-07 07:07:24 +03:00
2003-08-11 20:19:46 +04:00
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
2003-01-07 07:07:24 +03:00
=========================================================================*/
#include "cmCTestSubmit.h"
#include "cmSystemTools.h"
2003-01-08 06:24:45 +03:00
#include "curl/curl.h"
#include <sys/stat.h>
cmCTestSubmit::cmCTestSubmit() : m_HTTPProxy(), m_FTPProxy()
{
2003-04-02 00:30:32 +04:00
m_Verbose = false;
m_HTTPProxy = "";
m_HTTPProxyType = 0;
if ( getenv("HTTP_PROXY") )
{
m_HTTPProxyType = 1;
m_HTTPProxy = getenv("HTTP_PROXY");
if ( getenv("HTTP_PROXY_PORT") )
{
m_HTTPProxy += ":";
m_HTTPProxy += getenv("HTTP_PROXY_PORT");
}
if ( getenv("HTTP_PROXY_TYPE") )
{
cmStdString type = getenv("HTTP_PROXY_TYPE");
// HTTP/SOCKS4/SOCKS5
if ( type == "HTTP" )
{
m_HTTPProxyType = 1;
}
else if ( type == "SOCKS4" )
{
m_HTTPProxyType = 2;
}
else if ( type == "SOCKS5" )
{
m_HTTPProxyType = 3;
}
}
}
m_FTPProxy = "";
m_FTPProxyType = 0;
if ( getenv("FTP_PROXY") )
{
m_FTPProxyType = 1;
m_FTPProxy = getenv("FTP_PROXY");
if ( getenv("FTP_PROXY_PORT") )
{
m_FTPProxy += ":";
m_FTPProxy += getenv("FTP_PROXY_PORT");
}
if ( getenv("FTP_PROXY_TYPE") )
{
cmStdString type = getenv("FTP_PROXY_TYPE");
// HTTP/SOCKS4/SOCKS5
if ( type == "HTTP" )
{
m_FTPProxyType = 1;
}
else if ( type == "SOCKS4" )
{
m_FTPProxyType = 2;
}
else if ( type == "SOCKS5" )
{
m_FTPProxyType = 3;
}
}
}
2003-08-11 20:19:46 +04:00
if ( m_HTTPProxy.size() > 0 )
{
std::cout << " Use HTTP Proxy: " << m_HTTPProxy << std::endl;
}
if ( m_FTPProxy.size() > 0 )
{
std::cout << " Use FTP Proxy: " << m_FTPProxy << std::endl;
}
}
bool cmCTestSubmit::SubmitUsingFTP(const cmStdString& localprefix,
const std::vector<cmStdString>& files,
const cmStdString& remoteprefix,
const cmStdString& url)
2003-01-07 07:07:24 +03:00
{
2003-01-08 06:24:45 +03:00
CURL *curl;
CURLcode res;
FILE* ftpfile;
char error_buffer[1024];
2003-01-08 06:24:45 +03:00
/* In windows, this will init the winsock stuff */
::curl_global_init(CURL_GLOBAL_ALL);
2003-05-02 21:54:05 +04:00
cmStdString::size_type cc;
for ( cc = 0; cc < files.size(); cc ++ )
2003-01-07 07:07:24 +03:00
{
/* get a curl handle */
curl = curl_easy_init();
if(curl)
{
// Using proxy
if ( m_FTPProxyType > 0 )
{
curl_easy_setopt(curl, CURLOPT_PROXY, m_FTPProxy.c_str());
switch (m_FTPProxyType)
{
2003-08-11 20:19:46 +04:00
case 2:
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
break;
case 3:
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
break;
default:
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
}
}
2003-08-11 20:19:46 +04:00
// enable uploading
::curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
2003-08-11 20:19:46 +04:00
cmStdString local_file = localprefix + "/" + files[cc];
cmStdString upload_as = url + "/" + remoteprefix + files[cc];
2003-08-11 20:19:46 +04:00
2003-01-08 06:24:45 +03:00
struct stat st;
if ( ::stat(local_file.c_str(), &st) )
{
return false;
}
ftpfile = ::fopen(local_file.c_str(), "rb");
2003-04-02 18:19:27 +04:00
if ( m_Verbose )
{
2003-08-11 20:19:46 +04:00
std::cout << " Upload file: " << local_file.c_str() << " to "
<< upload_as.c_str() << std::endl;
2003-04-02 18:19:27 +04:00
}
2003-01-08 06:24:45 +03:00
2003-04-02 00:30:32 +04:00
if ( m_Verbose )
{
::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}
2003-01-08 06:24:45 +03:00
// specify target
::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
2003-08-11 20:19:46 +04:00
2003-01-08 06:24:45 +03:00
// now specify which file to upload
::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
// and give the size of the upload (optional)
::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(st.st_size));
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
2003-01-08 06:24:45 +03:00
// Now run off and do what you've been told!
res = ::curl_easy_perform(curl);
fclose(ftpfile);
if ( res )
{
2003-08-11 20:19:46 +04:00
std::cout << " Error when uploading file: " << local_file.c_str() << std::endl;
std::cout << " Error message was: " << error_buffer << std::endl;
2003-01-08 06:24:45 +03:00
::curl_easy_cleanup(curl);
::curl_global_cleanup();
return false;
}
// always cleanup
::curl_easy_cleanup(curl);
2003-08-11 20:19:46 +04:00
std::cout << " Uploaded: " + local_file << std::endl;
2003-01-08 06:24:45 +03:00
}
2003-01-07 07:07:24 +03:00
}
2003-01-08 06:24:45 +03:00
::curl_global_cleanup();
return true;
}
// Uploading files is simpler
bool cmCTestSubmit::SubmitUsingHTTP(const cmStdString& localprefix,
const std::vector<cmStdString>& files,
const cmStdString& remoteprefix,
const cmStdString& url)
{
CURL *curl;
CURLcode res;
FILE* ftpfile;
/* In windows, this will init the winsock stuff */
::curl_global_init(CURL_GLOBAL_ALL);
cmStdString::size_type cc, kk;
2003-01-16 20:30:02 +03:00
for ( cc = 0; cc < files.size(); cc ++ )
{
2003-01-16 20:30:02 +03:00
/* get a curl handle */
curl = curl_easy_init();
if(curl)
{
2003-01-16 20:30:02 +03:00
// Using proxy
if ( m_HTTPProxyType > 0 )
{
2003-01-16 20:30:02 +03:00
curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
switch (m_HTTPProxyType)
{
2003-08-11 20:19:46 +04:00
case 2:
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
break;
case 3:
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
break;
default:
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
2003-01-16 20:30:02 +03:00
}
}
2003-01-16 20:30:02 +03:00
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
/* HTTP PUT please */
curl_easy_setopt(curl, CURLOPT_PUT, TRUE);
2003-04-02 00:30:32 +04:00
if ( m_Verbose )
{
::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}
2003-01-16 20:30:02 +03:00
cmStdString local_file = localprefix + "/" + files[cc];
cmStdString remote_file = remoteprefix + files[cc];
cmStdString ofile = "";
2003-01-16 20:30:02 +03:00
for ( kk = 0; kk < remote_file.size(); kk ++ )
{
char c = remote_file[kk];
char hex[4] = { 0, 0, 0, 0 };
hex[0] = c;
switch ( c )
{
2003-08-11 20:19:46 +04:00
case '+':
case '?':
case '/':
case '\\':
case '&':
case ' ':
case '=':
case '%':
sprintf(hex, "%%%02X", (int)c);
ofile.append(hex);
break;
default:
ofile.append(hex);
2003-01-16 20:30:02 +03:00
}
}
cmStdString upload_as = url + "?FileName=" + ofile;
2003-01-16 20:30:02 +03:00
struct stat st;
if ( ::stat(local_file.c_str(), &st) )
{
return false;
}
ftpfile = ::fopen(local_file.c_str(), "rb");
2003-04-02 00:30:32 +04:00
if ( m_Verbose )
{
2003-08-11 20:19:46 +04:00
std::cout << " Upload file: " << local_file.c_str() << " to "
2003-04-02 00:30:32 +04:00
<< upload_as.c_str() << " Size: " << st.st_size << std::endl;
}
2003-01-16 20:30:02 +03:00
2003-08-11 20:19:46 +04:00
// specify target
::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
2003-01-16 20:30:02 +03:00
// now specify which file to upload
::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
// and give the size of the upload (optional)
::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(st.st_size));
2003-01-16 20:30:02 +03:00
// Now run off and do what you've been told!
res = ::curl_easy_perform(curl);
2003-01-16 20:30:02 +03:00
fclose(ftpfile);
if ( res )
{
2003-08-11 20:19:46 +04:00
std::cout << " Error when uploading file: " << local_file.c_str() << std::endl;
::curl_easy_cleanup(curl);
::curl_global_cleanup();
return false;
}
2003-01-16 20:30:02 +03:00
// always cleanup
::curl_easy_cleanup(curl);
2003-08-11 20:19:46 +04:00
std::cout << " Uploaded: " + local_file << std::endl;
}
}
::curl_global_cleanup();
return true;
2003-01-07 07:07:24 +03:00
}
bool cmCTestSubmit::TriggerUsingHTTP(const std::vector<cmStdString>& files,
const cmStdString& remoteprefix,
const cmStdString& url)
2003-01-12 05:47:35 +03:00
{
CURL *curl;
/* In windows, this will init the winsock stuff */
::curl_global_init(CURL_GLOBAL_ALL);
2003-08-11 20:19:46 +04:00
cmStdString::size_type cc, kk;
2003-01-16 21:02:11 +03:00
for ( cc = 0; cc < files.size(); cc ++ )
2003-01-12 05:47:35 +03:00
{
2003-01-16 21:02:11 +03:00
/* get a curl handle */
curl = curl_easy_init();
if(curl)
2003-01-16 20:45:24 +03:00
{
2003-01-16 21:02:11 +03:00
// Using proxy
if ( m_HTTPProxyType > 0 )
2003-01-16 20:45:24 +03:00
{
2003-01-16 21:02:11 +03:00
curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
switch (m_HTTPProxyType)
{
2003-08-11 20:19:46 +04:00
case 2:
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
break;
case 3:
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
break;
default:
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
2003-01-16 21:02:11 +03:00
}
2003-01-16 20:45:24 +03:00
}
2003-04-02 00:30:32 +04:00
::curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
if ( m_Verbose )
{
::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}
cmStdString file = remoteprefix + files[cc];
cmStdString ofile = "";
2003-01-12 05:47:35 +03:00
for ( kk = 0; kk < file.size(); kk ++ )
{
char c = file[kk];
char hex[4] = { 0, 0, 0, 0 };
hex[0] = c;
switch ( c )
{
2003-08-11 20:19:46 +04:00
case '+':
case '?':
case '/':
case '\\':
case '&':
case ' ':
case '=':
case '%':
sprintf(hex, "%%%02X", (int)c);
ofile.append(hex);
break;
default:
ofile.append(hex);
2003-01-12 05:47:35 +03:00
}
}
cmStdString turl = url + "?xmlfile=" + ofile;
2003-04-02 00:30:32 +04:00
if ( m_Verbose )
{
2003-08-11 20:19:46 +04:00
std::cout << " Trigger url: " << turl.c_str() << std::endl;
2003-04-02 00:30:32 +04:00
}
2003-01-12 05:47:35 +03:00
curl_easy_setopt(curl, CURLOPT_URL, turl.c_str());
2003-05-28 17:21:37 +04:00
if ( curl_easy_perform(curl) )
2003-01-12 05:47:35 +03:00
{
2003-08-11 20:19:46 +04:00
std::cout << " Error when triggering: " << turl.c_str() << std::endl;
2003-01-12 05:47:35 +03:00
::curl_easy_cleanup(curl);
::curl_global_cleanup();
return false;
}
2003-01-16 21:02:11 +03:00
// always cleanup
::curl_easy_cleanup(curl);
2003-05-02 21:54:05 +04:00
std::cout << std::endl;
2003-01-12 05:47:35 +03:00
}
}
::curl_global_cleanup();
2003-08-11 20:19:46 +04:00
std::cout << " Dart server triggered..." << std::endl;
2003-01-12 05:47:35 +03:00
return true;
}
bool cmCTestSubmit::SubmitUsingSCP(const cmStdString&,
const std::vector<cmStdString>&,
const cmStdString&,
const cmStdString&)
2003-01-07 07:07:24 +03:00
{
std::cout << "SubmitUsingSCP is not yet implemented" << std::endl;
2003-01-10 15:50:29 +03:00
return false;
2003-01-07 07:07:24 +03:00
}