Ejemplo n.º 1
0
 def ls_cc(self, name, id, pw):
     self.print_header(name, id)
     try:
         auth_client = ConfidentialAppAuthClient(client_id=id,
                                                 client_secret=pw)
         scopes = "urn:globus:auth:scope:transfer.api.globus.org:all"
         cc_authorizer = ClientCredentialsAuthorizer(auth_client, scopes)
         transfer_client = TransferClient(authorizer=cc_authorizer)
         results = transfer_client.endpoint_search(
             filter_scope="my-endpoints")
         endpoint_list = list(results)
         if endpoint_list:
             print("Owned endpoints:")
             for ep in endpoint_list:
                 print("{} ({})".format(ep['display_name'], ep['id']))
         else:
             print("(No owned endpoints.)")
         results = transfer_client.endpoint_search(
             filter_scope="shared-with-me")
         endpoint_list = list(results)
         if endpoint_list:
             print("Shared endpoints:")
             for ep in endpoint_list:
                 print("{} ({})".format(ep['display_name'], ep['id']))
         else:
             print("(No shared endpoints.)")
     except AuthAPIError as e:
         print(e)
Ejemplo n.º 2
0
class UOCloudTransferClient:
    def __init__(self, config: UOCloudSyncConfig):
        confidential_client = ConfidentialAppAuthClient(
            client_id=config.get_client_id(), client_secret=config.get_client_secret())
        scopes = "urn:globus:auth:scope:transfer.api.globus.org:all"
        cc_authorizer = ClientCredentialsAuthorizer(confidential_client, scopes)
        # create a new client
        self._transfer_client = TransferClient(authorizer=cc_authorizer)
        self._src_endpoint = None
        self._dest_endpoint = None

    def get_endpoint_id(self, endpoint_name: str):
        endpoints = self._transfer_client.endpoint_search(filter_fulltext=endpoint_name)
        # Just return the first result. Hope it is right!
        for ep in endpoints:
            return ep['id']

    def transfer_data(self, src_endpoint: str, src_path: Union[str, Path, PathLike],
                      dest_endpoint: str, dest_path: Union[str, Path, PathLike]):
        self._src_endpoint = src_endpoint
        self._dest_endpoint = dest_endpoint
        src_endpoint_id = self.get_endpoint_id(src_endpoint)
        if not src_endpoint_id:
            print(f'ERROR: Unable to find source endpoint id for: "{self._src_endpoint}"')
            return

        dest_endpoint_id = self.get_endpoint_id(dest_endpoint)
        if not dest_endpoint_id:
            print(f'ERROR: Unable to find destination endpoint id for: "{self._dest_endpoint}"')
            return

        transfer_data = TransferData(self._transfer_client,
                                     src_endpoint_id,
                                     dest_endpoint_id,
                                     encrypt_data=True)
        transfer_data.add_item(src_path, dest_path, recursive=True)
        try:
            print(
                f'Submitting a transfer task from {self._src_endpoint}:{src_path} to {self._dest_endpoint}:{dest_path}')
            task = self._transfer_client.submit_transfer(transfer_data)
        except TransferAPIError as e:
            print(str(e))
            sys.exit(1)
        task_id = task['task_id']
        print(f'\tWaiting for transfer to complete with task_id: {task_id}')
        while not self._transfer_client.task_wait(task_id=task_id, timeout=3600, polling_interval=60):
            print('.', end='')

        print('Transferred files:')
        for info in self._transfer_client.task_successful_transfers(task_id=task_id, num_results=None):
            print("\t{} -> {}".format(info["source_path"], info["destination_path"]))
