Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-23 15:57:26

0001 #include "CondCore/CondDB/interface/ConnectionPool.h"
0002 #include "CondCore/CondDB/interface/Utils.h"
0003 #include "CondCore/CondDB/interface/IOVEditor.h"
0004 #include "CondCore/CondDB/interface/IOVProxy.h"
0005 
0006 #include "CondCore/Utilities/interface/Utilities.h"
0007 #include "CondCore/Utilities/interface/CondDBTools.h"
0008 #include <iostream>
0009 
0010 #include <sstream>
0011 
0012 namespace cond {
0013 
0014   class EditTagUtilities : public cond::Utilities {
0015   public:
0016     EditTagUtilities();
0017     ~EditTagUtilities() override;
0018     int execute() override;
0019   };
0020 }  // namespace cond
0021 
0022 cond::EditTagUtilities::EditTagUtilities() : Utilities("conddb_edit_tag") {
0023   addConnectOption("connect", "c", "target connection string (required)");
0024   addAuthenticationOptions();
0025   addOption<std::string>("tag", "t", "target tag (required)");
0026   addOption<std::string>("payloadClassName", "C", "typename of the target payload object (required for new tags)");
0027   addOption<std::string>("timeType", "T", "the IOV time type (required for new tag)");
0028   addOption<std::string>(
0029       "synchronizationType", "S", "the IOV synchronization type (optional, default=any for new tags)");
0030   addOption<cond::Time_t>(
0031       "endOfValidity", "E", "the IOV sequence end of validity (optional, default=infinity for new tags");
0032   addOption<std::string>("description", "D", "user text (required for new tags)");
0033   addOption<std::string>("editingNote", "N", "editing note (required for existing tags)");
0034 }
0035 
0036 cond::EditTagUtilities::~EditTagUtilities() {}
0037 
0038 int cond::EditTagUtilities::execute() {
0039   std::string connect = getOptionValue<std::string>("connect");
0040 
0041   // this is mandatory
0042   std::string tag = getOptionValue<std::string>("tag");
0043   std::cout << "# Target tag is " << tag << std::endl;
0044 
0045   persistency::ConnectionPool connPool;
0046   if (hasOptionValue("authPath")) {
0047     connPool.setAuthenticationPath(getOptionValue<std::string>("authPath"));
0048   }
0049   connPool.configure();
0050 
0051   std::cout << "# Connecting to source database on " << connect << std::endl;
0052   persistency::Session session = connPool.createSession(connect, true);
0053 
0054   persistency::IOVEditor editor;
0055   persistency::TransactionScope tsc(session.transaction());
0056   tsc.start(false);
0057   bool exists = false;
0058   if (!session.existsDatabase()) {
0059     session.createDatabase();
0060   } else {
0061     exists = session.existsIov(tag);
0062   }
0063 
0064   bool change = false;
0065   std::map<std::string, std::pair<std::string, std::string> > changes;
0066 
0067   std::string payloadType("");
0068   if (hasOptionValue("payloadClassName"))
0069     payloadType = getOptionValue<std::string>("payloadClassName");
0070   std::string description("");
0071   if (hasOptionValue("description"))
0072     description = getOptionValue<std::string>("description");
0073   std::string timeType("");
0074   if (hasOptionValue("timeType"))
0075     timeType = getOptionValue<std::string>("timeType");
0076   std::string synchronizationType("");
0077   if (hasOptionValue("synchronizationType"))
0078     synchronizationType = getOptionValue<std::string>("synchronizationType");
0079   std::string editingNote("");
0080   if (hasOptionValue("editingNote"))
0081     editingNote = getOptionValue<std::string>("editingNote");
0082 
0083   if (exists) {
0084     if (!payloadType.empty()) {
0085       std::cout << "ERROR: can't change the payload type for an existing tag." << std::endl;
0086       return -1;
0087     }
0088     if (!timeType.empty()) {
0089       std::cout << "ERROR: can't change the time type for an existing tag." << std::endl;
0090       return -1;
0091     }
0092     if (editingNote.empty()) {
0093       std::cout << "ERROR: can't make changes to an existing tag without to provide the editing note." << std::endl;
0094       return -1;
0095     }
0096     editor = session.editIov(tag);
0097     if (!synchronizationType.empty()) {
0098       cond::SynchronizationType st = cond::synchronizationTypeFromName(synchronizationType);
0099       changes.insert(std::make_pair(
0100           "synchronizationType",
0101           std::make_pair(cond::synchronizationTypeNames(editor.synchronizationType()), synchronizationType)));
0102       editor.setSynchronizationType(st);
0103       change = true;
0104     }
0105     if (!description.empty()) {
0106       changes.insert(std::make_pair("description", std::make_pair(editor.description(), description)));
0107       editor.setDescription(description);
0108       change = true;
0109     }
0110   } else {
0111     if (payloadType.empty()) {
0112       std::cout << "ERROR: can't create the new tag, since the payload type has not been provided." << std::endl;
0113       return -1;
0114     }
0115     if (timeType.empty()) {
0116       std::cout << "ERROR: can't create the new tag, since the time type has not been provided." << std::endl;
0117       return -1;
0118     }
0119     if (description.empty()) {
0120       std::cout << "ERROR: can't create the new tag, since the description has not been provided." << std::endl;
0121       return -1;
0122     }
0123     if (synchronizationType.empty()) {
0124       std::cout << "# Synchronization type has not been provided. Using default value = \'any\'" << std::endl;
0125       synchronizationType = "any";
0126     }
0127     cond::TimeType tt = cond::time::timeTypeFromName(timeType);
0128     cond::SynchronizationType st = cond::synchronizationTypeFromName(synchronizationType);
0129     editor = session.createIov(payloadType, tag, tt, st);
0130     change = true;
0131     editor.setDescription(description);
0132   }
0133 
0134   if (hasOptionValue("endOfValidity")) {
0135     cond::Time_t endOfValidity = getOptionValue<cond::Time_t>("endOfValidity");
0136     changes.insert(std::make_pair(
0137         "endOfValidity", std::make_pair(std::to_string(editor.endOfValidity()), std::to_string(endOfValidity))));
0138     editor.setEndOfValidity(endOfValidity);
0139     change = true;
0140   }
0141 
0142   if (change) {
0143     if (exists) {
0144       bool more = false;
0145       std::cout << "# Modifying existing tag.";
0146       auto ie = changes.find("synchronizationType");
0147       if (ie != changes.end()) {
0148         std::cout << " " << ie->first << "=\"" << ie->second.second << "\" (was \"" << ie->second.first << "\")";
0149         more = true;
0150       }
0151       ie = changes.find("endOfValidity");
0152       if (ie != changes.end()) {
0153         if (more)
0154           std::cout << ",";
0155         std::cout << " " << ie->first << "=\"" << ie->second.second << "\" (was \"" << ie->second.first << "\")";
0156         more = true;
0157       }
0158       if (more)
0159         std::cout << std::endl;
0160       ie = changes.find("description");
0161       if (ie != changes.end()) {
0162         std::cout << "# " << ie->first << ": \"" << ie->second.second << "\" (was \"" << ie->second.first << "\")"
0163                   << std::endl;
0164       }
0165     } else {
0166       std::cout << "# Creating new tag " << tag << " with: payloadType=\"" << payloadType << "\", timeType=\""
0167                 << timeType;
0168       std::cout << "\", synchronizationType=\"" << synchronizationType << "\", endOfValidity=\""
0169                 << editor.endOfValidity() << "\"" << std::endl;
0170       std::cout << "# description: \"" << description << "\"" << std::endl;
0171     }
0172     std::string confirm("");
0173     std::cout << "# Confirm changes (Y/N)? [N]";
0174     std::getline(std::cin, confirm);
0175     if (confirm != "Y" && confirm != "y")
0176       return 0;
0177     editor.flush(editingNote);
0178     tsc.commit();
0179     std::cout << "# Changes committed. " << std::endl;
0180   } else {
0181     std::cout << "#No change needed to be saved." << std::endl;
0182   }
0183   return 0;
0184 }
0185 
0186 int main(int argc, char** argv) {
0187   cond::EditTagUtilities utilities;
0188   return utilities.run(argc, argv);
0189 }