Skip to content

Commit 2d47785

Browse files
committed
feat: use default FileCatalog if not explicitly specified
1 parent dea87f5 commit 2d47785

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

src/DIRAC/DataManagementSystem/scripts/dirac_dms_filecatalog_cli.py

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,66 @@
2020
2121
FC:/>
2222
"""
23-
import sys
2423

2524
from DIRAC.Core.Base.Script import Script
2625

2726

2827
@Script()
2928
def main():
30-
fcType = "FileCatalog"
29+
fcType = None
30+
catalog = None
3131
Script.registerSwitch("f:", "file-catalog=", f" Catalog client type to use (default {fcType})")
3232
Script.parseCommandLine(ignoreErrors=False)
3333

34-
from DIRAC import gConfig, exit as dexit
35-
from DIRAC.Resources.Catalog.FileCatalogFactory import FileCatalogFactory
36-
37-
fcType = gConfig.getValue("/LocalSite/FileCatalog", "")
38-
39-
res = gConfig.getSections("/Resources/FileCatalogs", listOrdered=True)
40-
if not res["OK"]:
41-
dexit(1)
42-
fcList = res["Value"]
43-
if not fcType:
44-
if res["OK"]:
45-
fcType = res["Value"][0]
34+
from DIRAC import exit as dexit
4635

4736
for switch in Script.getUnprocessedSwitches():
4837
if switch[0].lower() == "f" or switch[0].lower() == "file-catalog":
4938
fcType = switch[1]
5039

5140
if not fcType:
52-
print("No file catalog given and defaults could not be obtained")
53-
sys.exit(1)
41+
# A particular catalog is not specified, try to instantiate the catalog container
42+
from DIRAC.Resources.Catalog.FileCatalog import FileCatalog
43+
44+
catalog = FileCatalog()
45+
if not catalog.valid:
46+
print("Failed to create the FileCatalog container. Try to use a specific catalog with -f option")
47+
dexit(-1)
48+
result = catalog.getMasterCatalogNames()
49+
if not result["OK"]:
50+
print("Failed to get the Master catalog name for the FileCatalog container")
51+
dexit(-1)
52+
masterCatalog = result["Value"][0]
53+
readCatalogs = [c[0] for c in catalog.getReadCatalogs()]
54+
writeCatalogs = [c[0] for c in catalog.getWriteCatalogs()]
55+
allCatalogs = list(set([masterCatalog] + readCatalogs + writeCatalogs))
56+
57+
if len(allCatalogs) == 1:
58+
# If we have a single catalog in the container, let's use this catalog directly
59+
fcType = allCatalogs[0]
60+
catalog = None
61+
else:
62+
print("Starting FileCatalog container client:")
63+
print(f" {masterCatalog} - Master")
64+
for cat in allCatalogs:
65+
if cat != masterCatalog:
66+
cTypes = ["Write"] if cat in writeCatalogs else []
67+
cTypes.extend(["Read"] if cat in readCatalogs else [])
68+
print(f" {cat} - {'-'.join(cTypes)}")
69+
print("")
70+
71+
if fcType:
72+
from DIRAC.Resources.Catalog.FileCatalogFactory import FileCatalogFactory
73+
74+
result = FileCatalogFactory().createCatalog(fcType)
75+
if not result["OK"]:
76+
print(result["Message"])
77+
dexit(-1)
78+
catalog = result["Value"]
79+
print(f"Starting {fcType} client")
5480

5581
from DIRAC.DataManagementSystem.Client.FileCatalogClientCLI import FileCatalogClientCLI
5682

57-
result = FileCatalogFactory().createCatalog(fcType)
58-
if not result["OK"]:
59-
print(result["Message"])
60-
if fcList:
61-
print("Possible choices are:")
62-
for fc in fcList:
63-
print(" " * 5, fc)
64-
sys.exit(1)
65-
print(f"Starting {fcType} client")
66-
catalog = result["Value"]
6783
cli = FileCatalogClientCLI(catalog)
6884
cli.cmdloop()
6985

0 commit comments

Comments
 (0)