gdrive.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 """Wrapper module for gdrive command"""
5 
6 import os
7 import subprocess
8 import sys
9 
10 
11 # directory id in google drive of jsk
12 DIR_ID = '0B9P1L--7Wd2vUGplQkVLTFBWcFE'
13 
14 
16  url = 'https://drive.google.com/drive/u/1/folders/{}'.format(DIR_ID)
17  cmd = "gnome-open '{url}'".format(url=url)
18  subprocess.call(cmd, shell=True)
19 
20 
21 def run_gdrive(args=None, stdout=True):
22  if args is None:
23  args = ''
24  ros_home = os.getenv('ROS_HOME', os.path.expanduser('~/.ros'))
25  pkg_ros_home = os.path.join(ros_home, 'jsk_data')
26  config = os.path.join(pkg_ros_home, '.gdrive')
27  cmd = 'rosrun jsk_data drive-linux-x64 --config {config} {args}'\
28  .format(args=args, config=config)
29  if stdout:
30  return subprocess.check_output(cmd, shell=True)
31  else:
32  subprocess.call(cmd, shell=True)
33 
34 
36  """This should be called before any commands with gdrive"""
37  ros_home = os.getenv('ROS_HOME', os.path.expanduser('~/.ros'))
38  pkg_ros_home = os.path.join(ros_home, 'jsk_data')
39  config = os.path.join(pkg_ros_home, '.gdrive')
40  if os.path.exists(os.path.join(config, 'token.json')):
41  return
42  if not os.path.exists(pkg_ros_home):
43  os.makedirs(pkg_ros_home)
44  run_gdrive(stdout=False)
45 
46 
48  _init_gdrive()
49  args = '''list --query " '{id}' in parents" --noheader'''.format(id=DIR_ID)
50  return run_gdrive(args=args)
51 
52 
53 def info_gdrive(id, only_filename=False):
54  _init_gdrive()
55  args = 'info --id {id}'.format(id=id)
56  info = run_gdrive(args=args)
57  if only_filename:
58  return _info_gdrive_filename(stdout=info)
59  return info
60 
61 
63  for line in stdout.splitlines():
64  if line.startswith('Title: '):
65  return line.split()[-1]
66 
67 
68 def upload_gdrive(filename):
69  _init_gdrive()
70  args = 'upload --file {file} --parent {id}'.format(file=filename,
71  id=DIR_ID)
72  return run_gdrive(args=args)
73 
74 
75 def _find_id_by_filename(filename):
76  if len(filename) > 40:
77  filename = filename[:19] + '...' + filename[-18:]
78  for line in list_gdrive().splitlines():
79  file_id, title = line.split()[:2]
80  if filename == title:
81  return file_id
82  else:
83  sys.stderr.write('file not found: {0}\n'.format(filename))
84  sys.stderr.write('Run `jsk_data ls --public` to find files.\n')
85  return
86 
87 
88 def download_gdrive(filename):
89  _init_gdrive()
90  file_id = _find_id_by_filename(filename)
91  args = 'download --id {}'.format(file_id)
92  run_gdrive(args=args)
93 
94 
95 def delete_gdrive(filename):
96  _init_gdrive()
97  file_id = _find_id_by_filename(filename)
98  args = 'delete --id {}'.format(file_id)
99  run_gdrive(args=args)
def list_gdrive()
Definition: gdrive.py:47
def open_gdrive()
Definition: gdrive.py:15
def info_gdrive(id, only_filename=False)
Definition: gdrive.py:53
def delete_gdrive(filename)
Definition: gdrive.py:95
def run_gdrive(args=None, stdout=True)
Definition: gdrive.py:21
def _info_gdrive_filename(stdout)
Definition: gdrive.py:62
def download_gdrive(filename)
Definition: gdrive.py:88
def _init_gdrive()
Definition: gdrive.py:35
def _find_id_by_filename(filename)
Definition: gdrive.py:75
def upload_gdrive(filename)
Definition: gdrive.py:68


jsk_data
Author(s):
autogenerated on Tue Feb 6 2018 03:45:36