Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 62 additions & 14 deletions solvebio/cli/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,17 @@ def _upload_folder(
# be populated
all_folder_parts = sorted(all_folder_parts, key=lambda x: len(x.split("/")))
for folder in all_folder_parts:
print("{}Creating folder {}".format(
"[Dry Run] " if dry_run else "", folder))
Object.create_folder(vault, folder)
if dry_run:
try:
f = Object.get_by_full_path(folder)
if not f.is_folder:
print("[Dry Run] {} is not a folder - this will cause an error on upload.".format(folder))
else:
print("[Dry Run] Folder {} already exists - skipping creation".format(folder))
except NotFoundError:
print("[Dry Run] Creating folder {}".format(folder))
else:
Object.create_folder(vault, folder)

# Create files in parallel
# Signal handling allows for graceful exit upon KeyboardInterrupt
Expand Down Expand Up @@ -234,18 +242,32 @@ def _create_file_job(args):
try:
local_file_path, remote_folder_full_path, vault_path, dry_run, archive_folder, client_auth, follow_shortcuts \
= args
if dry_run:
print("[Dry Run] Uploading {} to {}".format(
local_file_path, remote_folder_full_path))
return

# Provides the global host, token, token_type
client = SolveClient(*client_auth)
remote_parent = Object.get_by_full_path(
remote_folder_full_path,
assert_type="folder",
follow_shortcuts=follow_shortcuts,
client=client
)

remote_parent = None
try:
remote_parent = Object.get_by_full_path(
remote_folder_full_path,
assert_type="folder",
follow_shortcuts=follow_shortcuts,
client=client
)
except NotFoundError as e:
if not dry_run:
raise e

if dry_run:
if not _object_exists(remote_parent, local_file_path, client):
print("[Dry Run] Uploading {} to {}".format(
local_file_path, remote_folder_full_path))
return
else:
print("[Dry Run] File {} already exists at {} - skipping upload".format(
local_file_path, remote_folder_full_path))
return

Object.upload_file(
local_file_path,
remote_parent.path,
Expand All @@ -260,6 +282,26 @@ def _create_file_job(args):
except Exception as e:
return e

def _object_exists(remote_parent, local_path, _client):
if remote_parent is None:
return False
full_path, path_dict = Object.validate_full_path(
os.path.join('{}:{}'.format(remote_parent.vault.full_path, remote_parent.path),
os.path.basename(local_path)), client=_client)
try:
obj = Object.get_by_full_path(full_path, client=_client)
if not obj.is_file:
return False
else:
# Check if the md5sum matches
local_md5 = md5sum(local_path)[0]
remote_md5 = obj.get("md5")
if remote_md5 and remote_md5 == local_md5:
return True
else:
return False
except NotFoundError:
return False

def _create_template_from_file(template_file, dry_run=False):
mode = "r"
Expand Down Expand Up @@ -752,9 +794,11 @@ def _download_recursive(

min_depth = min([x.depth for x in remote_objects])
num_at_min_depth = len([x for x in remote_objects if x.depth == min_depth])
if num_at_min_depth == 1:
if num_at_min_depth == 1 and not _is_single_file(remote_objects):
# when downloading from folder
base_folder_depth = min_depth
else:
# when downloading from vault root or singular file
base_folder_depth = min_depth - 1

downloaded_files = set()
Expand Down Expand Up @@ -995,6 +1039,10 @@ def _ls(full_path, recursive=False, follow_shortcuts=False):
return files


def _is_single_file(objects):
return len(objects) == 1 and objects[0].get("object_type") == "file"


def should_tag_by_object_type(args, object_):
"""Returns True if object matches object type requirements"""
valid = True
Expand Down
2 changes: 1 addition & 1 deletion solvebio/global_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def subjects(self):

# Executes a query to get a full API response which contains subjects list
gs = self.limit(0)
gs.execute(include_subjects=True)
gs.execute(include_subjects=True, include_all_subjects=True)

return gs._response.get('subjects')

Expand Down
1 change: 1 addition & 0 deletions solvebio/resource/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ def create_folder(cls, vault, full_path, tags=None, **kwargs):
parent_object_id = parent.id

# Make the API call
print("Creating folder {}".format(full_path))
new_obj = Object.create(
vault_id=vault.id,
parent_object_id=parent_object_id,
Expand Down
Loading