44# ######################################### #
55
66import os
7+ import sys
78import stat
89from subprocess import run , PIPE , CalledProcessError
910from pathlib import Path
@@ -109,17 +110,6 @@ def is_egroup_member(egroup, verbose=False):
109110# Overwrite Path methods
110111# ======================
111112
112- def _eos_exists (path , * args , ** kwargs ):
113- _assert_eos_accessible ("Cannot stat EOS paths." )
114- try :
115- ftype , _ = _get_type (path , * args , ** kwargs )
116- except FileNotFoundError :
117- return False
118- if ftype is not None :
119- return True
120- return Path (path .eos_path ).exists (* args , ** kwargs )
121-
122-
123113def _get_type (path , * args , ** kwargs ):
124114 success , result = _run_eos (['eos' , 'stat' , path .eos_path ], mgm = path .mgm ,
125115 _false_if_stderr_contains = 'failed to stat' , ** kwargs )
@@ -138,6 +128,16 @@ def _get_type(path, *args, **kwargs):
138128 raise NotImplementedError (f"File type not known. Output:\n { result } " )
139129 return None , None
140130
131+ def _eos_exists (path , * args , ** kwargs ):
132+ _assert_eos_accessible ("Cannot stat EOS paths." )
133+ try :
134+ ftype , _ = _get_type (path , * args , ** kwargs )
135+ except FileNotFoundError :
136+ return False
137+ if ftype is not None :
138+ return True
139+ return Path (path .eos_path ).exists ()
140+
141141def _eos_is_file (path , * args , ** kwargs ):
142142 _assert_eos_accessible ("Cannot stat EOS paths." )
143143 try :
@@ -146,7 +146,7 @@ def _eos_is_file(path, *args, **kwargs):
146146 return False
147147 if ftype is not None :
148148 return ftype == stat .S_IFREG
149- return Path .is_file (path , * args , ** kwargs )
149+ return Path .is_file (path )
150150
151151def _eos_is_dir (path , * args , ** kwargs ):
152152 _assert_eos_accessible ("Cannot stat EOS paths." )
@@ -156,7 +156,7 @@ def _eos_is_dir(path, *args, **kwargs):
156156 return False
157157 if ftype is not None :
158158 return ftype == stat .S_IFDIR
159- return Path .is_dir (path , * args , ** kwargs )
159+ return Path .is_dir (path )
160160
161161def _eos_is_symlink (path , * args , ** kwargs ):
162162 _assert_eos_accessible ("Cannot stat EOS paths." )
@@ -166,7 +166,7 @@ def _eos_is_symlink(path, *args, **kwargs):
166166 return False
167167 if ftype is not None :
168168 return ftype == stat .S_IFLNK
169- return Path .is_symlink (path , * args , ** kwargs )
169+ return Path .is_symlink (path )
170170
171171
172172def _parse_fileinfo (fileinfo , ftype = None , st_size = None ):
@@ -221,69 +221,104 @@ def _parse_fileinfo(fileinfo, ftype=None, st_size=None):
221221 # st_fstype, st_rsize, st_creator, st_type, st_file_attributes, st_reparse_tag
222222 return make_stat_result (stat_dict )
223223
224- def _eos_lstat (path , * args , ** kwargs ):
225- _assert_eos_accessible ("Cannot stat EOS paths." )
226- ftype , st_size = _get_type (path , * args , ** kwargs )
227- if ftype is None :
228- return Path .lstat (path , * args , ** kwargs )
229- elif ftype == stat .S_IFLNK :
230- # Special treatment: do NOT follow link
231- # Temporary solution: no way to retrieve other info currently
232- return make_stat_result ({'st_mode' : ftype + 0o0777 , 'st_size' : st_size })
233- else :
224+ if sys .version_info >= (3 , 10 ):
225+ def _eos_lstat (path , * args , ** kwargs ):
226+ _assert_eos_accessible ("Cannot stat EOS paths." )
227+ ftype , st_size = _get_type (path , * args , ** kwargs )
228+ if ftype is None :
229+ return os .stat (path .as_posix (), follow_symlinks = False )
230+ elif ftype == stat .S_IFLNK :
231+ # Special treatment: do NOT follow link
232+ # Temporary solution: no way to retrieve other info currently
233+ return make_stat_result ({'st_mode' : ftype + 0o0777 , 'st_size' : st_size })
234+ else :
235+ success , result = _run_eos (['eos' , 'fileinfo' , path .eos_path ], mgm = path .mgm ,
236+ _false_if_stderr_contains = 'No such file or directory' , ** kwargs )
237+ if not success :
238+ return os .stat (path .as_posix (), follow_symlinks = False )
239+ else :
240+ if not result :
241+ raise FileNotFoundError
242+ return _parse_fileinfo (result , ftype , st_size )
243+
244+ def _eos_stat (path , * args , follow_symlinks = True , ** kwargs ):
245+ _assert_eos_accessible ("Cannot stat EOS paths." )
246+ # The command `eos fileinfo` automatically resolves symlinks
247+ if not follow_symlinks :
248+ return _eos_lstat (path , * args , ** kwargs )
234249 success , result = _run_eos (['eos' , 'fileinfo' , path .eos_path ], mgm = path .mgm ,
235- _false_if_stderr_contains = 'No such file or directory' , ** kwargs )
250+ _false_if_stderr_contains = 'No such file or directory' , ** kwargs )
236251 if not success :
237- return Path .lstat (path , * args , ** kwargs )
252+ return os .stat (path , follow_symlinks = True )
253+ if not result :
254+ raise FileNotFoundError
255+ ftype , _ = _get_type (path , * args , ** kwargs )
256+ return _parse_fileinfo (result , ftype )
257+
258+ else :
259+ def _eos_lstat (path , * args , ** kwargs ):
260+ _assert_eos_accessible ("Cannot stat EOS paths." )
261+ ftype , st_size = _get_type (path , * args , ** kwargs )
262+ if ftype is None :
263+ return os .lstat (path .as_posix ())
264+ elif ftype == stat .S_IFLNK :
265+ # Special treatment: do NOT follow link
266+ # Temporary solution: no way to retrieve other info currently
267+ return make_stat_result ({'st_mode' : ftype + 0o0777 , 'st_size' : st_size })
238268 else :
239- if not result :
240- raise FileNotFoundError
241- return _parse_fileinfo (result , ftype , st_size )
269+ success , result = _run_eos (['eos' , 'fileinfo' , path .eos_path ], mgm = path .mgm ,
270+ _false_if_stderr_contains = 'No such file or directory' , ** kwargs )
271+ if not success :
272+ return os .lstat (path .as_posix ())
273+ else :
274+ if not result :
275+ raise FileNotFoundError
276+ return _parse_fileinfo (result , ftype , st_size )
242277
243- def _eos_stat (path , * args , ** kwargs ):
244- _assert_eos_accessible ("Cannot stat EOS paths." )
245- ftype , st_size = _get_type ( path , * args , ** kwargs )
246- # The command ` eos fileinfo` automatically resolves symlinks
247- success , result = _run_eos ([ 'eos' , 'fileinfo' , path . eos_path ], mgm = path . mgm ,
248- _false_if_stderr_contains = 'No such file or directory' , ** kwargs )
249- if not success :
250- return Path . stat ( path , * args , ** kwargs )
251- if not result :
252- raise FileNotFoundError
253- return _parse_fileinfo (result , ftype )
254-
255-
256- def _eos_touch (path , * args , ** kwargs ):
278+ def _eos_stat (path , * args , ** kwargs ):
279+ _assert_eos_accessible ("Cannot stat EOS paths." )
280+ # The command `eos fileinfo` automatically resolves symlinks
281+ success , result = _run_eos ([ ' eos' , ' fileinfo' , path . eos_path ], mgm = path . mgm ,
282+ _false_if_stderr_contains = 'No such file or directory' , ** kwargs )
283+ if not success :
284+ return os . stat ( path . as_posix ())
285+ if not result :
286+ raise FileNotFoundError
287+ ftype , _ = _get_type ( path , * args , ** kwargs )
288+ return _parse_fileinfo (result , ftype )
289+
290+
291+ def _eos_touch (path , mode = 0o666 , exist_ok = True , ** kwargs ):
257292 _assert_eos_accessible ("Cannot touch EOS paths." )
258293 success , result = _run_eos (['eos' , 'touch' , path .eos_path ], mgm = path .mgm , ** kwargs )
259294 if success :
260295 return result
261- return Path .touch (path , * args , ** kwargs )
296+ return Path .touch (path , mode = mode , exist_ok = exist_ok )
262297
263- def _eos_unlink (path , * args , ** kwargs ):
298+ def _eos_unlink (path , missing_ok = False , ** kwargs ):
264299 _assert_eos_accessible ("Cannot unlink EOS paths." )
265- if not path .is_symlink () and path .is_dir ():
300+ if not path .is_symlink (** kwargs ) and path .is_dir (** kwargs ):
266301 raise IsADirectoryError (f"{ path } is a directory." )
267302 success , result = _run_eos (['eos' , 'rm' , path .eos_path ], mgm = path .mgm , ** kwargs )
268303 if success :
269304 return result
270- return Path .unlink (path , * args , ** kwargs )
305+ return Path .unlink (path , missing_ok = missing_ok )
271306
272- def _eos_mkdir (path , * args , ** kwargs ):
307+ def _eos_mkdir (path , mode = 0o777 , parents = False , exist_ok = False , ** kwargs ):
273308 _assert_eos_accessible ("Cannot rmdir EOS paths." )
274309 success , result = _run_eos (['eos' , 'mkdir' , path .eos_path ], mgm = path .mgm , ** kwargs )
275310 if success :
276311 return result
277- return Path .mkdir (path , * args , ** kwargs )
312+ return Path .mkdir (path , mode = mode , parents = parents , exist_ok = exist_ok )
278313
279314def _eos_rmdir (path , * args , ** kwargs ):
280315 _assert_eos_accessible ("Cannot rmdir EOS paths." )
281- if path .is_symlink () or not path .is_dir ():
316+ if path .is_symlink (* args , ** kwargs ) or not path .is_dir (* args , ** kwargs ):
282317 raise NotADirectoryError (f"{ path } is not a directory." )
283318 success , result = _run_eos (['eos' , 'rmdir' , path .eos_path ], mgm = path .mgm , ** kwargs )
284319 if success :
285320 return result
286- return Path .rmdir (path , * args , ** kwargs )
321+ return Path .rmdir (path )
287322
288323
289324# Overwrite FsPath methods
0 commit comments