Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-06-11 22:51:07

0001 // -*- C++ -*-
0002 //
0003 // Package:     ParameterSet
0004 // Class  :     ConfigurationDescriptions
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  W. David Dagenhart
0010 //         Created:  17 December 2008
0011 //
0012 
0013 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0014 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
0015 #include "FWCore/ParameterSet/interface/defaultModuleLabel.h"
0016 #include "FWCore/Utilities/interface/Algorithms.h"
0017 #include "FWCore/Utilities/interface/EDMException.h"
0018 
0019 #include <fstream>
0020 #include <iostream>
0021 #include <iomanip>
0022 #include <sstream>
0023 #include <cstring>
0024 #include <cerrno>
0025 #include <cstring>
0026 
0027 namespace {
0028   void matchLabel(std::pair<std::string, edm::ParameterSetDescription> const& thePair,
0029                   std::string const& moduleLabel,
0030                   edm::ParameterSetDescription const*& psetDesc) {
0031     if (thePair.first == moduleLabel) {
0032       psetDesc = &thePair.second;
0033     }
0034   }
0035 }  // namespace
0036 
0037 static const char* const kSource = "Source";
0038 static const char* const kService = "Service";
0039 static const char* const k_source = "source";
0040 
0041 namespace edm {
0042 
0043   ConfigurationDescriptions::ConfigurationDescriptions(std::string const& baseType, std::string const& pluginName)
0044       : baseType_(baseType), pluginName_(pluginName), defaultDescDefined_(false) {}
0045 
0046   ConfigurationDescriptions::~ConfigurationDescriptions() {}
0047 
0048   void ConfigurationDescriptions::setComment(std::string const& value) { comment_ = value; }
0049 
0050   void ConfigurationDescriptions::setComment(char const* value) { comment_ = value; }
0051 
0052   void ConfigurationDescriptions::add(char const* label, ParameterSetDescription const& psetDescription) {
0053     std::string labelString(label);
0054     add(labelString, psetDescription);
0055   }
0056 
0057   void ConfigurationDescriptions::add(std::string const& label, ParameterSetDescription const& psetDescription) {
0058     if (0 == strcmp(baseType_.c_str(), kSource)) {
0059       if (0 != strcmp(label.c_str(), k_source)) {
0060         throw edm::Exception(edm::errors::LogicError,
0061                              "ConfigurationDescriptions::add, when adding a ParameterSetDescription for a source the "
0062                              "label must be \"source\"\n");
0063       }
0064       if (!descriptions_.empty() || defaultDescDefined_ == true) {
0065         throw edm::Exception(
0066             edm::errors::LogicError,
0067             "ConfigurationDescriptions::add, for a source only 1 ParameterSetDescription may be added\n");
0068       }
0069     } else if (0 == strcmp(baseType_.c_str(), kService)) {
0070       if (!descriptions_.empty() || defaultDescDefined_ == true) {
0071         throw edm::Exception(
0072             edm::errors::LogicError,
0073             "ConfigurationDescriptions::add, for a service only 1 ParameterSetDescription may be added\n");
0074       }
0075     }
0076 
0077     // To minimize the number of copies involved create an empty description first
0078     // and push it into the vector.  Then perform the copy.
0079     std::pair<std::string, ParameterSetDescription> pairWithEmptyDescription;
0080     descriptions_.push_back(pairWithEmptyDescription);
0081     std::pair<std::string, ParameterSetDescription>& pair = descriptions_.back();
0082 
0083     pair.first = label;
0084     pair.second = psetDescription;
0085   }
0086 
0087   void ConfigurationDescriptions::addWithDefaultLabel(ParameterSetDescription const& psetDescription) {
0088     std::string label;
0089     if (kService == baseType_) {
0090       label = pluginName_;
0091     } else if (kSource == baseType_) {
0092       label = "source";
0093     } else {
0094       label = defaultModuleLabel(pluginName_);
0095     }
0096     add(label, psetDescription);
0097   }
0098 
0099   void ConfigurationDescriptions::addDefault(ParameterSetDescription const& psetDescription) {
0100     if (0 == strcmp(baseType_.c_str(), kSource) || 0 == strcmp(baseType_.c_str(), kService)) {
0101       if (!descriptions_.empty() || defaultDescDefined_ == true) {
0102         throw edm::Exception(edm::errors::LogicError,
0103                              "ConfigurationDescriptions::addDefault, for a source or service only 1 "
0104                              "ParameterSetDescription may be added\n");
0105       }
0106     }
0107 
0108     defaultDescDefined_ = true;
0109     defaultDesc_ = psetDescription;
0110   }
0111 
0112   ParameterSetDescription* ConfigurationDescriptions::defaultDescription() {
0113     if (defaultDescDefined_) {
0114       return &defaultDesc_;
0115     }
0116     return nullptr;
0117   }
0118 
0119   ConfigurationDescriptions::iterator ConfigurationDescriptions::begin() { return descriptions_.begin(); }
0120 
0121   ConfigurationDescriptions::iterator ConfigurationDescriptions::end() { return descriptions_.end(); }
0122 
0123   void ConfigurationDescriptions::validate(ParameterSet& pset, std::string const& moduleLabel) const {
0124     ParameterSetDescription const* psetDesc = nullptr;
0125     for_all(descriptions_, std::bind(&matchLabel, std::placeholders::_1, std::cref(moduleLabel), std::ref(psetDesc)));
0126 
0127     // If there is a matching label
0128     if (psetDesc != nullptr) {
0129       psetDesc->validate(pset);
0130     }
0131     // Is there an explicit description to be used for a non standard label
0132     else if (defaultDescDefined_) {
0133       defaultDesc_.validate(pset);
0134     }
0135     // Otherwise use the first one.
0136     else if (!descriptions_.empty()) {
0137       descriptions_[0].second.validate(pset);
0138     }
0139     // It is possible for no descriptions to be defined and no validation occurs
0140     // for this module ever.
0141   }
0142 
0143   void ConfigurationDescriptions::writeCfis(std::set<std::string>& usedCfiFileNames) const {
0144     bool wroteClassFile = false;
0145     cfi::Paths paths;
0146     if (defaultDescDefined_) {
0147       paths = writeClassFile(defaultDesc_, not descriptions_.empty());
0148       wroteClassFile = true;
0149     } else if (descriptions_.size() == 1) {
0150       paths = writeClassFile(descriptions_.begin()->second, true);
0151       wroteClassFile = true;
0152     }
0153     CfiOptions ops = wroteClassFile ? CfiOptions{cfi::Untyped{paths}} : CfiOptions{cfi::Typed{}};
0154     for (auto& d : descriptions_) {
0155       writeCfiForLabel(
0156           d, baseType_, pluginName_, (not defaultDescDefined_) and (1 == descriptions_.size()), ops, usedCfiFileNames);
0157     }
0158   }
0159 
0160   namespace {
0161     std::string modifyPluginName(std::string iName) {
0162       auto found = iName.find("::");
0163       while (found != std::string::npos) {
0164         iName.replace(found, 2, "_");
0165         found = iName.find("::");
0166       }
0167       //Symbols that can appear in our plugin names but can't in python function names
0168       const std::string toReplace("@<>,");
0169       found = iName.find_first_of(toReplace);
0170       while (found != std::string::npos) {
0171         iName.replace(found, 1, "_");
0172         found = iName.find_first_of(toReplace, found);
0173       }
0174       return iName;
0175     }
0176   }  // namespace
0177 
0178   cfi::Paths ConfigurationDescriptions::writeClassFile(ParameterSetDescription const& iDesc,
0179                                                        bool willUseWithCfis) const {
0180     std::string pluginName = modifyPluginName(pluginName_);
0181 
0182     std::string fileName = pluginName + ".py";
0183     std::ofstream outFile(fileName.c_str());
0184     if (outFile.fail()) {
0185       edm::Exception ex(edm::errors::LogicError, "Creating class file failed.\n");
0186       ex << "Opening a file '" << fileName << "' failed.\n";
0187       ex << "Error code from errno " << errno << ": " << std::strerror(errno) << "\n";
0188 
0189       ex.addContext("Executing function ConfigurationDescriptions::writeDefault");
0190       throw ex;
0191     }
0192     outFile << "import FWCore.ParameterSet.Config as cms\n\n";
0193     outFile << "def " << pluginName
0194             << "(**kwargs):\n"
0195                "  mod = cms."
0196             << baseType_ << "('" << pluginName_ << "'";
0197 
0198     bool startWithComma = true;
0199     int indentation = 4;
0200     CfiOptions ops = willUseWithCfis ? CfiOptions{cfi::ClassFile{}} : CfiOptions{cfi::Typed{}};
0201     iDesc.writeCfi(outFile, startWithComma, indentation, ops);
0202 
0203     outFile << ")\n"
0204                "  for k,v in kwargs.items():\n"
0205                "    setattr(mod, k, v)\n"
0206                "  return mod\n";
0207 
0208     outFile.close();
0209     return std::holds_alternative<cfi::ClassFile>(ops) ? std::get<cfi::ClassFile>(ops).releasePaths() : cfi::Paths{};
0210   }
0211 
0212   void ConfigurationDescriptions::writeCfiForLabel(std::pair<std::string, ParameterSetDescription> const& labelAndDesc,
0213                                                    std::string const& baseType,
0214                                                    std::string const& pluginName,
0215                                                    bool isSameAsDefault,
0216                                                    CfiOptions& options,
0217                                                    std::set<std::string>& usedCfiFileNames) {
0218     if (0 == strcmp(baseType.c_str(), kService) && labelAndDesc.first != pluginName) {
0219       throw edm::Exception(edm::errors::LogicError,
0220                            "ConfigurationDescriptions::writeCfiForLabel\nFor a service the label and the plugin name "
0221                            "must be the same.\n")
0222           << "This error is probably caused by an incorrect label being passed\nto the ConfigurationDescriptions::add "
0223              "function earlier.\n"
0224           << "plugin name = \"" << pluginName << "\"  label name = \"" << labelAndDesc.first << "\"\n";
0225     }
0226 
0227     std::string cfi_filename;
0228     if (0 == strcmp(baseType.c_str(), kSource)) {
0229       cfi_filename = pluginName + "_cfi.py";
0230     } else {
0231       cfi_filename = labelAndDesc.first + "_cfi.py";
0232     }
0233     if (!usedCfiFileNames.insert(cfi_filename).second) {
0234       edm::Exception ex(edm::errors::LogicError,
0235                         "Two cfi files are being generated with the same name in the same directory.\n");
0236       ex << "The cfi file name is '" << cfi_filename << "' and\n"
0237          << "the module label is \'" << labelAndDesc.first << "\'.\n"
0238          << "This error is probably caused by an error in one or more fillDescriptions functions\n"
0239          << "where duplicate module labels are being passed to the ConfigurationDescriptions::add\n"
0240          << "function. All such module labels must be unique within a package.\n"
0241          << "If you do not want the generated cfi file and do not need more than one\n"
0242          << "description for a plugin, then a way to fix this is to use the addDefault\n"
0243          << "function instead of the add function.\n"
0244          << "There are 3 common ways this problem can happen.\n"
0245          << "1. This can happen when a module label is explicitly duplicated in one or more\n"
0246          << "fillDescriptions functions. Fix these by changing the module labels to be unique.\n"
0247          << "2. This can also happen when a module class is a template class and plugins are\n"
0248          << "defined by instantiations with differing template parameters and these plugins\n"
0249          << "share the same fillDescriptions function. Fix these by specializing the fillDescriptions\n"
0250          << "function for each template instantiation.\n"
0251          << "3. This can also happen when there is an inheritance heirarchy and multiple plugin modules\n"
0252          << "are defined using derived classes and the base class which share the same fillDescriptions\n"
0253          << "function. Fix these by redefining the fillDescriptions function in each derived class.\n";
0254       ex.addContext("Executing function ConfigurationDescriptions::writeCfiForLabel");
0255       throw ex;
0256     }
0257     std::ofstream outFile(cfi_filename.c_str());
0258     if (outFile.fail()) {
0259       edm::Exception ex(edm::errors::LogicError, "Creating cfi file failed.\n");
0260       ex << "Opening a file '" << cfi_filename << "' for module '" << labelAndDesc.first << "' failed.\n";
0261       ex << "Error code from errno " << errno << ": " << std::strerror(errno) << "\n";
0262 
0263       ex.addContext("Executing function ConfigurationDescriptions::writeCfiForLabel");
0264       throw ex;
0265     }
0266 
0267     bool startWithComma = true;
0268     if (not shouldWriteUntyped(options)) {
0269       outFile << "import FWCore.ParameterSet.Config as cms\n\n";
0270       outFile << labelAndDesc.first << " = cms." << baseType << "('" << pluginName << "'";
0271     } else {
0272       outFile << "import FWCore.ParameterSet.Config as cms\n\n";
0273 
0274       auto pythonName = modifyPluginName(pluginName);
0275       outFile << "from ." << pythonName << " import " << pythonName << "\n\n";
0276       outFile << labelAndDesc.first << " = " << pythonName << "(";
0277       startWithComma = false;
0278     }
0279     if (not isSameAsDefault) {
0280       int indentation = 2;
0281       labelAndDesc.second.writeCfi(outFile, startWithComma, indentation, options);
0282     }
0283     outFile << ")\n";
0284 
0285     outFile.close();
0286 
0287     if (0 == strcmp(baseType.c_str(), kSource)) {
0288       std::cout << pluginName << "\n";
0289     } else {
0290       std::cout << labelAndDesc.first << "\n";
0291     }
0292   }
0293 
0294   void ConfigurationDescriptions::print(std::ostream& os,
0295                                         std::string const& moduleLabel,
0296                                         bool brief,
0297                                         bool printOnlyLabels,
0298                                         size_t lineWidth,
0299                                         int indentation,
0300                                         int iPlugin) const {
0301     if (!brief) {
0302       if (!comment().empty()) {
0303         DocFormatHelper::wrapAndPrintText(os, comment(), indentation, lineWidth);
0304       }
0305       os << "\n";
0306     }
0307 
0308     if (descriptions_.empty() && !defaultDescDefined_) {
0309       char oldFill = os.fill();
0310       indentation += DocFormatHelper::offsetModuleLabel();
0311       os << std::setfill(' ') << std::setw(indentation) << "";
0312       os << "There are no PSet descriptions defined for this plugin.\n";
0313       os << std::setfill(' ') << std::setw(indentation) << "";
0314       os << "PSets will not be validated and no cfi files will be generated.\n";
0315       os << std::setfill(oldFill);
0316       if (!brief)
0317         os << "\n";
0318       return;
0319     }
0320 
0321     if (descriptions_.empty() && defaultDescDefined_ && defaultDesc_.isUnknown()) {
0322       indentation += DocFormatHelper::offsetModuleLabel();
0323       char oldFill = os.fill();
0324       os << std::setfill(' ') << std::setw(indentation) << "";
0325       os << "This plugin has not implemented the function which defines its\n";
0326       os << std::setfill(' ') << std::setw(indentation) << "";
0327       os << "configuration descriptions yet. No descriptions are available.\n";
0328       os << std::setfill(' ') << std::setw(indentation) << "";
0329       os << "Its PSets will not be validated, and no cfi files will be generated.\n";
0330       os << std::setfill(oldFill);
0331       if (!brief)
0332         os << "\n";
0333       return;
0334     }
0335 
0336     if (!brief) {
0337       std::stringstream ss;
0338       if (defaultDescDefined_) {
0339         if (descriptions_.empty()) {
0340           ss << "This plugin has only one PSet description. "
0341              << "This description is always used to validate configurations. "
0342              << "Because this configuration has no label, no cfi files will be generated.";
0343         } else {
0344           ss << "This plugin has " << (descriptions_.size() + 1U) << " PSet descriptions. "
0345              << "The description used to validate a configuration is selected by "
0346              << "matching the module labels. If none match, then the last description, "
0347              << "which has no label, is selected. "
0348              << "A cfi file will be generated for each configuration with a module label.";
0349         }
0350       } else {
0351         if (descriptions_.size() == 1U) {
0352           ss << "This plugin has " << descriptions_.size() << " PSet description. "
0353              << "This description is always used to validate configurations. "
0354              << "The label below is used when generating the cfi file.";
0355         } else {
0356           ss << "This plugin has " << descriptions_.size() << " PSet descriptions. "
0357              << "The description used to validate a configuration is selected by "
0358              << "matching the module labels. If none match the first description below is used. "
0359              << "The module labels below are also used when generating the cfi files.";
0360         }
0361       }
0362       DocFormatHelper::wrapAndPrintText(os, ss.str(), indentation, lineWidth);
0363       os << "\n";
0364     }
0365 
0366     indentation += DocFormatHelper::offsetModuleLabel();
0367 
0368     DescriptionCounter counter;
0369     counter.iPlugin = iPlugin;
0370     counter.iSelectedModule = 0;
0371     counter.iModule = 0;
0372 
0373     for (auto const& d : descriptions_) {
0374       printForLabel(d, os, moduleLabel, brief, printOnlyLabels, lineWidth, indentation, counter);
0375     }
0376 
0377     if (defaultDescDefined_) {
0378       printForLabel(os,
0379                     std::string("@default"),
0380                     defaultDesc_,
0381                     moduleLabel,
0382                     brief,
0383                     printOnlyLabels,
0384                     lineWidth,
0385                     indentation,
0386                     counter);
0387     }
0388   }
0389 
0390   void ConfigurationDescriptions::printForLabel(std::pair<std::string, ParameterSetDescription> const& labelAndDesc,
0391                                                 std::ostream& os,
0392                                                 std::string const& moduleLabel,
0393                                                 bool brief,
0394                                                 bool printOnlyLabels,
0395                                                 size_t lineWidth,
0396                                                 int indentation,
0397                                                 DescriptionCounter& counter) const {
0398     printForLabel(os,
0399                   labelAndDesc.first,
0400                   labelAndDesc.second,
0401                   moduleLabel,
0402                   brief,
0403                   printOnlyLabels,
0404                   lineWidth,
0405                   indentation,
0406                   counter);
0407   }
0408 
0409   void ConfigurationDescriptions::printForLabel(std::ostream& os,
0410                                                 std::string const& label,
0411                                                 ParameterSetDescription const& description,
0412                                                 std::string const& moduleLabel,
0413                                                 bool brief,
0414                                                 bool printOnlyLabels,
0415                                                 size_t lineWidth,
0416                                                 int indentation,
0417                                                 DescriptionCounter& counter) const {
0418     ++counter.iModule;
0419     if (!moduleLabel.empty() && label != moduleLabel)
0420       return;
0421     ++counter.iSelectedModule;
0422 
0423     std::stringstream ss;
0424     ss << counter.iPlugin << "." << counter.iSelectedModule;
0425     std::string section = ss.str();
0426 
0427     char oldFill = os.fill();
0428     os << std::setfill(' ') << std::setw(indentation) << "" << std::setfill(oldFill);
0429     os << section << " ";
0430     if (label == std::string("@default")) {
0431       os << "description without a module label\n";
0432     } else {
0433       if (!brief) {
0434         if (0 == strcmp(baseType_.c_str(), kSource) || 0 == strcmp(baseType_.c_str(), kService)) {
0435           os << "label: ";
0436         } else {
0437           os << "module label: ";
0438         }
0439       }
0440       os << label << "\n";
0441     }
0442 
0443     if (!brief) {
0444       if (!description.comment().empty()) {
0445         DocFormatHelper::wrapAndPrintText(os, description.comment(), indentation, lineWidth - indentation);
0446       }
0447       os << "\n";
0448     }
0449     if (printOnlyLabels)
0450       return;
0451 
0452     DocFormatHelper dfh;
0453     dfh.setBrief(brief);
0454     dfh.setLineWidth(lineWidth);
0455     dfh.setIndentation(indentation + DocFormatHelper::offsetTopLevelPSet());
0456     dfh.setSection(section);
0457     dfh.setParent(DocFormatHelper::TOP);
0458 
0459     description.print(os, dfh);
0460   }
0461 }  // namespace edm