Skip to content

Commit

Permalink
PD-4598 Add CURL error messages to AMRFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyacheslav Brover committed May 6, 2023
1 parent d61f6a1 commit d437940
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 133 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ release: clean
make all

common.o: common.hpp common.inc
curl_easy.o: curl_easy.hpp common.hpp common.inc
gff.o: gff.hpp common.hpp common.inc
alignment.o: alignment.hpp alignment.hpp common.inc

Expand All @@ -106,7 +107,7 @@ amrfinder: $(amrfinderOBJS)
$(CXX) -o $@ $(amrfinderOBJS) -pthread $(DBDIR)

amrfinder_update.o: common.hpp common.inc
amrfinder_updateOBJS=amrfinder_update.o common.o
amrfinder_updateOBJS=amrfinder_update.o common.o curl_easy.o
amrfinder_update: $(amrfinder_updateOBJS)
@if [ "$(TEST_UPDATE)" != "" ] ; \
then \
Expand Down
2 changes: 2 additions & 0 deletions amrfinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* gunzip (optional)
*
* Release changes:
* 05/06/2023 PD-4598 error messages in curl_easy.cpp
* 3.11.14 05/05/2023 extern "C" { #include <curl.h> }
* 3.11.13 05/04/2023 PD-4596 Prohibit ASCII characters only between 0x00 and 0x1F in GFF files
* 04/24/2023 PD-4583 Process files ending with ".gz", see https://github.com/ncbi/amr/issues/61, dependence on gunzip (optional)
* 04/19/2023 On failure no empty output file (-o) is created
Expand Down
128 changes: 10 additions & 118 deletions amrfinder_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,24 @@
* File Description:
* Updating of AMRFinder data
*
* Dependencies: curl.{h,c}
*
* Release changes: see amrfinder.cpp
*
*/



#define HTTPS // Otherwise: FTP
#define HTTPS 1 // 0: FTP



#ifdef _MSC_VER
#error "UNIX is required"
#endif

#undef NDEBUG
#include "common.inc"

#include <unistd.h>
#include <curl/curl.h>

#include "common.hpp"
using namespace Common_sp;



string curMinor;
#include "curl_easy.hpp"
using namespace CURL_sp;



Expand All @@ -66,111 +56,10 @@ namespace



struct Curl
{
CURL* eh {nullptr};


Curl ()
{ eh = curl_easy_init ();
QC_ASSERT (eh);
#ifndef HTTPS
curl_easy_setopt (eh, CURLOPT_FTP_USE_EPSV, 0);
#endif
}
~Curl ()
{ curl_easy_cleanup (eh); }


void download (const string &url,
const string &fName);
string read (const string &url);
};



size_t write_stream_cb (char* ptr,
size_t size,
size_t nMemb,
void* userData)
{
ASSERT (ptr);
ASSERT (size == 1);
ASSERT (userData);

OFStream& f = * static_cast <OFStream*> (userData);
FOR (size_t, i, nMemb)
f << ptr [i];;

return nMemb;
}



void Curl::download (const string &url,
const string &fName)
{
ASSERT (! url. empty ());
ASSERT (! fName. empty ());

{
OFStream f (fName);
curl_easy_setopt (eh, CURLOPT_URL, url. c_str ());
curl_easy_setopt (eh, CURLOPT_WRITEFUNCTION, write_stream_cb);
curl_easy_setopt (eh, CURLOPT_WRITEDATA, & f);
if (curl_easy_perform (eh))
throw runtime_error ("CURL: Cannot download from " + url);
}

ifstream f (fName);
string s;
f >> s;
if (s == "<?xml")
throw runtime_error ("Cannot download " + strQuote (fName));
}



size_t write_string_cb (char* ptr,
size_t size,
size_t nMemb,
void* userData)
{
ASSERT (ptr);
ASSERT (size == 1);
ASSERT (userData);

string& s = * static_cast <string*> (userData);
FOR (size_t, i, nMemb)
s += ptr [i];;

return nMemb;
}



string Curl::read (const string &url)
{
ASSERT (! url. empty ());

string s; s. reserve (1024); // PAR
curl_easy_setopt (eh, CURLOPT_URL, url. c_str ());
curl_easy_setopt (eh, CURLOPT_WRITEFUNCTION, write_string_cb);
curl_easy_setopt (eh, CURLOPT_WRITEDATA, & s);
if (curl_easy_perform (eh))
throw runtime_error ("CURL: Cannot read from " + url);

return s;
}

//



#ifdef TEST_UPDATE
#define URL "https://ftp.ncbi.nlm.nih.gov/pathogen/Technical/AMRFinder_technical/test_database/"
#else
#ifdef HTTPS
#if HTTPS
#define URL "https://ftp.ncbi.nlm.nih.gov/pathogen/Antimicrobial_resistance/AMRFinderPlus/database/"
#else
#define URL "ftp://ftp.ncbi.nlm.nih.gov/pathogen/Antimicrobial_resistance/AMRFinderPlus/database/"
Expand All @@ -191,7 +80,7 @@ string getLatestMinor (Curl &curl)

Vector<SoftwareVersion> vers;
for (string& line : dir)
#ifdef HTTPS
#if HTTPS
if (isLeft (line, "<a href="))
try
{
Expand Down Expand Up @@ -247,7 +136,7 @@ string getLatestDataVersion (Curl &curl,

Vector<DataVersion> dataVersions;
for (string& line : dir)
#ifdef HTTPS
#if HTTPS
if (isLeft (line, "<a href="))
try
{
Expand Down Expand Up @@ -308,6 +197,9 @@ void fetchAMRFile (Curl &curl,

struct ThisApplication : ShellApplication
{
string curMinor;


ThisApplication ()
: ShellApplication ("Update the database for AMRFinder from " URL "\n\
Requirement: the database directory contains subdirectories named by database versions.\
Expand Down Expand Up @@ -361,7 +253,7 @@ Requirement: the database directory contains subdirectories named by database ve
const Verbose vrb (qc_on);


Curl curl;
Curl curl;


// FTP site files
Expand Down
12 changes: 6 additions & 6 deletions common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ namespace
*os << endl
<< msg << endl << endl
#ifndef _MSC_VER
<< "HOSTNAME: " << (hostname ? hostname : "?") << endl
<< "SHELL: " << (shell ? shell : "?") << endl
<< "PWD: " << (pwd ? pwd : "?") << endl
<< "PATH: " << (path ? path : "?") << endl
<< "HOSTNAME: " << nvl (hostname, "?") << endl
<< "SHELL: " << nvl (shell, "?") << endl
<< "PWD: " << nvl (pwd, "?") << endl
<< "PATH: " << nvl (path, "?") << endl
#endif
<< "Progam name: " << programName << endl
<< "Command line: " << getCommandLine () << endl;
Expand Down Expand Up @@ -482,8 +482,8 @@ string pad (const string &s,

switch (right)
{
case efalse: return sp + s;
case etrue: return s + sp;
case efalse: return s + sp;
case etrue: return sp + s;
case enull:
{
const size_t half = sp. size () / 2;
Expand Down
40 changes: 33 additions & 7 deletions common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,14 @@ struct List : list<T>



// char*

inline const char* nvl (const char* s,
const char* nullS = "-")
{ return s ? s : nullS; }



// string

extern const string noString;
Expand Down Expand Up @@ -4022,19 +4030,27 @@ struct NumberItemGenerator : ItemGenerator

struct SoftwareVersion : Root
{
uint major {0};
uint minor {0};
uint major {0}; // there is ::major()
uint minor {0}; // there is ::minor()
uint patch {0};


SoftwareVersion () = default;
SoftwareVersion (uint major_arg,
uint minor_arg,
uint patch_arg)
{ major = major_arg;
minor = minor_arg;
patch = patch_arg;
}
explicit SoftwareVersion (const string &fName);
explicit SoftwareVersion (istream &is,
bool minorOnly = false);
private:
void init (string &&s,
bool minorOnly);
public:
void saveText (ostream &os) const override
void saveText (ostream &os) const final
{ os << major << '.' << minor << '.' << patch; }


Expand All @@ -4055,18 +4071,28 @@ struct SoftwareVersion : Root

struct DataVersion : Root
{
uint year {0};
uint year {0};
uint month {0};
uint day {0};
uint num {0};
uint day {0};
uint num {0};


DataVersion () = default;
DataVersion (uint year_arg,
uint month_arg,
uint day_arg,
uint num_arg)
: year (year_arg)
, month (month_arg)
, day (day_arg)
, num (num_arg)
{}
explicit DataVersion (const string &fName);
explicit DataVersion (istream &is);
private:
void init (string &&s);
public:
void saveText (ostream &os) const override
void saveText (ostream &os) const final
{ os << year
<< '-' << std::setfill ('0') << std::setw (2) << month
<< '-' << std::setfill ('0') << std::setw (2) << day
Expand Down
Loading

0 comments on commit d437940

Please sign in to comment.