1111from .tools import log_debug , log_info , log_error
1212
1313
14+ # TODO: replace xrdcp with 'eos cp'
15+ # Alternatively with pyxrootd (https://xrootd.slac.stanford.edu/doc/python/xrootd-python-0.1.0/index.html)
16+ # though the latter is not in active development
17+
18+
1419# Functions to work with the EOS file system
1520# ==========================================
1621EOS_MGM_URL = 'root://eosuser.cern.ch'
1722eos_env = {** os .environ , 'EOS_MGM_URL' : EOS_MGM_URL }
1823
1924
20- def is_xrdcp_installed ():
25+ def xrdcp_installed ():
2126 try :
2227 # Run the xrdcp command with the "--version" flag to check if it's installed
2328 cmd = subprocess .run (["xrdcp" , "--version" ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , check = True )
2429 if cmd .returncode == 0 :
2530 return True
2631 else :
2732 return False
28- except subprocess .CalledProcessError :
33+ except ( subprocess .CalledProcessError , FileNotFoundError ) :
2934 # xrdcp is not installed or returned an error
3035 return False
3136
@@ -80,26 +85,30 @@ def eos_rm(path, is_server=False):
8085
8186# Always use this file by file, not for a list of files
8287def cp_from_eos (source , target , maximum_trials = 10 , wait = 2.7 , is_server = False ):
83- try :
84- target = Path (target ).expanduser ().resolve ()
85- for i in range (maximum_trials ):
86- cmd = subprocess .run (['xrdcp' , '--cksum' , 'adler32' , _eos_path (source ), target .as_posix ()], env = eos_env )
87- if cmd .returncode == 0 and target .exists () and target .stat ().st_size != 0 :
88- return 0
89- if i != maximum_trials - 1 :
90- log_debug (f"Failed to copy { str (source )} to { str (target )} !\n Retrying ({ i } ).." , is_server = is_server )
91- sleep (abs (wait + random .normalvariate (0 , 0.1 * wait )))
92- log_error (f"Failed to copy { str (source )} to { str (target )} !" , is_server = is_server )
93- if not target .exists () or target .stat ().st_size == 0 :
94- log_error (f"Command succeeds but destination file is not created!" , is_server = is_server )
95- else :
96- stderr = cmd .stderr .decode ('UTF-8' ).strip ().split ('\n ' )
97- log_error (f"Command failed: { stderr } " , is_server = is_server )
98- log_error ("\n Giving up." , is_server = is_server )
99- return 1
100- except Exception as e :
101- log_error (f"Failed to copy { str (source )} to { str (target )} !\n " , e , is_server = is_server )
88+ if not xrdcp_installed ():
89+ log_error ("Error: xrdcp is not installed on your system. Cannot copy from EOS." , ValueError (), is_server = is_server )
10290 return 1
91+ else :
92+ try :
93+ target = Path (target ).expanduser ().resolve ()
94+ for i in range (maximum_trials ):
95+ cmd = subprocess .run (['xrdcp' , '--cksum' , 'adler32' , _eos_path (source ), target .as_posix ()], env = eos_env )
96+ if cmd .returncode == 0 and target .exists () and target .stat ().st_size != 0 :
97+ return 0
98+ if i != maximum_trials - 1 :
99+ log_debug (f"Failed to copy { str (source )} to { str (target )} !\n Retrying ({ i } ).." , is_server = is_server )
100+ sleep (abs (wait + random .normalvariate (0 , 0.1 * wait )))
101+ log_error (f"Failed to copy { str (source )} to { str (target )} !" , is_server = is_server )
102+ if not target .exists () or target .stat ().st_size == 0 :
103+ log_error (f"Command succeeds but destination file is not created!" , is_server = is_server )
104+ else :
105+ stderr = cmd .stderr .decode ('UTF-8' ).strip ().split ('\n ' )
106+ log_error (f"Command failed: { stderr } " , is_server = is_server )
107+ log_error ("\n Giving up." , is_server = is_server )
108+ return 1
109+ except Exception as e :
110+ log_error (f"Failed to copy { str (source )} to { str (target )} !\n " , e , is_server = is_server )
111+ return 1
103112
104113def mv_from_eos (source , target , maximum_trials = 10 , wait = 2.7 , is_server = False ):
105114 if not cp_from_eos (source , target , maximum_trials , wait , is_server = is_server ): # returncode 0 means success
@@ -108,28 +117,32 @@ def mv_from_eos(source, target, maximum_trials=10, wait=2.7, is_server=False):
108117
109118# Always use this file by file, not for a list of files
110119def cp_to_eos (source , target , maximum_trials = 10 , wait = 2.7 , is_server = False ):
111- try :
112- source = Path (source ).expanduser ().resolve ()
113- target = Path (target , source .name ).expanduser ().resolve ()
114- # target = Path(target).expanduser().resolve()
115- for i in range (maximum_trials ):
116- cmd = subprocess .run (['xrdcp' , '--cksum' , 'adler32' , source .as_posix (), _eos_path (target )], env = eos_env )
117- if cmd .returncode == 0 and eos_exists (target ):
118- return 0
119- if i != maximum_trials - 1 :
120- log_debug (f"Failed to copy { str (source )} to { str (target )} !\n Retrying ({ i } ).." , is_server = is_server )
121- sleep (abs (wait + random .normalvariate (0 , 0.1 * wait )))
122- log_error (f"Failed to copy { str (source )} to { str (target )} !" , is_server = is_server )
123- if not eos_exists (target ):
124- log_error (f"Command succeeds but destination file is not created!" , is_server = is_server )
125- else :
126- stderr = cmd .stderr .decode ('UTF-8' ).strip ().split ('\n ' )
127- log_error (f"Command failed: { stderr } " , is_server = is_server )
128- log_error ("\n Giving up." , is_server = is_server )
129- return 1
130- except Exception as e :
131- log_error (f"Failed to copy { str (source )} to { str (target )} !\n " , e , is_server = is_server )
120+ if not xrdcp_installed ():
121+ log_error ("Error: xrdcp is not installed on your system. Cannot copy to EOS." , ValueError (), is_server = is_server )
132122 return 1
123+ else :
124+ try :
125+ source = Path (source ).expanduser ().resolve ()
126+ target = Path (target , source .name ).expanduser ().resolve ()
127+ # target = Path(target).expanduser().resolve()
128+ for i in range (maximum_trials ):
129+ cmd = subprocess .run (['xrdcp' , '--cksum' , 'adler32' , source .as_posix (), _eos_path (target )], env = eos_env )
130+ if cmd .returncode == 0 and eos_exists (target ):
131+ return 0
132+ if i != maximum_trials - 1 :
133+ log_debug (f"Failed to copy { str (source )} to { str (target )} !\n Retrying ({ i } ).." , is_server = is_server )
134+ sleep (abs (wait + random .normalvariate (0 , 0.1 * wait )))
135+ log_error (f"Failed to copy { str (source )} to { str (target )} !" , is_server = is_server )
136+ if not eos_exists (target ):
137+ log_error (f"Command succeeds but destination file is not created!" , is_server = is_server )
138+ else :
139+ stderr = cmd .stderr .decode ('UTF-8' ).strip ().split ('\n ' )
140+ log_error (f"Command failed: { stderr } " , is_server = is_server )
141+ log_error ("\n Giving up." , is_server = is_server )
142+ return 1
143+ except Exception as e :
144+ log_error (f"Failed to copy { str (source )} to { str (target )} !\n " , e , is_server = is_server )
145+ return 1
133146
134147def mv_to_eos (source , target , maximum_trials = 10 , wait = 2.7 , is_server = False ):
135148 if not cp_to_eos (source , target , maximum_trials , wait , is_server = is_server ): # returncode 0 means success
0 commit comments