3
3
Remove files from the FileCatalog (and all replicas from Storage Elements)
4
4
5
5
Examples:
6
- $ drm ./some_lfn_file
6
+ $ drm /your/lfn/goes/here
7
+ $ drm -F myfilecontaininglfns.txt
7
8
"""
8
9
import os
9
10
10
11
import DIRAC
11
- from DIRAC import S_OK
12
+ from DIRAC import S_OK , gLogger
12
13
from DIRAC .Core .Base .Script import Script
13
14
from DIRAC .Interfaces .Utilities .DCommands import DSession
14
15
from DIRAC .Interfaces .Utilities .DCommands import DCatalog
15
16
from DIRAC .Interfaces .Utilities .DCommands import pathFromArgument
16
17
from DIRAC .Interfaces .Utilities .DConfigCache import ConfigCache
17
18
18
19
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"""
27
22
28
- targetSE = ""
23
+ def __init__ (self ):
24
+ """creates a Params class with default values"""
25
+ self .lfnFileName = ""
26
+ self .targetSE = ""
27
+ self .rmDirFlag = False
29
28
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
33
32
return S_OK ()
34
33
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 ()
36
41
37
- def setDirFlag (arg ):
38
- global rmDirFlag
39
- rmDirFlag = True
42
+ def setDirFlag (self , _ ):
43
+ """flag to remove directories recursively"""
44
+ self . rmDirFlag = True
40
45
return S_OK ()
41
46
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
+
46
54
55
+ @Script ()
56
+ def main ():
57
+ """where all the action is"""
47
58
configCache = ConfigCache ()
48
- Script . parseCommandLine ( ignoreErrors = True )
49
- configCache . cacheConfig ()
59
+ options = Params ( )
60
+ options . registerCLISwitches ()
50
61
62
+ Script .parseCommandLine (ignoreErrors = True )
51
63
args = Script .getPositionalArgs ()
52
64
65
+ configCache .cacheConfig ()
66
+
53
67
session = DSession ()
54
68
catalog = DCatalog ()
55
69
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 } " )
58
72
Script .showHelp (exitCode = - 1 )
59
73
60
74
lfns = set ()
61
75
for path in args :
62
76
lfns .add (pathFromArgument (session , path ))
63
77
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 } :" )
67
81
DIRAC .exit (- 1 )
68
- lfnFile = open (lfnFileName )
82
+ lfnFile = open (options . lfnFileName )
69
83
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 }
71
87
lfns .update (lfnSet )
72
88
73
89
from DIRAC .Interfaces .API .Dirac import Dirac
74
- from DIRAC import gLogger
75
90
from DIRAC .Core .Utilities .ReturnValues import returnSingleResult
76
91
from DIRAC .DataManagementSystem .Client .DataManager import DataManager
77
92
@@ -80,42 +95,42 @@ def setDirFlag(arg):
80
95
81
96
nLfns = len (lfns )
82
97
if nLfns > 1 :
83
- gLogger .notice ("Removing %d objects" % nLfns )
98
+ gLogger .notice (f "Removing { nLfns } objects" )
84
99
85
100
exitCode = 0
86
101
goodCounter = 0
87
102
badCounter = 0
88
103
for lfn in lfns :
89
- if rmDirFlag and not catalog .isFile (lfn ):
104
+ if options . rmDirFlag and not catalog .isFile (lfn ):
90
105
result = returnSingleResult (dm .cleanLogicalDirectory (lfn ))
91
106
if result ["OK" ]:
92
107
goodCounter += 1
93
108
else :
94
- print ( "ERROR:" , result ["Message" ])
109
+ gLogger . error ( result ["Message" ])
95
110
badCounter += 1
96
111
exitCode = 3
97
112
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 ))
100
115
else :
101
116
result = returnSingleResult (dirac .removeFile (lfn , printOutput = False ))
102
117
if not result ["OK" ]:
103
118
if "No such file or directory" == result ["Message" ]:
104
119
gLogger .notice (f"{ lfn } no such file" )
105
120
else :
106
- gLogger .error (f"ERROR { lfn } : { result ['Message' ]} " )
121
+ gLogger .error (f"{ lfn } : { result ['Message' ]} " )
107
122
badCounter += 1
108
123
exitCode = 2
109
124
else :
110
125
goodCounter += 1
111
126
if goodCounter % 10 == 0 :
112
- gLogger .notice ("%d files removed" % goodCounter )
127
+ gLogger .notice (f" { goodCounter } files removed" )
113
128
if badCounter :
114
- gLogger .notice ("%d files failed removal" % badCounter )
129
+ gLogger .notice (f" { badCounter } files failed removal" )
115
130
116
- gLogger .notice ("\n %d object(s) removed in total" % goodCounter )
131
+ gLogger .notice (f "\n { goodCounter } object(s) removed in total" )
117
132
if badCounter :
118
- gLogger .notice ("%d object(s) failed removal in total" % badCounter )
133
+ gLogger .notice (f" { badCounter } object(s) failed removal in total" )
119
134
120
135
DIRAC .exit (exitCode )
121
136
0 commit comments