Skip to content

Commit

Permalink
Infer remote directory from filename
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriy-serdyuk authored and maximecb committed Aug 28, 2017
1 parent 3cdfb08 commit 68ff4ea
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions fuel/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ def cache_file(filename):
"""
dataset_local_dir = config.local_data_path
dataset_remote_dirs = config.data_path
if any([remote_dir == "" for remote_dir in dataset_remote_dirs]) \
or dataset_local_dir == "":
dataset_remote_dir = os.path.dirname(filename)
if dataset_remote_dir == "" or dataset_local_dir == "":
log.debug("Local dataset cache is deactivated")

remote_name = filename
Expand All @@ -74,8 +73,7 @@ def cache_file(filename):
"Error : Specified name {} is not a file".format(remote_name))
return filename

if not any([remote_name.startswith(directory)
for directory in dataset_remote_dirs]):
if not remote_name.startswith(dataset_remote_dir):
log.warning(
common_msg +
"We cache in the local directory only what is"
Expand All @@ -89,10 +87,9 @@ def cache_file(filename):
stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH))

# Determine local path to which the file is to be cached
# TODO
local_name = os.path.join(dataset_local_dir,
os.path.relpath(remote_name,
dataset_remote_dirs))
dataset_remote_dir))

# Create the folder structure to receive the remote file
local_folder = os.path.split(local_name)[0]
Expand Down Expand Up @@ -133,7 +130,7 @@ def cache_file(filename):
return filename

# There is enough space; make a local copy of the file
copy_from_server_to_local(dataset_remote_dirs, dataset_local_dir,
copy_from_server_to_local(dataset_remote_dir, dataset_local_dir,
remote_name, local_name)
log.info(common_msg + "File {} has been locally cached to {}"
.format(remote_name, local_name))
Expand Down Expand Up @@ -182,7 +179,7 @@ def cache_file(filename):
return local_name


def copy_from_server_to_local(dataset_remote_dirs, dataset_local_dir,
def copy_from_server_to_local(dataset_remote_dir, dataset_local_dir,
remote_fname, local_fname):
"""Copies a remote file locally.
Expand Down Expand Up @@ -222,7 +219,7 @@ def copy_from_server_to_local(dataset_remote_dirs, dataset_local_dir,
if sep[0] == "":
sep = sep[1:]
for i in range(len(sep)):
orig_p = os.path.join(dataset_remote_dirs, *sep[:i + 1])
orig_p = os.path.join(dataset_remote_dir, *sep[:i + 1])
new_p = os.path.join(dataset_local_dir, *sep[:i + 1])
orig_st = os.stat(orig_p)
new_st = os.stat(new_p)
Expand Down

0 comments on commit 68ff4ea

Please sign in to comment.