Skip to content

Commit

Permalink
Merge pull request #1589 from iory/redownload
Browse files Browse the repository at this point in the history
[jsk_data] Add re-download code. Fix #1574
  • Loading branch information
k-okada committed Aug 14, 2018
2 parents dcd34b3 + 8194ffc commit 78e2b7a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
19 changes: 15 additions & 4 deletions jsk_data/src/jsk_data/download_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ def _get_package_source_path(pkg_name):


def download_data(pkg_name, path, url, md5, download_client=None,
extract=False, compressed_bags=None, quiet=True, chmod=True):
extract=False, compressed_bags=None, quiet=True, chmod=True,
n_times=2):
"""Install test data checking md5 and rosbag decompress if needed.
The downloaded data are located in cache_dir, and then linked to specified path.
cache_dir is set by environment variable `JSK_DATA_CACHE_DIR` if defined, set by ROS_HOME/data otherwise."""
cache_dir is set by environment variable `JSK_DATA_CACHE_DIR` if defined, set by ROS_HOME/data otherwise.
If download succeeded, return True, otherwise return False.
"""
if download_client is None:
if is_google_drive_url(url):
download_client = 'gdown'
Expand Down Expand Up @@ -161,9 +164,16 @@ def download_data(pkg_name, path, url, md5, download_client=None,
os.chmod(cache_dir, 0777)
cache_file = osp.join(cache_dir, osp.basename(path))
# check if cache exists, and update if necessary
if not (osp.exists(cache_file) and check_md5sum(cache_file, md5)):
try_download_count = 0
while not (osp.exists(cache_file) and check_md5sum(cache_file, md5)):
# Try n_times download.
# https://github.com/jsk-ros-pkg/jsk_common/issues/1574
if try_download_count >= n_times:
print('[ERROR] md5sum mismatch. aborting')
return False
if osp.exists(cache_file):
os.remove(cache_file)
try_download_count += 1
download(download_client, url, cache_file, quiet=quiet, chmod=chmod)
if osp.islink(path):
# overwrite the link
Expand All @@ -175,7 +185,7 @@ def download_data(pkg_name, path, url, md5, download_client=None,
# not link and exists so skipping
print('[%s] File exists, so skipping creating symlink.' % path,
file=sys.stderr)
return
return True
if extract:
# extract files in cache dir and create symlink for them
extracted_files = extract_file(cache_file, to_directory=cache_dir, chmod=True)
Expand All @@ -194,3 +204,4 @@ def download_data(pkg_name, path, url, md5, download_client=None,
pkg_path = _get_package_source_path(pkg_name)
compressed_bag = osp.join(pkg_path, compressed_bag)
decompress_rosbag(compressed_bag, quiet=quiet, chmod=chmod)
return True
17 changes: 17 additions & 0 deletions jsk_data/src/jsk_data/tests/test_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from nose.tools import assert_true

from jsk_data.download_data import download_data


def test_download_data_0(self):
succeeded = download_data(
pkg_name=PKG,
path='trained_data/drill_svm.xml',
url='https://drive.google.com/uc?id=0B5hRAGKTOm_KWW11R0FTX0xjTDg',
md5='762d0da4bcbf50e0e92939372988901a',
quiet=quiet,
)
assert_true(succeeded)

0 comments on commit 78e2b7a

Please sign in to comment.