|
1 | | -#!/usr/bin/env python |
2 | | -""" |
3 | | - Usage: files-list [OPTIONS] agave://SYSTEM/PATH |
4 | | -
|
5 | | -Lists files on a Tapis storage system. |
6 | | -
|
7 | | -The path must be specified as an agave URI. |
8 | | -Example: |
9 | | - files-list -l agave://tacc-data-user/path/to/dir |
10 | | -
|
11 | | - Flags: |
12 | | - -c, --cachedir Session cache directory. |
13 | | - -l, --long Use long listing format |
14 | | -""" |
15 | | -from __future__ import print_function |
16 | | -import argparse |
17 | | -import sys |
18 | | -from agavepy.agave import Agave |
19 | | -from agavepy import utils |
20 | | - |
21 | | -parser = argparse.ArgumentParser( |
22 | | - description="Lists files on a Tapis storage system. The path must be specified as an agave URI (i.e. agave://tacc-data-user/path/to/dir)") |
23 | | - |
24 | | -parser.add_argument( |
25 | | - "-c", |
26 | | - "--cachedir", |
27 | | - default=utils.credentials_cache_dir(), |
28 | | - help="Session cache directory.") |
29 | | - |
30 | | -parser.add_argument( |
31 | | - "-l", "--long", action="store_true", help="Use long listing format.") |
32 | | - |
33 | | -parser.add_argument("filepath", help="Path to list") |
34 | | - |
35 | | -if __name__ == "__main__": |
36 | | - try: |
37 | | - args = parser.parse_args() |
38 | | - |
39 | | - cache_dir = args.cachedir |
40 | | - long_format = args.long |
41 | | - uri = args.filepath |
42 | | - agave = Agave() |
43 | | - agave.load_client(cache_dir) |
44 | | - |
45 | | - # Refresh tokens if necessary. |
46 | | - agave.refresh_tokens() |
47 | | - # Save out session cache |
48 | | - if agave.client_name is not None: |
49 | | - agave.save_configs(cache_dir) |
50 | | - |
51 | | - # Obtain uri - remove the prefix 'agave://'. |
52 | | - uri = uri[len("agave://"):] |
53 | | - |
54 | | - # List files on remote system. |
55 | | - agave.files_list(uri, long_format=long_format) |
56 | | - sys.exit(0) |
57 | | - |
58 | | - except Exception as err: |
59 | | - print(err) |
60 | | - sys.exit(1) |
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# files-list |
| 4 | +# |
| 5 | +# author: opensource@tacc.cloud |
| 6 | +# |
| 7 | +# This script is part of the Agave API command line interface (CLI). |
| 8 | +# It retrieves a file listing on a remote system. If no jobs. Optionally, one can search by name, tag, |
| 9 | +# ontology, or start date |
| 10 | +# |
| 11 | + |
| 12 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 13 | + |
| 14 | +source "$DIR/common.sh" |
| 15 | + |
| 16 | +# Script logic -- TOUCH THIS {{{ |
| 17 | + |
| 18 | +# A list of all variables to prompt in interactive mode. These variables HAVE |
| 19 | +# to be named exactly as the longname option definition in usage(). |
| 20 | +interactive_opts=(apisecret apikey) |
| 21 | + |
| 22 | +# Print usage |
| 23 | +usage() { |
| 24 | + echo -n "$(basename $0) [OPTION]... [PATH] |
| 25 | +List files on a remote system. If no system is specified, your default storage |
| 26 | +system will be used. By specifying a system, the path given will be resolved |
| 27 | +on that remote system. Note that the system id, not hostname must be given. |
| 28 | + Options: |
| 29 | + -z, --access_token Access token |
| 30 | + -S, --systemId Specify the system id |
| 31 | + -L, --long Print response in unix-style ls -l format |
| 32 | + -l, --limit Maximum number of results to return |
| 33 | + -o, --offset Number of results to skip from the start |
| 34 | + --filter Comma separated list of fields to return in the response |
| 35 | + -H, --hosturl URL of the service |
| 36 | + -d, --development Run in dev mode using default dev server |
| 37 | + -f, --force Skip all user interaction |
| 38 | + -i, --interactive Prompt for values |
| 39 | + -q, --quiet Quiet (no output) |
| 40 | + -v, --verbose Verbose output |
| 41 | + -V, --veryverbose Very verbose output |
| 42 | + -h, --help Display this help and exit |
| 43 | + --version Output version information and exit |
| 44 | + --rich Provide rich response |
| 45 | +" |
| 46 | +} |
| 47 | + |
| 48 | +################################################################## |
| 49 | +################################################################## |
| 50 | +# Begin Script Logic # |
| 51 | +################################################################## |
| 52 | +################################################################## |
| 53 | + |
| 54 | +source "$DIR/files-common.sh" |
| 55 | + |
| 56 | +main() { |
| 57 | + #echo -n |
| 58 | + #set -x |
| 59 | + |
| 60 | + if [ -n "$systemId" ]; then |
| 61 | + hosturl="${hosturl}listings/system/${systemId}/${args}" |
| 62 | + else |
| 63 | + hosturl="${hosturl}listings/${args}" |
| 64 | + fi |
| 65 | + |
| 66 | + cmd="curl -sk -H \"${authheader}\" '${hosturl}?pretty=true$(pagination)'" |
| 67 | + |
| 68 | + if ((veryverbose)); then |
| 69 | + [ "$piped" -eq 0 ] && log "Calling $cmd" |
| 70 | + fi |
| 71 | + |
| 72 | + response=`curl -sk -H "${authheader}" "${hosturl}?pretty=true$(pagination)"` |
| 73 | + |
| 74 | + if [[ $(jsonquery "$response" "status") = 'success' ]]; then |
| 75 | + result=$(format_api_json "$response") |
| 76 | +# success "$result" |
| 77 | + echo "$result"; |
| 78 | + else |
| 79 | + errorresponse=$(jsonquery "$response" "message") |
| 80 | + err "$errorresponse" |
| 81 | + fi |
| 82 | + |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +format_api_json() { |
| 87 | + |
| 88 | + if ((veryverbose)); then |
| 89 | + json_prettyify "${1}" |
| 90 | + elif [[ $verbose -eq 1 ]]; then |
| 91 | + result=$(jsonquery "$1" "result" 1) |
| 92 | + json_prettyify "${result}" |
| 93 | + elif [[ $long -eq 1 ]]; then |
| 94 | + #lstng=$(lsify "$1" | awk '{ printf("%-10s %+12s %+18s %+32s %s\n", $1, $2, $3, $4, $5); }') |
| 95 | + lsify "$1" | column -t -s$'\t' |
| 96 | + elif [[ $rich -eq 1 ]]; then |
| 97 | + result=$(richify "$1") |
| 98 | + columnize "${result}" |
| 99 | + else |
| 100 | + result=$(jsonquery "$1" "result.[].name") |
| 101 | + echo "${result}" |
| 102 | + fi |
| 103 | +} |
| 104 | + |
| 105 | +lsify () { |
| 106 | + #jq '.result[] | {type,length,name,lastModified}' |
| 107 | + |
| 108 | + names=($(jsonquery "$1" "result.[].name")) |
| 109 | + #echo $names |
| 110 | + lengths=($(jsonquery "$1" "result.[].length")) |
| 111 | + #echo $lengths |
| 112 | + types=($(jsonquery "$1" "result.[].type")) |
| 113 | + #echo $types |
| 114 | + modifieds=($(jsonquery "$1" "result.[].lastModified")) |
| 115 | + #echo $modifieds |
| 116 | + pems=($(jsonquery "$1" "result.[].permissions")) |
| 117 | + thisyear=$(date +%Y) |
| 118 | + |
| 119 | + n=0 |
| 120 | + for i in "${names[@]}"; do |
| 121 | + up='' |
| 122 | + if [[ "${pems[$n]}" == "ALL" ]]; then |
| 123 | + up='rwx------' |
| 124 | + elif [[ "${pems[$n]}" == "READ" ]]; then |
| 125 | + up='r--------' |
| 126 | + elif [[ "${pems[$n]}" == "WRITE" ]]; then |
| 127 | + up='-w-------' |
| 128 | + elif [[ "${pems[$n]}" == "EXECUTE" ]]; then |
| 129 | + up='--x------' |
| 130 | + elif [[ "${pems[$n]}" == "READ_WRITE" ]]; then |
| 131 | + up='rw-------' |
| 132 | + elif [[ "${pems[$n]}" == "READ_EXECUTE" ]]; then |
| 133 | + up='r-x------' |
| 134 | + elif [[ "${pems[$n]}" == "WRITE_EXECUTE" ]]; then |
| 135 | + up='-wx------' |
| 136 | + elif [[ "${pems[$n]}" == "EXECUTE" ]]; then |
| 137 | + up='--x------' |
| 138 | + fi |
| 139 | + |
| 140 | + if [[ "${types[$n]}" == "dir" ]]; then |
| 141 | + up="d${up}" |
| 142 | + else |
| 143 | + up="-${up}" |
| 144 | + fi |
| 145 | + |
| 146 | + formatteddate=$(format_iso8601_time "${modifieds[$n]}") |
| 147 | + echo -e "${up}\t${username}\t${lengths[$n]}\t${formatteddate}\t$i" |
| 148 | + |
| 149 | +# moddate=($(echo ${modifieds[$n]} | sed -e 's/T.*//' -e 's/-/ /g')) |
| 150 | +# tmon=$(month_of_year ${moddate[1]}) |
| 151 | +# tday=$(echo ${moddate[1]} | sed 's/^0*//') |
| 152 | + |
| 153 | +# if [[ $thisyear = ${moddate[0]} ]]; then |
| 154 | +# modtime=$(echo ${modifieds[$n]} | sed -e 's/.*T//' -e 's/\:..\....-..\:..//') |
| 155 | +# echo -e "${up}\t${username}\t${lengths[$n]}\t${modtime}\t$i" |
| 156 | +# else |
| 157 | +# echo -e "${up}\t${username}\t${lengths[$n]}\t${tmon}\t${tday}\t${moddate[0]}\t$i" |
| 158 | +# fi |
| 159 | +# tmod=`date -d "$(echo ${modifieds[$n]} | sed -e 's/T//' -e 's/.000-05\:00\:00//' | sed -e )"` |
| 160 | +# echo -e "${up}\t${username}\t${lengths[$n]}\t${tmod}\t$i" |
| 161 | + n=$[n+1] |
| 162 | + done |
| 163 | + |
| 164 | +} |
| 165 | + |
| 166 | +################################################################## |
| 167 | +################################################################## |
| 168 | +# End Script Logic # |
| 169 | +################################################################## |
| 170 | +################################################################## |
| 171 | + |
| 172 | +# }}} |
| 173 | + |
| 174 | +# Parse command line options |
| 175 | +source "$DIR/options.sh" |
| 176 | + |
| 177 | +# Main loop {{{ |
| 178 | + |
| 179 | +# Print help if no arguments were passed. |
| 180 | +#[[ $# -eq 0 ]] && set -- "--help" |
| 181 | + |
| 182 | +# Read the options and set stuff |
| 183 | +while [[ $1 = -?* ]]; do |
| 184 | + case $1 in |
| 185 | + -h|--help) usage >&2; safe_exit ;; |
| 186 | + --version) version; copyright; disclaimer; safe_exit ;; |
| 187 | + -z|--access_token) shift; access_token=$1 ;; |
| 188 | + -S|--systemId) shift; systemId=$1 ;; |
| 189 | + -L|--long) long=1;; |
| 190 | + -l|--limit) shift; limit=$1;; |
| 191 | + -o|--offset) shift; offset=$1;; |
| 192 | + --filter) shift; responsefilter=$1 ;; |
| 193 | + -H|--hosturl) shift; hosturl=$1;; |
| 194 | + -d|--development) development=1 ;; |
| 195 | + -v|--verbose) verbose=1 ;; |
| 196 | + -V|--veryverbose) veryverbose=1; verbose=1 ;; |
| 197 | + -q|--quiet) quiet=1 ;; |
| 198 | + -i|--interactive) interactive=1 ;; |
| 199 | + -f|--force) force=1 ;; |
| 200 | + --rich) rich=1 ;; |
| 201 | + --endopts) shift; break ;; |
| 202 | + *) die "invalid option: $1" ;; |
| 203 | + esac |
| 204 | + shift |
| 205 | +done |
| 206 | + |
| 207 | +# Store the remaining part as arguments. |
| 208 | +args+=("$@") |
| 209 | + |
| 210 | +# }}} |
| 211 | + |
| 212 | +# Run the script logic |
| 213 | +source "$DIR/runner.sh" |
0 commit comments