Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
forsyth2 committed Oct 9, 2023
1 parent a0b9939 commit 90ff9af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
7 changes: 1 addition & 6 deletions zstash/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def create():
if hpss != "none":
url = urlparse(hpss)
if url.scheme == "globus":
globus_activate(hpss, args.globus_consent_prompt)
globus_activate(hpss)
else:
# config.hpss is not "none", so we need to
# create target HPSS directory
Expand Down Expand Up @@ -161,11 +161,6 @@ def setup_create() -> Tuple[str, argparse.Namespace]:
action="store_true",
help="Hard copy symlinks. This is useful for preventing broken links. Note that a broken link will result in a failed create.",
)
optional.add_argument(
"--globus-consent-prompt",
action="store_true",
help="Prompt for Globus consent. This is useful if you encounter a 'ConsentRequired' error.",
)
# Now that we're inside a subcommand, ignore the first two argvs
# (zstash create)
args: argparse.Namespace = parser.parse_args(sys.argv[2:])
Expand Down
61 changes: 35 additions & 26 deletions zstash/globus.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
from __future__ import absolute_import, print_function

import configparser
import json
import logging
import os
import os.path
import re
import socket
import sys

from fair_research_login.client import NativeClient
from globus_sdk import (
NativeAppAuthClient,
TransferAPIError,
TransferClient,
TransferData,
)
from globus_sdk import TransferAPIError, TransferClient, TransferData
from globus_sdk.services.transfer.response.iterable import IterableTransferResponse
from six.moves.urllib.parse import urlparse

Expand Down Expand Up @@ -43,24 +40,38 @@
archive_directory_listing: IterableTransferResponse = None


def globus_flow(ep=""):
CLIENT = NativeAppAuthClient(
client_id="6c1629cf-446c-49e7-af95-323c6412397f", app_name="Zstash"
)
scopes = "urn:globus:auth:scope:transfer.api.globus.org:all"
endpoint_scope = f"[ *https://auth.globus.org/scopes/{ep}/data_access ]"
data_access_sopes = scopes + endpoint_scope
CLIENT.oauth2_start_flow(refresh_tokens=True, requested_scopes=data_access_sopes)
authorize_url = CLIENT.oauth2_get_authorize_url()
print(f"Please go to this URL and login:\n\n{authorize_url}\n\n")
def check_endpoint_version_5(transfer_client, ep_id):
logging.debug(f"Checking {ep_id}")
output = json.loads(transfer_client.get_endpoint(ep_id))
logging.debug(f"Version is {output['gcs_version']}")
if int(output["gcs_version"].split(".")[0]) >= 5:
return True
else:
return False

get_input = getattr(__builtins__, "raw_input", input)
auth_code = get_input("Please enter the code you get after login here: ")
token_response = CLIENT.oauth2_exchange_code_for_tokens(auth_code)
print(token_response)

def submit_transfer_with_checks(transfer_client, transfer_data, remote_endpoint=None):
try:
task = transfer_client.submit_transfer(transfer_data)
except TransferAPIError as err:
if (
remote_endpoint
and check_endpoint_version_5(transfer_client, remote_endpoint)
and err.info.consent_required
):
native_client = NativeClient(
client_id="6c1629cf-446c-49e7-af95-323c6412397f", app_name="Zstash"
)
native_client.login(
requested_scopes=f"urn:globus:auth:scope:transfer.api.globus.org:all[ *https://auth.globus.org/scopes/{remote_endpoint}/data_access ]"
)
task = transfer_client.submit_transfer(transfer_data)
else:
raise err
return task


def globus_activate(hpss: str, globus_consent_prompt: bool = False):
def globus_activate(hpss: str):
"""
Read the local globus endpoint UUID from ~/.zstash.ini.
If the ini file does not exist, create an ini file with empty values,
Expand Down Expand Up @@ -116,9 +127,6 @@ def globus_activate(hpss: str, globus_consent_prompt: bool = False):
if remote_endpoint.upper() in hpss_endpoint_map.keys():
remote_endpoint = hpss_endpoint_map.get(remote_endpoint.upper())

if globus_consent_prompt:
globus_flow(remote_endpoint)

native_client = NativeClient(
client_id="6c1629cf-446c-49e7-af95-323c6412397f",
app_name="Zstash",
Expand Down Expand Up @@ -220,7 +228,7 @@ def globus_transfer(
)
else:
logger.error("Transfer FAILED")
task = transfer_client.submit_transfer(transfer_data)
task = submit_transfer_with_checks(transfer_client, transfer_data, dst_ep)
task_id = task.get("task_id")
transfer_data = None
except TransferAPIError as e:
Expand Down Expand Up @@ -294,7 +302,8 @@ def globus_finalize(non_blocking: bool = False):

if transfer_data:
try:
last_task = transfer_client.submit_transfer(transfer_data)
# We don't have remote_endpoint information to pass in here.
last_task = submit_transfer_with_checks(transfer_client, transfer_data)
last_task_id = last_task.get("task_id")
except TransferAPIError as e:
if e.code == "NoCredException":
Expand Down
7 changes: 1 addition & 6 deletions zstash/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ def setup_update() -> Tuple[argparse.Namespace, str]:
action="store_true",
help="Hard copy symlinks. This is useful for preventing broken links. Note that a broken link will result in a failed update.",
)
optional.add_argument(
"--globus-consent-prompt",
action="store_true",
help="Prompt for Globus consent. This is useful if you encounter a 'ConsentRequired' error.",
)
args: argparse.Namespace = parser.parse_args(sys.argv[2:])
if args.hpss and args.hpss.lower() == "none":
args.hpss = "none"
Expand Down Expand Up @@ -140,7 +135,7 @@ def update_database( # noqa: C901
hpss: str = config.hpss
else:
raise TypeError("Invalid config.hpss={}".format(config.hpss))
globus_activate(hpss, args.globus_consent_prompt)
globus_activate(hpss)
hpss_get(hpss, get_db_filename(cache), cache)
else:
error_str: str = (
Expand Down

0 comments on commit 90ff9af

Please sign in to comment.