10 from jsk_tools.cltool
import percol_select
24 __all__ = (
'cli',
'cmd_get',
'cmd_ls',
'cmd_put',
'cmd_pubinfo')
29 username = username
or os.environ.get(
'SSH_USER')
30 username = username
or os.environ[
'USER']
34 HOST =
'aries.jsk.t.u-tokyo.ac.jp' 35 DATA_DIR =
'/home/jsk/ros/data' 39 CONTEXT_SETTINGS = dict(
40 help_option_names=[
'-h',
'--help'],
44 @click.group(context_settings=CONTEXT_SETTINGS)
49 @cli.command(name=
'get', help=
'Download specified file.')
50 @click.option(
'-p',
'--public', is_flag=
True, help=
'Handle public files.')
51 @click.argument(
'query', default=
'')
53 """Download specified file.""" 57 candidates = [l.split()[1]
for l
in lines]
60 selected = percol_select(candidates)
61 if len(selected) != 1:
62 sys.stderr.write(
'Please select 1 filename.\n')
65 sys.stderr.write(
'Selected: {0}\n'.format(query))
70 cmd =
'rsync -avz --progress -e "ssh -o StrictHostKeyChecking=no"\ 71 --bwlimit=100000 {usr}@{host}:{dir}/private/{q} .' 72 cmd = cmd.format(usr=LOGIN_USER, host=HOST, dir=DATA_DIR, q=query)
73 subprocess.call(shlex.split(cmd))
78 ls_options = ls_options
or []
80 cmd =
'ls {opt} {dir}/private/{q}' 81 cmd = cmd.format(opt=
' '.join(ls_options), dir=DATA_DIR, q=query)
82 _, stdout, _ = ssh.exec_command(cmd)
83 files = stdout.read().splitlines()
87 @cli.command(name=
'ls', help=
'Get list of files.')
88 @click.option(
'-p',
'--public', is_flag=
True, help=
'Handle public files.')
89 @click.argument(
'query', required=
False)
90 @click.option(
'-s',
'--show-size', is_flag=
True, help=
'Display size.')
91 @click.option(
'--sort', help=
'Sort by ..',
92 type=click.Choice([
'time',
'size',
'extension',
'version']))
93 @click.option(
'-r',
'--reverse', is_flag=
True, help=
'Reverse the order.')
94 def cmd_ls(public, query, show_size, sort, reverse):
95 """Get list of files.""" 101 ls_options.append(
'--size')
102 ls_options.append(
'--human-readable')
104 ls_options.append(
'--sort={0}'.format(sort))
106 ls_options.append(
'--reverse')
111 'WARNING: if public=True, ignores all ls options\n')
117 @cli.command(name=
'put', help=
'Upload file to aries.')
118 @click.argument(
'filename', required=
True, type=click.Path(exists=
True))
120 '-p',
'--public', is_flag=
True,
121 help=(
'Handle public files. It will be uploaded to Google Drive. ' 122 'Go https://drive.google.com/open?id=0B9P1L--7Wd2vUGplQkVLTFBWcFE'))
124 '--stamped', is_flag=
True,
125 help=
'Rename file to with prefix of timestamp')
127 """Upload file to aries.""" 129 filename_org = filename
131 if filename_org != filename:
132 print(
'Filename is being changed: {0} -> {1}' 133 .format(filename_org, filename))
134 yn = raw_input(
'Are you sure?[Y/n]: ')
136 sys.stderr.write(
'Aborted!')
138 os.rename(filename_org, filename)
141 print(
'Uploading to Google Drive...')
143 for line
in stdout.splitlines():
144 if line.startswith(
'Title:'):
145 filename = line.split(
' ')[-1].strip()
146 elif line.startswith(
'Id:'):
147 file_id = line.split(
' ')[-1].strip()
149 print(
'You can download it by:')
151 print(
'$ wget {url} -O {file}'.format(url=dl_url, file=filename))
153 print(
'Uploading to aries...')
154 cmd =
'rsync -avz --progress -e "ssh -o StrictHostKeyChecking=no"\ 155 --bwlimit=100000 {file} {usr}@{host}:{dir}/private/' 156 cmd = cmd.format(file=filename, usr=LOGIN_USER, host=HOST,
158 subprocess.call(shlex.split(cmd))
162 @cli.command(name=
'pubinfo', help=
'Show public data info.')
163 @click.argument(
'filename', default=
'')
164 @click.option(
'-d',
'--download-cmd',
'show_dl_cmd', is_flag=
True,
165 help=
'Print out download command')
169 selected = percol_select(candidates)
170 if len(selected) != 1:
171 sys.stderr.write(
'Please select 1 filename.\n')
173 filename = selected[0].split()[1]
176 if len(filename) > 40:
177 filename = filename[:19] +
'...' + filename[-18:]
180 for line
in stdout.splitlines():
181 file_id, title = line.split()[:2]
182 if filename == title:
183 filename =
info_gdrive(id=file_id, only_filename=
True)
186 sys.stderr.write(
'file not found: {0}\n'.format(filename))
187 sys.stderr.write(
'Run `jsk_data ls --public` to find files.\n')
192 info =
'wget {url} -O {file}'.format(url=dl_url, file=filename)
193 sys.stdout.write(info)
200 Download URL: {dl_url}'''.format(id=file_id, file=filename,
201 view_url=view_url, dl_url=dl_url)
205 @cli.command(name=
'delete', help=
'Delete specified file.')
206 @click.option(
'-p',
'--public', is_flag=
True, help=
'Handle public files.')
207 @click.argument(
'filename', default=
'')
209 """Delete specified file.""" 211 sys.stderr.write(
'ERROR: public=False is not supported\n')
217 selected = percol_select(candidates)
218 if len(selected) != 1:
219 sys.stderr.write(
'Please select 1 filename.\n')
221 filename = selected[0].split()[1]
226 @cli.command(name=
'pubopen', help=
'Go to see files on browser')
def connect_ssh(host, username=None, password=None)
def _list_aries_files(query=None, ls_options=None)
def cmd_ls(public, query, show_size, sort, reverse)
def filename_with_timestamp(filename, sep=None)
def cmd_delete(public, filename)
def cmd_put(filename, public, stamped)
def info_gdrive(id, only_filename=False)
def delete_gdrive(filename)
def cmd_pubinfo(filename, show_dl_cmd)
def google_drive_file_url(id, download=False)
def _get_login_user(host)
def get_user_by_hostname(hostname)
def download_gdrive(filename)
def cmd_get(public, query)
def upload_gdrive(filename)