def _ls(irods_path): cmd = CommandWrapper(["ils", irods_path]) cmd() def remove_header_line(lines): return lines[1:] def remove_redundant_whitespace(lines): return [l.strip() for l in lines] def deal_with_collections(lines): fixed_lines = [] for l in lines: if l.startswith("C"): _, path = l.split() l = os.path.basename(path) fixed_lines.append(l) return fixed_lines text = cmd.stdout.strip() lines = text.split("\n") return deal_with_collections( remove_redundant_whitespace( remove_header_line(lines) ) )
def _get_text(irods_path): """Get raw text from iRODS.""" # Command to get contents of file to stdout. cmd = CommandWrapper([ "iget", irods_path, "-" ]) return cmd()
def _get_checksum(irods_path): # Get the hash. cmd = CommandWrapper(["ichksum", "-K", irods_path]) cmd() line = cmd.stdout.strip() info = line.split() compound_chksum = info[1] alg, checksum = compound_chksum.split(":") return checksum
def _put_text(irods_path, text): """Put raw text into iRODS.""" with tempfile.NamedTemporaryFile() as fh: fpath = fh.name fh.write(text) fh.flush() cmd = CommandWrapper([ "iput", "-f", fpath, irods_path ]) cmd() assert not os.path.isfile(fpath)
def _get_size_and_timestamp_with_cache(self, irods_path): if self._use_cache: if irods_path in self._size_and_timestamp_cache: return self._size_and_timestamp_cache[irods_path] cmd = CommandWrapper(["ils", "-l", irods_path]) cmd() text = cmd.stdout.strip() first_line = text.split("\n")[0].strip() info = first_line.split() size_in_bytes_str = info[3] size_in_bytes = int(size_in_bytes_str) time_str = info[4] dt = datetime.datetime.strptime(time_str, "%Y-%m-%d.%H:%M") utc_timestamp = int(time.mktime(dt.timetuple())) return size_in_bytes, utc_timestamp
def _build_size_and_timestamp_cache(self): cmd = CommandWrapper(["ils", "-l", self._data_abspath]) cmd() text = cmd.stdout.strip() for line in text.split("\n")[1:]: line = line.strip() info = line.split() size_in_bytes_str = info[3] size_in_bytes = int(size_in_bytes_str) time_str = info[4] dt = datetime.datetime.strptime(time_str, "%Y-%m-%d.%H:%M") utc_timestamp = int(time.mktime(dt.timetuple())) fname = info[6] fpath = os.path.join(self._data_abspath, fname) self._size_and_timestamp_cache[fpath] = ( size_in_bytes, utc_timestamp )
def _get_metadata_with_cache(self, irods_path, key): if self._use_cache: if irods_path in self._metadata_cache: if key in self._metadata_cache[irods_path]: return self._metadata_cache[irods_path][key] cmd = CommandWrapper(["imeta", "ls", "-d", irods_path, key]) cmd() text = cmd.stdout value_line = text.split('\n')[2] value = value_line.split(":")[1] value = value.strip() if self._use_cache: self._metadata_cache.setdefault( irods_path, {}).update({key: value}) return value
def _rm(irods_path): cmd = CommandWrapper(["irm", "-rf", irods_path]) cmd()
def _cp(fpath, irods_path): cmd = CommandWrapper(["iput", "-f", fpath, irods_path]) cmd()
def _mkdir(irods_path): cmd = CommandWrapper(["imkdir", irods_path]) cmd()
def _path_exists(irods_path): cmd = CommandWrapper(["ils", irods_path]) cmd(exit_on_failure=False) return cmd.success()
def _get_file(irods_path, local_abspath): cmd = CommandWrapper(["iget", irods_path, local_abspath]) cmd()
def _put_metadata(irods_path, key, value): cmd = CommandWrapper(["imeta", "set", "-d", irods_path, key, value]) cmd()