gdrive.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 """Wrapper module for gdrive command"""
00005 
00006 import os
00007 import subprocess
00008 import sys
00009 
00010 
00011 # directory id in google drive of jsk
00012 DIR_ID = '0B9P1L--7Wd2vUGplQkVLTFBWcFE'
00013 
00014 
00015 def open_gdrive():
00016     url = 'https://drive.google.com/drive/u/1/folders/{}'.format(DIR_ID)
00017     cmd = "gnome-open '{url}'".format(url=url)
00018     subprocess.call(cmd, shell=True)
00019 
00020 
00021 def run_gdrive(args=None, stdout=True):
00022     if args is None:
00023         args = ''
00024     ros_home = os.getenv('ROS_HOME', os.path.expanduser('~/.ros'))
00025     pkg_ros_home = os.path.join(ros_home, 'jsk_data')
00026     config = os.path.join(pkg_ros_home, '.gdrive')
00027     cmd = 'rosrun jsk_data drive-linux-x64 --config {config} {args}'\
00028           .format(args=args, config=config)
00029     if stdout:
00030         return subprocess.check_output(cmd, shell=True)
00031     else:
00032         subprocess.call(cmd, shell=True)
00033 
00034 
00035 def _init_gdrive():
00036     """This should be called before any commands with gdrive"""
00037     ros_home = os.getenv('ROS_HOME', os.path.expanduser('~/.ros'))
00038     pkg_ros_home = os.path.join(ros_home, 'jsk_data')
00039     config = os.path.join(pkg_ros_home, '.gdrive')
00040     if os.path.exists(os.path.join(config, 'token.json')):
00041         return
00042     if not os.path.exists(pkg_ros_home):
00043         os.makedirs(pkg_ros_home)
00044     run_gdrive(stdout=False)
00045 
00046 
00047 def list_gdrive():
00048     _init_gdrive()
00049     args = '''list --query " '{id}' in parents" --noheader'''.format(id=DIR_ID)
00050     return run_gdrive(args=args)
00051 
00052 
00053 def info_gdrive(id, only_filename=False):
00054     _init_gdrive()
00055     args = 'info --id {id}'.format(id=id)
00056     info = run_gdrive(args=args)
00057     if only_filename:
00058         return _info_gdrive_filename(stdout=info)
00059     return info
00060 
00061 
00062 def _info_gdrive_filename(stdout):
00063     for line in stdout.splitlines():
00064         if line.startswith('Title: '):
00065             return line.split()[-1]
00066 
00067 
00068 def upload_gdrive(filename):
00069     _init_gdrive()
00070     args = 'upload --file {file} --parent {id}'.format(file=filename,
00071                                                        id=DIR_ID)
00072     return run_gdrive(args=args)
00073 
00074 
00075 def _find_id_by_filename(filename):
00076     if len(filename) > 40:
00077         filename = filename[:19] + '...' + filename[-18:]
00078     for line in list_gdrive().splitlines():
00079         file_id, title = line.split()[:2]
00080         if filename == title:
00081             return file_id
00082     else:
00083         sys.stderr.write('file not found: {0}\n'.format(filename))
00084         sys.stderr.write('Run `jsk_data ls --public` to find files.\n')
00085         return
00086 
00087 
00088 def download_gdrive(filename):
00089     _init_gdrive()
00090     file_id = _find_id_by_filename(filename)
00091     args = 'download --id {}'.format(file_id)
00092     run_gdrive(args=args)
00093 
00094 
00095 def delete_gdrive(filename):
00096     _init_gdrive()
00097     file_id = _find_id_by_filename(filename)
00098     args = 'delete --id {}'.format(file_id)
00099     run_gdrive(args=args)


jsk_data
Author(s):
autogenerated on Fri Sep 8 2017 03:39:16