3535# cp file1 file2 ... link1 link points to dir1: copies files into dir1 (potentially overwrites)
3636# cp dir1 file1 error "cp: -r not specified; omitting directory 'dir1'"
3737# cp -r dir1 file1 error "cp: cannot overwrite non-directory 'file1' with directory 'dir1'"
38- # cp -r dir1 dir2 dir2 does not exist: copies dir1 into new dir2
38+ # cp -r dir1 dir2 dir2 does not exist: copies dir1 as new dir2
3939# cp -r dir1 dir2 dir2 does exist: copies dir1 inside dir2
4040# links are treated as the file/dir they point to, unless -P is specified, in which case (only for links as source argument) they are treated as separate files
4141
@@ -90,15 +90,15 @@ def cp(*args, recursive=False, follow_symlinks=True, **kwargs):
9090
9191 # t_new = time.time(); print(f"Setup done ({int(1e3*(t_new-t_prev))}ms)"); t_prev = t_new
9292 # Do the copy
93- stdout , stderr = _cp_regular (sources_targets , follow_symlinks )
93+ stdout , stderr = _cp_regular (sources_targets , follow_symlinks , ** kwargs )
9494 this_stdout += stdout
9595 this_stderr += stderr
9696 # t_new = time.time(); print(f"Regular done ({int(1e3*(t_new-t_prev))}ms)"); t_prev = t_new
97- stdout , stderr = _cp_afs (afs_sources_targets , follow_symlinks )
97+ stdout , stderr = _cp_afs (afs_sources_targets , follow_symlinks , ** kwargs )
9898 this_stdout += stdout
9999 this_stderr += stderr
100100 # t_new = time.time(); print(f"AFS done ({int(1e3*(t_new-t_prev))}ms)"); t_prev = t_new
101- stdout , stderr = _cp_eos (eos_sources_targets , follow_symlinks )
101+ stdout , stderr = _cp_eos (eos_sources_targets , follow_symlinks , ** kwargs )
102102 this_stdout += stdout
103103 this_stderr += stderr
104104 # t_new = time.time(); print(f"EOS done ({int(1e3*(t_new-t_prev))}ms)"); t_prev = t_new
@@ -169,7 +169,7 @@ def _loop_sources_and_verify(sources, target):
169169 return src_target_recursive
170170
171171
172- def _cp_regular (sources_targets , follow_symlinks ):
172+ def _cp_regular (sources_targets , follow_symlinks , ** kwargs ):
173173 stdout = ""
174174 stderr = ""
175175 for src , target , recursive in sources_targets :
@@ -205,22 +205,42 @@ def _cp_regular(sources_targets, follow_symlinks):
205205 return stdout , stderr # An empty error message means success
206206
207207
208- def _cp_afs (sources_targets , follow_symlinks ):
208+ def _cp_afs (sources_targets , follow_symlinks , method = None , ** kwargs ):
209209 from xaux .fs import _skip_afs_software , _force_xrdcp
210210 this_stdout = ""
211211 this_stderr = ""
212212 if _skip_afs_software :
213213 assert not _force_xrdcp
214214
215+ if method :
216+ if method not in ['mount' , 'xrdcp' ]:
217+ raise ValueError (f"Invalid method '{ method } ' for AFS copy. "
218+ + f"Use 'mount' or 'xrdcp'." )
219+ elif method == 'xrdcp' :
220+ if not _xrdcp_installed :
221+ raise RuntimeError ("xrdcp is not installed. Cannot copy "
222+ + "files to/from AFS using xrdcp." )
223+ elif _skip_afs_software :
224+ raise RuntimeError ("Cannot copy files to/from AFS using xrdcp "
225+ + "because AFS software is skipped." )
226+ elif method == 'mount' :
227+ if not _afs_mounted :
228+ raise RuntimeError ("AFS is not mounted. Cannot copy files "
229+ + "to/from AFS using mount." )
230+ elif _force_xrdcp :
231+ raise RuntimeError ("Cannot copy files to/from AFS using mount "
232+ + "because xrdcp is forced." )
233+
215234 # We first try to use xrdcp
216- if sources_targets and _xrdcp_installed and not _skip_afs_software :
235+ if sources_targets and _xrdcp_installed and not _skip_afs_software \
236+ and method != 'mount' :
217237 sources_targets , stdout , stderr = _cp_xrdcp (sources_targets )
218238 this_stdout += stdout
219239 this_stderr += stderr
220240
221241 # If xrdcp failed, we try to use the AFS mout
222242 regular_stderr = ""
223- if sources_targets :
243+ if sources_targets and method != 'xrdcp' :
224244 if _force_xrdcp :
225245 this_stderr += "Skipping AFS mount.\n "
226246 this_stderr += "Failed to copy files to AFS.\n "
@@ -242,29 +262,63 @@ def _cp_afs(sources_targets, follow_symlinks):
242262 return this_stdout , this_stderr + regular_stderr
243263
244264
245- def _cp_eos (sources_targets , follow_symlinks ):
265+ def _cp_eos (sources_targets , follow_symlinks , method = None , ** kwargs ):
246266 from xaux .fs import _skip_eos_software , _force_eoscmd , _force_xrdcp
247267 this_stdout = ""
248268 this_stderr = ""
249269 if _skip_eos_software :
250270 assert not _force_eoscmd
251271 assert not _force_xrdcp
252272
273+ if method :
274+ if method not in ['mount' , 'xrdcp' , 'eoscmd' ]:
275+ raise ValueError (f"Invalid method '{ method } ' for EOS copy. "
276+ + f"Use 'mount', 'xrdcp' or 'eoscmd'." )
277+ elif method == 'xrdcp' :
278+ if not _xrdcp_installed :
279+ raise RuntimeError ("xrdcp is not installed. Cannot copy "
280+ + "files to/from EOS using xrdcp." )
281+ elif _skip_eos_software :
282+ raise RuntimeError ("Cannot copy files to/from EOS using xrdcp "
283+ + "because EOS software is skipped." )
284+ elif _force_eoscmd :
285+ raise RuntimeError ("Cannot copy files to/from EOS using xrdcp "
286+ + "because eoscmd is forced." )
287+ elif method == 'eoscmd' :
288+ if not _eoscmd_installed :
289+ raise RuntimeError ("eoscmd is not installed. Cannot copy "
290+ + "files to/from EOS using eoscmd." )
291+ elif _skip_eos_software :
292+ raise RuntimeError ("Cannot copy files to/from EOS using eoscmd "
293+ + "because EOS software is skipped." )
294+ elif _force_xrdcp :
295+ raise RuntimeError ("Cannot copy files to/from EOS using eoscmd "
296+ + "because xrdcp is forced." )
297+ elif method == 'mount' :
298+ if not _eos_mounted :
299+ raise RuntimeError ("EOS is not mounted. Cannot copy files "
300+ + "to/from EOS using mount." )
301+ elif _force_xrdcp or _force_eoscmd :
302+ raise RuntimeError ("Cannot copy files to/from EOS using mount "
303+ + "because xrdcp or eoscmd are forced." )
304+
253305 # We first try to use xrdcp
254- if sources_targets and _xrdcp_installed and not _force_eoscmd and not _skip_eos_software :
306+ if sources_targets and _xrdcp_installed and not _force_eoscmd and not _skip_eos_software \
307+ and method != 'mount' and method != 'eoscmd' :
255308 sources_targets , stdout , stderr = _cp_xrdcp (sources_targets )
256309 this_stdout += stdout
257310 this_stderr += stderr
258311
259312 # If xrdcp failed, we try to use eos
260- if sources_targets and _eoscmd_installed and not _force_xrdcp and not _skip_eos_software :
313+ if sources_targets and _eoscmd_installed and not _force_xrdcp and not _skip_eos_software \
314+ and method != 'mount' and method != 'xrdcp' :
261315 sources_targets , stdout , stderr = _cp_eoscmd (sources_targets )
262316 this_stdout += stdout
263317 this_stderr += stderr
264318
265319 # If eos also failed, we try using the EOS mount
266320 regular_stderr = ""
267- if sources_targets :
321+ if sources_targets and method != 'xrdcp' and method != 'eoscmd' :
268322 if _force_xrdcp or _force_eoscmd :
269323 this_stderr += "Skipping EOS mount.\n "
270324 this_stderr += "Failed to copy files to EOS.\n "
0 commit comments