Repository: dmwm/wmcore
Issue: WMTweak fails with a not valid FWCore parameter type on 1.4.x series

**Impact of the bug**
WMAgent

**Describe the bug**
I'm testing WMAgent 1.4.0, which has several python3 fixes applied, including the PSetTweaks during runtime. And the job failure rate is 100%, reason is that there is an exception when tweaking the PSet because the data types is different than what CMSSW framework expects.

**How to reproduce it**
Will try to get something interactively, but one could also create a workflow and run it with the newest agent.

**Expected behavior**
We should be able to tweak the job PSet and move forward with the usual processing. 

**Additional context and error message**
Example workflow in testbed: amaltaro_SC_LumiMask_PhEDEx_Agent140_Val_200916_202755_4101
WMBS job id: 1179
wmagentJob.log
```
2020-09-16 18:52:15,997:INFO:Scram:    Invoking command: /cvmfs/cms.cern.ch/slc7_amd64_gcc700/cms/cmssw-patch/CMSSW_10_6_1_patch1/external/slc7_amd64_gcc700/bin/python2.7 -m WMCore.WMRuntime.ScriptInvoke WMTaskSpace.cmsRun1 SetupCMSSWPset 
2020-09-16 18:52:32,387:INFO:Scram:Subprocess stdout was:
2020-09-16 18:52:17,864:INFO:ScriptInvoke:Invoking scripts in current directory: /srv/job/WMTaskSpace/cmsRun1/CMSSW_10_6_1_patch1
2020-09-16 18:52:17,873:INFO:Bootstrap:Job Index = 1179
...
2020-09-16 18:52:18,203:INFO:SetupCMSSWPset:Executing SetupCMSSWPSet...
2020-09-16 18:52:32,204:INFO:SetupCMSSWPset:Tag chirp updates from CMSSW with step cmsRun1
2020-09-16 18:52:32,206:INFO:WMTweak:Not resizing job
2020-09-16 18:52:32,206:INFO:WMTweak:Adding 1 files to 'fileNames' attr
2020-09-16 18:52:32,207:INFO:WMTweak:Adding 0 files to 'secondaryFileNames' attr
2020-09-16 18:52:32,207:INFO:WMTweak:Setting 'maxEvents.input' attr to: -1
2020-09-16 18:52:32,207:INFO:WMTweak:Adding 1 run/lumis mask to 'lumisToProcess' attr
2020-09-16 18:52:32,209:INFO:SetupCMSSWPset:Running on site 'T1_US_FNAL_Disk', local PNN: 'T1_US_FNAL_Disk'
2020-09-16 18:52:32,209:INFO:SetupCMSSWPset:Pileup JSON configuration file: '/srv/job/WMTaskSpace/cmsRun1/pileupconf.json'
2020-09-16 18:52:32,217:INFO:SetupCMSSWPset:Requested pileup type data with 0 mixing modules
2020-09-16 18:52:32,217:INFO:SetupCMSSWPset:Requested pileup type mc with 1 mixing modules
2020-09-16 18:52:32,218:INFO:SetupCMSSWPset:Loaded module type: MixingModule
2020-09-16 18:52:32,218:INFO:SetupCMSSWPset:pileupType: mc with input attributes: True
2020-09-16 18:52:32,218:INFO:SetupCMSSWPset:Pileup set to read data remotely: False
2020-09-16 18:52:32,218:INFO:SetupCMSSWPset:Added 90000 events from the pileup blocks
2020-09-16 18:52:32,219:INFO:SetupCMSSWPset:Evaluating enforceGUIDInFileName parameter for secondary input data.
2020-09-16 18:52:32,219:INFO:SetupCMSSWPset:Evaluating if release CMSSW_10_6_1_patch1 supports enforceGUIDInFileName parameter...
2020-09-16 18:52:32,219:INFO:SetupCMSSWPset:CMSSW release does not support enforceGUIDInFileName.
Traceback (most recent call last):
  File "/cvmfs/cms.cern.ch/slc7_amd64_gcc700/external/python/2.7.14-pafccj/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/cvmfs/cms.cern.ch/slc7_amd64_gcc700/external/python/2.7.14-pafccj/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/srv/job/WMCore.zip/WMCore/WMRuntime/ScriptInvoke.py", line 117, in <module>
RuntimeError: Error invoking script for step WMTaskSpace.cmsRun1
withe Script module: SetupCMSSWPset
FEVTDEBUGHLToutput.root is not a valid <class 'FWCore.ParameterSet.Types.string'>Details:
  File "/srv/job/WMCore.zip/WMCore/WMRuntime/ScriptInvoke.py", line 109, in <module>
  File "/srv/job/WMCore.zip/WMCore/WMRuntime/ScriptInvoke.py", line 80, in invoke
  File "/srv/job/WMCore.zip/WMCore/WMRuntime/Scripts/SetupCMSSWPset.py", line 798, in __call__
    applyTweak(self.process, outTweak, self.fixupDict)
  File "/srv/job/PSetTweaks/WMTweak.py", line 319, in applyTweak
    setParameter(process, param, value)
  File "/srv/job/PSetTweaks/WMTweak.py", line 199, in setParameter
    lastPSet.setValue(value)
  File "/cvmfs/cms.cern.ch/slc7_amd64_gcc700/cms/cmssw-patch/CMSSW_10_6_1_patch1/python/FWCore/ParameterSet/Mixins.py", line 97, in setValue
    raise ValueError(str(value)+" is not a valid "+str(type(self)))
```

Changed files:
src/python/PSetTweaks/WMTweak.py

diff --git a/src/python/PSetTweaks/WMTweak.py b/src/python/PSetTweaks/WMTweak.py
index dbb600e93..91eb3b634 100644
--- a/src/python/PSetTweaks/WMTweak.py
+++ b/src/python/PSetTweaks/WMTweak.py
@@ -313,6 +313,9 @@ def applyTweak(process, tweak, fixup=None):
     making sure all the necessary PSets and configuration values exist).
     """
     for param, value in tweak:
+        if isinstance(value, type(u'')) and hasattr(value, "encode"):
+            logging.info("Found unicode parameter type for param: %s, with value: %s", param, value)
+            value = value.encode("utf-8")
         if fixup and param in fixup:
             fixup[param](process)