1515
1616#include "Common/Core/MetadataHelper.h"
1717
18+ #include <CCDB/CcdbApi.h>
1819#include <Framework/ConfigContext.h>
1920#include <Framework/ConfigParamRegistry.h>
2021#include <Framework/ConfigParamStore.h>
2425#include <TFile.h>
2526#include <TMap.h>
2627#include <TObjString.h>
28+ #include <TSystem.h>
2729
30+ #include <map>
2831#include <memory>
2932#include <string>
3033#include <utility>
@@ -57,8 +60,100 @@ auto readMetadata(std::unique_ptr<TFile>& currentFile) -> std::vector<o2::framew
5760 return results ;
5861}
5962
63+ // Create a file with all the versions of the O2 software with alienv q
64+ void createO2VersionFile ()
65+ {
66+ // Can do this only if on lxplus
67+ std ::string host = gSystem -> HostName () ? gSystem -> HostName () : "" ;
68+ if (host .find ("lxplus" ) == std ::string ::npos ) {
69+ LOG (warn ) << "Not on lxplus (" << host << "); skipping creation of /tmp/o2version.txt" ;
70+ return ;
71+ }
72+ // If file exists, do nothing
73+ std ::ifstream infile ("/tmp/o2version.txt" );
74+ if (infile .is_open ()) {
75+ return ;
76+ }
77+ gSystem -> Exec ("alienv q | grep VO_ALICE@O2:: > /tmp/o2version.txt" );
78+ }
79+
80+ std ::map < std ::string , bool > buildMapForCommitHash (const std ::string & hash )
81+ {
82+ // Change directory to /tmp
83+ std ::map < std ::string , bool > results ;
84+ std ::ifstream infileO2Versions ("/tmp/o2version.txt" );
85+ std ::string lineOfO2Version ;
86+ const std ::string fileContainingCommit = "/tmp/branches_" + hash + ".txt" ;
87+ std ::ifstream infileO2VersionsWithHash (fileContainingCommit );
88+ if (!infileO2VersionsWithHash .is_open ()) {
89+ gSystem -> cd ("/tmp/" );
90+ gSystem -> Exec ("git clone git@github.com:AliceO2Group/AliceO2.git" );
91+ gSystem -> cd ("AliceO2" );
92+ std ::string cmd = Form ("git branch -r --contains %s > %s 2>&1" , hash .c_str (), fileContainingCommit .c_str ());
93+ LOG (info ) << "Executing command " << cmd ;
94+ gSystem -> Exec (cmd .c_str ());
95+ }
96+ std ::string lineOfO2VersionsWithHash ;
97+ while (std ::getline (infileO2Versions , lineOfO2Version )) {
98+ // Extract the tag
99+ int stripSize = 4 ;
100+ std ::string tag = lineOfO2Version .substr (lineOfO2Version .find ("O2::" ) + stripSize );
101+ // Strip a trailing "-1" (some alienv entries append this)
102+ stripSize = 2 ;
103+ if (tag .size () >= stripSize && tag .compare (tag .size () - stripSize , stripSize , "-1" ) == 0 ) {
104+ tag .resize (tag .size () - stripSize );
105+ }
106+ LOG (debug ) << "Checking tag '" << lineOfO2Version << "' tag (" << tag << ")" ;
107+ bool found = false;
108+ infileO2VersionsWithHash .open (fileContainingCommit );
109+ while (std ::getline (infileO2VersionsWithHash , lineOfO2VersionsWithHash )) {
110+ // LOG(info) << "Comparing " << lineOfO2Version << " with " << lineOfO2VersionsWithHash;
111+ if (lineOfO2VersionsWithHash .find (tag ) != std ::string ::npos ) {
112+ LOG (info ) << "Tag " << tag << " contains hash " << hash ;
113+ found = true;
114+ break ;
115+ }
116+ }
117+ infileO2VersionsWithHash .close ();
118+ results [tag ] = found ;
119+ }
120+ return results ;
121+ }
122+
123+ void populateCCDBWithCommitAvailability (std ::map < string , bool > hasHashMap ,
124+ const std ::string commitHash const std ::string ccdbUrl = "http://ccdb-test.cern.ch:8080/" )
125+ {
126+ // First, init the CCDB manager to test if the ccdb is already populated
127+ o2 ::ccdb ::CcdbApi api ;
128+ api .init (ccdbUrl );
129+ if (!api .isHostReachable ()) {
130+ LOG (fatal ) << "CCDB host " << ccdbUrl << " is not reacheable, cannot go forward" ;
131+ }
132+ for (const auto& entry : hasHashMap ) {
133+ if (!entry .second ) { // Version of the code does not have the hash
134+ continue ;
135+ }
136+ LOG (info ) << "Populating CCDB with information that commit hash " << commitHash << " is contained in software tag " << entry .first ;
137+ std ::map < std ::string , std ::string > metadata ;
138+ metadata ["O2Version" ] = entry .first ;
139+ const std ::string ccdbPath = "O2Version/CommitHash/" + commitHash ;
140+ auto headers = api .retrieveHeaders (ccdbPath , metadata , -1 );
141+ if (headers .size () != 0 ) {
142+ LOG (info ) << "Entry in CCDB already present for commit hash " << commitHash << ", skipping creation" ;
143+ continue ;
144+ }
145+ LOG (info ) << "No entry in CCDB for commit hash " << commitHash << ", creating it" ;
146+ std ::string s = "available" ;
147+ api .storeAsTFileAny < std ::string > (& s , ccdbPath , metadata );
148+ }
149+ }
150+
60151void testMetadataHelper (std ::string aod = "/tmp/AO2D.root" )
61152{
153+ createO2VersionFile ();
154+ const std ::string commitHash = "63bc2e3893851ef0f849bb4c98c65eae1ba21e47" ;
155+ const std ::map < std ::string , bool > hasHashMap = buildMapForCommitHash (commitHash );
156+ populateCCDBWithCommitAvailability (hasHashMap , commitHash );
62157
63158 TFile * file = TFile ::Open (aod .c_str ());
64159 if (!file || file -> IsZombie ()) {
@@ -79,6 +174,23 @@ void testMetadataHelper(std::string aod = "/tmp/AO2D.root")
79174 aodCfg .options ().get < std ::string > ("aod-metadata-DataType" );
80175 o2 ::common ::core ::MetadataHelper metadataInfo ;
81176 metadataInfo .initMetadata (aodCfg );
177+ metadataInfo .set ("O2Version" , "epn-20250715" ); // Override the O2 version to a known one
82178 metadataInfo .print ();
83179 LOG (info ) << "Metadata label: " << metadataInfo .makeMetadataLabel ();
180+
181+ // Check if the hash is in the software tag
182+ const std ::string v = metadataInfo .getO2Version ();
183+ if (hasHashMap .find (v ) == hasHashMap .end ()) {
184+ LOG (fatal ) << "Software tag " << v << " not found in available O2 versions" ;
185+ }
186+ if (hasHashMap .at (v )) {
187+ LOG (info ) << "Hash " << commitHash << " is contained in software tag " << v ;
188+ } else {
189+ LOG (warn ) << "Hash " << commitHash << " is NOT contained in software tag " << v ;
190+ }
191+ if (metadataInfo .isCommitInSoftwareTag (commitHash )) {
192+ LOG (info ) << "MetadataHelper confirms that hash " << commitHash << " is contained in software tag " << v ;
193+ } else {
194+ LOG (warn ) << "MetadataHelper confirms that hash " << commitHash << " is NOT contained in software tag " << v ;
195+ }
84196}
0 commit comments