-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reimplement url2local without using the python c-api
- Loading branch information
Showing
7 changed files
with
117 additions
and
8 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/simplebuild_dgcode/data/pkgs/Utils/Utils/libinc/Cmd.hh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef Utils_Cmd_hh | ||
#define Utils_Cmd_hh | ||
|
||
#include <utility> | ||
#include <string> | ||
|
||
namespace Utils { | ||
//Launch command and get both exit code and stdout of command. The | ||
//reserve_size parameter can be used to reserve capacity in the output string, | ||
//but is only needed for a very small performance gain: | ||
std::pair<int, std::string> launch_cmd( const char* cmd, | ||
std::size_t reserve_size = 0 ); | ||
} | ||
|
||
#endif |
2 changes: 2 additions & 0 deletions
2
src/simplebuild_dgcode/data/pkgs/Utils/Utils/libinc/Url2Local.hh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/simplebuild_dgcode/data/pkgs/Utils/Utils/libsrc/Cmd.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "Utils/Cmd.hh" | ||
#include <array> | ||
#include <stdexcept> | ||
#include <cstdio> | ||
|
||
namespace { | ||
struct UtilsProcWrapper { | ||
//RAII wrapper | ||
FILE * procptr = nullptr; | ||
UtilsProcWrapper( const char * cmd ) | ||
: procptr(popen(cmd,"r")) | ||
{ | ||
} | ||
int close_and_get_rc() | ||
{ | ||
if ( !procptr ) | ||
throw std::runtime_error("launch_cmd: unexpected error"); | ||
auto pp = procptr; | ||
procptr = nullptr; | ||
return pclose( pp ); | ||
} | ||
~UtilsProcWrapper() | ||
{ | ||
if ( procptr ) | ||
pclose( procptr ); | ||
} | ||
}; | ||
} | ||
|
||
std::pair<int, std::string> Utils::launch_cmd( const char* cmd, | ||
std::size_t reserve_size ) | ||
{ | ||
std::array<char, 256> buf; | ||
std::pair<int, std::string> res; | ||
if (reserve_size) | ||
res.second.reserve( reserve_size ); | ||
UtilsProcWrapper theproc( cmd ); | ||
if ( theproc.procptr != nullptr ) { | ||
while ( fgets( buf.data(), buf.size(), theproc.procptr ) != nullptr ) | ||
res.second += buf.data(); | ||
} | ||
res.first = theproc.close_and_get_rc(); | ||
return res; | ||
} |
30 changes: 25 additions & 5 deletions
30
src/simplebuild_dgcode/data/pkgs/Utils/Utils/libsrc/Url2Local.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,30 @@ | ||
#include "Core/Python.hh" | ||
#include "Utils/Url2Local.hh" | ||
#include "Utils/Cmd.hh" | ||
#include "Core/String.hh" | ||
#include <stdexcept> | ||
#include <vector> | ||
|
||
std::string Utils::url2local(std::string input_filename, std::string cachedir) | ||
{ | ||
pyextra::ensurePyInit(); | ||
py::object mod = pyextra::pyimport("Utils.dl_url_to_local_cache"); | ||
auto res = mod.attr("dl_with_cache")(input_filename,cachedir); | ||
return res.cast<std::string>(); | ||
//Wrap sb_utils_url2local command, except for local files: | ||
if ( input_filename.find("://") == std::string::npos ) | ||
return input_filename;//not an url | ||
std::string cmd("sb_utils_url2local "); | ||
cmd += '"'; | ||
cmd += input_filename; | ||
cmd += "\" \""; | ||
cmd += cachedir; | ||
cmd += '"'; | ||
auto res = Utils::launch_cmd( cmd.c_str() ); | ||
if ( res.first != 0 ) | ||
throw std::runtime_error(std::string("cmd failed: ")+cmd); | ||
//Only return the last line of the output, and re-print the rest: | ||
std::vector<std::string> parts; | ||
Core::split_noempty(parts,res.second,"\n\r"); | ||
if ( parts.empty() ) | ||
throw std::runtime_error(std::string("cmd gave unexpected output: ")+cmd); | ||
auto itBack = std::prev(parts.end()); | ||
for ( auto it = parts.begin(); it != itBack; ++it ) | ||
std::cout<<*it<<std::endl; | ||
return std::move(*itBack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/simplebuild_dgcode/data/pkgs/Utils/Utils/scripts/url2local
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env python3 | ||
from Utils.dl_url_to_local_cache import _cli | ||
_cli() |
11 changes: 11 additions & 0 deletions
11
src/simplebuild_dgcode/data/pkgs_val/UnitTests/UtilsTests/app_url2local_cpp/main.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "Utils/Url2Local.hh" | ||
#include <iostream> | ||
int main( int argc, char** argv ) | ||
{ | ||
if (argc<2||argc>3) | ||
return 1; | ||
std::string fn = argv[1]; | ||
std::string cachedir = argc==3 ? argv[2] : ""; | ||
std::cout<< Utils::url2local(fn, cachedir)<<std::endl; | ||
return 0; | ||
} |