def copy_bundle(self, source_client, source_bundle_uuid, dest_client, dest_worksheet_uuid, copy_dependencies, add_to_worksheet):
        """
        Helper function that supports cp and wadd.
        Copies the source bundle to the target worksheet.
        Currently, this goes between two clients by downloading to the local
        disk and then uploading, which is not the most efficient.
        But having two clients talk directly to each other is complicated...
        """
        if copy_dependencies:
            source_info = source_client.get_bundle_info(source_bundle_uuid)
            # Copy all the dependencies, but only for run dependencies.
            for dep in source_info['dependencies']:
                self.copy_bundle(source_client, dep['parent_uuid'], dest_client, dest_worksheet_uuid, False, add_to_worksheet)
            self.copy_bundle(source_client, source_bundle_uuid, dest_client, dest_worksheet_uuid, False, add_to_worksheet)
            return

        # Check if the bundle already exists on the destination, then don't copy it
        # (although metadata could be different on source and destination).
        # TODO: sync the metadata.
        bundle = None
        try:
            bundle = dest_client.get_bundle_info(source_bundle_uuid)
        except:
            pass

        # Bundle already exists, just need to add to worksheet if desired.
        if bundle:
            if add_to_worksheet:
                dest_client.add_worksheet_item(dest_worksheet_uuid, worksheet_util.bundle_item(source_bundle_uuid))
            return

        source_info = source_client.get_bundle_info(source_bundle_uuid)
        if source_info is None:
            print >>self.stdout, 'Unable to read bundle %s' % source_bundle_uuid
            return

        source_desc = self.simple_bundle_str(source_info)
        if source_info['state'] not in [State.READY, State.FAILED]:
            print >>self.stdout, 'Not copying %s because it has non-final state %s' % (source_desc, source_info['state'])
            return

        print >>self.stdout, "Copying %s..." % source_desc
        target = (source_bundle_uuid, '')
        target_info = source_client.get_target_info(target, 0)
        
        source = None
        dest_file_uuid = None
        try:
            # Open source (as archive)
            if target_info is not None and target_info['type'] == 'directory':
                filename_suffix = '.tar.gz'
                if isinstance(source_client, LocalBundleClient):
                    source = source_client.open_tarred_gzipped_directory(target)
                else:
                    source = RPCFileHandle(
                        source_client.open_tarred_gzipped_directory(target),
                        source_client.proxy, finalize_on_close=True)
            elif target_info is not None and target_info['type'] == 'file':
                filename_suffix = '.gz'
                if isinstance(source_client, LocalBundleClient):
                    source = source_client.open_gzipped_file(target)
                else:
                    source = RPCFileHandle(
                        source_client.open_gzipped_file(target),
                        source_client.proxy, finalize_on_close=True)

            if source is not None:
                # Open target (temporary file)
                if isinstance(dest_client, LocalBundleClient):
                    dest_path = tempfile.mkstemp(filename_suffix)[1]
                    dest = open(dest_path, 'wb')
                else:
                    dest_file_uuid = dest_client.open_temp_file('bundle' + filename_suffix)
                    dest = RPCFileHandle(dest_file_uuid, dest_client.proxy)
                with closing(dest):
                    # Copy contents over from source to target.
                    file_util.copy(
                        source,
                        dest,
                        autoflush=False,
                        print_status='Copying %s from %s to %s' % (source_bundle_uuid, source_client.address, dest_client.address))

            # Set sources
            if source is None:
                sources = [None]
            elif isinstance(dest_client, LocalBundleClient):
                sources = [dest_path]
            else:
                sources = [dest_file_uuid]

            # Finally, install the archive (this function will delete it).
            if isinstance(dest_client, LocalBundleClient):
                result = dest_client.upload_bundle(
                    sources=sources,
                    follow_symlinks=False,
                    exclude_patterns=None,
                    git=False,
                    unpack=True,
                    remove_sources=True,
                    info=source_info,
                    worksheet_uuid=dest_worksheet_uuid,
                    add_to_worksheet=add_to_worksheet,
                )
            else:
                result = dest_client.finish_upload_bundle(
                    sources,
                    True,
                    source_info,
                    dest_worksheet_uuid,
                    add_to_worksheet)
            
            return result
        except:
            if source is not None:
                source.close()
            if dest_file_uuid is not None:
                dest_client.finalize_file(dest_file_uuid)
            raise
dataset/ETHPy150Open codalab/codalab-cli/codalab/lib/bundle_cli.py/BundleCLI.copy_bundle/original