Skip to content

Commit 5b7253d

Browse files
authored
Merge pull request #6899 from ic-hep/fixdrm
[8.0] fix: make -F (file) option work for drm command
2 parents f172154 + d74ecf8 commit 5b7253d

File tree

1 file changed

+57
-42
lines changed
  • src/DIRAC/Interfaces/scripts

1 file changed

+57
-42
lines changed

src/DIRAC/Interfaces/scripts/drm.py

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,90 @@
33
Remove files from the FileCatalog (and all replicas from Storage Elements)
44
55
Examples:
6-
$ drm ./some_lfn_file
6+
$ drm /your/lfn/goes/here
7+
$ drm -F myfilecontaininglfns.txt
78
"""
89
import os
910

1011
import DIRAC
11-
from DIRAC import S_OK
12+
from DIRAC import S_OK, gLogger
1213
from DIRAC.Core.Base.Script import Script
1314
from DIRAC.Interfaces.Utilities.DCommands import DSession
1415
from DIRAC.Interfaces.Utilities.DCommands import DCatalog
1516
from DIRAC.Interfaces.Utilities.DCommands import pathFromArgument
1617
from DIRAC.Interfaces.Utilities.DConfigCache import ConfigCache
1718

1819

19-
@Script()
20-
def main():
21-
lfnFileName = ""
22-
23-
def setLfnFileName(arg):
24-
global lfnFileName
25-
lfnFileName = arg
26-
return S_OK()
20+
class Params:
21+
"""handles input options for drm command"""
2722

28-
targetSE = ""
23+
def __init__(self):
24+
"""creates a Params class with default values"""
25+
self.lfnFileName = ""
26+
self.targetSE = ""
27+
self.rmDirFlag = False
2928

30-
def setSE(arg):
31-
global targetSE
32-
targetSE = arg
29+
def setLfnFileName(self, lfnFile):
30+
"""sets filename for file containing the lfns to de deleted"""
31+
self.lfnFileName = lfnFile
3332
return S_OK()
3433

35-
rmDirFlag = False
34+
def setSE(self, targetSE):
35+
"""
36+
sets the name of the storage element from which the files
37+
are to be removed
38+
"""
39+
self.targetSE = targetSE
40+
return S_OK()
3641

37-
def setDirFlag(arg):
38-
global rmDirFlag
39-
rmDirFlag = True
42+
def setDirFlag(self, _):
43+
"""flag to remove directories recursively"""
44+
self.rmDirFlag = True
4045
return S_OK()
4146

42-
Script.registerArgument(["lfn: logical file name"], mandatory=False)
43-
Script.registerSwitch("F:", "lfnFile=", "file containing a list of LFNs", setLfnFileName)
44-
Script.registerSwitch("D:", "destination-se=", "Storage Element from where to remove replica", setSE)
45-
Script.registerSwitch("r", "", "remove directory recursively", setDirFlag)
47+
def registerCLISwitches(self):
48+
"""adds options to the DIRAC options parser"""
49+
Script.registerArgument(["lfn: logical file name"], mandatory=False)
50+
Script.registerSwitch("F:", "lfnFile=", "file containing a list of LFNs", self.setLfnFileName)
51+
Script.registerSwitch("D:", "destination-se=", "Storage Element from where to remove replica", self.setSE)
52+
Script.registerSwitch("r", "", "remove directory recursively", self.setDirFlag)
53+
4654

55+
@Script()
56+
def main():
57+
"""where all the action is"""
4758
configCache = ConfigCache()
48-
Script.parseCommandLine(ignoreErrors=True)
49-
configCache.cacheConfig()
59+
options = Params()
60+
options.registerCLISwitches()
5061

62+
Script.parseCommandLine(ignoreErrors=True)
5163
args = Script.getPositionalArgs()
5264

65+
configCache.cacheConfig()
66+
5367
session = DSession()
5468
catalog = DCatalog()
5569

56-
if not args and not lfnFileName:
57-
print(f"Error: No argument provided\n{Script.scriptName}:")
70+
if not args and not options.lfnFileName:
71+
gLogger.error(f"No argument provided for:\n{Script.scriptName}")
5872
Script.showHelp(exitCode=-1)
5973

6074
lfns = set()
6175
for path in args:
6276
lfns.add(pathFromArgument(session, path))
6377

64-
if lfnFileName:
65-
if not os.path.exists(lfnFileName):
66-
print(f"Error: non-existent file {lfnFileName}:")
78+
if options.lfnFileName:
79+
if not os.path.exists(options.lfnFileName):
80+
gLogger.error(f"non-existent file {options.lfnFileName}:")
6781
DIRAC.exit(-1)
68-
lfnFile = open(lfnFileName)
82+
lfnFile = open(options.lfnFileName)
6983
lfnList = lfnFile.readlines()
70-
lfnSet = {pathFromArgument(session, lfn.strip()) for lfn in lfnList if lfn}
84+
# ignore empty lines anywhere in the file
85+
lfnList = [lfn.strip() for lfn in lfnList]
86+
lfnSet = {pathFromArgument(session, lfn) for lfn in lfnList if lfn}
7187
lfns.update(lfnSet)
7288

7389
from DIRAC.Interfaces.API.Dirac import Dirac
74-
from DIRAC import gLogger
7590
from DIRAC.Core.Utilities.ReturnValues import returnSingleResult
7691
from DIRAC.DataManagementSystem.Client.DataManager import DataManager
7792

@@ -80,42 +95,42 @@ def setDirFlag(arg):
8095

8196
nLfns = len(lfns)
8297
if nLfns > 1:
83-
gLogger.notice("Removing %d objects" % nLfns)
98+
gLogger.notice(f"Removing {nLfns} objects")
8499

85100
exitCode = 0
86101
goodCounter = 0
87102
badCounter = 0
88103
for lfn in lfns:
89-
if rmDirFlag and not catalog.isFile(lfn):
104+
if options.rmDirFlag and not catalog.isFile(lfn):
90105
result = returnSingleResult(dm.cleanLogicalDirectory(lfn))
91106
if result["OK"]:
92107
goodCounter += 1
93108
else:
94-
print("ERROR:", result["Message"])
109+
gLogger.error(result["Message"])
95110
badCounter += 1
96111
exitCode = 3
97112
else:
98-
if targetSE:
99-
result = returnSingleResult(dirac.removeReplica(lfn, targetSE, printOutput=False))
113+
if options.targetSE:
114+
result = returnSingleResult(dirac.removeReplica(lfn, options.targetSE, printOutput=False))
100115
else:
101116
result = returnSingleResult(dirac.removeFile(lfn, printOutput=False))
102117
if not result["OK"]:
103118
if "No such file or directory" == result["Message"]:
104119
gLogger.notice(f"{lfn} no such file")
105120
else:
106-
gLogger.error(f"ERROR {lfn}: {result['Message']}")
121+
gLogger.error(f"{lfn}: {result['Message']}")
107122
badCounter += 1
108123
exitCode = 2
109124
else:
110125
goodCounter += 1
111126
if goodCounter % 10 == 0:
112-
gLogger.notice("%d files removed" % goodCounter)
127+
gLogger.notice(f"{goodCounter} files removed")
113128
if badCounter:
114-
gLogger.notice("%d files failed removal" % badCounter)
129+
gLogger.notice(f"{badCounter} files failed removal")
115130

116-
gLogger.notice("\n%d object(s) removed in total" % goodCounter)
131+
gLogger.notice(f"\n{goodCounter} object(s) removed in total")
117132
if badCounter:
118-
gLogger.notice("%d object(s) failed removal in total" % badCounter)
133+
gLogger.notice(f"{badCounter} object(s) failed removal in total")
119134

120135
DIRAC.exit(exitCode)
121136

0 commit comments

Comments
 (0)