Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:47

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   bool debug = hasDebug();
0040   std::string connect = getOptionValue<std::string>("connect");
0041 
0042   // this is mandatory
0043   std::string tag = getOptionValue<std::string>("tag");
0044   std::cout << "# Target tag is " << tag << std::endl;
0045 
0046   persistency::ConnectionPool connPool;
0047   if (hasOptionValue("authPath")) {
0048     connPool.setAuthenticationPath(getOptionValue<std::string>("authPath"));
0049   }
0050   connPool.configure();
0051 
0052   std::cout << "# Connecting to source database on " << connect << std::endl;
0053   persistency::Session session = connPool.createSession(connect, true);
0054 
0055   persistency::IOVEditor editor;
0056   persistency::TransactionScope tsc(session.transaction());
0057   tsc.start(false);
0058   bool exists = false;
0059   if (!session.existsDatabase()) {
0060     session.createDatabase();
0061   } else {
0062     exists = session.existsIov(tag);
0063   }
0064 
0065   bool change = false;
0066   std::map<std::string, std::pair<std::string, std::string> > changes;
0067 
0068   std::string payloadType("");
0069   if (hasOptionValue("payloadClassName"))
0070     payloadType = getOptionValue<std::string>("payloadClassName");
0071   std::string description("");
0072   if (hasOptionValue("description"))
0073     description = getOptionValue<std::string>("description");
0074   std::string timeType("");
0075   if (hasOptionValue("timeType"))
0076     timeType = getOptionValue<std::string>("timeType");
0077   std::string synchronizationType("");
0078   if (hasOptionValue("synchronizationType"))
0079     synchronizationType = getOptionValue<std::string>("synchronizationType");
0080   std::string editingNote("");
0081   if (hasOptionValue("editingNote"))
0082     editingNote = getOptionValue<std::string>("editingNote");
0083 
0084   if (exists) {
0085     if (!payloadType.empty()) {
0086       std::cout << "ERROR: can't change the payload type for an existing tag." << std::endl;
0087       return -1;
0088     }
0089     if (!timeType.empty()) {
0090       std::cout << "ERROR: can't change the time type for an existing tag." << std::endl;
0091       return -1;
0092     }
0093     if (editingNote.empty()) {
0094       std::cout << "ERROR: can't make changes to an existing tag without to provide the editing note." << std::endl;
0095       return -1;
0096     }
0097     editor = session.editIov(tag);
0098     if (!synchronizationType.empty()) {
0099       cond::SynchronizationType st = cond::synchronizationTypeFromName(synchronizationType);
0100       changes.insert(std::make_pair(
0101           "synchronizationType",
0102           std::make_pair(cond::synchronizationTypeNames(editor.synchronizationType()), synchronizationType)));
0103       editor.setSynchronizationType(st);
0104       change = true;
0105     }
0106     if (!description.empty()) {
0107       changes.insert(std::make_pair("description", std::make_pair(editor.description(), description)));
0108       editor.setDescription(description);
0109       change = true;
0110     }
0111   } else {
0112     if (payloadType.empty()) {
0113       std::cout << "ERROR: can't create the new tag, since the payload type has not been provided." << std::endl;
0114       return -1;
0115     }
0116     if (timeType.empty()) {
0117       std::cout << "ERROR: can't create the new tag, since the time type has not been provided." << std::endl;
0118       return -1;
0119     }
0120     if (description.empty()) {
0121       std::cout << "ERROR: can't create the new tag, since the description has not been provided." << std::endl;
0122       return -1;
0123     }
0124     if (synchronizationType.empty()) {
0125       std::cout << "# Synchronization type has not been provided. Using default value = \'any\'" << std::endl;
0126       synchronizationType = "any";
0127     }
0128     cond::TimeType tt = cond::time::timeTypeFromName(timeType);
0129     cond::SynchronizationType st = cond::synchronizationTypeFromName(synchronizationType);
0130     editor = session.createIov(payloadType, tag, tt, st);
0131     change = true;
0132     editor.setDescription(description);
0133   }
0134 
0135   if (hasOptionValue("endOfValidity")) {
0136     cond::Time_t endOfValidity = getOptionValue<cond::Time_t>("endOfValidity");
0137     changes.insert(std::make_pair(
0138         "endOfValidity", std::make_pair(std::to_string(editor.endOfValidity()), std::to_string(endOfValidity))));
0139     editor.setEndOfValidity(endOfValidity);
0140     change = true;
0141   }
0142 
0143   if (change) {
0144     if (exists) {
0145       bool more = false;
0146       std::cout << "# Modifying existing tag.";
0147       auto ie = changes.find("synchronizationType");
0148       if (ie != changes.end()) {
0149         std::cout << " " << ie->first << "=\"" << ie->second.second << "\" (was \"" << ie->second.first << "\")";
0150         more = true;
0151       }
0152       ie = changes.find("endOfValidity");
0153       if (ie != changes.end()) {
0154         if (more)
0155           std::cout << ",";
0156         std::cout << " " << ie->first << "=\"" << ie->second.second << "\" (was \"" << ie->second.first << "\")";
0157         more = true;
0158       }
0159       if (more)
0160         std::cout << std::endl;
0161       ie = changes.find("description");
0162       if (ie != changes.end()) {
0163         std::cout << "# " << ie->first << ": \"" << ie->second.second << "\" (was \"" << ie->second.first << "\")"
0164                   << std::endl;
0165       }
0166     } else {
0167       std::cout << "# Creating new tag " << tag << " with: payloadType=\"" << payloadType << "\", timeType=\""
0168                 << timeType;
0169       std::cout << "\", synchronizationType=\"" << synchronizationType << "\", endOfValidity=\""
0170                 << editor.endOfValidity() << "\"" << std::endl;
0171       std::cout << "# description: \"" << description << "\"" << std::endl;
0172     }
0173     std::string confirm("");
0174     std::cout << "# Confirm changes (Y/N)? [N]";
0175     std::getline(std::cin, confirm);
0176     if (confirm != "Y" && confirm != "y")
0177       return 0;
0178     editor.flush(editingNote);
0179     tsc.commit();
0180     std::cout << "# Changes committed. " << std::endl;
0181   } else {
0182     std::cout << "#No change needed to be saved." << std::endl;
0183   }
0184   return 0;
0185 }
0186 
0187 int main(int argc, char** argv) {
0188   cond::EditTagUtilities utilities;
0189   return utilities.run(argc, argv);
0190 }