11 #include <boost/bind.hpp>
12 #include <boost/filesystem/operations.hpp>
13 #include <boost/mem_fn.hpp>
14 #include <boost/program_options.hpp>
26 using namespace edmplugin;
40 #define PER_PROCESS_DSO 20
42 #define PER_PROCESS_DSO 200
46 ostream& operator<<(std::ostream& o, vector<std::string>
const& iValue) {
49 for(std::vector<std::string>::const_iterator it = iValue.begin(), itEnd = iValue.end();
64 iBase->
newPluginAdded_.connect(boost::bind(boost::mem_fn(&Listener::newPlugin),
this, _1, _2));
67 nameAndTypes_.push_back(NameAndType(iInfo.
name_, iCategory));
70 NameAndTypes nameAndTypes_;
74 using namespace boost::program_options;
76 static char const*
const kPathsOpt =
"paths";
77 static char const*
const kPathsCommandOpt =
"paths,p";
80 static char const*
const kHelpOpt =
"help";
84 descString +=
" [options] [[--";
85 descString += kPathsOpt;
86 descString +=
"] path [path]] \nAllowed options";
87 options_description desc(descString);
90 if(!defaultDirList.empty()) {
91 defaultDir = defaultDirList[0];
95 (kPathsCommandOpt,
value<std::vector<std::string> >()->default_value(
96 std::vector<std::string>(1, defaultDir))
97 ,
"a directory or a list of files to scan")
101 positional_options_description
p;
102 p.add(kPathsOpt, -1);
106 store(command_line_parser(argc, argv).
options(desc).positional(
p).
run(), vm);
108 }
catch(
error const& iException) {
125 int returnValue = EXIT_SUCCESS;
128 std::vector<std::string> requestedPaths(vm[kPathsOpt].as<std::vector<std::string> >());
132 std::vector<std::string>
files;
133 bool removeMissingFiles =
false;
134 if(boost::filesystem::is_directory(directory)) {
135 if (requestedPaths.size() > 1) {
136 std::cerr <<
"if a directory is given then only one argument is allowed" << std::endl;
141 removeMissingFiles =
true;
143 boost::filesystem::directory_iterator
file (directory);
144 boost::filesystem::directory_iterator
end;
146 path cacheFile(directory);
149 std::time_t cacheLastChange(0);
150 if(exists(cacheFile)) {
151 cacheLastChange = last_write_time(cacheFile);
157 path shortName(file->path().filename());
161 if (stringName.size() < kPluginPrefix.size()) {
164 if(stringName.substr(0, kPluginPrefix.size()) != kPluginPrefix) {
168 if(last_write_time(filename) > cacheLastChange) {
169 files.push_back(stringName);
174 directory = directory.branch_path();
175 for(std::vector<std::string>::iterator it = requestedPaths.begin(), itEnd = requestedPaths.end();
179 std::cerr <<
"the file '" << f.string() <<
"' does not exist" << std::endl;
182 if (is_directory(f)) {
183 std::cerr <<
"either one directory or a list of files are allowed as arguments" << std::endl;
186 if(directory != f.branch_path()) {
187 std::cerr <<
"all files must have be in the same directory (" << directory.string() <<
")\n"
188 " the file " << f.string() <<
" does not." << std::endl;
190 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 47
191 files.push_back(f.filename().string());
193 files.push_back(f.leaf());
198 path cacheFile(directory);
202 if(exists(cacheFile)) {
203 std::ifstream cf(cacheFile.string().c_str());
205 cms::Exception(
"FailedToOpen") <<
"unable to open file '" << cacheFile.string() <<
"' for reading even though it is present.\n"
206 "Please check permissions on the file.";
215 pfm->
newFactory_.connect(boost::bind(boost::mem_fn(&Listener::newFactory), &listener, _1));
216 edm::for_all(*pfm, boost::bind(boost::mem_fn(&Listener::newFactory), &listener, _1));
220 std::string temporaryFilename = (cacheFile.string() +
".tmp");
221 std::ofstream cf(temporaryFilename.c_str());
224 << temporaryFilename <<
"' for writing.\n"
225 "Please check permissions on the file.";
234 pid_t worker = fork();
241 while (ci && fi != fe)
243 path loadableFile(directory);
244 loadableFile /= (files[fi]);
245 listener.nameAndTypes_.clear();
253 ltp[files[fi]] = listener.nameAndTypes_;
256 if(iException.
category() ==
"PluginLibraryLoadError") {
257 std::cerr <<
"Caught exception " << iException.
what() <<
" will ignore " << files[fi] <<
" and continue." << std::endl;
263 std::cerr <<
"Caught exception " << iException.what() << std::endl;
277 waitpid(worker, &status, 0);
278 if (WIFEXITED(status) ==
true && status != 0)
280 std::cerr <<
"Error while processing." << std::endl;
290 std::ifstream icf(temporaryFilename.c_str());
292 cms::Exception(
"FailedToOpen") <<
"unable to open file '" << temporaryFilename.c_str() <<
"' for reading even though it is present.\n"
293 "Please check permissions on the file.";
297 for (CacheParser::LoadableToPlugins::iterator itFile = ltp.begin() ;
298 itFile != ltp.end() ;
301 old[itFile->first] = itFile->second;
306 if(removeMissingFiles) {
307 for(CacheParser::LoadableToPlugins::iterator itFile = old.begin();
310 path loadableFile(directory);
311 loadableFile /= (itFile->first);
312 if(not exists(loadableFile)) {
313 std::cout <<
"removing file '" << temporaryFilename.c_str() <<
"'" << std::endl;
314 CacheParser::LoadableToPlugins::iterator itToItemBeingRemoved = itFile;
317 old.erase(itToItemBeingRemoved);
326 std::ofstream fcf(temporaryFilename.c_str());
328 cms::Exception(
"FailedToOpen") <<
"unable to open file '" << temporaryFilename.c_str() <<
"' for writing.\n"
329 "Please check permissions on the file.";
332 rename(temporaryFilename.c_str(), cacheFile.string().c_str());
334 std::cerr <<
"Caught exception " << iException.what() << std::endl;
335 returnValue = EXIT_FAILURE;
340 std::cerr << iException.what() << std::endl;
virtual char const * what() const
const boost::filesystem::path & cachefileName()
std::pair< std::string, std::string > NameAndType
bool tryToFind(const SharedLibrary &iLoadable)
Check to see if any capabilities are in the file, returns 'true' if found.
std::string const & category() const
const std::string & pluginPrefix()
int main(int argc, char **argv)
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
std::map< boost::filesystem::path, NameAndTypes > LoadableToPlugins
PluginManager::Config config()
edm::signalslot::Signal< void(const PluginFactoryBase *)> newFactory_
std::vector< NameAndType > NameAndTypes
edm::signalslot::Signal< void(const std::string &, const PluginInfo &)> newPluginAdded_
signal containing plugin category, and plugin info for newly added plugin
static PluginCapabilities * get()
static void write(const CategoryToInfos &, std::ostream &)
static const char *const kHelpCommandOpt
Config & searchPath(const SearchPath &iPath)
static void read(std::istream &, const boost::filesystem::path &iDirectory, CategoryToInfos &oOut)
static const char *const kHelpOpt
static PluginFactoryManager * get()