ATLAS Offline Software
Loading...
Searching...
No Matches
FCcmd.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
8
10#include "GaudiKernel/Bootstrap.h"
11#include "GaudiKernel/ISvcLocator.h"
13
14#include <exception>
15#include <memory>
16
17using namespace pool;
18using std::cerr, std::cout, std::endl;
19using std::string;
20
22 cout <<
23 "Usage: " << endl <<
24 "FCcmd <action> <item> [-p PFName -l LFName -n newPFName -g GUID -u catalogName -h]" << endl <<
25 " supported actions are:" <<endl <<
26 " register : <item> = lfn|pfn (registering a PFN requires a GUID)" <<endl <<
27 " list : <item> = guid|lfn|pfn" <<endl <<
28 " delete : deletes an entry using LFN or PFN" <<endl <<
29 " rename : rename PFName to a new PFName" <<endl <<
30 " example: FCcmd list pfn -l myLogicalFileName" << endl;
31}
32
33
34class Options {
35 public:
36 Options(int argc, char* argv[]);
37
38 inline const string& getProgName() const;
39 inline const string& getAction() const;
40 inline const string& getItem() const;
41 string getOptByName( char opt ) const;
42 inline bool exists( char opt ) const;
43
44 private:
45 std::map<char, string> m_argMap;
46 string m_action;
47 string m_item;
48 string m_progName;
49
50 // No copying or assignment is supported
51 Options(const Options&) = delete;
52 Options& operator=(const Options&) = delete;
53};
54
55// Parse command line and keep information about arguments
56Options::Options(int argc, char* argv[]) {
57 if( argc > 0 ) {
58 m_progName = string( argv[0] );
59 }
60 if( argc > 1 ) {
61 m_action = string( argv[1] );
62 }
63 if( argc > 2 ) {
64 m_item = string( argv[2] );
65 }
66 // scan from the beginning (in case there is a lone -h)
67 int pos = 1;
68 while( argc > pos ) {
69 char opt {};
70 string val;
71 string arg = string( argv[pos++] );
72 if( arg.length() == 2 && arg[0] == '-' ) {
73 opt = arg[1];
74 } else continue;
75 if( argc > pos ) {
76 arg = string( argv[pos] );
77 if( arg.length() != 2 or arg[0] != '-' ) {
78 val = std::move(arg);
79 pos++;
80 }
81 }
82 m_argMap[opt] = std::move(val);
83 }
84}
85
86inline string Options::getOptByName( char opt ) const {
87 auto it = m_argMap.find( opt );
88 return (it != m_argMap.end())? it->second : "" ;
89}
90
91inline const string& Options::getProgName() const { return m_progName; }
92inline const string& Options::getAction() const { return m_action; }
93inline const string& Options::getItem() const { return m_item; }
94inline bool Options::exists( char opt ) const { return m_argMap.find(opt) != m_argMap.end(); }
95
96
97
98int main(int argc, char** argv)
99{
101
102 Options options(argc, argv);
103 // possible options:
104 // const char* opts[] = {"n","l","p","g","u","h",0};
105 if( options.exists('h') ) {
106 printUsage();
107 return 0;
108 }
109 if( argc < 3 ){
110 cerr << "ERROR! insufficient number of arguments" << endl;
111 printUsage();
112 return 1;
113 }
114 string catName;
115 // In case none of the options below work, the default FC name is specified in FileCatalogUtils::addCatalog()
116 if( options.exists('u') ){
117 catName=options.getOptByName('u');
118 }else{
119 catName=getEnvStr("POOL_CATALOG");
120 }
121 string lfn = options.getOptByName('l');
122 string pfn = options.getOptByName('p');
123 string newpfn = options.getOptByName('n');
124 string guid = options.getOptByName('g');
125 string action = options.getAction();
126 string item = options.getItem();
127
128 try{
129 SmartIF<Gaudi::IFileCatalogMgr> catalogMgr = Gaudi::svcLocator()->service<Gaudi::IFileCatalogMgr>( "Gaudi::MultiFileCatalog" );
130 SmartIF<Gaudi::IFileCatalog> mycatalog( catalogMgr );
131 FileCatalogUtils::addCatalog( *catalogMgr, catName, true );
132 mycatalog->init();
133
134 if( action == "delete" ) {
135 if( !lfn.empty() ){
136 mycatalog->deleteFID( mycatalog->lookupLFN( lfn ) );
137 }else if( !pfn.empty() ) {
138 mycatalog->deleteFID( mycatalog->lookupPFN( pfn ) );
139 }else {
140 cerr << "ERROR! Use PFN or LFN to specify the entry for deletion: " << endl;
141 printUsage();
142 return 2;
143 }
144
145 } else if( action == "list" ) {
146 if( item == "lfn" ) {
147 Gaudi::IFileCatalog::Strings fids;
148 if( !pfn.empty() ) {
149 fids.push_back( mycatalog->lookupPFN( pfn ) );
150 }else{
151 mycatalog->getFID( fids );
152 }
153 for( const auto& fid: fids ) {
154 Gaudi::IFileCatalog::Files files;
155 mycatalog->getLFN( fid, files );
156 for( const auto& file: files ) {
157 cout << file.first << " , " << file.second << endl;
158 }
159 }
160 }
161 else if( item == "pfn" ) {
162 Gaudi::IFileCatalog::Strings fids;
163 if( !lfn.empty() ) {
164 fids.push_back( mycatalog->lookupLFN( lfn ) );
165 } else if( !guid.empty() ) {
166 fids.emplace_back( std::move(guid) );
167 } else {
168 // go through all FIDs in the catalog
169 mycatalog->getFID( fids );
170 }
171 for( const auto& fid: fids ) {
172 Gaudi::IFileCatalog::Files files;
173 mycatalog->getPFN( fid, files );
174 for( const auto& file: files ) {
175 string pf = file.first;
176 string filetype = (file.second.empty()? string("NULL") : file.second);
177 cout<<pf<<" "<<filetype<<endl;
178 }
179 }
180 }
181 else if( item == "guid" ) {
182 Gaudi::IFileCatalog::Strings fids;
183 if( !pfn.empty() ){
184 fids.push_back( mycatalog->lookupPFN( pfn ) );
185 } else if( !lfn.empty() ){
186 fids.push_back( mycatalog->lookupLFN( lfn ) );
187 } else {
188 mycatalog->getFID( fids );
189 }
190 for( const auto& fid: fids ) {
191 cout << fid << endl;
192 }
193 }
194 else {
195 cerr<< "ERROR! Unsupported catalog item type for listing: "<< item << endl;
196 printUsage();
197 return 2;
198 }
199
200 } else if( action == "register" ) {
201 if( item == "lfn" ) {
202 if( pfn.empty() || lfn.empty() ){
203 cerr<<"ERROR! You must specify PFName using -p and LFName using -l" << endl;
204 return 3;
205 }
206 mycatalog->registerLFN( mycatalog->lookupPFN(pfn), lfn );
207 }
208 else if( item == "pfn" ) {
209 if( pfn.empty() or guid.empty() ) {
210 cerr<<"ERROR! You must specify PFName using -p and GUID using -g options" << endl;
211 return 3;
212 }
213 FileCatalogUtils::registerPFN(*mycatalog, pfn, "ROOT_All", guid);
214 }
215 else {
216 cerr << "ERROR! Unsupported catalog item type for registration: "<< item << endl;
217 printUsage();
218 return 3;
219 }
220
221 } else if( action == "rename" ) {
222 if( pfn.empty() || newpfn.empty() ){
223 cerr<<"ERROR! You must specify the current PFName using -p and the new PFName using -n option" << endl;
224 return 4;
225 }
226 mycatalog->renamePFN(pfn, newpfn);
227 }
228 else {
229 cerr << "ERROR! Unsupported action: " << action << endl;
230 printUsage();
231 return 10;
232 }
233
234 mycatalog->commit();
235
236 }catch (const std::runtime_error& er){
237 cerr<<er.what()<<endl;
238 return 1;
239 }catch (const std::exception& er){
240 cerr<<er.what()<<endl;
241 return 1;
242 }
243}
void printUsage()
Definition FCcmd.cxx:21
const string & getAction() const
Definition FCcmd.cxx:92
string getOptByName(char opt) const
Definition FCcmd.cxx:86
Options(const Options &)=delete
bool exists(char opt) const
Definition FCcmd.cxx:94
Options & operator=(const Options &)=delete
std::map< char, string > m_argMap
Definition FCcmd.cxx:45
string m_action
Definition FCcmd.cxx:46
string m_progName
Definition FCcmd.cxx:48
Options(int argc, char *argv[])
Definition FCcmd.cxx:56
string m_item
Definition FCcmd.cxx:47
const string & getItem() const
Definition FCcmd.cxx:93
const string & getProgName() const
Definition FCcmd.cxx:91
static bool initGaudi()
initialize the Gaudi framework for standalone executables
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:52
int main()
Definition hello.cxx:18
void registerPFN(Gaudi::IFileCatalog &fc, const std::string &pfn, const std::string &ftype, std::string &fid)
Register PFN, assign new FID if not given. Throws if the PFN is already registered.
std::string addCatalog(Gaudi::IFileCatalogMgr &mgr, const std::string &connect, bool forWriting=false)
Add new catalog, identified by name, to the existing ones, optionally as the write catalog.
std::string getEnvStr(const std::string &key)
Read an environment variable into string (returns empty string if not set).
TFile * file