Ejemplo n.º 3
0
class Transfer:
    '''
    Modified Transfer, add an option to pass refresh token to avoid the web login
    '''
    def __init__(self,
                 src_endpoint_name,
                 dst_endpoint_name,
                 transfer_rt=None,
                 log_lv=logging.INFO):
        log_format = '%(asctime)-15s %(levelname)s:\t  class:%(name)s %(message)s'
        logging.basicConfig(format=log_format)
        self.logger = logging.getLogger(self.__class__.__name__)
        self.logger.setLevel(log_lv)
        self.logger.debug('CLIENT_ID: {0}'.format(CLIENT_ID))
        self.client = NativeAppAuthClient(CLIENT_ID)
        self.client.oauth2_start_flow(refresh_tokens=True)

        if transfer_rt is not None:
            self.authorizer = RefreshTokenAuthorizer(transfer_rt, self.client)
        else:
            authorize_url = self.client.oauth2_get_authorize_url()
            print('Please go to this URL and login: {0}'.format(authorize_url))

            get_input = getattr(__builtins__, 'raw_input', input)
            auth_code = get_input(
                'Please enter the code you get after login here: ').strip()
            token_response = self.client.oauth2_exchange_code_for_tokens(
                auth_code)
            self.globus_auth_data = token_response.by_resource_server[
                'auth.globus.org']
            self.globus_transfer_data = token_response.by_resource_server[
                'transfer.api.globus.org']
            auth_token = self.globus_auth_data['access_token']
            transfer_token = self.globus_transfer_data['access_token']
            transfer_rt = self.globus_transfer_data['refresh_token']
            transfer_at = self.globus_transfer_data['access_token']
            expires_at_s = self.globus_transfer_data['expires_at_seconds']
            self.authorizer = RefreshTokenAuthorizer(transfer_rt,
                                                     self.client,
                                                     access_token=transfer_at,
                                                     expires_at=expires_at_s)
        self.transferClient = TransferClient(authorizer=self.authorizer)
        self.src_endpoint = None
        self.dst_endpoint = None
        for ep in self.transferClient.endpoint_search(
                filter_scope="shared-with-me"):
            if ep["display_name"] == src_endpoint_name:
                self.src_endpoint = ep
                self.logger.info('Source endpoint: [{0}] {1}'.format(
                    self.src_endpoint['id'],
                    self.src_endpoint['display_name']))
        if self.src_endpoint is None:
            self.logger.error(
                'No endpoint shared with you with name: {0}'.format(
                    src_endpoint_name))
            raise LookupError
        for ep in self.transferClient.endpoint_search(
                filter_scope="my-endpoints"):
            if ep['display_name'] == dst_endpoint_name:
                self.dst_endpoint = ep
                self.logger.info('Destination endpoint: [{0}] {1}'.format(
                    self.dst_endpoint['id'],
                    self.dst_endpoint['display_name']))
        if self.dst_endpoint is None:
            self.logger.error('You don\'t have endpoint named: {0}'.format(
                dst_endpoint_name))
            raise LookupError

    def transfer_dir(self, src_dir, dst_dir):
        transfer_data = TransferData(self.transferClient,
                                     self.src_endpoint['id'],
                                     self.dst_endpoint['id'])
        transfer_data.add_item(src_dir, dst_dir, recursive=True)
        result = self.transferClient.submit_transfer(transfer_data)
        self.logger.info('task [{0}] {1}'.format(result['task_id'],
                                                 result['code']))
        return result

    def transfer_file(self, src_file, dst_file):
        transfer_data = TransferData(self.transferClient,
                                     self.src_endpoint['id'],
                                     self.dst_endpoint['id'])
        transfer_data.add_item(src_file, dst_file)
        result = self.transferClient.submit_transfer(transfer_data)
        self.logger.info('task_id [{0}] {1}'.format(result['task_id'],
                                                    result['code']))
        return result

    def ls_src_dir(self, path, ls_filter=''):
        # using iteration to get every entry from result
        # an entry contain two keys: 'name' and 'type'
        # type define the entry is a file or folder
        result = self.transferClient.operation_ls(self.src_endpoint['id'],
                                                  path=path,
                                                  filter=ls_filter)
        for entry in result:
            self.logger.debug('name: {0}\ttype: {1}'.format(
                entry["name"], entry["type"]))
        return result

    def task_list(self, num_results=10):
        result = self.transferClient.task_list(num_results=num_results)
        for task in result:
            self.logger.debug('task_id: [{0}]\t status: {1}'.format(
                task['task_id'], task['status']))

        result = self.transferClient.task_list(num_results=num_results)
        return result

    def get_task(self, task_id):
        return self.transferClient.get_task(task_id)