def listdir(path): if not isdir(path): raise OSError, '%s is not a directory.' % path result = [] if path.startswith('/pnfs/') and (prefer_grid or not pnfs_is_mounted): if debug: print '*** Larbatch_posix: Listdir %s using ifdh.' % path # Get normalized tail. tail = os.path.normpath(path[-6:]) # Call "ifdh ls". contents = larbatch_utilities.ifdh_ls(path, 1) # Loop over contents returned by ifdh. # Normalize the paths returned by "ifdh ls", which in this context mainly # has the effect of stripping off trailing '/' on directories. # Filter out parent directory, which ifdh sometimes (usually?) includes in result. for c in contents: nc = os.path.normpath(c.strip()) if not nc.endswith(tail): result.append(os.path.basename(nc)) else: if debug: print '*** Larbatch_posix: Listdir %s using posix.' % path #result = os.listdir(path) # To reduce hang risk, read contents of directory in a subprocess with # a timeout. cmd = ['ls', path] jobinfo = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) q = Queue.Queue() thread = threading.Thread( target=larbatch_utilities.wait_for_subprocess, args=[jobinfo, q]) thread.start() thread.join(timeout=60) if thread.is_alive(): if debug: print '*** Larbatch_posix: Terminating subprocess.' jobinfo.terminate() thread.join() rc = q.get() jobout = q.get() joberr = q.get() if rc == 0: for word in jobout.split(): result.append(word) # Done. return result
def exists(path): result = False if path.startswith('/pnfs/') and (prefer_grid or not pnfs_is_mounted): if debug: print('*** Larbatch_posix: Check existence of %s using ifdh.' % path) # Do "ifdh ls." try: larbatch_utilities.ifdh_ls(path, 0) result = True except: result = False else: if debug: print('*** Larbatch_posix: Check existence of %s using posix.' % path) #result = os.path.exists(path) # In order to reduce hang risk from stat'ing file, # check existence by getting contents of parent directory. npath = os.path.normpath(path) # Strip trailing '/'. dir = os.path.dirname(npath) base = os.path.basename(npath) if dir == '': dir = '.' if isdir(dir): files = listdir(dir) for filename in files: if base == filename: result = True # Done. return result