320def file_name(fname):
321 """take a file name, return the pair (protocol, 'real' file name)
322 """
323 fname = os.path.expanduser(os.path.expandvars(fname))
324
325 def _normalize_uri(uri):
326 if uri.startswith('/'):
327 return 'file:'+uri
328 return uri
329
330 from urllib.parse import urlsplit
331 url = urlsplit(_normalize_uri(fname))
332 protocol = url.scheme
333 def _normalize(fname):
334 from posixpath import normpath
335 fname = normpath(fname)
336 if fname.startswith('//'): fname = fname[1:]
337 return fname
338
339 if protocol in ('', 'file', 'pfn'):
340 protocol = ''
341 fname = _normalize(url.path)
342
343
344 if fname.startswith('/castor/'):
345 protocol = 'rfio'
346 fname = protocol + ':' + fname
347
348 elif protocol in ('rfio', 'castor'):
349 protocol = 'rfio'
350 fname = _normalize(url.path)
351 fname = protocol+':'+fname
352
353 elif protocol in ('root','dcap', 'dcache', 'http', 'https', 'dav', 'davs'):
354 pass
355
356 elif protocol in ('gsidcap',):
357 protocol = 'gfal:gsidcap'
358 pass
359
360 elif protocol in ('lfn','fid',):
361
362 from PyUtils.PoolFile import PoolFileCatalog as pfc
363 fname = pfc().pfn(protocol+':'+url.path)
364 pass
365
366 elif protocol in ('ami',):
367
368 for token in ('ami:', '//', '/'):
369 if fname.startswith(token):
370 fname = fname[len(token):]
371 fname = 'ami://' + fname
372 pass
373
374 else:
375 print(f
'## warning: unknown protocol [{protocol}]. we will just return our input')
376 pass
377
378 return (protocol, fname)
379